git

package
v0.2.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 10, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ShouldIgnoreFile

func ShouldIgnoreFile(file string, ignorePatterns []string) bool

ShouldIgnoreFile checks if a file should be ignored based on patterns

Types

type GitVCS

type GitVCS struct{}

GitVCS implements the VCS interface for Git

func (*GitVCS) CreateCommit

func (g *GitVCS) CreateCommit(repoPath string, message string) error

CreateCommit creates a git commit with the given message

Parameters:

  • repoPath: The file system path to the git repository
  • message: The commit message

Returns:

  • error: An error if the git command fails or if there are issues accessing the repository

func (*GitVCS) GetCommitInfo

func (g *GitVCS) GetCommitInfo(repoPath string, commitHash string) (string, error)

GetCommitInfo returns formatted information about the commit If commitHash is empty, returns info about the last commit

Parameters:

  • repoPath: The file system path to the git repository
  • commitHash: The hash of the commit to get info for (or empty for the last commit)

Returns:

  • string: The formatted commit info
  • error: An error if the git command fails or if there are issues accessing the repository

func (*GitVCS) GetCurrentBranch

func (g *GitVCS) GetCurrentBranch(repoPath string) (string, error)

GetCurrentBranch returns the name of the current branch in the git repository at the specified path.

Parameters:

  • repoPath: The file system path to the git repository

Returns:

  • string: The name of the current branch
  • error: An error if the git command fails or if there are issues accessing the repository

func (*GitVCS) GetDiff

func (g *GitVCS) GetDiff(repoPath string) (string, error)

GetDiff retrieves the staged git diff for the specified repository path. It runs the "git diff --staged -U2" command and filters out lines that start with "index", "---", and "+++".

Parameters:

  • repoPath: The file path to the git repository.

Returns:

  • A string containing the filtered diff output.
  • An error if the command fails or if the specified path is not a git repository.

func (*GitVCS) GetLastCommitHash

func (g *GitVCS) GetLastCommitHash(repoPath string) (string, error)

GetLastCommitHash returns the hash of the last commit Parameters:

  • repoPath: The file system path to the git repository

Returns:

  • string: The hash of the last commit
  • error: An error if the git command fails or if there are issues accessing the repository

func (*GitVCS) GetStagedDiffFiltered

func (g *GitVCS) GetStagedDiffFiltered(repoPath string, cfgManager config.ManagerInterface) (string, error)

GetStagedDiffFiltered returns the git diff for staged changes, excluding files that match the patterns specified in the config manager under the "file_ignore" key.

Parameters:

  • repoPath: The file system path to the git repository
  • cfgManager: The config manager to use for retrieving ignore patterns

Returns:

  • string: The filtered diff output
  • error: An error if the git command fails or if there are issues accessing the repository

The function will return an empty string if there are no staged files in the repository. If the git command fails, it returns a detailed error message including the exit code.

func (*GitVCS) GetStagedFiles

func (g *GitVCS) GetStagedFiles(repoPath string) ([]string, error)

GetStagedFiles returns a list of files that are currently staged for commit in the git repository at the specified path. It executes the 'git diff --staged --name-only' command to get the list of staged files.

Parameters:

  • repoPath: The file system path to the git repository

Returns:

  • []string: A slice containing the paths of all staged files, or nil if no files are staged
  • error: An error if the git command fails or if there are issues accessing the repository

The function will return (nil, nil) if there are no staged files in the repository. If the git command fails, it returns a detailed error message including the exit code.

func (*GitVCS) HasStagedChanges

func (g *GitVCS) HasStagedChanges(repoPath string) (bool, error)

HasStagedChanges checks if there are any staged changes in the git repository at the given path. It runs "git diff --staged --quiet" command and interprets the exit code to determine if there are staged changes.

Parameters:

  • repoPath: The file system path to the git repository

Returns:

  • bool: true if there are staged changes, false otherwise
  • error: nil if the command executed successfully, error otherwise

The function returns true if the git diff command exits with code 1 (staged changes present), false if it exits with code 0 (no staged changes), and an error for any other exit code or if the command fails to execute.

type SVNVCS

type SVNVCS struct{}

SVNVCS implements the VCS interface for SVN

func (*SVNVCS) CreateCommit

func (s *SVNVCS) CreateCommit(repoPath, message string) error

CreateCommit creates a SVN commit with the given message

Parameters:

  • repoPath: The file system path to the SVN repository
  • message: The commit message

