rename path => track
This commit is contained in:
parent
7369c01d82
commit
b500178a06
@ -1,90 +0,0 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/github/git-lfs/lfs"
|
||||
"github.com/spf13/cobra"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
addCmd = &cobra.Command{
|
||||
Use: "add",
|
||||
Short: "Add an entry to .gitattributes",
|
||||
Run: addCommand,
|
||||
}
|
||||
)
|
||||
|
||||
func addCommand(cmd *cobra.Command, args []string) {
|
||||
lfs.InstallHooks(false)
|
||||
|
||||
if len(args) < 1 {
|
||||
Print("git lfs path add <path> [path]*")
|
||||
return
|
||||
}
|
||||
|
||||
addTrailingLinebreak := needsTrailingLinebreak(".gitattributes")
|
||||
knownPaths := findPaths()
|
||||
attributesFile, err := os.OpenFile(".gitattributes", os.O_RDWR|os.O_APPEND|os.O_CREATE, 0660)
|
||||
if err != nil {
|
||||
Print("Error opening .gitattributes file")
|
||||
return
|
||||
}
|
||||
|
||||
if addTrailingLinebreak {
|
||||
if _, err := attributesFile.WriteString("\n"); err != nil {
|
||||
Print("Error writing to .gitattributes")
|
||||
}
|
||||
}
|
||||
|
||||
for _, t := range args {
|
||||
isKnownPath := false
|
||||
for _, k := range knownPaths {
|
||||
if t == k.Path {
|
||||
isKnownPath = true
|
||||
}
|
||||
}
|
||||
|
||||
if isKnownPath {
|
||||
Print("%s already supported", t)
|
||||
continue
|
||||
}
|
||||
|
||||
_, err := attributesFile.WriteString(fmt.Sprintf("%s filter=lfs -crlf\n", t))
|
||||
if err != nil {
|
||||
Print("Error adding path %s", t)
|
||||
continue
|
||||
}
|
||||
Print("Adding path %s", t)
|
||||
}
|
||||
|
||||
attributesFile.Close()
|
||||
}
|
||||
|
||||
func needsTrailingLinebreak(filename string) bool {
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
buf := make([]byte, 16384)
|
||||
bytesRead := 0
|
||||
for {
|
||||
n, err := file.Read(buf)
|
||||
if err == io.EOF {
|
||||
break
|
||||
} else if err != nil {
|
||||
return false
|
||||
}
|
||||
bytesRead = n
|
||||
}
|
||||
|
||||
return !strings.HasSuffix(string(buf[0:bytesRead]), "\n")
|
||||
}
|
||||
|
||||
func init() {
|
||||
RootCmd.AddCommand(addCmd)
|
||||
}
|
@ -1,87 +0,0 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"github.com/github/git-lfs/lfs"
|
||||
"github.com/spf13/cobra"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
pathCmd = &cobra.Command{
|
||||
Use: "path",
|
||||
Short: "Manipulate .gitattributes",
|
||||
Run: pathCommand,
|
||||
}
|
||||
)
|
||||
|
||||
func pathCommand(cmd *cobra.Command, args []string) {
|
||||
lfs.InstallHooks(false)
|
||||
|
||||
Print("Listing paths")
|
||||
knownPaths := findPaths()
|
||||
for _, t := range knownPaths {
|
||||
Print(" %s (%s)", t.Path, t.Source)
|
||||
}
|
||||
}
|
||||
|
||||
type mediaPath struct {
|
||||
Path string
|
||||
Source string
|
||||
}
|
||||
|
||||
func findAttributeFiles() []string {
|
||||
paths := make([]string, 0)
|
||||
|
||||
repoAttributes := filepath.Join(lfs.LocalGitDir, "info", "attributes")
|
||||
if _, err := os.Stat(repoAttributes); err == nil {
|
||||
paths = append(paths, repoAttributes)
|
||||
}
|
||||
|
||||
filepath.Walk(lfs.LocalWorkingDir, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !info.IsDir() && (filepath.Base(path) == ".gitattributes") {
|
||||
paths = append(paths, path)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
return paths
|
||||
}
|
||||
|
||||
func findPaths() []mediaPath {
|
||||
paths := make([]mediaPath, 0)
|
||||
wd, _ := os.Getwd()
|
||||
|
||||
for _, path := range findAttributeFiles() {
|
||||
attributes, err := os.Open(path)
|
||||
if err != nil {
|
||||
return paths
|
||||
}
|
||||
|
||||
scanner := bufio.NewScanner(attributes)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
if strings.Contains(line, "filter=lfs") {
|
||||
fields := strings.Fields(line)
|
||||
relPath, _ := filepath.Rel(wd, path)
|
||||
paths = append(paths, mediaPath{Path: fields[0], Source: relPath})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return paths
|
||||
}
|
||||
|
||||
func init() {
|
||||
RootCmd.AddCommand(pathCmd)
|
||||
}
|
151
commands/command_track.go
Normal file
151
commands/command_track.go
Normal file
@ -0,0 +1,151 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"github.com/github/git-lfs/lfs"
|
||||
"github.com/spf13/cobra"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
trackCmd = &cobra.Command{
|
||||
Use: "track",
|
||||
Short: "Manipulate .gitattributes",
|
||||
Run: trackCommand,
|
||||
}
|
||||
)
|
||||
|
||||
func trackCommand(cmd *cobra.Command, args []string) {
|
||||
lfs.InstallHooks(false)
|
||||
|
||||
if len(args) == 0 {
|
||||
Print("Listing tracked paths")
|
||||
knownPaths := findPaths()
|
||||
for _, t := range knownPaths {
|
||||
Print(" %s (%s)", t.Path, t.Source)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
addTrailingLinebreak := needsTrailingLinebreak(".gitattributes")
|
||||
knownPaths := findPaths()
|
||||
attributesFile, err := os.OpenFile(".gitattributes", os.O_RDWR|os.O_APPEND|os.O_CREATE, 0660)
|
||||
if err != nil {
|
||||
Print("Error opening .gitattributes file")
|
||||
return
|
||||
}
|
||||
|
||||
if addTrailingLinebreak {
|
||||
if _, err := attributesFile.WriteString("\n"); err != nil {
|
||||
Print("Error writing to .gitattributes")
|
||||
}
|
||||
}
|
||||
|
||||
for _, t := range args {
|
||||
isKnownPath := false
|
||||
for _, k := range knownPaths {
|
||||
if t == k.Path {
|
||||
isKnownPath = true
|
||||
}
|
||||
}
|
||||
|
||||
if isKnownPath {
|
||||
Print("%s already supported", t)
|
||||
continue
|
||||
}
|
||||
|
||||
_, err := attributesFile.WriteString(fmt.Sprintf("%s filter=lfs -crlf\n", t))
|
||||
if err != nil {
|
||||
Print("Error adding path %s", t)
|
||||
continue
|
||||
}
|
||||
Print("Tracking %s", t)
|
||||
}
|
||||
|
||||
attributesFile.Close()
|
||||
}
|
||||
|
||||
type mediaPath struct {
|
||||
Path string
|
||||
Source string
|
||||
}
|
||||
|
||||
func findAttributeFiles() []string {
|
||||
paths := make([]string, 0)
|
||||
|
||||
repoAttributes := filepath.Join(lfs.LocalGitDir, "info", "attributes")
|
||||
if _, err := os.Stat(repoAttributes); err == nil {
|
||||
paths = append(paths, repoAttributes)
|
||||
}
|
||||
|
||||
filepath.Walk(lfs.LocalWorkingDir, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !info.IsDir() && (filepath.Base(path) == ".gitattributes") {
|
||||
paths = append(paths, path)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
return paths
|
||||
}
|
||||
|
||||
func findPaths() []mediaPath {
|
||||
paths := make([]mediaPath, 0)
|
||||
wd, _ := os.Getwd()
|
||||
|
||||
for _, path := range findAttributeFiles() {
|
||||
attributes, err := os.Open(path)
|
||||
if err != nil {
|
||||
return paths
|
||||
}
|
||||
|
||||
scanner := bufio.NewScanner(attributes)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
if strings.Contains(line, "filter=lfs") {
|
||||
fields := strings.Fields(line)
|
||||
relPath, _ := filepath.Rel(wd, path)
|
||||
paths = append(paths, mediaPath{Path: fields[0], Source: relPath})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return paths
|
||||
}
|
||||
|
||||
func needsTrailingLinebreak(filename string) bool {
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
buf := make([]byte, 16384)
|
||||
bytesRead := 0
|
||||
for {
|
||||
n, err := file.Read(buf)
|
||||
if err == io.EOF {
|
||||
break
|
||||
} else if err != nil {
|
||||
return false
|
||||
}
|
||||
bytesRead = n
|
||||
}
|
||||
|
||||
return !strings.HasSuffix(string(buf[0:bytesRead]), "\n")
|
||||
}
|
||||
|
||||
func init() {
|
||||
RootCmd.AddCommand(trackCmd)
|
||||
}
|
@ -10,24 +10,18 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
removeCmd = &cobra.Command{
|
||||
Use: "remove",
|
||||
untrackCmd = &cobra.Command{
|
||||
Use: "untrack",
|
||||
Short: "Remove an entry from .gitattributes",
|
||||
Run: removeCommand,
|
||||
}
|
||||
|
||||
rmCmd = &cobra.Command{
|
||||
Use: "rm",
|
||||
Short: "Remove an entry from .gitattributes",
|
||||
Run: removeCommand,
|
||||
Run: untrackCommand,
|
||||
}
|
||||
)
|
||||
|
||||
func removeCommand(cmd *cobra.Command, args []string) {
|
||||
func untrackCommand(cmd *cobra.Command, args []string) {
|
||||
lfs.InstallHooks(false)
|
||||
|
||||
if len(args) < 1 {
|
||||
Print("git lfs path rm <path> [path]*")
|
||||
Print("git lfs untrack <path> [path]*")
|
||||
return
|
||||
}
|
||||
|
||||
@ -59,7 +53,7 @@ func removeCommand(cmd *cobra.Command, args []string) {
|
||||
if !removeThisPath {
|
||||
attributesFile.WriteString(line + "\n")
|
||||
} else {
|
||||
Print("Removing path %s", fields[0])
|
||||
Print("Untracking %s", fields[0])
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -68,6 +62,5 @@ func removeCommand(cmd *cobra.Command, args []string) {
|
||||
}
|
||||
|
||||
func init() {
|
||||
RootCmd.AddCommand(rmCmd)
|
||||
RootCmd.AddCommand(removeCmd)
|
||||
RootCmd.AddCommand(untrackCmd)
|
||||
}
|
@ -8,15 +8,15 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPath(t *testing.T) {
|
||||
func TestTrack(t *testing.T) {
|
||||
repo := NewRepository(t, "attributes")
|
||||
defer repo.Test()
|
||||
|
||||
prePushHookFile := filepath.Join(repo.Path, ".git", "hooks", "pre-push")
|
||||
customHook := []byte("echo 'yo'")
|
||||
|
||||
cmd := repo.Command("path")
|
||||
cmd.Output = "Listing paths\n" +
|
||||
cmd := repo.Command("track")
|
||||
cmd.Output = "Listing tracked paths\n" +
|
||||
" *.mov (.git/info/attributes)\n" +
|
||||
" *.jpg (.gitattributes)\n" +
|
||||
" *.gif (a/.gitattributes)\n" +
|
||||
@ -39,14 +39,14 @@ func TestPath(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestPathOnEmptyRepository(t *testing.T) {
|
||||
func TestTrackOnEmptyRepository(t *testing.T) {
|
||||
repo := NewRepository(t, "empty")
|
||||
defer repo.Test()
|
||||
|
||||
prePushHookFile := filepath.Join(repo.Path, ".git", "hooks", "pre-push")
|
||||
|
||||
cmd := repo.Command("add", "*.gif")
|
||||
cmd.Output = "Adding path *.gif"
|
||||
cmd := repo.Command("track", "*.gif")
|
||||
cmd.Output = "Tracking *.gif"
|
||||
|
||||
cmd.Before(func() {
|
||||
// write attributes file in .git
|
||||
@ -58,9 +58,9 @@ func TestPathOnEmptyRepository(t *testing.T) {
|
||||
// assert path was added
|
||||
assert.Equal(t, "*.mov filter=lfs -crlf\n*.gif filter=lfs -crlf\n", repo.ReadFile(".gitattributes"))
|
||||
|
||||
expected := "Listing paths\n *.mov (.gitattributes)\n *.gif (.gitattributes)\n"
|
||||
expected := "Listing tracked paths\n *.mov (.gitattributes)\n *.gif (.gitattributes)\n"
|
||||
|
||||
assert.Equal(t, expected, repo.MediaCmd("path"))
|
||||
assert.Equal(t, expected, repo.MediaCmd("track"))
|
||||
|
||||
// assert hook was created
|
||||
stat, err := os.Stat(prePushHookFile)
|
||||
@ -68,18 +68,18 @@ func TestPathOnEmptyRepository(t *testing.T) {
|
||||
assert.Equal(t, false, stat.IsDir())
|
||||
})
|
||||
|
||||
cmd = repo.Command("path")
|
||||
cmd.Output = "Listing paths"
|
||||
cmd = repo.Command("track")
|
||||
cmd.Output = "Listing tracked paths"
|
||||
}
|
||||
|
||||
func TestAddPathWithoutTrailingLinebreak(t *testing.T) {
|
||||
func TestTrackWithoutTrailingLinebreak(t *testing.T) {
|
||||
repo := NewRepository(t, "empty")
|
||||
defer repo.Test()
|
||||
|
||||
prePushHookFile := filepath.Join(repo.Path, ".git", "hooks", "pre-push")
|
||||
|
||||
cmd := repo.Command("add", "*.gif")
|
||||
cmd.Output = "Adding path *.gif"
|
||||
cmd := repo.Command("track", "*.gif")
|
||||
cmd.Output = "Tracking *.gif"
|
||||
|
||||
cmd.Before(func() {
|
||||
// write attributes file in .git
|
||||
@ -91,9 +91,9 @@ func TestAddPathWithoutTrailingLinebreak(t *testing.T) {
|
||||
// assert path was added
|
||||
assert.Equal(t, "*.mov filter=lfs -crlf\n*.gif filter=lfs -crlf\n", repo.ReadFile(".gitattributes"))
|
||||
|
||||
expected := "Listing paths\n *.mov (.gitattributes)\n *.gif (.gitattributes)\n"
|
||||
expected := "Listing tracked paths\n *.mov (.gitattributes)\n *.gif (.gitattributes)\n"
|
||||
|
||||
assert.Equal(t, expected, repo.MediaCmd("path"))
|
||||
assert.Equal(t, expected, repo.MediaCmd("track"))
|
||||
|
||||
// assert hook was created
|
||||
stat, err := os.Stat(prePushHookFile)
|
||||
@ -101,6 +101,6 @@ func TestAddPathWithoutTrailingLinebreak(t *testing.T) {
|
||||
assert.Equal(t, false, stat.IsDir())
|
||||
})
|
||||
|
||||
cmd = repo.Command("path")
|
||||
cmd.Output = "Listing paths"
|
||||
cmd = repo.Command("track")
|
||||
cmd.Output = "Listing tracked paths"
|
||||
}
|
Loading…
Reference in New Issue
Block a user