// Copyright 2017-2025 WIT.COM Inc. All rights reserved. // Use of this source code is governed by the GPL 3.0 package main import ( "strings" "go.wit.com/lib/protobuf/forgepb" "go.wit.com/lib/protobuf/gitpb" "go.wit.com/log" ) // FORGE USES THESE TO RECOVER FROM WHEN TOOLKITS FAIL TO LOAD // so don't delete them func doDev() (string, error) { if argv.Dev.DeleteUser { found := gitpb.NewRepos() setForgeMode(forgepb.ForgeMode_MASTER) for repo := range me.forge.Repos.IterByNamespace() { uremoteref := repo.GetRemoteTag(repo.GetUserBranchName()) if uremoteref == nil { continue } found.Repos = append(found.Repos, repo) if !repo.IsMasterBranch() { log.Info("you just be in the master branch") repo.State = "needs master branch" continue } doFixDeleteUserBranches(repo, uremoteref) } me.forge.PrintHumanTable(found) } return "", nil } func doFixDeleteUserBranches(repo *gitpb.Repo, remoteRef *gitpb.GitTag) { // log.Info("delete remote user", repo.FullPath) localTag := repo.IsBranchLocal(repo.GetUserBranchName()) if localTag != nil { // log.Info("THERE IS local user", repo.FullPath, localTag.Refname) log.Printf("%-13.13s %-55.55s %v\n", "LOCAL USER", repo.FullPath, localTag.Refname+" local branch exists") doFixDeleteUserLocalBranch(repo, remoteRef, localTag) return } log.Printf("%-13.13s %-55.55s %v\n", "NO LOCAL USER", repo.FullPath, "only has remote user branch") } // git push --delete origin jcarr // git push origin :refs/remotes/origin/jcarr func doFixDeleteRemoteUserBranch(repo *gitpb.Repo, remoteRef *gitpb.GitTag, localRef *gitpb.GitTag) (error, *notesList) { return nil, nil } // git branch -D refs/heads/jcarr // git update-ref -d refs/heads/jcarr func doFixDeleteUserLocalBranch(repo *gitpb.Repo, remoteRef *gitpb.GitTag, localRef *gitpb.GitTag) (error, *notesList) { notes := new(notesList) // get local user, remote user & local devel branches dref := repo.GetLocalDevelRef() localUser := repo.NewCompareRef(localRef) remoteUser := repo.NewCompareRef(remoteRef) localDevel := repo.NewCompareRef(dref) // Is the localUser completely inside the remoteUser branch? hashok, hashbad, cmd1, cmd2, err := remoteUser.DeleteBranch(localUser) for _, line := range hashok { notes.addNote("OK", repo, remoteUser.GetRef(), cmd1, line) } for _, line := range hashbad { notes.addNote("BAD", repo, localUser.GetRef(), cmd2, line) } if err == nil { // git update-ref -d refs/heads/jcarr cmd := []string{"git", "update-ref", "-d", localUser.GetRefname()} log.Printf("%-13.13s %-55.55s %v %s\n", "CMD OK", repo.FullPath, cmd1, "") log.Printf("%-13.13s %-55.55s %v %s\n", "CMD OK", repo.FullPath, cmd2, "") log.Printf("%-13.13s %-55.55s %v %s\n", "SAFE TO DELETE", repo.FullPath, cmd, "add --fix") if argv.Fix { err := repo.RunVerbose(cmd) if err != nil { log.Info(localUser.GetRefname(), repo.FullPath) s := "local user branch could not be deleted" me.sh.BadExit(s, err) } return nil, notes } return log.Errorf("argv needs --fix"), notes } // Is the localUser completely inside the localDevel branch? hashok, hashbad, cmd1, cmd2, err = localDevel.DeleteBranch(localUser) for _, line := range hashok { notes.addNote("OK", repo, localDevel.GetRef(), cmd1, line) } for _, line := range hashbad { notes.addNote("BAD", repo, localUser.GetRef(), cmd2, line) } // git update-ref -d refs/heads/jcarr if err == nil { cmd := []string{"git", "update-ref", "-d", localUser.GetRefname()} log.Printf("%-13.13s %-55.55s %v %s\n", "CMD OK", repo.FullPath, cmd1, "") log.Printf("%-13.13s %-55.55s %v %s\n", "CMD OK", repo.FullPath, cmd2, "") log.Printf("%-13.13s %-55.55s %v %s\n", "SAFE TO DELETE", repo.FullPath, cmd, "add --fix") if argv.Fix { err := repo.RunVerbose(cmd) if err != nil { log.Info(localUser.GetRefname(), repo.FullPath) s := "local user branch could not be deleted" me.sh.BadExit(s, err) } } return nil, notes } else { log.Printf("%-13.13s %-55.55s %v %s %v\n", "CMD ERR", repo.FullPath, err, "NOT SAFE TO DELETE. Reload()?", cmd1) log.Printf("%-13.13s %-55.55s %v %s %v\n", "CMD ERR", repo.FullPath, err, "NOT SAFE TO DELETE. Reload()?", cmd2) } // log.Printf("%-13.13s %-55.55s %v %s\n", "NOT SAFE", repo.FullPath, localUser.GetRefname(), "NOT SAFE TO DELETE") return log.Errorf("NOT SAFE"), notes } // notes.addNote("SAFE", repo, remoteUser, line) func (notes *notesList) addNote(note string, repo *gitpb.Repo, ref *gitpb.GitTag, cmd []string, line string) { newnote := new(DeleteBranchNotes) newnote.Note = note newnote.Refname = ref.Refname newnote.Fullpath = repo.FullPath newnote.Cmd = cmd parts := strings.Split(line, "%00") // git log doesn't actually convert %00 to NULL if len(parts) != 4 { log.Info("len", len(parts)) panic("nope") } newnote.H = parts[0] newnote.Ae = parts[1] newnote.As = parts[2] newnote.S = parts[3] // newnote.Hash = parts[0] log.Printf("%-5.5s %7.7s %-55.55s %v\n", note, newnote.H, newnote.Fullpath, cmd) notes.all = append(notes.all, newnote) } type notesList struct { all []*DeleteBranchNotes } type DeleteBranchNotes struct { Note string Refname string Fullpath string Cmd []string // git log variable names H string S string Ae string As string }