Skip to content

Commit f56f240

Browse files
committed
Merge pull request git-lfs#168 from hawser/v0.4.0-bugs
V0.4.0 bugs
2 parents 0cc22d2 + 887117c commit f56f240

File tree

3 files changed

+74
-3
lines changed

3 files changed

+74
-3
lines changed

commands/command_add.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import (
44
"fmt"
55
"github.com/hawser/git-hawser/hawser"
66
"github.com/spf13/cobra"
7+
"io"
78
"os"
9+
"strings"
810
)
911

1012
var (
@@ -23,13 +25,20 @@ func addCommand(cmd *cobra.Command, args []string) {
2325
return
2426
}
2527

28+
addTrailingLinebreak := needsTrailingLinebreak(".gitattributes")
2629
knownPaths := findPaths()
2730
attributesFile, err := os.OpenFile(".gitattributes", os.O_RDWR|os.O_APPEND|os.O_CREATE, 0660)
2831
if err != nil {
2932
Print("Error opening .gitattributes file")
3033
return
3134
}
3235

36+
if addTrailingLinebreak {
37+
if _, err := attributesFile.WriteString("\n"); err != nil {
38+
Print("Error writing to .gitattributes")
39+
}
40+
}
41+
3342
for _, t := range args {
3443
isKnownPath := false
3544
for _, k := range knownPaths {
@@ -54,6 +63,28 @@ func addCommand(cmd *cobra.Command, args []string) {
5463
attributesFile.Close()
5564
}
5665

66+
func needsTrailingLinebreak(filename string) bool {
67+
file, err := os.Open(filename)
68+
if err != nil {
69+
return false
70+
}
71+
72+
defer file.Close()
73+
buf := make([]byte, 16384)
74+
bytesRead := 0
75+
for {
76+
n, err := file.Read(buf)
77+
if err == io.EOF {
78+
break
79+
} else if err != nil {
80+
return false
81+
}
82+
bytesRead = n
83+
}
84+
85+
return !strings.HasSuffix(string(buf[0:bytesRead]), "\n")
86+
}
87+
5788
func init() {
5889
RootCmd.AddCommand(addCmd)
5990
}

commands/path_test.go

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,51 @@ func TestPathOnEmptyRepository(t *testing.T) {
4747

4848
cmd := repo.Command("add", "*.gif")
4949
cmd.Output = "Adding path *.gif"
50+
51+
cmd.Before(func() {
52+
// write attributes file in .git
53+
path := filepath.Join(".gitattributes")
54+
repo.WriteFile(path, "*.mov filter=hawser -crlf\n")
55+
})
56+
57+
cmd.After(func() {
58+
// assert path was added
59+
assert.Equal(t, "*.mov filter=hawser -crlf\n*.gif filter=hawser -crlf\n", repo.ReadFile(".gitattributes"))
60+
61+
expected := "Listing paths\n *.mov (.gitattributes)\n *.gif (.gitattributes)\n"
62+
63+
assert.Equal(t, expected, repo.MediaCmd("path"))
64+
65+
// assert hook was created
66+
stat, err := os.Stat(prePushHookFile)
67+
assert.Equal(t, nil, err)
68+
assert.Equal(t, false, stat.IsDir())
69+
})
70+
71+
cmd = repo.Command("path")
72+
cmd.Output = "Listing paths"
73+
}
74+
75+
func TestAddPathWithoutTrailingLinebreak(t *testing.T) {
76+
repo := NewRepository(t, "empty")
77+
defer repo.Test()
78+
79+
prePushHookFile := filepath.Join(repo.Path, ".git", "hooks", "pre-push")
80+
81+
cmd := repo.Command("add", "*.gif")
82+
cmd.Output = "Adding path *.gif"
83+
84+
cmd.Before(func() {
85+
// write attributes file in .git
86+
path := filepath.Join(".gitattributes")
87+
repo.WriteFile(path, "*.mov filter=hawser -crlf")
88+
})
89+
5090
cmd.After(func() {
5191
// assert path was added
52-
assert.Equal(t, "*.gif filter=hawser -crlf\n", repo.ReadFile(".gitattributes"))
92+
assert.Equal(t, "*.mov filter=hawser -crlf\n*.gif filter=hawser -crlf\n", repo.ReadFile(".gitattributes"))
5393

54-
expected := "Listing paths\n *.gif (.gitattributes)\n"
94+
expected := "Listing paths\n *.mov (.gitattributes)\n *.gif (.gitattributes)\n"
5595

5696
assert.Equal(t, expected, repo.MediaCmd("path"))
5797

hawser/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func callExternalPut(filehash, filename string, lm *linkMeta, cb CopyCallback) *
250250
verifyReq.Header.Set(h, v)
251251
}
252252

253-
verifyCreds, err := setRequestHeaders(req)
253+
verifyCreds, err := setRequestHeaders(verifyReq)
254254
if err != nil {
255255
return Errorf(err, "Error attempting to verify %s", filename)
256256
}

0 commit comments

Comments
 (0)