File: refs.go

package info (click to toggle)
git-lfs 2.13.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,384 kB
  • sloc: sh: 16,421; makefile: 418; ruby: 100
file content (94 lines) | stat: -rw-r--r-- 2,120 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package git

import (
	"fmt"

	"github.com/rubyist/tracerx"
)

type RefUpdate struct {
	git    Env
	remote string
	left   *Ref
	right  *Ref
}

func NewRefUpdate(g Env, remote string, l, r *Ref) *RefUpdate {
	return &RefUpdate{
		git:    g,
		remote: remote,
		left:   l,
		right:  r,
	}
}

func (u *RefUpdate) Left() *Ref {
	return u.left
}

func (u *RefUpdate) LeftCommitish() string {
	return refCommitish(u.Left())
}

func (u *RefUpdate) Right() *Ref {
	if u.right == nil {
		u.right = defaultRemoteRef(u.git, u.remote, u.Left())
	}
	return u.right
}

// defaultRemoteRef returns the remote ref receiving a push based on the current
// repository config and local ref being pushed.
//
// See push.default rules in https://git-scm.com/docs/git-config
func defaultRemoteRef(g Env, remote string, left *Ref) *Ref {
	pushMode, _ := g.Get("push.default")
	switch pushMode {
	case "", "simple":
		brRemote, _ := g.Get(fmt.Sprintf("branch.%s.remote", left.Name))
		if brRemote == remote {
			// in centralized workflow, work like 'upstream' with an added safety to
			// refuse to push if the upstream branch’s name is different from the
			// local one.
			return trackingRef(g, left)
		}

		// When pushing to a remote that is different from the remote you normally
		// pull from, work as current.
		return left
	case "upstream", "tracking":
		// push the current branch back to the branch whose changes are usually
		// integrated into the current branch
		return trackingRef(g, left)
	case "current":
		// push the current branch to update a branch with the same name on the
		// receiving end.
		return left
	default:
		tracerx.Printf("WARNING: %q push mode not supported", pushMode)
		return left
	}
}

func trackingRef(g Env, left *Ref) *Ref {
	if merge, ok := g.Get(fmt.Sprintf("branch.%s.merge", left.Name)); ok {
		return ParseRef(merge, "")
	}
	return left
}

func (u *RefUpdate) RightCommitish() string {
	return refCommitish(u.Right())
}

func refCommitish(r *Ref) string {
	if len(r.Sha) > 0 {
		return r.Sha
	}
	return r.Name
}

// copy of env
type Env interface {
	Get(key string) (val string, ok bool)
}