git-lfs/commands/command_ls_files.go

60 lines
1.1 KiB
Go
Raw Normal View History

package commands
import (
2015-03-19 19:30:55 +00:00
"github.com/github/git-lfs/git"
"github.com/github/git-lfs/lfs"
2015-05-25 18:20:50 +00:00
"github.com/github/git-lfs/vendor/_nuts/github.com/spf13/cobra"
)
var (
lsFilesCmd = &cobra.Command{
2015-09-17 22:08:28 +00:00
Use: "ls-files",
Run: lsFilesCommand,
}
)
func lsFilesCommand(cmd *cobra.Command, args []string) {
requireInRepo()
2014-10-11 14:28:46 +00:00
var ref string
var err error
if len(args) == 1 {
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 {
Exit(err.Error())
2014-10-11 14:28:46 +00:00
}
ref = fullref.Sha
}
scanOpt := &lfs.ScanRefsOptions{SkipDeletedBlobs: true}
2015-09-19 06:59:39 +00:00
listFiles := make(map[string][]string)
fileTree, err := lfs.ScanTree(ref)
if err != nil {
Panic(err, "Could not scan for Git LFS tree")
}
for _, p := range fileTree {
listFiles[p.Sha1] = append(listFiles[p.Sha1], p.Name)
}
pointers, err := lfs.ScanRefs(ref, "", scanOpt)
if err != nil {
2015-03-19 19:30:55 +00:00
Panic(err, "Could not scan for Git LFS files")
}
for _, p := range pointers {
2014-10-08 13:04:07 +00:00
Print(p.Name)
2015-09-19 06:59:39 +00:00
if len(listFiles[p.Sha1]) > 1 {
for _, v := range listFiles[p.Sha1][1:] {
Print(v + " (duplicate of " + p.Name + ")")
}
}
delete(listFiles, p.Sha1)
}
}
func init() {
RootCmd.AddCommand(lsFilesCmd)
}