Compare API is comparing different refs than passed.
Summary
Compare API is comparing different refs than passed as arguments.
Steps to reproduce
Push after each step.
- Create new repository
- Add four files (
file1.txt
,file2.txt
,file3.txt
,file4.txt
) - Initial commit - Modify three files (
file1.txt
,file2.txt
,file3.txt
), commit and tag asc1
- Modify one file (
file1.txt
), amend last commit and tag ac2
- Use GitLab Repository API to get diff between
c1
andc2
: https://gitlab.com/api/v4/projects/:projectId/repository/compare?from=c1&to=c2
API will respond with diff of three files: file1.txt, file2.txt and file3.txt but only file1.txt was changed between c1
and c2
Example Project
https://gitlab.com/maciejt.nowak/gitlab-api-compare
What is the current bug behavior?
Compare API is comparing is returning diff between merge base of both refs and to
ref. In case of amends/rebases it includes changes between different ref than requested.
What is the expected correct behavior?
Compare API should use exactly the refs that were passed, so correct diff for amends/rebases will be returned/
(What you should see instead)
Relevant logs and/or screenshots
API call: https://gitlab.com/api/v4/projects/7127504/repository/compare?from=c1&to=c2
Matching git call:
> git diff (git merge-base c1 c2) c2
diff --git a/file1.txt b/file1.txt
index 49351eb..6986db4 100644
--- a/file1.txt
+++ b/file1.txt
@@ -1 +1,3 @@
-File 1
\ No newline at end of file
+File 1
+Added in C1
+Amended
\ No newline at end of file
diff --git a/file2.txt b/file2.txt
index 9fbb45e..65e608b 100644
--- a/file2.txt
+++ b/file2.txt
@@ -1 +1,2 @@
-File 2
\ No newline at end of file
+File 2
+Added in C1
\ No newline at end of file
diff --git a/file3.txt b/file3.txt
index 7b648e9..9e67925 100644
--- a/file3.txt
+++ b/file3.txt
@@ -1 +1,2 @@
-File 3
\ No newline at end of file
+File 3
+Added in C1
\ No newline at end of file
Expected
> git diff c1..c2
diff --git a/file1.txt b/file1.txt
index 1e2fc47..6986db4 100644
--- a/file1.txt
+++ b/file1.txt
@@ -1,2 +1,3 @@
File 1
-Added in C1
\ No newline at end of file
+Added in C1
+Amended
\ No newline at end of file
Output of checks
This bug happens on GitLab.com This bug happens on GitLab CE 10.8.4 installation
Results of GitLab environment info
This bug happens on GitLab.com
Results of GitLab application Check
This bug happens on GitLab.com
Possible fixes
I didn't had a chance to try applying the change so this description is based on code analysis only.
Compare API (https://gitlab.com/gitlab-org/gitlab-ce/blob/master/lib/api/repositories.rb#L105) is using Gitlab::Git::Compare
with default value false
for parameter straight
(https://gitlab.com/gitlab-org/gitlab-ce/blob/master/lib/gitlab/git/compare.rb#L8). This flag is than passed to Gitlab::Git::Diff.between
call (https://gitlab.com/gitlab-org/gitlab-ce/blob/master/lib/gitlab/git/compare.rb#L41). Inside that method base commit for diff is replaced by merge base (https://gitlab.com/gitlab-org/gitlab-ce/blob/master/lib/gitlab/git/diff.rb#L37).
I suppose that changing the straight
value passed in Compare API will be a breaking change it could be fixed by adding straight
parameter to API call with default value of false
and allowing users to override it to true
as needed.