tweak ls-files output

* simpler, easier to chop up with unix tools
* show LFS OID, not Git SHA1
* only scan the tree, not the refs
* show whether files are LFS pointers or full objects
This commit is contained in:
Rick Olson 2015-09-25 10:30:01 -06:00
parent 21bc33e8c0
commit 2082b26ce7
2 changed files with 27 additions and 20 deletions

@ -1,7 +1,7 @@
package commands package commands
import ( import (
"strings" "os"
"github.com/github/git-lfs/git" "github.com/github/git-lfs/git"
"github.com/github/git-lfs/lfs" "github.com/github/git-lfs/lfs"
@ -32,32 +32,31 @@ func lsFilesCommand(cmd *cobra.Command, args []string) {
ref = fullref.Sha ref = fullref.Sha
} }
showShaLen := 7 showOidLen := 10
if longOIDs { if longOIDs {
showShaLen = 40 showOidLen = 64
} }
scanOpt := &lfs.ScanRefsOptions{SkipDeletedBlobs: true} files, err := lfs.ScanTree(ref)
listFiles := make(map[string][]string)
fileTree, err := lfs.ScanTree(ref)
if err != nil { if err != nil {
Panic(err, "Could not scan for Git LFS tree") Panic(err, "Could not scan for Git LFS tree: %s", err)
}
for _, p := range fileTree {
listFiles[p.Sha1] = append(listFiles[p.Sha1], p.Name)
} }
pointers, err := lfs.ScanRefs(ref, "", scanOpt) for _, p := range files {
if err != nil { Print("%s %s %s", p.Oid[0:showOidLen], lsFilesMarker(p), p.Name)
Panic(err, "Could not scan for Git LFS files")
} }
for _, p := range pointers { }
Print(p.Sha1[0:showShaLen] + " ==> [ " + strings.Join(listFiles[p.Sha1], ",") + " ]")
delete(listFiles, p.Sha1) func lsFilesMarker(p *lfs.WrappedPointer) string {
info, err := os.Stat(p.Name)
if err == nil && info.Size() == p.Size {
return "*"
} }
return "-"
} }
func init() { func init() {
lsFilesCmd.Flags().BoolVarP(&longOIDs, "long", "l", false, "Show object ID(s) 40 characters") lsFilesCmd.Flags().BoolVarP(&longOIDs, "long", "l", false, "")
RootCmd.AddCommand(lsFilesCmd) RootCmd.AddCommand(lsFilesCmd)
} }

@ -15,7 +15,7 @@ begin_test "ls-files"
echo "missing" > missing.dat echo "missing" > missing.dat
git add missing.dat git add missing.dat
git commit -m "add missing file" git commit -m "add missing file"
[ "74f2f1c ==> [ missing.dat ]" = "$(git lfs ls-files)" ] [ "6bbd052ab0 * missing.dat" = "$(git lfs ls-files)" ]
git rm missing.dat git rm missing.dat
git add some.dat some.txt git add some.dat some.txt
@ -80,7 +80,11 @@ begin_test "ls-files: show duplicate files"
git add one.tgz git add one.tgz
git add two.tgz git add two.tgz
git commit -m "add duplicate files" git commit -m "add duplicate files"
[ "67f58a4 ==> [ one.tgz,two.tgz ]" = "$(git lfs ls-files)" ]
expected="$(echo "a1fff0ffef * one.tgz
a1fff0ffef * two.tgz")"
[ "$expected" = "$(git lfs ls-files)" ]
) )
end_test end_test
@ -98,6 +102,10 @@ begin_test "ls-files: show duplicate files with long OID"
git add one.tgz git add one.tgz
git add two.tgz git add two.tgz
git commit -m "add duplicate files with long OID" git commit -m "add duplicate files with long OID"
[ "67f58a40df1e32b439f55e722178c337042d2148 ==> [ one.tgz,two.tgz ]" = "$(git lfs ls-files --long)" ]
expected="$(echo "a1fff0ffefb9eace7230c24e50731f0a91c62f9cefdfe77121c2f607125dffae * one.tgz
a1fff0ffefb9eace7230c24e50731f0a91c62f9cefdfe77121c2f607125dffae * two.tgz")"
[ "$expected" = "$(git lfs ls-files --long)" ]
) )
end_test end_test