summaryrefslogtreecommitdiff
path: root/reloadBranches.go
blob: 39a0a12f6d9fdf65c755d35cdfa76de4e014aeba (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package gitpb

import (
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"
	"unicode/utf8"

	"go.wit.com/lib/gui/shell"
	"go.wit.com/log"
	timestamppb "google.golang.org/protobuf/types/known/timestamppb"
)

// TODO: make this report the error somewhere
// This is supposed to check all the branches to make sure
// they are the same. that was originally what this was for
// now I think it's jsut probably dumb old code that doesn't
// need to be here

// actually, this is to attempt to verify absolutely everything
// is pushed upstream before doing a rm -rf ~/go/src
// TODO: revisit this code in the autotypist later
func (repo *Repo) CheckBranches() string {
	var hashCheck string
	all := repo.GetBranches()
	path := filepath.Join(repo.FullPath, ".git/refs/")
	for _, b := range all {
		parts := strings.Split(b, "/")
		rdir := "heads"
		if len(parts) == 2 {
			rdir = "remotes"
		}
		fullfile := filepath.Join(path, rdir, b)

		// check if the ref name is "HEAD". if so, skip
		runeCount := utf8.RuneCountInString(fullfile)
		// Convert the string to a slice of runes
		runes := []rune(fullfile)
		// Slice the last 4 runes
		lastFour := runes[runeCount-4:]
		if string(lastFour) == "HEAD" {
			// assume HEAD is always a valid branch
			// log.Info("skip HEAD. always valid branch name", fullfile)
			continue
		}

		content, _ := ioutil.ReadFile(fullfile)
		hash := strings.TrimSpace(string(content))
		if hashCheck == "" {
			hashCheck = hash
		}
		var cmd []string
		cmd = append(cmd, "git", "show", "-s", "--format=%ci", hash)
		r := shell.PathRunLog(repo.GetFullPath(), cmd, INFO)
		if r.Error != nil {
			log.Log(WARN, "CheckBranches() git show error:", r.Error)
		}
		// git show -s --format=%ci <hash> will give you the time
		// log.Log(REPO, fullfile)
		if hash == hashCheck {
			// log.Info("notsure why this git show is here", hash)
		} else {
			// log.Printf("UNKNOWN BRANCH %-50s %s %s %s\n", repo.GetFullPath(), r.Stdout, cmd, b)
			return b
		}
	}
	return ""
}

func (repo *Repo) GetBranches() []string {
	var all []string
	var heads []string
	var remotes []string
	heads = ListFiles(filepath.Join(repo.GetFullPath(), "/.git/refs/heads"))
	remotes = ListFiles(filepath.Join(repo.GetFullPath(), "/.git/refs/remotes"))

	all = heads

	all = append(all, remotes...)

	// for _, branch := range all {
	//	log.Info("getBranches()", branch)
	// }
	return all
}

// goes in one directory so it gets remote branch names
// old code. todo: modernize it
func ListFiles(directory string) []string {
	var files []string
	fileInfo, err := os.ReadDir(directory)
	if err != nil {
		log.Error(err)
		return nil
	}

	for _, file := range fileInfo {
		if file.IsDir() {
			dirname := file.Name()
			newdir, _ := os.ReadDir(directory + "/" + dirname)
			for _, file := range newdir {
				if !file.IsDir() {
					files = append(files, dirname+"/"+file.Name())
				}
			}
		} else {
			files = append(files, file.Name())
		}
	}

	return files
}

// forge doesn't want a remote user branch
// this will make sure the user branch is only local
func (repo *Repo) checkUserBranch() error {
	ubn := repo.GetUserBranchName()
	log.Info("user branch name:", ubn, repo.GetGoPath())
	if repo.Config == nil {
		return fmt.Errorf("GitConfig == nil")
	}

	for _, l := range repo.Config.Local {
		log.Info("local branch name:", l.Name)
	}

	return nil
}

func (repo *Repo) ExamineBranches() *GitTag {
	var hashCheck string
	all := repo.GetBranches()
	path := filepath.Join(repo.FullPath, ".git/refs/")
	for _, b := range all {
		parts := strings.Split(b, "/")
		rdir := "heads"
		if len(parts) == 2 {
			rdir = "remotes"
		}
		fullfile := filepath.Join(path, rdir, b)

		// check if the ref name is "HEAD". if so, skip
		runeCount := utf8.RuneCountInString(fullfile)
		// Convert the string to a slice of runes
		runes := []rune(fullfile)
		// Slice the last 4 runes
		lastFour := runes[runeCount-4:]
		if string(lastFour) == "HEAD" {
			// assume HEAD is always a valid branch
			// log.Info("skip HEAD. always valid branch name", fullfile)
			continue
		}

		content, _ := ioutil.ReadFile(fullfile)
		hash := strings.TrimSpace(string(content))
		if hashCheck == "" {
			hashCheck = hash
		}
		var cmd []string
		cmd = append(cmd, "git", "show", "-s", "--format=%cI", hash)
		r := shell.PathRunLog(repo.GetFullPath(), cmd, INFO)
		if r.Error != nil {
			log.Log(WARN, "CheckBranches() git show error:", r.Error)
		}
		// git show -s --format=%ci <hash> will give you the time
		// log.Log(REPO, fullfile)
		if hash == hashCheck {
			// log.Info("notsure why this git show is here", hash)
		} else {
			// log.Printf("UNKNOWN BRANCH %-50s %s %s %s\n", repo.GetFullPath(), r.Stdout, cmd, b)
			tag := new(GitTag)
			tag.Refname = b
			tag.Hash = hash
			if len(r.Stdout) > 0 {
				tagtime := parseDateRFC3339(r.Stdout[0])
				tag.Creatordate = timestamppb.New(tagtime)
			}
			return tag
		}
	}
	return nil
}