diff options
author | Michael Muré <batolettre@gmail.com> | 2018-07-25 18:01:32 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2018-07-25 18:01:32 +0200 |
commit | 6a12373965aff9f80147f8b5bff6a5a104927365 (patch) | |
tree | ccc1e34fba059512acab39286a86f6b73c5ad318 /repository | |
parent | 49c90eab26875cbf3094d9a546ad29b426e174a1 (diff) | |
download | git-bug-6a12373965aff9f80147f8b5bff6a5a104927365.tar.gz |
more refactoring to have reusable bug action across different UI
Diffstat (limited to 'repository')
-rw-r--r-- | repository/git.go | 52 | ||||
-rw-r--r-- | repository/mock_repo.go | 17 | ||||
-rw-r--r-- | repository/repo.go | 4 |
3 files changed, 65 insertions, 8 deletions
diff --git a/repository/git.go b/repository/git.go index c6bc5009..17b2096f 100644 --- a/repository/git.go +++ b/repository/git.go @@ -3,7 +3,6 @@ package repository import ( "bytes" - "crypto/sha1" "fmt" "github.com/MichaelMure/git-bug/util" "io" @@ -19,7 +18,7 @@ type GitRepo struct { // Run the given git command with the given I/O reader/writers, returning an error if it fails. func (repo *GitRepo) runGitCommandWithIO(stdin io.Reader, stdout, stderr io.Writer, args ...string) error { - fmt.Println("Running git", strings.Join(args, " ")) + //fmt.Println("Running git", strings.Join(args, " ")) cmd := exec.Command("git", args...) cmd.Dir = repo.Path @@ -74,17 +73,29 @@ func NewGitRepo(path string) (*GitRepo, error) { return nil, err } +func InitGitRepo(path string) (*GitRepo, error) { + repo := &GitRepo{Path: path} + _, err := repo.runGitCommand("init", path) + if err != nil { + return nil, err + } + return repo, nil +} + +func InitBareGitRepo(path string) (*GitRepo, error) { + repo := &GitRepo{Path: path} + _, err := repo.runGitCommand("init", "--bare", path) + if err != nil { + return nil, err + } + return repo, nil +} + // GetPath returns the path to the repo. func (repo *GitRepo) GetPath() string { return repo.Path } -// GetRepoStateHash returns a hash which embodies the entire current state of a repository. -func (repo *GitRepo) GetRepoStateHash() (string, error) { - stateSummary, err := repo.runGitCommand("show-ref") - return fmt.Sprintf("%x", sha1.Sum([]byte(stateSummary))), err -} - // GetUserName returns the name the the user has used to configure git func (repo *GitRepo) GetUserName() (string, error) { return repo.runGitCommand("config", "user.name") @@ -189,6 +200,24 @@ func (repo *GitRepo) UpdateRef(ref string, hash util.Hash) error { // ListRefs will return a list of Git ref matching the given refspec func (repo *GitRepo) ListRefs(refspec string) ([]string, error) { + stdout, err := repo.runGitCommand("for-each-ref", "--format=%(refname)", refspec) + + if err != nil { + return nil, err + } + + splitted := strings.Split(stdout, "\n") + + if len(splitted) == 1 && splitted[0] == "" { + return []string{}, nil + } + + return splitted, nil +} + +// ListIds will return a list of Git ref matching the given refspec, +// stripped to only the last part of the ref +func (repo *GitRepo) ListIds(refspec string) ([]string, error) { // the format option will strip the ref name to keep only the last part (ie, the bug id) stdout, err := repo.runGitCommand("for-each-ref", "--format=%(refname:lstrip=-1)", refspec) @@ -274,3 +303,10 @@ func (repo *GitRepo) GetTreeHash(commit util.Hash) (util.Hash, error) { return util.Hash(stdout), nil } + +// Add a new remote to the repository +func (repo *GitRepo) AddRemote(name string, url string) error { + _, err := repo.runGitCommand("remote", "add", name, url) + + return err +} diff --git a/repository/mock_repo.go b/repository/mock_repo.go index 4bd25738..f8653c64 100644 --- a/repository/mock_repo.go +++ b/repository/mock_repo.go @@ -3,6 +3,8 @@ package repository import ( "crypto/sha1" "fmt" + "strings" + "github.com/MichaelMure/git-bug/util" ) @@ -134,6 +136,21 @@ func (r *mockRepoForTest) ListRefs(refspec string) ([]string, error) { return keys, nil } +// ListIds will return a list of Git ref matching the given refspec, +// stripped to only the last part of the ref +func (r *mockRepoForTest) ListIds(refspec string) ([]string, error) { + keys := make([]string, len(r.refs)) + + i := 0 + for k := range r.refs { + splitted := strings.Split(k, "/") + keys[i] = splitted[len(splitted)-1] + i++ + } + + return keys, nil +} + func (r *mockRepoForTest) ListCommits(ref string) ([]util.Hash, error) { var hashes []util.Hash diff --git a/repository/repo.go b/repository/repo.go index 6ab7be91..38d8a6cb 100644 --- a/repository/repo.go +++ b/repository/repo.go @@ -48,6 +48,10 @@ type Repo interface { // ListRefs will return a list of Git ref matching the given refspec ListRefs(refspec string) ([]string, error) + // ListIds will return a list of Git ref matching the given refspec, + // stripped to only the last part of the ref + ListIds(refspec string) ([]string, error) + // RefExist will check if a reference exist in Git RefExist(ref string) (bool, error) |