aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/vcs/git.go
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2020-10-15 14:54:25 +0200
committerDmitry Vyukov <dvyukov@google.com>2020-10-21 10:22:10 +0200
commitdd564a9d29871f791d7856edf3b38142b5b2c5e3 (patch)
tree0172ebab17fe35630e372f4d7305a2cbd61f0ba6 /pkg/vcs/git.go
parente2605999d9d1fa4e98bcf850ed007cfd98f27383 (diff)
pkg/vcs: add repo OptPrecious and OptDontSandbox options
The pkg/vcs code assumed that we fully manage the repo within an autonomous program. In particular it tried to repair any errors by dropping and re-creating the repo. This does not work well for command-line tools that work with a user-provided repo. Add OptPrecious for such uses. Update #2171
Diffstat (limited to 'pkg/vcs/git.go')
-rw-r--r--pkg/vcs/git.go56
1 files changed, 38 insertions, 18 deletions
diff --git a/pkg/vcs/git.go b/pkg/vcs/git.go
index f487b60c2..aeb9d734d 100644
--- a/pkg/vcs/git.go
+++ b/pkg/vcs/git.go
@@ -23,16 +23,26 @@ import (
type git struct {
dir string
- sandbox bool
ignoreCC map[string]bool
+ precious bool
+ sandbox bool
}
-func newGit(dir string, ignoreCC map[string]bool) *git {
- return &git{
+func newGit(dir string, ignoreCC map[string]bool, opts []RepoOpt) *git {
+ git := &git{
dir: dir,
- sandbox: true,
ignoreCC: ignoreCC,
+ sandbox: true,
}
+ for _, opt := range opts {
+ switch opt {
+ case OptPrecious:
+ git.precious = true
+ case OptDontSandbox:
+ git.sandbox = false
+ }
+ }
+ return git
}
func filterEnv() []string {
@@ -86,11 +96,8 @@ func (git *git) Poll(repo, branch string) (*Commit, error) {
}
func (git *git) CheckoutBranch(repo, branch string) (*Commit, error) {
- git.reset()
- if _, err := git.git("reset", "--hard"); err != nil {
- if err := git.initRepo(err); err != nil {
- return nil, err
- }
+ if err := git.repair(); err != nil {
+ return nil, err
}
_, err := git.git("fetch", repo, branch)
if err != nil {
@@ -103,11 +110,8 @@ func (git *git) CheckoutBranch(repo, branch string) (*Commit, error) {
}
func (git *git) CheckoutCommit(repo, commit string) (*Commit, error) {
- git.reset()
- if _, err := git.git("reset", "--hard"); err != nil {
- if err := git.initRepo(err); err != nil {
- return nil, err
- }
+ if err := git.repair(); err != nil {
+ return nil, err
}
if err := git.fetchRemote(repo); err != nil {
return nil, err
@@ -124,8 +128,10 @@ func (git *git) fetchRemote(repo string) error {
}
func (git *git) SwitchCommit(commit string) (*Commit, error) {
- git.git("reset", "--hard")
- git.git("clean", "-fdx")
+ if !git.precious {
+ git.git("reset", "--hard")
+ git.git("clean", "-fdx")
+ }
if _, err := git.git("checkout", commit); err != nil {
return nil, err
}
@@ -133,6 +139,9 @@ func (git *git) SwitchCommit(commit string) (*Commit, error) {
}
func (git *git) clone(repo, branch string) error {
+ if git.precious {
+ return fmt.Errorf("won't reinit precious repo")
+ }
if err := git.initRepo(nil); err != nil {
return err
}
@@ -145,12 +154,23 @@ func (git *git) clone(repo, branch string) error {
return nil
}
-func (git *git) reset() {
+func (git *git) reset() error {
// This function tries to reset git repo state to a known clean state.
+ if git.precious {
+ return nil
+ }
git.git("reset", "--hard")
git.git("clean", "-fdx")
git.git("bisect", "reset")
- git.git("reset", "--hard")
+ _, err := git.git("reset", "--hard")
+ return err
+}
+
+func (git *git) repair() error {
+ if err := git.reset(); err != nil {
+ return git.initRepo(err)
+ }
+ return nil
}
func (git *git) initRepo(reason error) error {