ls-files show duplicate with OID(s)

This commit is contained in:
Bhuridech Sudsee 2015-09-25 12:30:23 +07:00 committed by Rick Olson
parent 9f88f2fc52
commit 21bc33e8c0
2 changed files with 32 additions and 11 deletions

@ -1,12 +1,15 @@
package commands package commands
import ( import (
"strings"
"github.com/github/git-lfs/git" "github.com/github/git-lfs/git"
"github.com/github/git-lfs/lfs" "github.com/github/git-lfs/lfs"
"github.com/github/git-lfs/vendor/_nuts/github.com/spf13/cobra" "github.com/github/git-lfs/vendor/_nuts/github.com/spf13/cobra"
) )
var ( var (
longOIDs = false
lsFilesCmd = &cobra.Command{ lsFilesCmd = &cobra.Command{
Use: "ls-files", Use: "ls-files",
Run: lsFilesCommand, Run: lsFilesCommand,
@ -29,6 +32,11 @@ func lsFilesCommand(cmd *cobra.Command, args []string) {
ref = fullref.Sha ref = fullref.Sha
} }
showShaLen := 7
if longOIDs {
showShaLen = 40
}
scanOpt := &lfs.ScanRefsOptions{SkipDeletedBlobs: true} scanOpt := &lfs.ScanRefsOptions{SkipDeletedBlobs: true}
listFiles := make(map[string][]string) listFiles := make(map[string][]string)
fileTree, err := lfs.ScanTree(ref) fileTree, err := lfs.ScanTree(ref)
@ -44,16 +52,12 @@ func lsFilesCommand(cmd *cobra.Command, args []string) {
Panic(err, "Could not scan for Git LFS files") Panic(err, "Could not scan for Git LFS files")
} }
for _, p := range pointers { for _, p := range pointers {
Print(p.Name) Print(p.Sha1[0:showShaLen] + " ==> [ " + strings.Join(listFiles[p.Sha1], ",") + " ]")
if len(listFiles[p.Sha1]) > 1 {
for _, v := range listFiles[p.Sha1][1:] {
Print(v + " (duplicate of " + p.Name + ")")
}
}
delete(listFiles, p.Sha1) delete(listFiles, p.Sha1)
} }
} }
func init() { func init() {
lsFilesCmd.Flags().BoolVarP(&longOIDs, "long", "l", false, "Show object ID(s) 40 characters")
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"
[ "missing.dat" = "$(git lfs ls-files)" ] [ "74f2f1c ==> [ 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
@ -70,8 +70,8 @@ begin_test "ls-files: show duplicate files"
( (
set -e set -e
mkdir dupRepo mkdir dupRepoShort
cd dupRepo cd dupRepoShort
git init git init
git lfs track "*.tgz" | grep "Tracking \*.tgz" git lfs track "*.tgz" | grep "Tracking \*.tgz"
@ -80,7 +80,24 @@ 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"
[ "$(git lfs ls-files)" = "$(printf 'one.tgz\ntwo.tgz (duplicate of one.tgz)')" ] [ "67f58a4 ==> [ one.tgz,two.tgz ]" = "$(git lfs ls-files)" ]
)
end_test
begin_test "ls-files: show duplicate files with long OID"
(
set -e
mkdir dupRepoLong
cd dupRepoLong
git init
git lfs track "*.tgz" | grep "Tracking \*.tgz"
echo "test content" > one.tgz
echo "test content" > two.tgz
git add one.tgz
git add two.tgz
git commit -m "add duplicate files with long OID"
[ "67f58a40df1e32b439f55e722178c337042d2148 ==> [ one.tgz,two.tgz ]" = "$(git lfs ls-files --long)" ]
) )
end_test end_test