Skip to content

Commit 6f963d1

Browse files
committed
Fix bug
1 parent 64ad8f6 commit 6f963d1

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

modules/git/repo_compare.go

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -246,18 +246,40 @@ func (repo *Repository) GetDiffOrPatch(base, head string, w io.Writer, patch, bi
246246

247247
// GetDiff generates and returns patch data between given revisions, optimized for human readability
248248
func (repo *Repository) GetDiff(base, head string, w io.Writer) error {
249-
return NewCommand(repo.Ctx, "diff", "-p").AddDynamicArguments(base, head).Run(&RunOpts{
250-
Dir: repo.Path,
251-
Stdout: w,
252-
})
249+
stderr := new(bytes.Buffer)
250+
err := NewCommand(repo.Ctx, "diff", "-p").AddDynamicArguments(base + "..." + head).
251+
Run(&RunOpts{
252+
Dir: repo.Path,
253+
Stdout: w,
254+
Stderr: stderr,
255+
})
256+
if err != nil && bytes.Contains(stderr.Bytes(), []byte("no merge base")) {
257+
return NewCommand(repo.Ctx, "diff", "-p").AddDynamicArguments(base, head).
258+
Run(&RunOpts{
259+
Dir: repo.Path,
260+
Stdout: w,
261+
})
262+
}
263+
return err
253264
}
254265

255266
// GetDiffBinary generates and returns patch data between given revisions, including binary diffs.
256267
func (repo *Repository) GetDiffBinary(base, head string, w io.Writer) error {
257-
return NewCommand(repo.Ctx, "diff", "-p", "--binary", "--histogram").AddDynamicArguments(base, head).Run(&RunOpts{
258-
Dir: repo.Path,
259-
Stdout: w,
260-
})
268+
stderr := new(bytes.Buffer)
269+
err := NewCommand(repo.Ctx, "diff", "-p", "--binary", "--histogram").AddDynamicArguments(base + "..." + head).
270+
Run(&RunOpts{
271+
Dir: repo.Path,
272+
Stdout: w,
273+
Stderr: stderr,
274+
})
275+
if err != nil && bytes.Contains(stderr.Bytes(), []byte("no merge base")) {
276+
return NewCommand(repo.Ctx, "diff", "-p", "--binary", "--histogram").AddDynamicArguments(base, head).
277+
Run(&RunOpts{
278+
Dir: repo.Path,
279+
Stdout: w,
280+
})
281+
}
282+
return err
261283
}
262284

263285
// GetPatch generates and returns format-patch data between given revisions, able to be used with `git apply`

services/gitdiff/gitdiff.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,7 @@ parsingLoop:
641641
}
642642
case strings.HasPrefix(line, "new file"):
643643
curFile.Type = DiffFileAdd
644+
curFile.OldName = ""
644645
curFile.IsCreated = true
645646
if strings.HasPrefix(line, "new file mode ") {
646647
curFile.Mode = prepareValue(line, "new file mode ")
@@ -688,7 +689,7 @@ parsingLoop:
688689
if line[len(line)-2] == '\t' {
689690
curFile.Name = curFile.Name[:len(curFile.Name)-1]
690691
}
691-
if curFile.OldName == "" {
692+
if curFile.OldName == "" && curFile.Type != DiffFileAdd {
692693
curFile.OldName = curFile.Name
693694
}
694695
} else {

tests/integration/api_pull_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func TestAPIViewPulls(t *testing.T) {
5151
assert.Len(t, pull.RequestedReviewersTeams, 0)
5252
assert.EqualValues(t, 5, pull.RequestedReviewers[0].ID)
5353
assert.EqualValues(t, 6, pull.RequestedReviewers[1].ID)
54-
assert.EqualValues(t, 2, pull.ChangedFiles)
54+
assert.EqualValues(t, 1, pull.ChangedFiles)
5555

5656
if assert.EqualValues(t, 5, pull.ID) {
5757
resp = ctx.Session.MakeRequest(t, NewRequest(t, "GET", pull.DiffURL), http.StatusOK)
@@ -97,7 +97,7 @@ func TestAPIViewPulls(t *testing.T) {
9797
assert.NoError(t, err)
9898
if assert.Len(t, patch.Files, pull.ChangedFiles) {
9999
assert.Equal(t, "README.md", patch.Files[0].Name)
100-
assert.Empty(t, patch.Files[0].OldName)
100+
assert.NotEmpty(t, patch.Files[0].OldName)
101101
assert.EqualValues(t, pull.Additions, patch.Files[0].Addition)
102102
assert.EqualValues(t, pull.Deletions, patch.Files[0].Deletion)
103103
assert.Equal(t, gitdiff.DiffFileChange, patch.Files[0].Type)

0 commit comments

Comments
 (0)