11package commands
22
33import (
4+ "encoding/json"
45 "os"
56 "strings"
67
1819 lsFilesScanDeleted = false
1920 lsFilesShowSize = false
2021 lsFilesShowNameOnly = false
22+ lsFilesJSON = false
2123 debug = false
2224)
2325
26+ type lsFilesObject struct {
27+ Name string `json:"name"`
28+ Size int64 `json:"size"`
29+ Checkout bool `json:"checkout"`
30+ Downloaded bool `json:"downloaded"`
31+ OidType string `json:"oid_type"`
32+ Oid string `json:"oid"`
33+ Version string `json:"version"`
34+ }
35+
2436func lsFilesCommand (cmd * cobra.Command , args []string ) {
2537 setupRepository ()
2638
@@ -67,6 +79,7 @@ func lsFilesCommand(cmd *cobra.Command, args []string) {
6779 }
6880
6981 seen := make (map [string ]struct {})
82+ var items []lsFilesObject
7083
7184 gitscanner := lfs .NewGitScanner (cfg , func (p * lfs.WrappedPointer , err error ) {
7285 if err != nil {
@@ -96,6 +109,16 @@ func lsFilesCommand(cmd *cobra.Command, args []string) {
96109 p .OidType ,
97110 p .Oid ,
98111 p .Version ))
112+ } else if lsFilesJSON {
113+ items = append (items , lsFilesObject {
114+ Name : p .Name ,
115+ Size : p .Size ,
116+ Checkout : fileExistsOfSize (p ),
117+ Downloaded : cfg .LFSObjectExists (p .Oid , p .Size ),
118+ OidType : p .OidType ,
119+ Oid : p .Oid ,
120+ Version : p .Version ,
121+ })
99122 } else {
100123 msg := []string {p .Oid [:showOidLen ], lsFilesMarker (p ), p .Name }
101124 if lsFilesShowNameOnly {
@@ -144,6 +167,16 @@ func lsFilesCommand(cmd *cobra.Command, args []string) {
144167 Exit (tr .Tr .Get ("Could not scan for Git LFS tree: %s" , err ))
145168 }
146169 }
170+ if lsFilesJSON {
171+ data := struct {
172+ Files []lsFilesObject `json:"files"`
173+ }{Files : items }
174+ encoder := json .NewEncoder (os .Stdout )
175+ encoder .SetIndent ("" , " " )
176+ if err := encoder .Encode (data ); err != nil {
177+ ExitWithError (err )
178+ }
179+ }
147180}
148181
149182// Returns true if a pointer appears to be properly smudge on checkout
@@ -170,5 +203,6 @@ func init() {
170203 cmd .Flags ().BoolVar (& lsFilesScanDeleted , "deleted" , false , "" )
171204 cmd .Flags ().StringVarP (& includeArg , "include" , "I" , "" , "Include a list of paths" )
172205 cmd .Flags ().StringVarP (& excludeArg , "exclude" , "X" , "" , "Exclude a list of paths" )
206+ cmd .Flags ().BoolVarP (& lsFilesJSON , "json" , "" , false , "print output in JSON" )
173207 })
174208}
0 commit comments