Skip to content

Commit b611ab8

Browse files
authored
chore: Improve logging by moving git operations progress to DEBUG level (#40)
1 parent e9ce453 commit b611ab8

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ COMMAND SUPPLIED
151151
git-xargs --version
152152
```
153153
154-
### Installion option 2: Run go get
154+
### Installation option 2: Run go get
155155
156156
1. Ensure you have Golang installed and working properly on your system. [Follow the official Golang install guide](https://golang.org/doc/install) to get started.
157157
@@ -333,14 +333,14 @@ echo "gruntwork-io/terragrunt gruntwork-io/terratest" | git-xargs \
333333
| `--commit-message` | The commit message to use when creating commits. If you supply this flag, but neither the optional `--pull-request-title` or `--pull-request-description` flags, then the commit message value will be used for all three. | String | No |
334334
| `--skip-pull-requests` | If you don't want any pull requests opened, but would rather have your changes committed directly to your specified branch, pass this flag. Note that it won't work if your Github repo is configured with branch protections on the branch you're trying to commit directly to! | Boolean | No |
335335
| `--skip-archived-repos` | If you want to exclude archived (read-only) repositories from the list of targeted repos, pass this flag. | Boolean | No |
336-
| `--dry-run` | If you are in the process of testing out `git-xargs` or your intial set of targeted repos, but you don't want to make any changes via the Github API (pushing your local changes or opening pull requests) you can pass the dry-run branch. This is useful because the output report will still tell you which repos would have been affected, without actually making changes via the Github API to your remote repositories. | Boolean | No |
336+
| `--dry-run` | If you are in the process of testing out `git-xargs` or your initial set of targeted repos, but you don't want to make any changes via the Github API (pushing your local changes or opening pull requests) you can pass the dry-run branch. This is useful because the output report will still tell you which repos would have been affected, without actually making changes via the Github API to your remote repositories. | Boolean | No |
337337
| `--max-concurrent-repos` | Limits the number of concurrent processed repositories. This is only useful if you encounter issues and need throttling when running on a very large number of repos. Default is `0` (Unlimited) | Integer | No |
338338
339339
## Best practices, tips and tricks
340340
341341
### Write your script to run against a single repo
342342
343-
Write your script as if its operating on a single repo, then target many repos with `git-xargs`. Remember that at runtime, each of the scripts you select will be run, in the order you specify, once per repo that you've targeted.
343+
Write your script as if it's operating on a single repo, then target many repos with `git-xargs`. Remember that at runtime, each of the scripts you select will be run, in the order you specify, once per repo that you've targeted.
344344
345345
### Handling prerequisites and third party binaries
346346
@@ -360,7 +360,7 @@ This section provides a more in-depth look at how the `git-xargs` tool works und
360360
1. it will run all your selected scripts against your selected repos
361361
1. it will commit any changes in each of the repos (with a commit message you can optionally specify via the `--commit-message` flag)
362362
1. it will push your local branch with your new commits to your repo's remote
363-
1. it will call the Github API to open a pull request with a title and description that you can optionally specify via the `--pull-request-title` and `--pull-request-description` flags, respectively), unless you pass the `--skip-pull-requests` flag
363+
1. it will call the Github API to open a pull request with a title and description that you can optionally specify via the `--pull-request-title` and `--pull-request-description` flags, respectively, unless you pass the `--skip-pull-requests` flag
364364
1. it will print out a detailed run summary to STDOUT that explains exactly what happened with each repo and provide links to successfully opened pull requests that you can quickly follow from your terminal. If any repos encountered errors at runtime (whether they weren't able to be cloned, or script errors were encountered during processing, etc) all of this will be spelled out in detail in the final report so you know exactly what succeeded and what went wrong.
365365
366366
## Tasks this tool is well-suited for
@@ -374,7 +374,7 @@ The following is a non-exhaustive list of potential use cases for `git-xargs`:
374374
- Add new files to repos
375375
- Delete specific files, when present, from repos
376376
- Modify `package.json` files in-place across repos to bump a node.js dependency using `jq` https://stedolan.github.io/jq/
377-
- Update your Terraform module library from Terraform 0.13 to 0.14 .
377+
- Update your Terraform module library from Terraform 0.13 to 0.14.
378378
- Remove stray files of any kind, when found, across repos using `find` and its `exec` option
379379
- Add baseline tests to repos that are missing them by copying over a common local folder where they are defined
380380
- Refactor multiple Golang tools to use new libraries by executing `go get` to install and uninstall packages, and modify the source code files' import references

repository/process.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ func ProcessRepos(gitxargsConfig *config.GitXargsConfig, repos []*github.Reposit
4747
// 8. Track all successfully opened pull requests via the stats tracker so that we can print them out as part of our final
4848
// run report that is displayed in table format to the operator following each run
4949
func processRepo(config *config.GitXargsConfig, repo *github.Repository) error {
50+
logger := logging.GetLogger("git-xargs")
51+
5052
// Create a new temporary directory in the default temp directory of the system, but append
5153
// git-xargs-<repo-name> to it so that it's easier to find when you're looking for it
5254
repositoryDir, localRepository, cloneErr := cloneLocalRepository(config, repo)
@@ -86,5 +88,9 @@ func processRepo(config *config.GitXargsConfig, repo *github.Repository) error {
8688
return err
8789
}
8890

91+
logger.WithFields(logrus.Fields{
92+
"Repo name": repo.GetName(),
93+
}).Info("Repository successfully processed")
94+
8995
return nil
9096
}

repository/repo-operations.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package repository
22

33
import (
4+
"bytes"
45
"context"
56
"fmt"
67
"io/ioutil"
@@ -41,15 +42,20 @@ func cloneLocalRepository(config *config.GitXargsConfig, repo *github.Repository
4142
return repositoryDir, nil, errors.WithStackTrace(tmpDirErr)
4243
}
4344

45+
gitProgressBuffer := bytes.NewBuffer(nil)
4446
localRepository, err := config.GitClient.PlainClone(repositoryDir, false, &git.CloneOptions{
4547
URL: repo.GetCloneURL(),
46-
Progress: os.Stdout,
48+
Progress: gitProgressBuffer,
4749
Auth: &http.BasicAuth{
4850
Username: repo.GetOwner().GetLogin(),
4951
Password: os.Getenv("GITHUB_OAUTH_TOKEN"),
5052
},
5153
})
5254

55+
logger.WithFields(logrus.Fields{
56+
"Repo": repo.GetName(),
57+
}).Debug(gitProgressBuffer)
58+
5359
if err != nil {
5460
logger.WithFields(logrus.Fields{
5561
"Error": err,
@@ -178,16 +184,21 @@ func checkoutLocalBranch(config *config.GitXargsConfig, ref *plumbing.Reference,
178184
}
179185

180186
// Pull latest code from remote branch if it exists to avoid fast-forwarding errors
187+
gitProgressBuffer := bytes.NewBuffer(nil)
181188
po := &git.PullOptions{
182189
RemoteName: "origin",
183190
ReferenceName: branchName,
184191
Auth: &http.BasicAuth{
185192
Username: remoteRepository.GetOwner().GetLogin(),
186193
Password: os.Getenv("GITHUB_OAUTH_TOKEN"),
187194
},
188-
Progress: os.Stdout,
195+
Progress: gitProgressBuffer,
189196
}
190197

198+
logger.WithFields(logrus.Fields{
199+
"Repo": remoteRepository.GetName(),
200+
}).Debug(gitProgressBuffer)
201+
191202
pullErr := worktree.Pull(po)
192203

193204
if pullErr != nil {
@@ -276,7 +287,10 @@ func commitLocalChanges(status git.Status, config *config.GitXargsConfig, reposi
276287

277288
for filepath := range status {
278289
if status.IsUntracked(filepath) {
279-
fmt.Printf("Found untracked file %s. Adding to stage", filepath)
290+
logger.WithFields(logrus.Fields{
291+
"Filepath": filepath,
292+
}).Debug("Found untracked file. Adding to stage")
293+
280294
_, addErr := worktree.Add(filepath)
281295
if addErr != nil {
282296
logger.WithFields(logrus.Fields{

0 commit comments

Comments
 (0)