From 2fe035fbf0e1d08f3c546bc100a723c5d6c314d6 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Tue, 7 Jan 2025 04:59:01 -0600 Subject: [PATCH] migrate in some old code --- reloadBranches.go | 116 ++++++++++++++++++++++++++++++++++++++++++++ reloadCheckDirty.go | 1 + repo.proto | 1 + 3 files changed, 118 insertions(+) create mode 100644 reloadBranches.go diff --git a/reloadBranches.go b/reloadBranches.go new file mode 100644 index 0000000..b7e3a15 --- /dev/null +++ b/reloadBranches.go @@ -0,0 +1,116 @@ +package gitpb + +import ( + "io/ioutil" + "os" + "path/filepath" + "strings" + "unicode/utf8" + + "go.wit.com/lib/gui/shell" + "go.wit.com/log" +) + +// 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() bool { + var hashCheck string + var perfect bool = true + 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" { + log.Info("skip HEAD fullfile", 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, GITPB) + if r.Error != nil { + log.Log(GITPBWARN, "CheckBranches() git show error:", r.Error) + } + // git show -s --format=%ci will give you the time + // log.Log(REPO, fullfile) + if hash == hashCheck { + // log.Info("notsure why this git show is here", hash) + } else { + log.Log(GITPBWARN, repo.GetFullPath(), hash, r.Stdout, b) + log.Log(GITPBWARN, "UNKNOWN BRANCHES IN THIS REPO", cmd) + // repo.versionCmdOutput.SetText("UNKNOWN BRANCHES") + perfect = false + // parts := strings.Split(b, "/") + // log.Warn("git push", parts) + } + } + return perfect +} + +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 +} diff --git a/reloadCheckDirty.go b/reloadCheckDirty.go index 34bcacb..172a373 100644 --- a/reloadCheckDirty.go +++ b/reloadCheckDirty.go @@ -52,6 +52,7 @@ func (repo *Repo) CheckDirty() bool { bad = true } } + repo.DirtyList = r.Stdout pbnow := timestamppb.New(time.Now()) repo.Times.LastDirty = pbnow diff --git a/repo.proto b/repo.proto index 0e7ec44..9097561 100644 --- a/repo.proto +++ b/repo.proto @@ -32,6 +32,7 @@ message Repo { // `autogenpb:marshal` string masterVersion = 19; // just store this for now string develVersion = 20; // string userVersion = 21; // + repeated string dirtyList = 22; // store the list from git status --porcelain } message Repos { // `autogenpb:marshal`