2018-01-05 22:01:29 +00:00
|
|
|
|
package git
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
|
|
"github.com/rubyist/tracerx"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type RefUpdate struct {
|
commands,git,lfs: rename left/right RefUpdate vars
Since at least PR #2706, and indeed earlier, the local and remote
refs which are part of the RefUpdate structure have been referred
to as the "left" and "right" refs, terminology which stems from
the origin of this structure in the "git lfs pre-push" hook command,
where each line of input contains commit information in the form:
<local ref> <local sha1> <remote ref> <remote sha1>
However, outside of this context, "left" and "right" are ambiguous
terms, and may potentially be confused with the left and right
refs specified in a Git-style two-dot range notation. We therefore
replace these terms with "local" and "remote", which should help
clarify their purpose throughout the codebase.
2022-02-07 03:09:53 +00:00
|
|
|
|
git Env
|
|
|
|
|
remote string
|
|
|
|
|
localRef *Ref
|
|
|
|
|
remoteRef *Ref
|
2018-01-05 22:01:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
commands,git,lfs: rename left/right RefUpdate vars
Since at least PR #2706, and indeed earlier, the local and remote
refs which are part of the RefUpdate structure have been referred
to as the "left" and "right" refs, terminology which stems from
the origin of this structure in the "git lfs pre-push" hook command,
where each line of input contains commit information in the form:
<local ref> <local sha1> <remote ref> <remote sha1>
However, outside of this context, "left" and "right" are ambiguous
terms, and may potentially be confused with the left and right
refs specified in a Git-style two-dot range notation. We therefore
replace these terms with "local" and "remote", which should help
clarify their purpose throughout the codebase.
2022-02-07 03:09:53 +00:00
|
|
|
|
func NewRefUpdate(g Env, remote string, localRef, remoteRef *Ref) *RefUpdate {
|
2018-01-05 22:01:29 +00:00
|
|
|
|
return &RefUpdate{
|
commands,git,lfs: rename left/right RefUpdate vars
Since at least PR #2706, and indeed earlier, the local and remote
refs which are part of the RefUpdate structure have been referred
to as the "left" and "right" refs, terminology which stems from
the origin of this structure in the "git lfs pre-push" hook command,
where each line of input contains commit information in the form:
<local ref> <local sha1> <remote ref> <remote sha1>
However, outside of this context, "left" and "right" are ambiguous
terms, and may potentially be confused with the left and right
refs specified in a Git-style two-dot range notation. We therefore
replace these terms with "local" and "remote", which should help
clarify their purpose throughout the codebase.
2022-02-07 03:09:53 +00:00
|
|
|
|
git: g,
|
|
|
|
|
remote: remote,
|
|
|
|
|
localRef: localRef,
|
|
|
|
|
remoteRef: remoteRef,
|
2018-01-05 22:01:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
commands,git,lfs: rename left/right RefUpdate vars
Since at least PR #2706, and indeed earlier, the local and remote
refs which are part of the RefUpdate structure have been referred
to as the "left" and "right" refs, terminology which stems from
the origin of this structure in the "git lfs pre-push" hook command,
where each line of input contains commit information in the form:
<local ref> <local sha1> <remote ref> <remote sha1>
However, outside of this context, "left" and "right" are ambiguous
terms, and may potentially be confused with the left and right
refs specified in a Git-style two-dot range notation. We therefore
replace these terms with "local" and "remote", which should help
clarify their purpose throughout the codebase.
2022-02-07 03:09:53 +00:00
|
|
|
|
func (u *RefUpdate) LocalRef() *Ref {
|
|
|
|
|
return u.localRef
|
2018-01-05 22:01:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
commands,git,lfs: rename left/right RefUpdate vars
Since at least PR #2706, and indeed earlier, the local and remote
refs which are part of the RefUpdate structure have been referred
to as the "left" and "right" refs, terminology which stems from
the origin of this structure in the "git lfs pre-push" hook command,
where each line of input contains commit information in the form:
<local ref> <local sha1> <remote ref> <remote sha1>
However, outside of this context, "left" and "right" are ambiguous
terms, and may potentially be confused with the left and right
refs specified in a Git-style two-dot range notation. We therefore
replace these terms with "local" and "remote", which should help
clarify their purpose throughout the codebase.
2022-02-07 03:09:53 +00:00
|
|
|
|
func (u *RefUpdate) LocalRefCommitish() string {
|
|
|
|
|
return refCommitish(u.LocalRef())
|
2018-01-05 22:01:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
commands,git,lfs: rename left/right RefUpdate vars
Since at least PR #2706, and indeed earlier, the local and remote
refs which are part of the RefUpdate structure have been referred
to as the "left" and "right" refs, terminology which stems from
the origin of this structure in the "git lfs pre-push" hook command,
where each line of input contains commit information in the form:
<local ref> <local sha1> <remote ref> <remote sha1>
However, outside of this context, "left" and "right" are ambiguous
terms, and may potentially be confused with the left and right
refs specified in a Git-style two-dot range notation. We therefore
replace these terms with "local" and "remote", which should help
clarify their purpose throughout the codebase.
2022-02-07 03:09:53 +00:00
|
|
|
|
func (u *RefUpdate) RemoteRef() *Ref {
|
|
|
|
|
if u.remoteRef == nil {
|
|
|
|
|
u.remoteRef = defaultRemoteRef(u.git, u.remote, u.LocalRef())
|
2018-01-05 22:01:29 +00:00
|
|
|
|
}
|
commands,git,lfs: rename left/right RefUpdate vars
Since at least PR #2706, and indeed earlier, the local and remote
refs which are part of the RefUpdate structure have been referred
to as the "left" and "right" refs, terminology which stems from
the origin of this structure in the "git lfs pre-push" hook command,
where each line of input contains commit information in the form:
<local ref> <local sha1> <remote ref> <remote sha1>
However, outside of this context, "left" and "right" are ambiguous
terms, and may potentially be confused with the left and right
refs specified in a Git-style two-dot range notation. We therefore
replace these terms with "local" and "remote", which should help
clarify their purpose throughout the codebase.
2022-02-07 03:09:53 +00:00
|
|
|
|
return u.remoteRef
|
2018-01-05 22:01:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
commands,git,lfs: rename left/right RefUpdate vars
Since at least PR #2706, and indeed earlier, the local and remote
refs which are part of the RefUpdate structure have been referred
to as the "left" and "right" refs, terminology which stems from
the origin of this structure in the "git lfs pre-push" hook command,
where each line of input contains commit information in the form:
<local ref> <local sha1> <remote ref> <remote sha1>
However, outside of this context, "left" and "right" are ambiguous
terms, and may potentially be confused with the left and right
refs specified in a Git-style two-dot range notation. We therefore
replace these terms with "local" and "remote", which should help
clarify their purpose throughout the codebase.
2022-02-07 03:09:53 +00:00
|
|
|
|
func defaultRemoteRef(g Env, remote string, localRef *Ref) *Ref {
|
2018-01-05 22:01:29 +00:00
|
|
|
|
pushMode, _ := g.Get("push.default")
|
|
|
|
|
switch pushMode {
|
|
|
|
|
case "", "simple":
|
commands,git,lfs: rename left/right RefUpdate vars
Since at least PR #2706, and indeed earlier, the local and remote
refs which are part of the RefUpdate structure have been referred
to as the "left" and "right" refs, terminology which stems from
the origin of this structure in the "git lfs pre-push" hook command,
where each line of input contains commit information in the form:
<local ref> <local sha1> <remote ref> <remote sha1>
However, outside of this context, "left" and "right" are ambiguous
terms, and may potentially be confused with the left and right
refs specified in a Git-style two-dot range notation. We therefore
replace these terms with "local" and "remote", which should help
clarify their purpose throughout the codebase.
2022-02-07 03:09:53 +00:00
|
|
|
|
brRemote, _ := g.Get(fmt.Sprintf("branch.%s.remote", localRef.Name))
|
2018-01-05 22:01:29 +00:00
|
|
|
|
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.
|
commands,git,lfs: rename left/right RefUpdate vars
Since at least PR #2706, and indeed earlier, the local and remote
refs which are part of the RefUpdate structure have been referred
to as the "left" and "right" refs, terminology which stems from
the origin of this structure in the "git lfs pre-push" hook command,
where each line of input contains commit information in the form:
<local ref> <local sha1> <remote ref> <remote sha1>
However, outside of this context, "left" and "right" are ambiguous
terms, and may potentially be confused with the left and right
refs specified in a Git-style two-dot range notation. We therefore
replace these terms with "local" and "remote", which should help
clarify their purpose throughout the codebase.
2022-02-07 03:09:53 +00:00
|
|
|
|
return trackingRef(g, localRef)
|
2018-01-05 22:01:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// When pushing to a remote that is different from the remote you normally
|
|
|
|
|
// pull from, work as current.
|
commands,git,lfs: rename left/right RefUpdate vars
Since at least PR #2706, and indeed earlier, the local and remote
refs which are part of the RefUpdate structure have been referred
to as the "left" and "right" refs, terminology which stems from
the origin of this structure in the "git lfs pre-push" hook command,
where each line of input contains commit information in the form:
<local ref> <local sha1> <remote ref> <remote sha1>
However, outside of this context, "left" and "right" are ambiguous
terms, and may potentially be confused with the left and right
refs specified in a Git-style two-dot range notation. We therefore
replace these terms with "local" and "remote", which should help
clarify their purpose throughout the codebase.
2022-02-07 03:09:53 +00:00
|
|
|
|
return localRef
|
2018-01-05 22:01:29 +00:00
|
|
|
|
case "upstream", "tracking":
|
|
|
|
|
// push the current branch back to the branch whose changes are usually
|
|
|
|
|
// integrated into the current branch
|
commands,git,lfs: rename left/right RefUpdate vars
Since at least PR #2706, and indeed earlier, the local and remote
refs which are part of the RefUpdate structure have been referred
to as the "left" and "right" refs, terminology which stems from
the origin of this structure in the "git lfs pre-push" hook command,
where each line of input contains commit information in the form:
<local ref> <local sha1> <remote ref> <remote sha1>
However, outside of this context, "left" and "right" are ambiguous
terms, and may potentially be confused with the left and right
refs specified in a Git-style two-dot range notation. We therefore
replace these terms with "local" and "remote", which should help
clarify their purpose throughout the codebase.
2022-02-07 03:09:53 +00:00
|
|
|
|
return trackingRef(g, localRef)
|
2018-01-05 22:01:29 +00:00
|
|
|
|
case "current":
|
|
|
|
|
// push the current branch to update a branch with the same name on the
|
|
|
|
|
// receiving end.
|
commands,git,lfs: rename left/right RefUpdate vars
Since at least PR #2706, and indeed earlier, the local and remote
refs which are part of the RefUpdate structure have been referred
to as the "left" and "right" refs, terminology which stems from
the origin of this structure in the "git lfs pre-push" hook command,
where each line of input contains commit information in the form:
<local ref> <local sha1> <remote ref> <remote sha1>
However, outside of this context, "left" and "right" are ambiguous
terms, and may potentially be confused with the left and right
refs specified in a Git-style two-dot range notation. We therefore
replace these terms with "local" and "remote", which should help
clarify their purpose throughout the codebase.
2022-02-07 03:09:53 +00:00
|
|
|
|
return localRef
|
2018-01-05 22:01:29 +00:00
|
|
|
|
default:
|
|
|
|
|
tracerx.Printf("WARNING: %q push mode not supported", pushMode)
|
commands,git,lfs: rename left/right RefUpdate vars
Since at least PR #2706, and indeed earlier, the local and remote
refs which are part of the RefUpdate structure have been referred
to as the "left" and "right" refs, terminology which stems from
the origin of this structure in the "git lfs pre-push" hook command,
where each line of input contains commit information in the form:
<local ref> <local sha1> <remote ref> <remote sha1>
However, outside of this context, "left" and "right" are ambiguous
terms, and may potentially be confused with the left and right
refs specified in a Git-style two-dot range notation. We therefore
replace these terms with "local" and "remote", which should help
clarify their purpose throughout the codebase.
2022-02-07 03:09:53 +00:00
|
|
|
|
return localRef
|
2018-01-05 22:01:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
commands,git,lfs: rename left/right RefUpdate vars
Since at least PR #2706, and indeed earlier, the local and remote
refs which are part of the RefUpdate structure have been referred
to as the "left" and "right" refs, terminology which stems from
the origin of this structure in the "git lfs pre-push" hook command,
where each line of input contains commit information in the form:
<local ref> <local sha1> <remote ref> <remote sha1>
However, outside of this context, "left" and "right" are ambiguous
terms, and may potentially be confused with the left and right
refs specified in a Git-style two-dot range notation. We therefore
replace these terms with "local" and "remote", which should help
clarify their purpose throughout the codebase.
2022-02-07 03:09:53 +00:00
|
|
|
|
func trackingRef(g Env, localRef *Ref) *Ref {
|
|
|
|
|
if merge, ok := g.Get(fmt.Sprintf("branch.%s.merge", localRef.Name)); ok {
|
2018-01-05 22:01:29 +00:00
|
|
|
|
return ParseRef(merge, "")
|
|
|
|
|
}
|
commands,git,lfs: rename left/right RefUpdate vars
Since at least PR #2706, and indeed earlier, the local and remote
refs which are part of the RefUpdate structure have been referred
to as the "left" and "right" refs, terminology which stems from
the origin of this structure in the "git lfs pre-push" hook command,
where each line of input contains commit information in the form:
<local ref> <local sha1> <remote ref> <remote sha1>
However, outside of this context, "left" and "right" are ambiguous
terms, and may potentially be confused with the left and right
refs specified in a Git-style two-dot range notation. We therefore
replace these terms with "local" and "remote", which should help
clarify their purpose throughout the codebase.
2022-02-07 03:09:53 +00:00
|
|
|
|
return localRef
|
2018-01-05 22:01:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
commands,git,lfs: rename left/right RefUpdate vars
Since at least PR #2706, and indeed earlier, the local and remote
refs which are part of the RefUpdate structure have been referred
to as the "left" and "right" refs, terminology which stems from
the origin of this structure in the "git lfs pre-push" hook command,
where each line of input contains commit information in the form:
<local ref> <local sha1> <remote ref> <remote sha1>
However, outside of this context, "left" and "right" are ambiguous
terms, and may potentially be confused with the left and right
refs specified in a Git-style two-dot range notation. We therefore
replace these terms with "local" and "remote", which should help
clarify their purpose throughout the codebase.
2022-02-07 03:09:53 +00:00
|
|
|
|
func (u *RefUpdate) RemoteRefCommitish() string {
|
|
|
|
|
return refCommitish(u.RemoteRef())
|
2018-01-05 22:01:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|