| 1 | package git |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os/exec" |
| 6 | "sort" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | |
| 10 | git "github.com/go-git/go-git/v5" |
| 11 | "github.com/go-git/go-git/v5/plumbing" |
| 12 | "github.com/go-git/go-git/v5/plumbing/object" |
| 13 | ) |
| 14 | |
| 15 | // Branch is the wrapper of go-git's Reference struct. In addition to that, it |
| 16 | // also holds name of the branch, pullable and pushable commit count from the |
| 17 | // branchs' upstream. It also tracks if the repository has unstaged or uncommit- |
| 18 | // ed changes |
| 19 | type Branch struct { |
| 20 | Name string |
| 21 | Reference *plumbing.Reference |
| 22 | Upstream *RemoteBranch |
| 23 | Commits []*Commit |
| 24 | State *BranchState |
| 25 | Pushables string |
| 26 | Pullables string |
| 27 | Clean bool |
| 28 | } |
| 29 | |
| 30 | // BranchState hold the ref commit |
| 31 | type BranchState struct { |
| 32 | Commit *Commit |
| 33 | } |
| 34 | |
| 35 | const ( |
| 36 | revlistCommand = "rev-list" |
| 37 | hashLength = 40 |
| 38 | ) |
| 39 | |
| 40 | // search for branches in go-git way. It is useful to do so that checkout and |
| 41 | // checkout error handling can be handled by code rather than struggling with |
| 42 | // git command and its output |
| 43 | func (r *Repository) initBranches() error { |
| 44 | lbs := make([]*Branch, 0) |
| 45 | bs, err := r.Repo.Branches() |
| 46 | if err != nil { |
| 47 | return err |
| 48 | } |
| 49 | defer bs.Close() |
| 50 | headRef, err := r.Repo.Head() |
| 51 | if err != nil { |
| 52 | return err |
| 53 | } |
| 54 | var branchFound bool |
| 55 | var push, pull string |
| 56 | _ = bs.ForEach(func(b *plumbing.Reference) error { |
| 57 | if b.Type() != plumbing.HashReference { |
| 58 | return nil |
| 59 | } |
| 60 | clean := r.isClean() |
| 61 | branch := &Branch{ |
| 62 | Name: b.Name().Short(), |
| 63 | Reference: b, |
| 64 | State: &BranchState{}, |
| 65 | Pushables: push, |
| 66 | Pullables: pull, |
| 67 | Clean: clean, |
| 68 | } |
| 69 | if b.Name() == headRef.Name() { |
| 70 | r.State.Branch = branch |
| 71 | branchFound = true |
| 72 | } |
| 73 | lbs = append(lbs, branch) |
| 74 | |
| 75 | return nil |
| 76 | }) |
| 77 | if !branchFound { |
| 78 | branch := &Branch{ |
| 79 | Name: headRef.Hash().String(), |
| 80 | Reference: headRef, |
| 81 | State: &BranchState{}, |
| 82 | Pushables: "?", |
| 83 | Pullables: "?", |
| 84 | Clean: r.isClean(), |
| 85 | } |
| 86 | lbs = append(lbs, branch) |
| 87 | r.State.Branch = branch |
| 88 | } |
| 89 | rb, err := getUpstream(r, r.State.Branch.Name) |
| 90 | if err == nil { |
| 91 | r.State.Branch.Upstream = rb |
| 92 | } |
| 93 | |
| 94 | r.Branches = lbs |
| 95 | return nil |
| 96 | } |
| 97 | |
| 98 | // Checkout to given branch. If any errors occur, the method returns it instead |
| 99 | // of returning nil |
| 100 | func (r *Repository) Checkout(b *Branch) error { |
| 101 | if b.Name == r.State.Branch.Name { |
| 102 | return nil |
| 103 | } |
| 104 | |
| 105 | w, err := r.Repo.Worktree() |
| 106 | if err != nil { |
| 107 | return err |
| 108 | } |
| 109 | if err = w.Checkout(&git.CheckoutOptions{ |
| 110 | Branch: b.Reference.Name(), |
| 111 | }); err != nil { |
| 112 | return err |
| 113 | } |
| 114 | r.State.Branch = b |
| 115 | |
| 116 | rb, err := getUpstream(r, r.State.Branch.Name) |
| 117 | if err == nil { |
| 118 | r.State.Branch.Upstream = rb |
| 119 | } |
| 120 | _ = b.initCommits(r) |
| 121 | |
| 122 | if err := r.Publish(BranchUpdated, nil); err != nil { |
| 123 | return err |
| 124 | } |
| 125 | if err := r.SyncRemoteAndBranch(b); err != nil { |
| 126 | return err |
| 127 | } |
| 128 | return r.Publish(RepositoryUpdated, nil) |
| 129 | } |
| 130 | |
| 131 | // checking the branch if it has any changes from its head revision. Initially |
| 132 | // I implemented this with go-git but it was incredibly slow and there is also |
| 133 | // an issue about it: https://github.com/src-d/go-git/issues/844 |
| 134 | func (r *Repository) isClean() bool { |
| 135 | args := []string{"status"} |
| 136 | cmd := exec.Command("git", args...) |
| 137 | cmd.Dir = r.AbsPath |
| 138 | out, err := cmd.CombinedOutput() |
| 139 | if err != nil { |
| 140 | return false |
| 141 | } |
| 142 | s := string(out) |
| 143 | if strings.HasSuffix(s, "\n") { |
| 144 | s = s[:len(s)-1] |
| 145 | } |
| 146 | if len(s) >= 0 { |
| 147 | vs := strings.Split(s, "\n") |
| 148 | line := vs[len(vs)-1] |
| 149 | // earlier versions of git returns "working directory clean" instead of |
| 150 | //"working tree clean" message |
| 151 | if strings.Contains(line, "working tree clean") || |
| 152 | strings.Contains(line, "working directory clean") { |
| 153 | return true |
| 154 | } |
| 155 | } |
| 156 | return false |
| 157 | } |
| 158 | |
| 159 | // RevListOptions defines the rules of rev-list func |
| 160 | type RevListOptions struct { |
| 161 | // Ref1 is the first reference hash to link |
| 162 | Ref1 string |
| 163 | // Ref2 is the second reference hash to link |
| 164 | Ref2 string |
| 165 | } |
| 166 | |
| 167 | // RevList is the legacy implementation of "git rev-list" command. |
| 168 | func RevList(r *Repository, options RevListOptions) ([]*object.Commit, error) { |
| 169 | args := make([]string, 0) |
| 170 | args = append(args, revlistCommand) |
| 171 | if len(options.Ref1) > 0 && len(options.Ref2) > 0 { |
| 172 | arg1 := options.Ref1 + ".." + options.Ref2 |
| 173 | args = append(args, arg1) |
| 174 | } |
| 175 | cmd := exec.Command("git", args...) |
| 176 | cmd.Dir = r.AbsPath |
| 177 | out, err := cmd.CombinedOutput() |
| 178 | if err != nil { |
| 179 | return nil, err |
| 180 | } |
| 181 | s := string(out) |
| 182 | hashes := strings.Split(s, "\n") |
| 183 | commits := make([]*object.Commit, 0) |
| 184 | for _, hash := range hashes { |
| 185 | if len(hash) == hashLength { |
| 186 | c, err := r.Repo.CommitObject(plumbing.NewHash(hash)) |
| 187 | if err != nil { |
| 188 | continue |
| 189 | } |
| 190 | commits = append(commits, c) |
| 191 | } |
| 192 | } |
| 193 | sort.Sort(CommitTime(commits)) |
| 194 | return commits, nil |
| 195 | } |
| 196 | |
| 197 | // SyncRemoteAndBranch synchronizes remote branch with current branch |
| 198 | func (r *Repository) SyncRemoteAndBranch(b *Branch) error { |
| 199 | headRef, err := r.Repo.Head() |
| 200 | if err != nil { |
| 201 | return err |
| 202 | } |
| 203 | if b.Upstream == nil { |
| 204 | b.Pullables = "?" |
| 205 | b.Pushables = "?" |
| 206 | return nil |
| 207 | } |
| 208 | |
| 209 | head := headRef.Hash().String() |
| 210 | var push, pull string |
| 211 | pushables, err := RevList(r, RevListOptions{ |
| 212 | Ref1: b.Upstream.Reference.Hash().String(), |
| 213 | Ref2: head, |
| 214 | }) |
| 215 | if err != nil { |
| 216 | push = "?" |
| 217 | } else { |
| 218 | push = strconv.Itoa(len(pushables)) |
| 219 | } |
| 220 | pullables, err := RevList(r, RevListOptions{ |
| 221 | Ref1: head, |
| 222 | Ref2: b.Upstream.Reference.Hash().String(), |
| 223 | }) |
| 224 | if err != nil { |
| 225 | pull = "?" |
| 226 | } else { |
| 227 | pull = strconv.Itoa(len(pullables)) |
| 228 | } |
| 229 | b.Pullables = pull |
| 230 | b.Pushables = push |
| 231 | return nil |
| 232 | } |
| 233 | |
| 234 | // InitializeCommits loads the commits |
| 235 | func (b *Branch) InitializeCommits(r *Repository) error { |
| 236 | return b.initCommits(r) |
| 237 | } |
| 238 | |
| 239 | func getUpstream(r *Repository, branchName string) (*RemoteBranch, error) { |
| 240 | args := []string{"config", "--get", "branch." + branchName + ".remote"} |
| 241 | cmd := exec.Command("git", args...) |
| 242 | cmd.Dir = r.AbsPath |
| 243 | cr, err := cmd.CombinedOutput() |
| 244 | if err != nil { |
| 245 | return nil, fmt.Errorf("upstream not found") |
| 246 | } |
| 247 | |
| 248 | args = []string{"config", "--get", "branch." + branchName + ".merge"} |
| 249 | cmd = exec.Command("git", args...) |
| 250 | cmd.Dir = r.AbsPath |
| 251 | cm, err := cmd.CombinedOutput() |
| 252 | if err != nil || !strings.Contains(string(cm), branchName) { |
| 253 | return nil, fmt.Errorf("default merge branch found") |
| 254 | } |
| 255 | |
| 256 | for _, rm := range r.Remotes { |
| 257 | if rm.Name == strings.TrimSpace(string(cr)) { |
| 258 | r.State.Remote = rm |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | for _, rb := range r.State.Remote.Branches { |
| 263 | if rb.Name == r.State.Remote.Name+"/"+branchName { |
| 264 | return rb, nil |
| 265 | } |
| 266 | } |
| 267 | return nil, fmt.Errorf("upstream not found") |
| 268 | } |