Skip to content

Delete local branch when repo branch is deleted #6497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions models/repo_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions routers/repo/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ func DeleteBranchPost(ctx *context.Context) {
return
}

// Delete branch in local copy if it exists
if err := ctx.Repo.Repository.DeleteLocalBranch(branchName); err != nil {
ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", branchName))
return
}

ctx.Flash.Success(ctx.Tr("repo.branch.deletion_success", branchName))
}

Expand Down