ls-files will use a scanner and honor the current branch, or take a branch on the command line
This commit is contained in:
parent
91b0999b42
commit
3b13dbfb19
@ -2,10 +2,8 @@ package commands
|
||||
|
||||
import (
|
||||
"github.com/github/git-media/gitmedia"
|
||||
"github.com/github/git-media/pointer"
|
||||
"github.com/github/git-media/scanner"
|
||||
"github.com/spf13/cobra"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -17,21 +15,23 @@ var (
|
||||
)
|
||||
|
||||
func lsFilesCommand(cmd *cobra.Command, args []string) {
|
||||
filepath.Walk(gitmedia.LocalLinkDir, func(path string, info os.FileInfo, err error) error {
|
||||
if !info.IsDir() {
|
||||
base, _ := filepath.Rel(gitmedia.LocalLinkDir, path)
|
||||
firstTwo := filepath.Dir(base)
|
||||
rest := filepath.Base(base)
|
||||
ref, err := gitmedia.CurrentRef()
|
||||
if err != nil {
|
||||
Panic(err, "Could not ls-files")
|
||||
}
|
||||
|
||||
link, err := pointer.FindLink(firstTwo + rest)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
Print(link.Name)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if len(args) == 1 {
|
||||
ref = args[0]
|
||||
}
|
||||
|
||||
pointers, err := scanner.Scan(ref)
|
||||
if err != nil {
|
||||
Panic(err, "Could not scan for git media files")
|
||||
}
|
||||
|
||||
for _, p := range pointers {
|
||||
Print(p.Name)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
@ -7,11 +7,19 @@ import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func Scan(ref string) ([]*pointer.Pointer, error) {
|
||||
type ScannedPointer struct {
|
||||
Name string
|
||||
*pointer.Pointer
|
||||
}
|
||||
|
||||
func Scan(ref string) ([]*ScannedPointer, error) {
|
||||
fileNameMap := make(map[string]string, 0)
|
||||
|
||||
// Gets all objects git knows about
|
||||
var buf bytes.Buffer
|
||||
objects, _ := git.RevListObjects(ref, "", ref == "")
|
||||
for _, o := range objects {
|
||||
fileNameMap[o.Sha1] = o.Name
|
||||
buf.WriteString(o.Sha1 + "\n")
|
||||
}
|
||||
|
||||
@ -36,14 +44,15 @@ func Scan(ref string) ([]*pointer.Pointer, error) {
|
||||
|
||||
r := bytes.NewBufferString(data)
|
||||
|
||||
pointers := make([]*pointer.Pointer, 0)
|
||||
pointers := make([]*ScannedPointer, 0)
|
||||
for {
|
||||
l, err := r.ReadBytes('\n')
|
||||
if err != nil { // Probably check for EOF
|
||||
break
|
||||
}
|
||||
|
||||
s, _ := strconv.Atoi(string(bytes.Fields(l)[2]))
|
||||
fields := bytes.Fields(l)
|
||||
s, _ := strconv.Atoi(string(fields[2]))
|
||||
|
||||
nbuf := make([]byte, s)
|
||||
_, err = r.Read(nbuf)
|
||||
@ -51,9 +60,12 @@ func Scan(ref string) ([]*pointer.Pointer, error) {
|
||||
return nil, err // Legit errors
|
||||
}
|
||||
|
||||
sha1 := string(fields[0])
|
||||
name := fileNameMap[sha1]
|
||||
|
||||
p, err := pointer.Decode(bytes.NewBuffer(nbuf))
|
||||
if err == nil {
|
||||
pointers = append(pointers, p)
|
||||
pointers = append(pointers, &ScannedPointer{name, p})
|
||||
}
|
||||
|
||||
_, err = r.ReadBytes('\n') // Extra \n inserted by cat-file
|
||||
|
Loading…
Reference in New Issue
Block a user