Skip to content

Commit 3acbb9f

Browse files
committed
Add tests and RevParse() function
1 parent 471b160 commit 3acbb9f

File tree

2 files changed

+77
-7
lines changed

2 files changed

+77
-7
lines changed

modules/git/tree.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,14 @@ func (repo *Repository) GetTreePathLatestCommit(refName, treePath string) (*Comm
7373
}
7474
return repo.GetCommit(strings.TrimSpace(stdout))
7575
}
76+
77+
// rev-parse parses the output of `git rev-parse` command
78+
func (repo *Repository) RevParse(ref string, file string) (string, error) {
79+
stdout, _, err := NewCommand("rev-parse").
80+
AddDynamicArguments(ref+":"+file).
81+
RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path})
82+
if err != nil {
83+
return "", err
84+
}
85+
return strings.TrimSpace(stdout), nil
86+
}

tests/integration/compare_test.go

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/url"
1010
"strings"
1111
"testing"
12+
"time"
1213

1314
"code.gitea.io/gitea/models/db"
1415
"code.gitea.io/gitea/models/unittest"
@@ -160,7 +161,7 @@ func TestCompareCodeExpand(t *testing.T) {
160161
})
161162
}
162163

163-
func TestCompareRawDiff(t *testing.T) {
164+
func TestCompareRawDiffNormal(t *testing.T) {
164165
onGiteaRun(t, func(t *testing.T, u *url.URL) {
165166
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
166167
repo, err := repo_service.CreateRepositoryDirectly(db.DefaultContext, user1, user1, repo_service.CreateRepoOptions{
@@ -171,16 +172,19 @@ func TestCompareRawDiff(t *testing.T) {
171172
}, true)
172173
assert.NoError(t, err)
173174
session := loginUser(t, user1.Name)
175+
174176
r, _ := gitrepo.OpenRepository(db.DefaultContext, repo)
177+
175178
oldRef, _ := r.GetBranchCommit(repo.DefaultBranch)
179+
oldBlobRef, _ := r.RevParse(oldRef.ID.String(), "README.md")
180+
176181
testEditFile(t, session, user1.Name, repo.Name, "main", "README.md", strings.Repeat("a\n", 2))
182+
177183
newRef, _ := r.GetBranchCommit(repo.DefaultBranch)
178-
fmt.Println("oldRef", oldRef.ID.String())
179-
fmt.Println("newRef", newRef.ID.String())
184+
newBlobRef, _ := r.RevParse(newRef.ID.String(), "README.md")
180185

181186
req := NewRequest(t, "GET", fmt.Sprintf("/user1/test_raw_diff/compare/%s...%s.diff", oldRef.ID.String(), newRef.ID.String()))
182187
resp := session.MakeRequest(t, req, http.StatusOK)
183-
fmt.Println("resp", resp.Body.String())
184188

185189
expected := fmt.Sprintf(`diff --git a/README.md b/README.md
186190
index %s..%s 100644
@@ -191,9 +195,64 @@ index %s..%s 100644
191195
-
192196
+a
193197
+a
194-
`,
195-
oldRef.ID.String()[:7], newRef.ID.String()[:7])
198+
`, oldBlobRef[:7], newBlobRef[:7])
199+
assert.Equal(t, expected, resp.Body.String())
200+
})
201+
}
202+
203+
func TestCompareRawDiffPatch(t *testing.T) {
204+
onGiteaRun(t, func(t *testing.T, u *url.URL) {
205+
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
206+
repo, err := repo_service.CreateRepositoryDirectly(db.DefaultContext, user1, user1, repo_service.CreateRepoOptions{
207+
Name: "test_raw_diff",
208+
Readme: "Default",
209+
AutoInit: true,
210+
DefaultBranch: "main",
211+
}, true)
212+
assert.NoError(t, err)
213+
session := loginUser(t, user1.Name)
214+
215+
r, _ := gitrepo.OpenRepository(db.DefaultContext, repo)
196216

197-
assert.Equal(t, resp.Body.String(), expected)
217+
// Get the old commit and blob reference
218+
oldRef, _ := r.GetBranchCommit(repo.DefaultBranch)
219+
oldBlobRef, _ := r.RevParse(oldRef.ID.String(), "README.md")
220+
221+
resp := testEditFile(t, session, user1.Name, repo.Name, "main", "README.md", strings.Repeat("a\n", 2))
222+
223+
newRef, _ := r.GetBranchCommit(repo.DefaultBranch)
224+
newBlobRef, _ := r.RevParse(newRef.ID.String(), "README.md")
225+
226+
// Get the last modified time from the response header
227+
respTs, _ := time.Parse(time.RFC1123, resp.Result().Header.Get("Last-Modified"))
228+
respTs = respTs.In(time.Local)
229+
230+
// Format the timestamp to match the expected format in the patch
231+
customFormat := "Mon, 02 Jan 2006 15:04:05"
232+
respTsStr := respTs.Format(customFormat)
233+
234+
req := NewRequest(t, "GET", fmt.Sprintf("/user1/test_raw_diff/compare/%s...%s.patch", oldRef.ID.String(), newRef.ID.String()))
235+
resp = session.MakeRequest(t, req, http.StatusOK)
236+
237+
expected := fmt.Sprintf(`From %s Mon Sep 17 00:00:00 2001
238+
From: User One <[email protected]>
239+
Date: %s +0300
240+
Subject: [PATCH] Update README.md
241+
242+
---
243+
README.md | 4 ++--
244+
1 file changed, 2 insertions(+), 2 deletions(-)
245+
246+
diff --git a/README.md b/README.md
247+
index %s..%s 100644
248+
--- a/README.md
249+
+++ b/README.md
250+
@@ -1,2 +1,2 @@
251+
-# test_raw_diff
252+
-
253+
+a
254+
+a
255+
`, newRef.ID.String(), respTsStr, oldBlobRef[:7], newBlobRef[:7])
256+
assert.Equal(t, expected, resp.Body.String())
198257
})
199258
}

0 commit comments

Comments
 (0)