diff options
| author | İbrahim Serdar Açıkgöz <serdaracikgoz86@gmail.com> | 2018-12-05 15:51:27 +0300 |
|---|---|---|
| committer | İbrahim Serdar Açıkgöz <serdaracikgoz86@gmail.com> | 2018-12-05 15:51:27 +0300 |
| commit | c61f67ea883683a3c9e643592f9a17b9ab8e4109 (patch) | |
| tree | efbf65a0d7bbe03b1cbb6a1f1258cddfc19af4c3 | |
| parent | fixing conflicts after squashing commits (diff) | |
| download | gitbatch-c61f67ea883683a3c9e643592f9a17b9ab8e4109.tar.gz | |
prune option while fetching initiated
| -rw-r--r-- | pkg/git/git-commands.go | 10 | ||||
| -rw-r--r-- | pkg/git/remote.go | 2 | ||||
| -rw-r--r-- | pkg/git/remotebranch.go | 27 | ||||
| -rw-r--r-- | pkg/gui/remotebranchview.go | 8 |
4 files changed, 41 insertions, 6 deletions
diff --git a/pkg/git/git-commands.go b/pkg/git/git-commands.go index 9518322..fdcec12 100644 --- a/pkg/git/git-commands.go +++ b/pkg/git/git-commands.go @@ -93,6 +93,16 @@ func (entity *RepoEntity) FetchWithGit(remote string) error { return nil } +// DryFetchAndPruneWithGit is wrapper of the git fetch <remote> --prune --dry-run command +func (entity *RepoEntity) DryFetchAndPruneWithGit(remote string) string { + args := []string{"fetch", remote, "--prune", "--dry-run"} + d, err := helpers.RunCommandWithOutput(entity.AbsPath, "git", args) + if err != nil { + return "?" + } + return d +} + // PullWithGit is wrapper of the git pull <remote>/<branch> command func (entity *RepoEntity) PullWithGit(remote, branch string) error { args := []string{"pull", remote, branch} diff --git a/pkg/git/remote.go b/pkg/git/remote.go index 7ea896f..d52f4d3 100644 --- a/pkg/git/remote.go +++ b/pkg/git/remote.go @@ -48,7 +48,7 @@ func (entity *RepoEntity) loadRemotes() error { Name: rm.Config().Name, URL: rm.Config().URLs, } - remote.loadRemoteBranches(&r) + remote.loadRemoteBranches(entity) if len(remote.Branches) > 0 { remote.Branch = remote.Branches[0] } diff --git a/pkg/git/remotebranch.go b/pkg/git/remotebranch.go index 6ece4a2..a07d10e 100644 --- a/pkg/git/remotebranch.go +++ b/pkg/git/remotebranch.go @@ -2,10 +2,11 @@ package git import ( "errors" + "regexp" "strings" + "github.com/isacikgoz/gitbatch/pkg/helpers" log "github.com/sirupsen/logrus" - "gopkg.in/src-d/go-git.v4" "gopkg.in/src-d/go-git.v4/plumbing" "gopkg.in/src-d/go-git.v4/plumbing/storer" ) @@ -15,6 +16,7 @@ import ( type RemoteBranch struct { Name string Reference *plumbing.Reference + Deleted bool } // NextRemoteBranch iterates to the next remote branch @@ -36,19 +38,21 @@ func (remote *Remote) NextRemoteBranch() error { // search for the remote branches of the remote. It takes the go-git's repo // pointer in order to get storer struct -func (remote *Remote) loadRemoteBranches(r *git.Repository) error { +func (remote *Remote) loadRemoteBranches(entity *RepoEntity) error { remote.Branches = make([]*RemoteBranch, 0) - bs, err := remoteBranchesIter(r.Storer) + bs, err := remoteBranchesIter(entity.Repository.Storer) if err != nil { log.Warn("Cannot initiate iterator " + err.Error()) return err } defer bs.Close() err = bs.ForEach(func(b *plumbing.Reference) error { + deleted := false if strings.Split(b.Name().Short(), "/")[0] == remote.Name { remote.Branches = append(remote.Branches, &RemoteBranch{ Name: b.Name().Short(), Reference: b, + Deleted: deleted, }) } return nil @@ -86,3 +90,20 @@ func (remote *Remote) switchRemoteBranch(remoteBranchName string) error { } return errors.New("Remote branch not found.") } + +func deletedRemoteBranches(entity *RepoEntity, remote string) ([]string, error) { + deletedRemoteBranches := make([]string, 0) + output := entity.DryFetchAndPruneWithGit(remote) + output = helpers.TrimTrailingNewline(output) + re := regexp.MustCompile(` - \[deleted\].+-> `) + if output != "?" { + sliced := strings.Split(output, "\n") + for _, s := range sliced { + if re.MatchString(s) { + ss := re.ReplaceAllString(s, "") + deletedRemoteBranches = append(deletedRemoteBranches, ss) + } + } + } + return deletedRemoteBranches, nil +} diff --git a/pkg/gui/remotebranchview.go b/pkg/gui/remotebranchview.go index f720830..25b5b06 100644 --- a/pkg/gui/remotebranchview.go +++ b/pkg/gui/remotebranchview.go @@ -19,12 +19,16 @@ func (gui *Gui) updateRemoteBranches(g *gocui.Gui, entity *git.RepoEntity) error trb := len(entity.Remote.Branches) if trb > 0 { for i, r := range entity.Remote.Branches { + rName := r.Name + if r.Deleted { + rName = rName + ws + dirty + } if r.Name == entity.Remote.Branch.Name { currentindex = i - fmt.Fprintln(out, selectionIndicator+r.Name) + fmt.Fprintln(out, selectionIndicator+rName) continue } - fmt.Fprintln(out, tab+r.Name) + fmt.Fprintln(out, tab+rName) } if err = gui.smartAnchorRelativeToLine(out, currentindex, trb); err != nil { return err |
