summaryrefslogtreecommitdiff
path: root/doDev.go
blob: be4ea36de5a47141594c41812bdf9c080553208c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// 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
}