add a trailing line break to .gitattributes before adding new entries
This commit is contained in:
parent
ec0d0ed669
commit
887117cef7
@ -4,7 +4,9 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/hawser/git-hawser/hawser"
|
"github.com/hawser/git-hawser/hawser"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -23,6 +25,7 @@ func addCommand(cmd *cobra.Command, args []string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
addTrailingLinebreak := needsTrailingLinebreak(".gitattributes")
|
||||||
knownPaths := findPaths()
|
knownPaths := findPaths()
|
||||||
attributesFile, err := os.OpenFile(".gitattributes", os.O_RDWR|os.O_APPEND|os.O_CREATE, 0660)
|
attributesFile, err := os.OpenFile(".gitattributes", os.O_RDWR|os.O_APPEND|os.O_CREATE, 0660)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -30,6 +33,12 @@ func addCommand(cmd *cobra.Command, args []string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if addTrailingLinebreak {
|
||||||
|
if _, err := attributesFile.WriteString("\n"); err != nil {
|
||||||
|
Print("Error writing to .gitattributes")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for _, t := range args {
|
for _, t := range args {
|
||||||
isKnownPath := false
|
isKnownPath := false
|
||||||
for _, k := range knownPaths {
|
for _, k := range knownPaths {
|
||||||
@ -54,6 +63,28 @@ func addCommand(cmd *cobra.Command, args []string) {
|
|||||||
attributesFile.Close()
|
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() {
|
func init() {
|
||||||
RootCmd.AddCommand(addCmd)
|
RootCmd.AddCommand(addCmd)
|
||||||
}
|
}
|
||||||
|
@ -47,11 +47,51 @@ func TestPathOnEmptyRepository(t *testing.T) {
|
|||||||
|
|
||||||
cmd := repo.Command("add", "*.gif")
|
cmd := repo.Command("add", "*.gif")
|
||||||
cmd.Output = "Adding path *.gif"
|
cmd.Output = "Adding path *.gif"
|
||||||
|
|
||||||
|
cmd.Before(func() {
|
||||||
|
// write attributes file in .git
|
||||||
|
path := filepath.Join(".gitattributes")
|
||||||
|
repo.WriteFile(path, "*.mov filter=hawser -crlf\n")
|
||||||
|
})
|
||||||
|
|
||||||
cmd.After(func() {
|
cmd.After(func() {
|
||||||
// assert path was added
|
// assert path was added
|
||||||
assert.Equal(t, "*.gif filter=hawser -crlf\n", repo.ReadFile(".gitattributes"))
|
assert.Equal(t, "*.mov filter=hawser -crlf\n*.gif filter=hawser -crlf\n", repo.ReadFile(".gitattributes"))
|
||||||
|
|
||||||
expected := "Listing paths\n *.gif (.gitattributes)\n"
|
expected := "Listing paths\n *.mov (.gitattributes)\n *.gif (.gitattributes)\n"
|
||||||
|
|
||||||
|
assert.Equal(t, expected, repo.MediaCmd("path"))
|
||||||
|
|
||||||
|
// assert hook was created
|
||||||
|
stat, err := os.Stat(prePushHookFile)
|
||||||
|
assert.Equal(t, nil, err)
|
||||||
|
assert.Equal(t, false, stat.IsDir())
|
||||||
|
})
|
||||||
|
|
||||||
|
cmd = repo.Command("path")
|
||||||
|
cmd.Output = "Listing paths"
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAddPathWithoutTrailingLinebreak(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.Before(func() {
|
||||||
|
// write attributes file in .git
|
||||||
|
path := filepath.Join(".gitattributes")
|
||||||
|
repo.WriteFile(path, "*.mov filter=hawser -crlf")
|
||||||
|
})
|
||||||
|
|
||||||
|
cmd.After(func() {
|
||||||
|
// assert path was added
|
||||||
|
assert.Equal(t, "*.mov filter=hawser -crlf\n*.gif filter=hawser -crlf\n", repo.ReadFile(".gitattributes"))
|
||||||
|
|
||||||
|
expected := "Listing paths\n *.mov (.gitattributes)\n *.gif (.gitattributes)\n"
|
||||||
|
|
||||||
assert.Equal(t, expected, repo.MediaCmd("path"))
|
assert.Equal(t, expected, repo.MediaCmd("path"))
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user