From aa02463ded9e8618375db4a3cb3c1b2593e96a8e Mon Sep 17 00:00:00 2001 From: John Olheiser <42128690+jolheiser@users.noreply.github.com> Date: Mon, 8 Apr 2019 00:08:02 -0500 Subject: Delete local branch if it exists (#6497) Signed-off-by: jolheiser --- models/repo_branch.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'models') diff --git a/models/repo_branch.go b/models/repo_branch.go index 9ea4ce45fb..1c62a3d67d 100644 --- a/models/repo_branch.go +++ b/models/repo_branch.go @@ -54,6 +54,38 @@ func (repo *Repository) CheckoutNewBranch(oldBranch, newBranch string) error { return checkoutNewBranch(repo.RepoPath(), repo.LocalCopyPath(), oldBranch, newBranch) } +// deleteLocalBranch deletes a branch from a local repo cache +// First checks out default branch to avoid trying to delete the currently checked out branch +func deleteLocalBranch(localPath, defaultBranch, deleteBranch string) error { + if !com.IsExist(localPath) { + return nil + } + + if !git.IsBranchExist(localPath, deleteBranch) { + return nil + } + + // Must NOT have branch currently checked out + // Checkout default branch first + if err := git.Checkout(localPath, git.CheckoutOptions{ + Timeout: time.Duration(setting.Git.Timeout.Pull) * time.Second, + Branch: defaultBranch, + }); err != nil { + return fmt.Errorf("git checkout %s: %v", defaultBranch, err) + } + + cmd := git.NewCommand("branch") + cmd.AddArguments("-D") + cmd.AddArguments(deleteBranch) + _, err := cmd.RunInDir(localPath) + return err +} + +// DeleteLocalBranch deletes a branch from the local repo +func (repo *Repository) DeleteLocalBranch(branchName string) error { + return deleteLocalBranch(repo.LocalCopyPath(), repo.DefaultBranch, branchName) +} + // Branch holds the branch information type Branch struct { Path string -- cgit v1.2.3