// Copyright 2017-2025 WIT.COM Inc. All rights reserved. // Use of this source code is governed by the GPL 3.0 package main import ( "os" "path/filepath" "strings" "go.wit.com/lib/fhelp" "go.wit.com/lib/gui/shell" "go.wit.com/lib/protobuf/gitpb" "go.wit.com/log" ) func doGit() error { log.DaemonMode(true) defer log.DaemonMode(false) if argv.Git.Log != nil { fstr := "--format=\"%h %>(24)%ar %>(20)%an %s" cmd := []string{"git", "log", fstr} shell.RunVerbose(cmd) okExit("") } if argv.Git.Tag != nil { cmd := []string{"git", "for-each-ref", "--sort=taggerdate", "--format"} cmd = append(cmd, "%(tag)%00%(taggerdate:raw)%00%(taggername)%00%(subject)") cmd = append(cmd, "refs/tags") result := shell.RunQuiet(cmd) for i, line := range result.Stdout { parts := strings.Split(line, "\x00") log.Infof("LINE: %d %v\n", i, parts) } } if argv.Git.Who != nil { if _, err := fhelp.CheckCmd("git-who"); err != nil { if fhelp.QuestionUser("install git-who") { log.Info("go install -v -x github.com/sinclairtarget/git-who@latest") } else { log.Info("not installing") } me.sh.GoodExit("git who should be installed") } cmd := []string{"git", "who"} shell.RunVerbose(cmd) okExit("") } if argv.Git.Pull != nil { doPull(".config/wit") doPull("tools") okExit("") } if argv.Git.Push != nil { doPush(".config/wit") doPush("tools") okExit("") } return nil } func findRepo(wpath string) (*gitpb.Repo, bool) { d := filepath.Join(me.homedir, wpath) if !shell.IsDir(d) { return nil, false } repo, err := gitpb.NewRepo(d) if err != nil { log.Info("path error", d, err) return nil, false } return repo, true } func doPull(wpath string) { repo, ok := findRepo(wpath) if !ok { return } os.Chdir(repo.FullPath) exitOnErrorRealtime([]string{"git", "pull"}) } func doPush(wpath string) { doPull(wpath) d := filepath.Join(me.homedir, wpath) if !shell.IsDir(d) { return } repo, err := gitpb.NewRepo(d) if err != nil { log.Info("path error", d, err) return } if repo == nil { log.Info("repo is nil", d, err) return } if err := repo.GitCommit(); err != nil { badExit(err) } repo.RunRealtime([]string{"git", "push"}) }