| 1 | package commands |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | |
| 9 | "github.com/git-lfs/git-lfs/v3/errors" |
| 10 | "github.com/git-lfs/git-lfs/v3/git" |
| 11 | "github.com/git-lfs/git-lfs/v3/lfs" |
| 12 | "github.com/git-lfs/git-lfs/v3/tools/humanize" |
| 13 | "github.com/git-lfs/git-lfs/v3/tr" |
| 14 | "github.com/spf13/cobra" |
| 15 | ) |
| 16 | |
| 17 | var ( |
| 18 | longOIDs = false |
| 19 | lsFilesScanAll = false |
| 20 | lsFilesScanDeleted = false |
| 21 | lsFilesShowSize = false |
| 22 | lsFilesShowNameOnly = false |
| 23 | lsFilesJSON = false |
| 24 | debug = false |
| 25 | ) |
| 26 | |
| 27 | type lsFilesObject struct { |
| 28 | Name string `json:"name"` |
| 29 | Size int64 `json:"size"` |
| 30 | Checkout bool `json:"checkout"` |
| 31 | Downloaded bool `json:"downloaded"` |
| 32 | OidType string `json:"oid_type"` |
| 33 | Oid string `json:"oid"` |
| 34 | Version string `json:"version"` |
| 35 | } |
| 36 | |
| 37 | func lsFilesCommand(cmd *cobra.Command, args []string) { |
| 38 | setupRepository() |
| 39 | |
| 40 | var ref string |
| 41 | var includeRef string |
| 42 | var scanRange = false |
| 43 | if len(args) > 0 { |
| 44 | if lsFilesScanAll { |
| 45 | Exit(tr.Tr.Get("Cannot use --all with explicit reference")) |
| 46 | } else if args[0] == "--all" { |
| 47 | // Since --all is a valid argument to "git rev-parse", |
| 48 | // if we try to give it to git.ResolveRef below, we'll |
| 49 | // get an unexpected result. |
| 50 | // |
| 51 | // So, let's check early that the caller invoked the |
| 52 | // command correctly. |
| 53 | Exit(tr.Tr.Get("Did you mean `git lfs ls-files --all --` ?")) |
| 54 | } |
| 55 | |
| 56 | ref = args[0] |
| 57 | if len(args) > 1 { |
| 58 | if lsFilesScanDeleted { |
| 59 | Exit(tr.Tr.Get("Cannot use --deleted with reference range")) |
| 60 | } |
| 61 | includeRef = args[1] |
| 62 | scanRange = true |
| 63 | } |
| 64 | } else { |
| 65 | fullref, err := git.CurrentRef() |
| 66 | if err != nil { |
| 67 | ref, err = git.EmptyTree() |
| 68 | if err != nil { |
| 69 | ExitWithError(errors.Wrap( |
| 70 | err, tr.Tr.Get("Could not read empty Git tree object"))) |
| 71 | } |
| 72 | } else { |
| 73 | ref = fullref.Sha |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | showOidLen := 10 |
| 78 | if longOIDs { |
| 79 | showOidLen = 64 |
| 80 | } |
| 81 | |
| 82 | seen := make(map[string]struct{}) |
| 83 | var items []lsFilesObject |
| 84 | |
| 85 | gitscanner := lfs.NewGitScanner(cfg, func(p *lfs.WrappedPointer, err error) { |
| 86 | if err != nil { |
| 87 | Exit(tr.Tr.Get("Could not scan for Git LFS tree: %s", err)) |
| 88 | return |
| 89 | } |
| 90 | |
| 91 | if p.Size == 0 { |
| 92 | return |
| 93 | } |
| 94 | |
| 95 | if !lsFilesScanAll && !scanRange { |
| 96 | if _, ok := seen[p.Name]; ok { |
| 97 | return |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | if debug { |
| 102 | // TRANSLATORS: these strings should have the colons |
| 103 | // aligned in a column. |
| 104 | Print( |
| 105 | tr.Tr.Get("filepath: %s\n size: %d\ncheckout: %v\ndownload: %v\n oid: %s %s\n version: %s\n", |
| 106 | p.Name, |
| 107 | p.Size, |
| 108 | fileExistsOfSize(p), |
| 109 | cfg.LFSObjectExists(p.Oid, p.Size), |
| 110 | p.OidType, |
| 111 | p.Oid, |
| 112 | p.Version)) |
| 113 | } else if lsFilesJSON { |
| 114 | items = append(items, lsFilesObject{ |
| 115 | Name: p.Name, |
| 116 | Size: p.Size, |
| 117 | Checkout: fileExistsOfSize(p), |
| 118 | Downloaded: cfg.LFSObjectExists(p.Oid, p.Size), |
| 119 | OidType: p.OidType, |
| 120 | Oid: p.Oid, |
| 121 | Version: p.Version, |
| 122 | }) |
| 123 | } else { |
| 124 | msg := []string{p.Oid[:showOidLen], lsFilesMarker(p), p.Name} |
| 125 | if lsFilesShowNameOnly { |
| 126 | msg = []string{p.Name} |
| 127 | } |
| 128 | if lsFilesShowSize { |
| 129 | size := humanize.FormatBytes(uint64(p.Size)) |
| 130 | msg = append(msg, "("+size+")") |
| 131 | } |
| 132 | |
| 133 | Print(strings.Join(msg, " ")) |
| 134 | } |
| 135 | |
| 136 | seen[p.Name] = struct{}{} |
| 137 | }) |
| 138 | |
| 139 | includeArg, excludeArg := getIncludeExcludeArgs(cmd) |
| 140 | gitscanner.Filter = buildFilepathFilter(cfg, includeArg, excludeArg, false) |
| 141 | |
| 142 | if len(args) == 0 { |
| 143 | // Only scan the index when "git lfs ls-files" was invoked with |
| 144 | // no arguments. |
| 145 | // |
| 146 | // Do so to avoid showing "mixed" results, e.g., ls-files output |
| 147 | // from a specific historical revision, and the index. |
| 148 | if err := gitscanner.ScanIndex(ref, "", nil); err != nil { |
| 149 | Exit(tr.Tr.Get("Could not scan for Git LFS index: %s", err)) |
| 150 | } |
| 151 | } |
| 152 | if lsFilesScanAll { |
| 153 | if err := gitscanner.ScanAll(nil); err != nil { |
| 154 | Exit(tr.Tr.Get("Could not scan for Git LFS history: %s", err)) |
| 155 | } |
| 156 | } else { |
| 157 | var err error |
| 158 | if lsFilesScanDeleted { |
| 159 | err = gitscanner.ScanRefWithDeleted(ref, nil) |
| 160 | } else if scanRange { |
| 161 | err = gitscanner.ScanRefRange(includeRef, ref, nil) |
| 162 | } else { |
| 163 | err = gitscanner.ScanTree(ref, nil) |
| 164 | } |
| 165 | |
| 166 | if err != nil { |
| 167 | Exit(tr.Tr.Get("Could not scan for Git LFS tree: %s", err)) |
| 168 | } |
| 169 | } |
| 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 | } |
| 180 | } |
| 181 | |
| 182 | // Returns true if a pointer appears to be properly smudge on checkout |
| 183 | func fileExistsOfSize(p *lfs.WrappedPointer) bool { |
| 184 | path := cfg.Filesystem().DecodePathname(p.Name) |
| 185 | info, err := os.Stat(filepath.Join(cfg.LocalWorkingDir(), path)) |
| 186 | return err == nil && info.Size() == p.Size |
| 187 | } |
| 188 | |
| 189 | func lsFilesMarker(p *lfs.WrappedPointer) string { |
| 190 | if fileExistsOfSize(p) { |
| 191 | return "*" |
| 192 | } |
| 193 | return "-" |
| 194 | } |
| 195 | |
| 196 | func init() { |
| 197 | RegisterCommand("ls-files", lsFilesCommand, func(cmd *cobra.Command) { |
| 198 | cmd.Flags().BoolVarP(&longOIDs, "long", "l", false, "") |
| 199 | cmd.Flags().BoolVarP(&lsFilesShowSize, "size", "s", false, "") |
| 200 | cmd.Flags().BoolVarP(&lsFilesShowNameOnly, "name-only", "n", false, "") |
| 201 | cmd.Flags().BoolVarP(&debug, "debug", "d", false, "") |
| 202 | cmd.Flags().BoolVarP(&lsFilesScanAll, "all", "a", false, "") |
| 203 | cmd.Flags().BoolVar(&lsFilesScanDeleted, "deleted", false, "") |
| 204 | cmd.Flags().StringVarP(&includeArg, "include", "I", "", "Include a list of paths") |
| 205 | cmd.Flags().StringVarP(&excludeArg, "exclude", "X", "", "Exclude a list of paths") |
| 206 | cmd.Flags().BoolVarP(&lsFilesJSON, "json", "j", false, "Give the output in a stable JSON format for scripts") |
| 207 | }) |
| 208 | } |