Skip to content

Commit 1ba5873

Browse files
authored
Clean up temporary clone paths by default (#113)
- Add flag (--keep-cloned-repositories) to preserve them
1 parent ef93643 commit 1ba5873

File tree

6 files changed

+29
-0
lines changed

6 files changed

+29
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ echo "gruntwork-io/terragrunt gruntwork-io/terratest" | git-xargs \
426426
| `--no-skip-ci` | By default, git-xargs will prepend \"[skip ci]\" to its commit messages to prevent large git-xargs jobs from creating expensive CI jobs excessively. If you pass the `--no-skip-ci` flag, then git-xargs will not prepend \"[skip ci]\". Default: false, meaning that \"[skip ci]\" will be prepended to commit messages. | Bool | No |
427427
| `--reviewers` | An optional slice of GitHub usernames, separated by commas, to request reviews from after a pull request is successfully opened. Default: empty slice, meaning that no reviewers will be requested. | String | No |
428428
| `--team-reviewers` | An optional slice of GitHub team names, separated by commas, to request reviews from after a pull request is successfully opened. Default: empty slice, meaning that no team reviewers will be requested. IMPORTANT: Please read and understand [the GitHub restrictions](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/requesting-a-pull-request-review) on this functionality before using it! Only certain GitHub organizations / payment plans support this functionality. | String | No |
429+
| `--keep-cloned-repositories` | By default, git-xargs will delete the repositories it clones to your temporary file directory once it has completed processing that repo, to save space on your machine. If you wish to retain the local repositories, pass this flag. | Bool | No |
429430
## Best practices, tips and tricks
430431

431432
### Write your script to run against a single repo

cmd/git-xargs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func parseGitXargsConfig(c *cli.Context) (*config.GitXargsConfig, error) {
4242
config.PullRequestRetries = c.Int("max-pr-retries")
4343
config.SecondsToSleepWhenRateLimited = c.Int("seconds-to-wait-when-rate-limited")
4444
config.NoSkipCI = c.Bool("no-skip-ci")
45+
config.RetainLocalRepos = c.Bool("keep-cloned-repositories")
4546
// By default, prepend "[skip ci]" to commit messages, unless the user passed --no-skip-ci
4647
if config.NoSkipCI == false {
4748
commitMsgWithCISkip := fmt.Sprintf("%s %s", "[skip ci]", config.CommitMessage)

common/common.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const (
2424
MaxPullRequestRetriesFlagName = "max-pr-retries"
2525
SecondsToWaitWhenRateLimitedFlagName = "seconds-to-wait-when-rate-limited"
2626
NoSkipCIFlagName = "no-skip-ci"
27+
KeepClonedRepositoriesFlagName = "keep-cloned-repositories"
2728
DefaultMaxConcurrentRepos = 0
2829
DefaultSecondsBetweenPRs = 1
2930
DefaultMaxPullRequestRetries = 3
@@ -109,4 +110,8 @@ var (
109110
Name: NoSkipCIFlagName,
110111
Usage: "By default, git-xargs prepends \"[skip ci]\" to its commit messages. Pass this flag to prevent \"[skip ci]\" from being prepending to commit messages.",
111112
}
113+
GenericKeepClonedRepositoriesFlag = cli.BoolFlag{
114+
Name: KeepClonedRepositoriesFlagName,
115+
Usage: "By default, git-xargs deletes the cloned repositories from the temp directory after the command has finished running, to save space on your machine. Pass this flag to prevent git-xargs from deleting the cloned repositories.",
116+
}
112117
)

config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type GitXargsConfig struct {
3939
PullRequestRetries int
4040
SecondsToSleepWhenRateLimited int
4141
NoSkipCI bool
42+
RetainLocalRepos bool
4243
Ticker *time.Ticker
4344
}
4445

@@ -70,6 +71,7 @@ func NewGitXargsConfig() *GitXargsConfig {
7071
SecondsToSleepWhenRateLimited: common.DefaultSecondsToWaitWhenRateLimited,
7172
PullRequestRetries: common.DefaultMaxPullRequestRetries,
7273
NoSkipCI: false,
74+
RetainLocalRepos: false,
7375
}
7476
}
7577

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ func setupApp() *cli.App {
7777
common.GenericMaxPullRequestRetriesFlag,
7878
common.GenericSecondsToWaitWhenRateLimitedFlag,
7979
common.GenericNoSkipCIFlag,
80+
common.GenericKeepClonedRepositoriesFlag,
8081
}
8182

8283
app.Action = cmd.RunGitXargs

repository/process.go

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

33
import (
4+
"os"
45
"sync"
56
"time"
67

@@ -70,6 +71,19 @@ func ProcessRepos(gitxargsConfig *config.GitXargsConfig, repos []*github.Reposit
7071
return nil
7172
}
7273

74+
// cleanupTempDir removes the temporary directory that was created for the local clone of the repo
75+
// It logs a debug-level error if the directory could not be removed, but does not return an error
76+
func cleanupTempDir(repositoryDir string) {
77+
logger := logging.GetLogger("git-xargs")
78+
removeErr := os.RemoveAll(repositoryDir)
79+
if removeErr != nil {
80+
logger.WithFields(logrus.Fields{
81+
"Error": removeErr,
82+
"Temp directory": repositoryDir,
83+
}).Debug("Error encountered while removing temporary directory")
84+
}
85+
}
86+
7387
// 1. Attempt to clone it to the local filesystem. To avoid conflicts, this generates a new directory for each repo FOR EACH run, so heavy use of this tool may inflate your /tmp/ directory size
7488
// 2. Look up the HEAD ref of the repo, and create a new branch from that ref, specific to this tool so that we can safely make our changes in the branch
7589
// 3. Execute the supplied command against the locally cloned repo
@@ -86,6 +100,11 @@ func processRepo(config *config.GitXargsConfig, repo *github.Repository) error {
86100
// git-xargs-<repo-name> to it so that it's easier to find when you're looking for it
87101
repositoryDir, localRepository, cloneErr := cloneLocalRepository(config, repo)
88102

103+
// if user did not pass retention flag, defer cleanup of the repositoryDir
104+
if config.RetainLocalRepos == false {
105+
defer cleanupTempDir(repositoryDir)
106+
}
107+
89108
if cloneErr != nil {
90109
return cloneErr
91110
}

0 commit comments

Comments
 (0)