@@ -3,6 +3,7 @@ package commands
33import (
44 "bufio"
55 "bytes"
6+ "encoding/json"
67 "fmt"
78 "io/ioutil"
89 "os"
2930 trackNoModifyAttrsFlag bool
3031 trackNoExcludedFlag bool
3132 trackFilenameFlag bool
33+ trackJSONFlag bool
3234)
3335
3436func 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+
224237func 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}
0 commit comments