Skip to content

Commit 91e04f8

Browse files
committed
Add gitattributes support to diff
1 parent 44b8b07 commit 91e04f8

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

services/gitdiff/gitdiff.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,25 @@ func GetDiffRangeWithWhitespaceBehavior(repoPath, beforeCommitID, afterCommitID
12041204
// FIXME: graceful: These commands should likely have a timeout
12051205
ctx, cancel := context.WithCancel(git.DefaultContext)
12061206
defer cancel()
1207+
1208+
// Attempt to find a top-level .gitattributes and temporarily
1209+
// store in `info/attributes` within the bare repo
1210+
var gitAttrOut bytes.Buffer
1211+
gitAttrArgs := []string{"show", fmt.Sprintf("%s:.gitattributes", afterCommitID)}
1212+
gitAttrCmd := exec.CommandContext(ctx, git.GitExecutable, gitAttrArgs...)
1213+
gitAttrCmd.Dir = repoPath
1214+
gitAttrCmd.Stderr = os.Stderr
1215+
gitAttrCmd.Stdout = &gitAttrOut
1216+
err = gitAttrCmd.Run()
1217+
if err == nil {
1218+
gitAttrFile := fmt.Sprintf("%s/info/attributes", repoPath)
1219+
if err = ioutil.WriteFile(gitAttrFile, gitAttrOut.Bytes(), 0644); err != nil {
1220+
return nil, fmt.Errorf("GitAttrWrite: %v", err)
1221+
}
1222+
// Cleanup the file after we're finished diffing
1223+
defer os.Remove(gitAttrFile)
1224+
}
1225+
12071226
var cmd *exec.Cmd
12081227
if (len(beforeCommitID) == 0 || beforeCommitID == git.EmptySHA) && commit.ParentCount() == 0 {
12091228
diffArgs := []string{"diff", "--src-prefix=\\a/", "--dst-prefix=\\b/", "-M"}

0 commit comments

Comments
 (0)