Skip to content

Commit 6d2e84e

Browse files
committed
Merge pull request git-lfs#4 from github/rewrite-client
LFS changes
2 parents e370b10 + 16f882d commit 6d2e84e

28 files changed

+2341
-1391
lines changed

commands/command_add.go

Lines changed: 0 additions & 90 deletions
This file was deleted.

commands/command_path.go

Lines changed: 0 additions & 87 deletions
This file was deleted.

commands/command_smudge.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func smudgeCommand(cmd *cobra.Command, args []string) {
5656
Error(err.Error())
5757
}
5858

59-
err = ptr.Smudge(os.Stdout, cb)
59+
err = ptr.Smudge(os.Stdout, filename, cb)
6060
if file != nil {
6161
file.Close()
6262
}

commands/command_track.go

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
package commands
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"github.com/github/git-lfs/lfs"
7+
"github.com/spf13/cobra"
8+
"io"
9+
"os"
10+
"path/filepath"
11+
"strings"
12+
)
13+
14+
var (
15+
trackCmd = &cobra.Command{
16+
Use: "track",
17+
Short: "Manipulate .gitattributes",
18+
Run: trackCommand,
19+
}
20+
)
21+
22+
func trackCommand(cmd *cobra.Command, args []string) {
23+
lfs.InstallHooks(false)
24+
25+
if len(args) == 0 {
26+
Print("Listing tracked paths")
27+
knownPaths := findPaths()
28+
for _, t := range knownPaths {
29+
Print(" %s (%s)", t.Path, t.Source)
30+
}
31+
return
32+
}
33+
34+
addTrailingLinebreak := needsTrailingLinebreak(".gitattributes")
35+
knownPaths := findPaths()
36+
attributesFile, err := os.OpenFile(".gitattributes", os.O_RDWR|os.O_APPEND|os.O_CREATE, 0660)
37+
if err != nil {
38+
Print("Error opening .gitattributes file")
39+
return
40+
}
41+
42+
if addTrailingLinebreak {
43+
if _, err := attributesFile.WriteString("\n"); err != nil {
44+
Print("Error writing to .gitattributes")
45+
}
46+
}
47+
48+
for _, t := range args {
49+
isKnownPath := false
50+
for _, k := range knownPaths {
51+
if t == k.Path {
52+
isKnownPath = true
53+
}
54+
}
55+
56+
if isKnownPath {
57+
Print("%s already supported", t)
58+
continue
59+
}
60+
61+
_, err := attributesFile.WriteString(fmt.Sprintf("%s filter=lfs -crlf\n", t))
62+
if err != nil {
63+
Print("Error adding path %s", t)
64+
continue
65+
}
66+
Print("Tracking %s", t)
67+
}
68+
69+
attributesFile.Close()
70+
}
71+
72+
type mediaPath struct {
73+
Path string
74+
Source string
75+
}
76+
77+
func findAttributeFiles() []string {
78+
paths := make([]string, 0)
79+
80+
repoAttributes := filepath.Join(lfs.LocalGitDir, "info", "attributes")
81+
if _, err := os.Stat(repoAttributes); err == nil {
82+
paths = append(paths, repoAttributes)
83+
}
84+
85+
filepath.Walk(lfs.LocalWorkingDir, func(path string, info os.FileInfo, err error) error {
86+
if err != nil {
87+
return err
88+
}
89+
90+
if !info.IsDir() && (filepath.Base(path) == ".gitattributes") {
91+
paths = append(paths, path)
92+
}
93+
return nil
94+
})
95+
96+
return paths
97+
}
98+
99+
func findPaths() []mediaPath {
100+
paths := make([]mediaPath, 0)
101+
wd, _ := os.Getwd()
102+
103+
for _, path := range findAttributeFiles() {
104+
attributes, err := os.Open(path)
105+
if err != nil {
106+
return paths
107+
}
108+
109+
scanner := bufio.NewScanner(attributes)
110+
for scanner.Scan() {
111+
line := scanner.Text()
112+
if line == "" {
113+
continue
114+
}
115+
116+
if strings.Contains(line, "filter=lfs") {
117+
fields := strings.Fields(line)
118+
relPath, _ := filepath.Rel(wd, path)
119+
paths = append(paths, mediaPath{Path: fields[0], Source: relPath})
120+
}
121+
}
122+
}
123+
124+
return paths
125+
}
126+
127+
func needsTrailingLinebreak(filename string) bool {
128+
file, err := os.Open(filename)
129+
if err != nil {
130+
return false
131+
}
132+
133+
defer file.Close()
134+
buf := make([]byte, 16384)
135+
bytesRead := 0
136+
for {
137+
n, err := file.Read(buf)
138+
if err == io.EOF {
139+
break
140+
} else if err != nil {
141+
return false
142+
}
143+
bytesRead = n
144+
}
145+
146+
return !strings.HasSuffix(string(buf[0:bytesRead]), "\n")
147+
}
148+
149+
func init() {
150+
RootCmd.AddCommand(trackCmd)
151+
}

commands/command_rm.go renamed to commands/command_untrack.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,18 @@ import (
1010
)
1111

1212
var (
13-
removeCmd = &cobra.Command{
14-
Use: "remove",
13+
untrackCmd = &cobra.Command{
14+
Use: "untrack",
1515
Short: "Remove an entry from .gitattributes",
16-
Run: removeCommand,
17-
}
18-
19-
rmCmd = &cobra.Command{
20-
Use: "rm",
21-
Short: "Remove an entry from .gitattributes",
22-
Run: removeCommand,
16+
Run: untrackCommand,
2317
}
2418
)
2519

26-
func removeCommand(cmd *cobra.Command, args []string) {
20+
func untrackCommand(cmd *cobra.Command, args []string) {
2721
lfs.InstallHooks(false)
2822

2923
if len(args) < 1 {
30-
Print("git lfs path rm <path> [path]*")
24+
Print("git lfs untrack <path> [path]*")
3125
return
3226
}
3327

@@ -59,7 +53,7 @@ func removeCommand(cmd *cobra.Command, args []string) {
5953
if !removeThisPath {
6054
attributesFile.WriteString(line + "\n")
6155
} else {
62-
Print("Removing path %s", fields[0])
56+
Print("Untracking %s", fields[0])
6357
}
6458
}
6559
}
@@ -68,6 +62,5 @@ func removeCommand(cmd *cobra.Command, args []string) {
6862
}
6963

7064
func init() {
71-
RootCmd.AddCommand(rmCmd)
72-
RootCmd.AddCommand(removeCmd)
65+
RootCmd.AddCommand(untrackCmd)
7366
}

0 commit comments

Comments
 (0)