Skip to content

Commit 5241272

Browse files
authored
Merge pull request git-lfs#5006 from bk2204/track-json
Add --json output for git lfs track
2 parents c9e2a13 + 6f37e98 commit 5241272

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

commands/command_track.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package commands
33
import (
44
"bufio"
55
"bytes"
6+
"encoding/json"
67
"fmt"
78
"io/ioutil"
89
"os"
@@ -29,6 +30,7 @@ var (
2930
trackNoModifyAttrsFlag bool
3031
trackNoExcludedFlag bool
3132
trackFilenameFlag bool
33+
trackJSONFlag bool
3234
)
3335

3436
func trackCommand(cmd *cobra.Command, args []string) {
@@ -44,6 +46,10 @@ func trackCommand(cmd *cobra.Command, args []string) {
4446
return
4547
}
4648

49+
if trackJSONFlag {
50+
Exit(tr.Tr.Get("--json option can't be combined with arguments"))
51+
}
52+
4753
mp := gitattr.NewMacroProcessor()
4854

4955
// Intentionally do _not_ consider global- and system-level
@@ -221,8 +227,36 @@ ArgsLoop:
221227
}
222228
}
223229

230+
type PatternData struct {
231+
Pattern string `json:"pattern"`
232+
Source string `json:"source"`
233+
Lockable bool `json:"lockable"`
234+
Tracked bool `json:"tracked"`
235+
}
236+
224237
func listPatterns() {
225238
knownPatterns := getAllKnownPatterns()
239+
if trackJSONFlag {
240+
patterns := struct {
241+
Patterns []PatternData `json:"patterns"`
242+
}{Patterns: make([]PatternData, 0, len(knownPatterns))}
243+
for _, p := range knownPatterns {
244+
patterns.Patterns = append(patterns.Patterns, PatternData{
245+
Pattern: p.Path,
246+
Source: p.Source.String(),
247+
Tracked: p.Tracked,
248+
Lockable: p.Lockable,
249+
})
250+
}
251+
encoder := json.NewEncoder(os.Stdout)
252+
encoder.SetIndent("", " ")
253+
err := encoder.Encode(patterns)
254+
if err != nil {
255+
ExitWithError(err)
256+
}
257+
return
258+
}
259+
226260
if len(knownPatterns) < 1 {
227261
return
228262
}
@@ -337,5 +371,6 @@ func init() {
337371
cmd.Flags().BoolVarP(&trackNoModifyAttrsFlag, "no-modify-attrs", "", false, "skip modifying .gitattributes file")
338372
cmd.Flags().BoolVarP(&trackNoExcludedFlag, "no-excluded", "", false, "skip listing excluded paths")
339373
cmd.Flags().BoolVarP(&trackFilenameFlag, "filename", "", false, "treat this pattern as a literal filename")
374+
cmd.Flags().BoolVarP(&trackJSONFlag, "json", "", false, "print output in JSON")
340375
})
341376
}

t/t-track.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,3 +711,44 @@ begin_test "track: escaped glob pattern with spaces in .gitattributes"
711711
assert_pointer "main" "$filename" "$contents_oid" 15
712712
)
713713
end_test
714+
715+
begin_test "--json output"
716+
(
717+
set -e
718+
719+
reponame="track-json"
720+
git init "$reponame"
721+
cd "$reponame"
722+
723+
git lfs track '*.dat'
724+
git lfs track --lockable '*.bin'
725+
echo 'a.dat !filter' >>.gitattributes
726+
727+
git lfs track --json > actual
728+
cat >expected <<-EOF
729+
{
730+
"patterns": [
731+
{
732+
"pattern": "*.dat",
733+
"source": ".gitattributes",
734+
"lockable": false,
735+
"tracked": true
736+
},
737+
{
738+
"pattern": "*.bin",
739+
"source": ".gitattributes",
740+
"lockable": true,
741+
"tracked": true
742+
},
743+
{
744+
"pattern": "a.dat",
745+
"source": ".gitattributes",
746+
"lockable": false,
747+
"tracked": false
748+
}
749+
]
750+
}
751+
EOF
752+
diff -u actual expected
753+
)
754+
end_test

0 commit comments

Comments
 (0)