Returns:

  • error: An error if the SVN command fails or if there are issues accessing the repository

func (*SVNVCS) GetCommitInfo

func (s *SVNVCS) GetCommitInfo(repoPath, commitHash string) (string, error)

GetCommitInfo returns formatted information about the commit If commitHash is empty, returns info about the last commit

Parameters:

  • repoPath: The file system path to the SVN repository
  • commitHash: The hash of the commit to get info for (or empty for the last commit)

Returns:

  • string: The formatted commit info
  • error: An error if the SVN command fails or if there are issues accessing the repository

func (*SVNVCS) GetCurrentBranch

func (s *SVNVCS) GetCurrentBranch(repoPath string) (string, error)

GetCurrentBranch returns the name of the current branch in the SVN repository at the specified path. It runs the "svn info --show-item url" command to get the URL of the current branch, and extracts the branch name from it.

Parameters:

  • repoPath: The file system path to the SVN repository

Returns:

  • string: The name of the current branch
  • error: An error if the SVN command fails or if there are issues accessing the repository

func (*SVNVCS) GetDiff

func (s *SVNVCS) GetDiff(repoPath string) (string, error)

GetDiff retrieves the diff of the SVN repository at the specified path. It runs the "svn diff" command and returns its output as a string.

Parameters:

  • repoPath: The file system path to the SVN repository

Returns:

  • string: The diff output
  • error: An error if the SVN command fails or if there are issues accessing the repository

func (*SVNVCS) GetLastCommitHash

func (s *SVNVCS) GetLastCommitHash(repoPath string) (string, error)

GetLastCommitHash returns the number of the last commit in the SVN repository at the specified path.

Parameters:

  • repoPath: The file system path to the SVN repository

Returns:

  • string: The number of the last commit (as a string)
  • error: An error if the SVN command fails or if there are issues accessing the repository

func (*SVNVCS) GetStagedDiffFiltered

func (s *SVNVCS) GetStagedDiffFiltered(repoPath string, cfgManager config.ManagerInterface) (string, error)

GetStagedDiffFiltered returns the diff of staged changes, excluding files that match the patterns specified in the config manager under the "file_ignore" key.

Parameters:

  • repoPath: The file system path to the SVN repository
  • cfgManager: The config manager to use for retrieving ignore patterns

Returns:

  • string: The filtered diff output
  • error: An error if the svn command fails or if there are issues accessing the repository

func (*SVNVCS) GetStagedFiles

func (s *SVNVCS) GetStagedFiles(repoPath string) ([]string, error)

GetStagedFiles returns a list of files that are currently staged for commit in the SVN repository at the specified path. It executes the 'svn status' command and filters out lines that start with '?' and empty lines. The remaining lines are split after the first 7 characters and the resulting paths are returned in a slice.

Parameters:

  • repoPath: The file system path to the SVN repository

Returns:

  • []string: A slice containing the paths of all staged files, or nil if no files are staged
  • error: An error if the svn command fails or if there are issues accessing the repository

func (*SVNVCS) HasStagedChanges

func (s *SVNVCS) HasStagedChanges(repoPath string) (bool, error)

HasStagedChanges checks if there are any staged changes in the SVN repository at the given path. It runs the "svn status" command and checks if the output contains any lines. If there are lines, it returns true, otherwise false.

Parameters:

  • repoPath: The file system path to the SVN repository

Returns:

  • bool: true if there are staged changes, false otherwise
  • error: nil if the command executed successfully, error otherwise

type VCS

type VCS interface {
	GetDiff(repoPath string) (string, error)
	HasStagedChanges(repoPath string) (bool, error)
	GetStagedFiles(repoPath string) ([]string, error)
	GetStagedDiffFiltered(repoPath string, cfgManager config.ManagerInterface) (string, error)
	GetCurrentBranch(repoPath string) (string, error)
	GetCommitInfo(repoPath, commitHash string) (string, error)
	GetLastCommitHash(repoPath string) (string, error)
	CreateCommit(repoPath, message string) error
}

VCS defines the interface for version control operations

func NewVCS

func NewVCS(vcsType VCSType) (VCS, error)

NewVCS creates a new VCS object based on the given type.

Parameters:

  • vcsType: The type of VCS to create

Returns:

  • A VCS object of the specified type
  • An error if the type is not recognized

type VCSType

type VCSType string

VCSType represents the type of version control system

const (
	Git VCSType = "git"
	SVN VCSType = "svn"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL