2014-09-22 21:26:43 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2015-09-25 16:30:01 +00:00
|
|
|
"os"
|
2017-12-01 17:33:54 +00:00
|
|
|
"strings"
|
2015-09-25 05:30:23 +00:00
|
|
|
|
2016-11-15 17:01:18 +00:00
|
|
|
"github.com/git-lfs/git-lfs/git"
|
|
|
|
"github.com/git-lfs/git-lfs/lfs"
|
2017-12-01 18:09:15 +00:00
|
|
|
"github.com/git-lfs/git-lfs/tools/humanize"
|
2016-05-23 18:02:27 +00:00
|
|
|
"github.com/spf13/cobra"
|
2014-09-22 21:26:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-12-19 23:59:06 +00:00
|
|
|
longOIDs = false
|
|
|
|
lsFilesScanAll = false
|
|
|
|
lsFilesScanDeleted = false
|
|
|
|
lsFilesShowSize = false
|
|
|
|
debug = false
|
2014-09-22 21:26:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func lsFilesCommand(cmd *cobra.Command, args []string) {
|
2015-09-08 16:23:27 +00:00
|
|
|
requireInRepo()
|
2015-09-08 15:29:53 +00:00
|
|
|
|
2014-10-11 14:28:46 +00:00
|
|
|
var ref string
|
2014-09-22 21:26:43 +00:00
|
|
|
|
2014-10-03 16:17:26 +00:00
|
|
|
if len(args) == 1 {
|
2017-12-19 23:52:38 +00:00
|
|
|
if lsFilesScanAll {
|
|
|
|
Exit("fatal: cannot use --all with explicit reference")
|
2018-08-31 19:30:28 +00:00
|
|
|
} else if args[0] == "--all" {
|
|
|
|
// Since --all is a valid argument to "git rev-parse",
|
|
|
|
// if we try to give it to git.ResolveRef below, we'll
|
|
|
|
// get an unexpected result.
|
|
|
|
//
|
|
|
|
// So, let's check early that the caller invoked the
|
|
|
|
// command correctly.
|
|
|
|
Exit("fatal: did you mean \"git lfs ls-files --all --\" ?")
|
2017-12-19 23:52:38 +00:00
|
|
|
}
|
2018-08-31 19:30:28 +00:00
|
|
|
|
2014-10-03 16:17:26 +00:00
|
|
|
ref = args[0]
|
2014-10-11 14:28:46 +00:00
|
|
|
} else {
|
2015-08-21 14:18:26 +00:00
|
|
|
fullref, err := git.CurrentRef()
|
2014-10-11 14:28:46 +00:00
|
|
|
if err != nil {
|
2017-12-19 19:13:28 +00:00
|
|
|
ref = git.RefBeforeFirstCommit
|
|
|
|
} else {
|
|
|
|
ref = fullref.Sha
|
2014-10-11 14:28:46 +00:00
|
|
|
}
|
2014-10-03 16:17:26 +00:00
|
|
|
}
|
2014-09-22 21:26:43 +00:00
|
|
|
|
2015-09-25 16:30:01 +00:00
|
|
|
showOidLen := 10
|
2015-09-25 05:30:23 +00:00
|
|
|
if longOIDs {
|
2015-09-25 16:30:01 +00:00
|
|
|
showOidLen = 64
|
2015-09-25 05:30:23 +00:00
|
|
|
}
|
|
|
|
|
2017-12-19 19:24:21 +00:00
|
|
|
seen := make(map[string]struct{})
|
|
|
|
|
2016-11-29 17:56:03 +00:00
|
|
|
gitscanner := lfs.NewGitScanner(func(p *lfs.WrappedPointer, err error) {
|
2018-03-20 23:22:37 +00:00
|
|
|
if err != nil {
|
|
|
|
Exit("Could not scan for Git LFS tree: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-12-19 23:32:18 +00:00
|
|
|
if !lsFilesScanAll {
|
|
|
|
if _, ok := seen[p.Name]; ok {
|
|
|
|
return
|
|
|
|
}
|
2017-12-19 19:24:21 +00:00
|
|
|
}
|
|
|
|
|
2017-01-05 16:06:20 +00:00
|
|
|
if debug {
|
|
|
|
Print(
|
|
|
|
"filepath: %s\n"+
|
|
|
|
" size: %d\n"+
|
|
|
|
"checkout: %v\n"+
|
|
|
|
"download: %v\n"+
|
|
|
|
" oid: %s %s\n"+
|
|
|
|
" version: %s\n",
|
|
|
|
p.Name,
|
|
|
|
p.Size,
|
|
|
|
fileExistsOfSize(p),
|
2017-10-25 01:20:09 +00:00
|
|
|
cfg.LFSObjectExists(p.Oid, p.Size),
|
2017-01-05 16:06:20 +00:00
|
|
|
p.OidType,
|
|
|
|
p.Oid,
|
|
|
|
p.Version)
|
|
|
|
} else {
|
2017-12-01 17:33:54 +00:00
|
|
|
msg := []string{p.Oid[:showOidLen], lsFilesMarker(p), p.Name}
|
|
|
|
if lsFilesShowSize {
|
2017-12-01 18:09:15 +00:00
|
|
|
size := humanize.FormatBytes(uint64(p.Size))
|
|
|
|
msg = append(msg, "("+size+")")
|
2017-12-01 17:33:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Print(strings.Join(msg, " "))
|
2017-01-05 16:06:20 +00:00
|
|
|
}
|
2017-12-19 19:24:21 +00:00
|
|
|
|
|
|
|
seen[p.Name] = struct{}{}
|
2016-11-29 17:56:03 +00:00
|
|
|
})
|
|
|
|
defer gitscanner.Close()
|
2016-11-18 00:36:38 +00:00
|
|
|
|
2017-12-19 16:54:57 +00:00
|
|
|
includeArg, excludeArg := getIncludeExcludeArgs(cmd)
|
|
|
|
gitscanner.Filter = buildFilepathFilter(cfg, includeArg, excludeArg)
|
|
|
|
|
2018-08-31 19:42:18 +00:00
|
|
|
if len(args) == 0 {
|
|
|
|
// Only scan the index when "git lfs ls-files" was invoked with
|
|
|
|
// no arguments.
|
|
|
|
//
|
|
|
|
// Do so to avoid showing "mixed" results, e.g., ls-files output
|
|
|
|
// from a specific historical revision, and the index.
|
|
|
|
if err := gitscanner.ScanIndex(ref, nil); err != nil {
|
|
|
|
Exit("Could not scan for Git LFS index: %s", err)
|
|
|
|
}
|
2017-12-19 19:24:35 +00:00
|
|
|
}
|
2017-12-19 23:32:04 +00:00
|
|
|
if lsFilesScanAll {
|
|
|
|
if err := gitscanner.ScanAll(nil); err != nil {
|
|
|
|
Exit("Could not scan for Git LFS history: %s", err)
|
|
|
|
}
|
|
|
|
} else {
|
2017-12-19 23:59:06 +00:00
|
|
|
var err error
|
|
|
|
if lsFilesScanDeleted {
|
|
|
|
err = gitscanner.ScanRefWithDeleted(ref, nil)
|
|
|
|
} else {
|
|
|
|
err = gitscanner.ScanTree(ref)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2017-12-19 23:32:04 +00:00
|
|
|
Exit("Could not scan for Git LFS tree: %s", err)
|
|
|
|
}
|
2016-11-18 00:36:38 +00:00
|
|
|
}
|
2015-09-25 16:30:01 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 12:21:18 +00:00
|
|
|
// Returns true if a pointer appears to be properly smudge on checkout
|
|
|
|
func fileExistsOfSize(p *lfs.WrappedPointer) bool {
|
2018-06-25 04:49:53 +00:00
|
|
|
path := cfg.Filesystem().DecodePathname(p.Name)
|
|
|
|
info, err := os.Stat(path)
|
2017-08-29 12:21:18 +00:00
|
|
|
return err == nil && info.Size() == p.Size
|
|
|
|
}
|
|
|
|
|
|
|
|
func lsFilesMarker(p *lfs.WrappedPointer) string {
|
|
|
|
if fileExistsOfSize(p) {
|
2015-09-25 16:30:01 +00:00
|
|
|
return "*"
|
2014-10-03 16:17:26 +00:00
|
|
|
}
|
2015-09-25 16:30:01 +00:00
|
|
|
return "-"
|
2014-09-22 21:26:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-09-01 16:09:38 +00:00
|
|
|
RegisterCommand("ls-files", lsFilesCommand, func(cmd *cobra.Command) {
|
2016-08-10 15:33:25 +00:00
|
|
|
cmd.Flags().BoolVarP(&longOIDs, "long", "l", false, "")
|
2017-12-01 17:33:54 +00:00
|
|
|
cmd.Flags().BoolVarP(&lsFilesShowSize, "size", "s", false, "")
|
2017-01-05 16:06:20 +00:00
|
|
|
cmd.Flags().BoolVarP(&debug, "debug", "d", false, "")
|
2017-12-19 23:31:39 +00:00
|
|
|
cmd.Flags().BoolVarP(&lsFilesScanAll, "all", "a", false, "")
|
2017-12-19 23:59:06 +00:00
|
|
|
cmd.Flags().BoolVar(&lsFilesScanDeleted, "deleted", false, "")
|
2017-12-19 16:54:57 +00:00
|
|
|
cmd.Flags().StringVarP(&includeArg, "include", "I", "", "Include a list of paths")
|
|
|
|
cmd.Flags().StringVarP(&excludeArg, "exclude", "X", "", "Exclude a list of paths")
|
2016-08-10 15:33:25 +00:00
|
|
|
})
|
2014-09-22 21:26:43 +00:00
|
|
|
}
|