From 2cdf2c3cdaffd62d28cc3e797aeba9159709dc03 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Thu, 7 Mar 2024 19:31:52 -0600 Subject: [PATCH] working on go-clone --- common.go | 6 +++++ git.go | 8 ++++++ new.go | 77 ++++++++++++++++++++++++++++++++++++++++++------------- unix.go | 23 +++++++++++++++++ 4 files changed, 96 insertions(+), 18 deletions(-) diff --git a/common.go b/common.go index 6392eea..bd1cc60 100644 --- a/common.go +++ b/common.go @@ -1,6 +1,7 @@ package repostatus import ( + "os" "strings" "unicode" @@ -111,10 +112,15 @@ func (rs *RepoStatus) IsGoLang() bool { return false } +// experiment to go package type func (rs *RepoStatus) RepoType() string { if !rs.IsGoLang() { return "" } + if !rs.Exists("go.mod") { + return "" + } + os.Setenv("GO111MODULE", "off") err, output := rs.RunCmd([]string{"go", "list", "-f", "'{{if eq .Name \"main\"}}binary{{else}}library{{end}}'"}) if err == nil { output = strings.Trim(output, "'") diff --git a/git.go b/git.go index abf58c0..a8ba548 100644 --- a/git.go +++ b/git.go @@ -209,6 +209,10 @@ func (rs *RepoStatus) CheckDirty() bool { if last == "nothing to commit, working tree clean" { log.Log(REPO, "CheckDirty() no", rs.realPath.String()) rs.dirtyLabel.SetValue("no") + if start == "" { + // don't record a change as this is the initial run + return false + } if start != "no" { log.Log(REPOWARN, "is no longer dirty") rs.NoteChange("is no longer dirty") @@ -217,6 +221,10 @@ func (rs *RepoStatus) CheckDirty() bool { } rs.dirtyLabel.SetValue("dirty") + if start == "" { + // don't record a change as this is the initial run + return false + } if start != "dirty" { log.Log(REPOWARN, "is now dirty") rs.NoteChange("is now dirty") diff --git a/new.go b/new.go index 4e4403c..b4acda4 100644 --- a/new.go +++ b/new.go @@ -7,6 +7,7 @@ import ( "strings" "go.wit.com/lib/gadgets" + "go.wit.com/lib/gui/shell" "go.wit.com/log" ) @@ -41,34 +42,38 @@ func New(path string) (*RepoStatus, error) { return r, err } -func NewRepoStatusWindow(path string) (error, *RepoStatus) { - var realpath string - var isGoLang bool = false +func SetWorkPath(path string) { + os.Setenv("REPO_WORK_PATH", path) +} - if windowMap[path] == nil { - log.Log(INFO, "NewRepoStatusWindow() adding new", path) - } else { - log.Warn("This already exists for path", path) - log.Warn("should return windowMap[path] here") - return nil, windowMap[path] - } +// guess the paths. returns +// realpath : the actual path on the filesystem +// goSrcPath : this could be ~/go/src, or where the go.work file is +// goPath : go.wit.com/lib/gui/repostatus (for example) +// true/false if the repo is a golang repo +func guessPaths(path string) (string, string, string, bool, error) { + var realpath, goSrcDir string + var isGoLang bool = false homeDir, err := os.UserHomeDir() if err != nil { log.Log(WARN, "Error getting home directory:", err) - return err, nil - } - goSrcDir := filepath.Join(homeDir, "go/src") - - rs := &RepoStatus{ - ready: false, + return path, realpath, goSrcDir, false, err } + goSrcDir = filepath.Join(homeDir, "go/src") // allow arbitrary paths, otherwise, assume the repo is in ~/go/src + // unless REPO_WORK_PATH was set. to over-ride ~/go/src + // todo, look for go.work + if os.Getenv("REPO_WORK_PATH") == "" { + os.Setenv("REPO_WORK_PATH", goSrcDir) + } else { + goSrcDir = os.Getenv("REPO_WORK_PATH") + } + if strings.HasPrefix(path, "/") { realpath = path } else if strings.HasPrefix(path, "~") { - // TODO: example this to homedir tmp := strings.TrimPrefix(path, "~") realpath = filepath.Join(homeDir, tmp) } else { @@ -78,18 +83,54 @@ func NewRepoStatusWindow(path string) (error, *RepoStatus) { if !IsDirectory(realpath) { log.Log(REPOWARN, "directory doesn't exist", realpath) // directory doesn't exist. exit with nil and error nil - return errors.New(realpath + " does not exist"), nil + return path, realpath, goSrcDir, false, errors.New(realpath + " does not exist") } + clone(goSrcDir, path) filename := filepath.Join(realpath, ".git/config") _, err = os.Open(filename) if err != nil { // log.Log(WARN, "Error reading .git/config:", filename, err) // log.Log(WARN, "TODO: find .git/config in parent directory") + return path, realpath, goSrcDir, false, err + } + return path, realpath, goSrcDir, isGoLang, nil +} + +// attempt to git clone if the go path doesn't exist +func clone(wdir string, path string) { + fullpath := filepath.Join(wdir, path) + if IsDirectory(fullpath) { + return + } + err := os.Chdir(wdir) + if err != nil { + return + } + + base := filepath.Join(wdir, filepath.Dir(path)) + shell.RunPath(base, []string{"git", "clone", "http://" + path}) +} + +func NewRepoStatusWindow(path string) (error, *RepoStatus) { + path, realpath, goSrcDir, isGoLang, err := guessPaths(path) + if err != nil { return err, nil } + if windowMap[path] == nil { + log.Log(INFO, "NewRepoStatusWindow() adding new", path) + } else { + log.Warn("This already exists for path", path) + log.Warn("should return windowMap[path] here") + return nil, windowMap[path] + } + + rs := &RepoStatus{ + ready: false, + } + rs.tags = make(map[string]string) rs.window = gadgets.RawBasicWindow("GO Repo Details " + path) rs.window.Horizontal() diff --git a/unix.go b/unix.go index 6c7faef..a3b1647 100644 --- a/unix.go +++ b/unix.go @@ -449,3 +449,26 @@ func (rs *RepoStatus) DoAll(all [][]string) bool { } return true } + +func ScanGitDirectories(srcDir string) []string { + var all []string + err := filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error { + if err != nil { + log.Log(REPOWARN, "Error accessing path:", path, err) + return nil + } + + // Check if the path is a directory and has a .git subdirectory + if info.IsDir() && IsGitDir(path) { + all = append(all, path) + } + + return nil + }) + + if err != nil { + log.Log(REPOWARN, "Error walking the path:", srcDir, err) + } + + return all +}