Debian Codegraph @

git/refs.go

raw symbols SCIP
1 package git
2
3 import (
4 "fmt"
5
6 "github.com/rubyist/tracerx"
7 )
8
9 type RefUpdate struct {
10 git Env
11 remote string
12 localRef *Ref
13 remoteRef *Ref
14 }
15
16 func NewRefUpdate(g Env, remote string, localRef, remoteRef *Ref) *RefUpdate {
17 return &RefUpdate{
18 git: g,
19 remote: remote,
20 localRef: localRef,
21 remoteRef: remoteRef,
22 }
23 }
24
25 func (u *RefUpdate) LocalRef() *Ref {
26 return u.localRef
27 }
28
29 func (u *RefUpdate) LocalRefCommitish() string {
30 return refCommitish(u.LocalRef())
31 }
32
33 func (u *RefUpdate) RemoteRef() *Ref {
34 if u.remoteRef == nil {
35 u.remoteRef = defaultRemoteRef(u.git, u.remote, u.LocalRef())
36 }
37 return u.remoteRef
38 }
39
40 // defaultRemoteRef returns the remote ref receiving a push based on the current
41 // repository config and local ref being pushed.
42 //
43 // See push.default rules in https://git-scm.com/docs/git-config
44 func defaultRemoteRef(g Env, remote string, localRef *Ref) *Ref {
45 pushMode, _ := g.Get("push.default")
46 switch pushMode {
47 case "", "simple":
48 brRemote, _ := g.Get(fmt.Sprintf("branch.%s.remote", localRef.Name))
49 if brRemote == remote {
50 // in centralized workflow, work like 'upstream' with an added safety to
51 // refuse to push if the upstream branch’s name is different from the
52 // local one.
53 return TrackingRef(g, localRef)
54 }
55
56 // When pushing to a remote that is different from the remote you normally
57 // pull from, work as current.
58 return localRef
59 case "upstream", "tracking":
60 // push the current branch back to the branch whose changes are usually
61 // integrated into the current branch
62 return TrackingRef(g, localRef)
63 case "current":
64 // push the current branch to update a branch with the same name on the
65 // receiving end.
66 return localRef
67 default:
68 tracerx.Printf("WARNING: %q push mode not supported", pushMode)
69 return localRef
70 }
71 }
72
73 // TrackingRef returns the upstream tracking ref for the given local ref, or the
74 // local ref itself if no tracking is configured.
75 func TrackingRef(g Env, localRef *Ref) *Ref {
76 if merge, ok := g.Get(fmt.Sprintf("branch.%s.merge", localRef.Name)); ok {
77 return ParseRef(merge, "")
78 }
79 return localRef
80 }
81
82 func (u *RefUpdate) RemoteRefCommitish() string {
83 return refCommitish(u.RemoteRef())
84 }
85
86 func refCommitish(r *Ref) string {
87 if len(r.Sha) > 0 {
88 return r.Sha
89 }
90 return r.Name
91 }
92
93 // copy of env
94 type Env interface {
95 Get(key string) (val string, ok bool)
96 }