From: George Dunlap Date: Fri, 5 May 2017 14:33:28 +0000 (+0100) Subject: git: Delete stackgit data when deleting or resetting branches X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=44fac9f2943e284e7bcacf989bb3e6b23dbafd14;p=people%2Fgdunlap%2Fxsatool git: Delete stackgit data when deleting or resetting branches If a developer has modified a branch to be a stackgit branch as part of development, simply deleting or resetting that branch with git commands will "work", but will leave leftover state lying around which may cause problems later. Before deleting or resetting a branch, check to see if the branch is a stackgit branch. If so, use `stg branch --delete --force` to get rid of all the stackgit data. Signed-off-by: George Dunlap --- diff --git a/git.go b/git.go index ba214a0..f49c5f0 100644 --- a/git.go +++ b/git.go @@ -47,6 +47,29 @@ func (r Repo) gitCmd(args ...string) (out []byte, err error) { return } +// Just for fun, stackgit doesn't have a way to set the working directory +func (r Repo) stgCmd(args ...string) (out []byte, err error) { + if r.Path != "" { + cwd, err := os.Getwd() + if err != nil { + return nil, err + } + defer os.Chdir(cwd) + + os.Chdir(r.Path) + } + fmt.Println(args) + out, err = exec.Command("stg", args...).CombinedOutput() + fmt.Println(string(out)) + return +} + +// Will return false if stg is not installed. +func (r Repo) IsStg(branch string) (bool) { + _, err := r.stgCmd("series", "-b", branch) + return err == nil +} + func GitClone(gpath string, origin string) (r Repo, err error) { if !path.IsAbs(gpath) { cwd, err := os.Getwd() @@ -100,6 +123,14 @@ func (r Repo) GetPath() string { // FIXME: Option to fail if it already exists? func (r Repo) MakeBranch(branch string, ref string) (err error) { + // Can't simply reset a stackgit branch, but if we delete the + // old one and checkout the new one it's the same effect + if r.IsStg(branch) { + _, err = r.stgCmd("branch", "--delete", "--force", branch) + if err != nil { + return + } + } _, err = r.gitCmd("checkout", "-B", branch, ref) if err == nil { _, err = r.gitCmd("clean", "-ffdx") @@ -108,6 +139,14 @@ func (r Repo) MakeBranch(branch string, ref string) (err error) { return } +// These are basically identical now; but I feel like there needs to +// be a distinction made +func (r Repo) ResetBranch(branch string, ref string) (err error) { + return r.MakeBranch(branch, ref) +} + + + func (r Repo) Checkout(branch string) (out []byte, err error) { out, err = r.gitCmd("checkout", branch) if err == nil { @@ -142,13 +181,11 @@ func (r Repo) SetRef(ref string, value string) (out []byte, err error) { } func (r Repo) DeleteBranch(branch string) (out []byte, err error) { - out, err = r.gitCmd("branch", "-D", branch) - - return -} - -func (r Repo) ResetBranch(branch string, commit string) (out []byte, err error) { - out, err = r.gitCmd("checkout", "-B", branch, commit) + if r.IsStg(branch) { + out, err = r.stgCmd("branch", "--delete", "--force", branch) + } else { + out, err = r.gitCmd("branch", "-D", branch) + } return } diff --git a/systemtest.go b/systemtest.go index ac0b686..906be23 100644 --- a/systemtest.go +++ b/systemtest.go @@ -128,7 +128,7 @@ func InitRepo(st *SystemTest) bool { // Only update the local branch if it already exists if _, tErr := r.ParseRef(branch); tErr == nil { - if _, err = r.ResetBranch(branch, fullRef); err != nil { + if err = r.ResetBranch(branch, fullRef); err != nil { return } }