commands/track: teach track how to --dry-run

This commit is contained in:
Taylor Blau 2016-07-01 14:21:54 -06:00
parent 5e18bf73ce
commit 8b8ea26400
3 changed files with 49 additions and 12 deletions

@ -28,6 +28,7 @@ var (
trackNoTouchFlag bool trackNoTouchFlag bool
trackVerboseLoggingFlag bool trackVerboseLoggingFlag bool
trackDryRunFlag bool
) )
func trackCommand(cmd *cobra.Command, args []string) { func trackCommand(cmd *cobra.Command, args []string) {
@ -109,21 +110,23 @@ ArgsLoop:
continue continue
} }
if !trackDryRunFlag {
encodedArg := strings.Replace(pattern, " ", "[[:space:]]", -1) encodedArg := strings.Replace(pattern, " ", "[[:space:]]", -1)
_, err = attributesFile.WriteString(fmt.Sprintf("%s filter=lfs diff=lfs merge=lfs -text\n", encodedArg)) _, err := attributesFile.WriteString(fmt.Sprintf("%s filter=lfs diff=lfs merge=lfs -text\n", encodedArg))
if err != nil { if err != nil {
Print("Error adding path %s", pattern) Print("Error adding path %s", pattern)
continue continue
} }
}
Print("Tracking %s", pattern) Print("Tracking %s", pattern)
if !trackNoTouchFlag { if !trackNoTouchFlag || trackDryRunFlag {
now := time.Now()
for _, f := range gittracked { for _, f := range gittracked {
if trackVerboseLoggingFlag { if trackVerboseLoggingFlag || trackDryRunFlag {
Print("Git LFS: touching %s", f) Print("Git LFS: touching %s", f)
} }
if !trackDryRunFlag {
err := os.Chtimes(f, now, now) err := os.Chtimes(f, now, now)
if err != nil { if err != nil {
LoggedError(err, "Error marking %q modified", f) LoggedError(err, "Error marking %q modified", f)
@ -133,6 +136,7 @@ ArgsLoop:
} }
} }
} }
}
type mediaPath struct { type mediaPath struct {
Path string Path string
@ -229,6 +233,7 @@ func blocklistItem(name string) string {
func init() { func init() {
trackCmd.Flags().BoolVarP(&trackNoTouchFlag, "no-touch", "n", false, "skip modifying files matched by the glob") trackCmd.Flags().BoolVarP(&trackNoTouchFlag, "no-touch", "n", false, "skip modifying files matched by the glob")
trackCmd.Flags().BoolVarP(&trackVerboseLoggingFlag, "verbose", "v", false, "log which files are being tracked and modified") trackCmd.Flags().BoolVarP(&trackVerboseLoggingFlag, "verbose", "v", false, "log which files are being tracked and modified")
trackCmd.Flags().BoolVarP(&trackDryRunFlag, "dry-run", "d", false, "preview results of running `git lfs track`")
RootCmd.AddCommand(trackCmd) RootCmd.AddCommand(trackCmd)
} }

@ -21,6 +21,17 @@ the currently-tracked paths.
If enabled, have `git lfs track` log files which it will touch. Disabled by If enabled, have `git lfs track` log files which it will touch. Disabled by
default. default.
* `--dry-run` `d`:
If enabled, have `git lfs track` log all actions it would normally take
(adding entries to .gitattributes, touching files on disk, etc) without
preforming any mutative operations to the disk.
`git lfs track --dry-run [files]` is equivalent to calling
`git lfs track --no-touch --verbose` and reverting any changes made to
.gitattributes.
Disabled by default.
## EXAMPLES ## EXAMPLES
* List the paths that Git LFS is currently tracking: * List the paths that Git LFS is currently tracking:

@ -80,6 +80,27 @@ begin_test "track --verbose"
) )
end_test end_test
begin_test "track --dry-run"
(
set -e
reponame="track_dry_run"
mkdir "$reponame"
cd "$reponame"
git init
touch foo.dat
git add foo.dat
git lfs track --dry-run "foo.dat" 2>&1 > track.log
grep "Tracking foo.dat" track.log
grep "Git LFS: touching foo.dat" track.log
git status --porcelain 2>&1 > status.log
grep "A foo.dat" status.log
)
end_test
begin_test "track directory" begin_test "track directory"
( (
set -e set -e