From e8303f96cce4cc9aa8b745a3832d2e99573ea77e Mon Sep 17 00:00:00 2001 From: gabrie30 Date: Wed, 15 May 2024 17:17:51 -0700 Subject: [PATCH] Add more debugging output to git commands when debug mode is enabled (#412) --- README.md | 2 +- cmd/root.go | 4 ++++ git/git.go | 38 +++++++++++++++++++++++++++++++++++--- sample-conf.yaml | 6 ++++++ 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f5c2880..bf62cd8 100644 --- a/README.md +++ b/README.md @@ -342,5 +342,5 @@ Alternatively, Windows users can also install ghorg using [scoop](https://scoop. - If your GitHub Personal Access Token is only finding public repos, give your token all the repos permissions - Make sure your `$ git --version` is >= 2.19.0 - Check for other software, such as anti-malware, that could interfere with ghorgs ability to create large number of connections, see [issue 132](https://github.com/gabrie30/ghorg/issues/132#issuecomment-889357960). You can also lower the concurrency with `--concurrency=n` default is 25. -- To debug yourself you can call ghorg with the GHORG_DEBUG=true env e.g `GHORG_DEBUG=true ghorg clone kubernetes --concurrency=1` +- To debug yourself you can call ghorg with the GHORG_DEBUG=true env e.g `GHORG_DEBUG=true ghorg clone kubernetes`. Note, when this env is set concurrency is set to a value of 1 - If you've gotten this far and still have an issue feel free to raise an issue diff --git a/cmd/root.go b/cmd/root.go index f9c0301..979f91d 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -207,6 +207,10 @@ func InitConfig() { if os.Getenv("GHORG_DEBUG") != "" { fmt.Println("-------- Setting Default ENV values ---------") + if os.Getenv("GHORG_CONCURRENCY_DEBUG") == "" { + fmt.Println("Setting concurrency to 1, this can be overwritten by setting GHORG_CONCURRENCY_DEBUG; however when using concurrency with GHORG_DEBUG, not all debugging output will be printed in serial order.") + os.Setenv("GHORG_CONCURRENCY", "1") + } } getOrSetDefaults("GHORG_ABSOLUTE_PATH_TO_CLONE_TO") diff --git a/git/git.go b/git/git.go index a4876ea..c7f8e4a 100644 --- a/git/git.go +++ b/git/git.go @@ -27,10 +27,19 @@ func NewGit() GitClient { return GitClient{} } -func printDebugCmd(cmd *exec.Cmd) { +func printDebugCmd(cmd *exec.Cmd, repo scm.Repo) error { fmt.Println("------------- GIT DEBUG -------------") + fmt.Print("Repo Data: ") + spew.Dump(repo) + fmt.Print("Command Ran: ") spew.Dump(*cmd) fmt.Println("") + output, err := cmd.CombinedOutput() + fmt.Printf("Command Output: %s\n", string(output)) + if err != nil { + fmt.Printf("Error: %v\n", err) + } + return err } func (g GitClient) Clone(repo scm.Repo) error { @@ -61,7 +70,7 @@ func (g GitClient) Clone(repo scm.Repo) error { cmd := exec.Command("git", args...) if os.Getenv("GHORG_DEBUG") != "" { - printDebugCmd(cmd) + return printDebugCmd(cmd, repo) } err := cmd.Run() @@ -72,6 +81,9 @@ func (g GitClient) SetOriginWithCredentials(repo scm.Repo) error { args := []string{"remote", "set-url", "origin", repo.CloneURL} cmd := exec.Command("git", args...) cmd.Dir = repo.HostPath + if os.Getenv("GHORG_DEBUG") != "" { + return printDebugCmd(cmd, repo) + } return cmd.Run() } @@ -79,24 +91,38 @@ func (g GitClient) SetOrigin(repo scm.Repo) error { args := []string{"remote", "set-url", "origin", repo.URL} cmd := exec.Command("git", args...) cmd.Dir = repo.HostPath + if os.Getenv("GHORG_DEBUG") != "" { + return printDebugCmd(cmd, repo) + } return cmd.Run() } func (g GitClient) Checkout(repo scm.Repo) error { cmd := exec.Command("git", "checkout", repo.CloneBranch) cmd.Dir = repo.HostPath + + if os.Getenv("GHORG_DEBUG") != "" { + return printDebugCmd(cmd, repo) + } + return cmd.Run() } func (g GitClient) Clean(repo scm.Repo) error { cmd := exec.Command("git", "clean", "-f", "-d") cmd.Dir = repo.HostPath + if os.Getenv("GHORG_DEBUG") != "" { + return printDebugCmd(cmd, repo) + } return cmd.Run() } func (g GitClient) UpdateRemote(repo scm.Repo) error { cmd := exec.Command("git", "remote", "update") cmd.Dir = repo.HostPath + if os.Getenv("GHORG_DEBUG") != "" { + return printDebugCmd(cmd, repo) + } return cmd.Run() } @@ -119,7 +145,7 @@ func (g GitClient) Pull(repo scm.Repo) error { cmd.Dir = repo.HostPath if os.Getenv("GHORG_DEBUG") != "" { - printDebugCmd(cmd) + return printDebugCmd(cmd, repo) } return cmd.Run() @@ -128,6 +154,9 @@ func (g GitClient) Pull(repo scm.Repo) error { func (g GitClient) Reset(repo scm.Repo) error { cmd := exec.Command("git", "reset", "--hard", "origin/"+repo.CloneBranch) cmd.Dir = repo.HostPath + if os.Getenv("GHORG_DEBUG") != "" { + return printDebugCmd(cmd, repo) + } return cmd.Run() } @@ -141,5 +170,8 @@ func (g GitClient) FetchAll(repo scm.Repo) error { } cmd := exec.Command("git", args...) cmd.Dir = repo.HostPath + if os.Getenv("GHORG_DEBUG") != "" { + return printDebugCmd(cmd, repo) + } return cmd.Run() } diff --git a/sample-conf.yaml b/sample-conf.yaml index a2b78db..f571102 100644 --- a/sample-conf.yaml +++ b/sample-conf.yaml @@ -157,6 +157,12 @@ GHORG_NO_TOKEN: false # flag (--config) # GHORG_CONFIG: +# Get verbose debugging output +# NOTE: This setting cannot be configured through the configuration file or the CLI. It can only be set as an environment variable. +# For example: GHORG_DEBUG=true ghorg clone kubernetes +# When using this env concurrency is set to a value of 1, this behavior can be overwritten for debugging concurrency issues by setting GHORG_CONCURRENCY_DEBUG=true in addition to setting GHORG_DEBUG=true +# GHORG_DEBUG: + # +-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ # |G|I|T|H|U|B| |S|P|E|C|I|F|I|C| # +-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+