recreate user branches after delete

This commit is contained in:
Jeff Carr 2025-01-19 08:48:17 -06:00
parent 62dd96ccec
commit ab9f3bf62d
2 changed files with 67 additions and 10 deletions

View File

@ -101,7 +101,9 @@ func rillCheckoutUser(repo *gitpb.Repo) error {
// repo is already on user branch
return nil
}
repo.CheckoutUser()
if err := repo.CheckoutUser(); err != nil {
return err
}
return nil
}

View File

@ -49,6 +49,20 @@ func doCleanRepo(repo *gitpb.Repo) error {
for _, l := range repo.GitConfig.Local {
log.Info("\tlocal branch name:", l.Name)
}
if argv.Clean.User != nil {
base := repo.GetUserBranchName()
if repo.Exists(filepath.Join(".git/refs/heads", base)) {
log.Info("Delete local branch:", base, repo.GetGoPath())
err := forceDeleteUserBranch(repo, base)
repo.Reload()
if err != nil {
log.Info("Delete local branch ERROR:", err)
return err
}
// return fmt.Errorf("todo")
}
return nil
}
for name, b := range repo.GitConfig.Branches {
if b.Name == "" {
@ -109,7 +123,7 @@ func verifyLocalBranchIsMerged(repo *gitpb.Repo, branch *gitpb.GitBranch) error
err = fmt.Errorf("repo %s BRANCH AND REMOTE CAN BE DELETED %s", repo.GetGoPath(), branch.Name)
log.Info(err)
if argv.Clean.Force != nil {
err = forceDeleteBranch(repo, base)
err = BADforceDeleteBranch(repo, base)
repo.Reload()
}
return err
@ -130,7 +144,7 @@ func verifyLocalBranchIsMerged(repo *gitpb.Repo, branch *gitpb.GitBranch) error
err := fmt.Errorf("repo %s BRANCH CAN PROBABLY BE DELETED base=%s fullname=%s", repo.GetGoPath(), base, branch.Name)
log.Info(err)
if argv.Clean.Force != nil {
err = forceDeleteBranch(repo, base)
err = BADforceDeleteBranch(repo, base)
repo.Reload()
}
return err
@ -182,7 +196,7 @@ func doCleanUserBranch(repo *gitpb.Repo, branch *gitpb.GitBranch) error {
}
log.Info("THIS USER BRANCH IS CLEAN TO DELETE", branch.Name)
if argv.Clean.Force != nil {
err := forceDeleteBranch(repo, branch.Name)
err := BADforceDeleteBranch(repo, branch.Name)
repo.Reload()
if err != nil {
return err
@ -276,7 +290,7 @@ func isSafeToDelete(repo *gitpb.Repo, old string) error {
}
// literally ignore all errors. delete everthing with no checks for now
func forceDeleteBranch(repo *gitpb.Repo, branch string) error {
func forceDeleteUserBranch(repo *gitpb.Repo, branch string) error {
if repo.IsDirty() {
log.Info(repo.GetGoPath(), "is dirty")
return nil
@ -285,6 +299,45 @@ func forceDeleteBranch(repo *gitpb.Repo, branch string) error {
log.Info(repo.GetGoPath(), branch, "is not the user branch", repo.GetUserBranchName())
return nil
}
if err := isSafeToDelete(repo, branch); err != nil {
log.Info(err)
return err
}
if err := requiresGitPush(repo, branch); err != nil {
log.Info(err)
return err
}
configSave = true
cmd := []string{"git", "branch", "-D", branch}
if _, err := repo.RunVerbose(cmd); err != nil {
log.Info("THE GIT BRANCH DELETE ERROR IS:", err)
// return err
}
log.Info("THIS USER REMOTE BRANCH MUST BE DELETED HERE", branch)
if repo.Exists(filepath.Join(".git/refs/remote/origin", branch)) {
// git push origin --delete jcarr
cmd = []string{"git", "push", "origin", "--delete", branch}
if _, err := repo.RunVerbose(cmd); err != nil {
log.Info("THE GIT BRANCH DELETE ERROR IS:", err)
// return err
}
}
cmd = []string{"git", "branch", "-D", "--remote", "origin/" + branch}
if _, err := repo.RunVerbose(cmd); err != nil {
log.Info("THE GIT BRANCH DELETE ERROR IS:", err)
// return err
}
// return fmt.Errorf("one at a time %s", repo.GetGoPath())
return nil
}
// literally ignore all errors. delete everthing with no checks for now
func BADforceDeleteBranch(repo *gitpb.Repo, branch string) error {
if repo.IsDirty() {
log.Info(repo.GetGoPath(), "is dirty")
return nil
}
configSave = true
cmd := []string{"git", "branch", "-D", branch}
@ -293,12 +346,14 @@ func forceDeleteBranch(repo *gitpb.Repo, branch string) error {
// return err
}
log.Info("THIS USER REMOTE BRANCH MUST BE DELETED HERE", branch)
if repo.Exists(filepath.Join(".git/refs/remote/origin", branch)) {
// git push origin --delete jcarr
cmd = []string{"git", "push", "origin", "--delete", branch}
if _, err := repo.RunVerbose(cmd); err != nil {
log.Info("THE GIT BRANCH DELETE ERROR IS:", err)
// return err
}
}
cmd = []string{"git", "branch", "-D", "--remote", "origin/" + branch}
if _, err := repo.RunVerbose(cmd); err != nil {
log.Info("THE GIT BRANCH DELETE ERROR IS:", err)