Debian Codegraph @

internal/command/pull.go

raw symbols SCIP
1 package command
2
3 import (
4 "os"
5 "strings"
6
7 gogit "github.com/go-git/go-git/v5"
8 "github.com/go-git/go-git/v5/plumbing"
9 "github.com/go-git/go-git/v5/plumbing/transport"
10 "github.com/go-git/go-git/v5/plumbing/transport/http"
11 "github.com/go-git/go-git/v5/storage"
12 gerr "github.com/isacikgoz/gitbatch/internal/errors"
13 "github.com/isacikgoz/gitbatch/internal/git"
14 )
15
16 var (
17 pullTryCount int
18 pullMaxTry = 1
19 )
20
21 // PullOptions defines the rules for pull operation
22 type PullOptions struct {
23 // Name of the remote to fetch from. Defaults to origin.
24 RemoteName string
25 // ReferenceName Remote branch to clone. If empty, uses HEAD.
26 ReferenceName string
27 // Fetch only ReferenceName if true.
28 SingleBranch bool
29 // Credentials holds the user and password information
30 Credentials *git.Credentials
31 // Process logs the output to stdout
32 Progress bool
33 // Force allows the pull to update a local branch even when the remote
34 // branch does not descend from it.
35 Force bool
36 // Mode is the command mode
37 CommandMode Mode
38 }
39
40 // Pull incorporates changes from a remote repository into the current branch.
41 func Pull(r *git.Repository, o *PullOptions) (err error) {
42 pullTryCount = 0
43
44 // here we configure pull operation
45 switch o.CommandMode {
46 case ModeLegacy:
47 err = pullWithGit(r, o)
48 return err
49 case ModeNative:
50 err = pullWithGoGit(r, o)
51 return err
52 }
53 return nil
54 }
55
56 func pullWithGit(r *git.Repository, options *PullOptions) (err error) {
57 args := make([]string, 0)
58 args = append(args, "pull")
59 // parse options to command line arguments
60 if len(options.RemoteName) > 0 {
61 args = append(args, options.RemoteName)
62 }
63 if options.Force {
64 args = append(args, "-f")
65 }
66 ref, _ := r.Repo.Head()
67 if out, err := Run(r.AbsPath, "git", args); err != nil {
68 return gerr.ParseGitError(out, err)
69 }
70 newref, _ := r.Repo.Head()
71 r.SetWorkStatus(git.Success)
72 msg, err := getMergeMessage(r, ref.Hash().String(), newref.Hash().String())
73 if err != nil {
74 msg = "couldn't get stat"
75 }
76 r.State.Message = msg
77 return r.Refresh()
78 }
79
80 func pullWithGoGit(r *git.Repository, options *PullOptions) (err error) {
81 opt := &gogit.PullOptions{
82 RemoteName: options.RemoteName,
83 SingleBranch: options.SingleBranch,
84 Force: options.Force,
85 }
86 if len(options.ReferenceName) > 0 {
87 ref := plumbing.NewRemoteReferenceName(options.RemoteName, options.ReferenceName)
88 opt.ReferenceName = ref
89 }
90 // if any credential is given, let's add it to the git.PullOptions
91 if options.Credentials != nil {
92 protocol, err := git.AuthProtocol(r.State.Remote)
93 if err != nil {
94 return err
95 }
96 if protocol == git.AuthProtocolHTTP || protocol == git.AuthProtocolHTTPS {
97 opt.Auth = &http.BasicAuth{
98 Username: options.Credentials.User,
99 Password: options.Credentials.Password,
100 }
101 } else {
102 return gerr.ErrInvalidAuthMethod
103 }
104 }
105 if options.Progress {
106 opt.Progress = os.Stdout
107 }
108 w, err := r.Repo.Worktree()
109 if err != nil {
110 return err
111 }
112 ref, _ := r.Repo.Head()
113 if err = w.Pull(opt); err != nil {
114 if err == gogit.NoErrAlreadyUpToDate {
115 // log.Error("error: " + err.Error())
116 // Already up-to-date
117 // TODO: submit a PR for this kind of error, this type of catch is lame
118 } else if err == storage.ErrReferenceHasChanged && pullTryCount < pullMaxTry {
119 pullTryCount++
120 if err := Fetch(r, &FetchOptions{
121 RemoteName: options.RemoteName,
122 }); err != nil {
123 return err
124 }
125 return Pull(r, options)
126 } else if strings.Contains(err.Error(), "SSH_AUTH_SOCK") {
127 // The env variable SSH_AUTH_SOCK is not defined, maybe git can handle this
128 return pullWithGit(r, options)
129 } else if err == transport.ErrAuthenticationRequired {
130 return gerr.ErrAuthenticationRequired
131 } else {
132 return pullWithGit(r, options)
133 }
134 }
135 newref, _ := r.Repo.Head()
136
137 msg, err := getMergeMessage(r, ref.Hash().String(), newref.Hash().String())
138 if err != nil {
139 msg = "couldn't get stat"
140 }
141 r.SetWorkStatus(git.Success)
142 r.State.Message = msg
143 return r.Refresh()
144 }