Skip to content

Commit 24f15da

Browse files
committed
Add tests and RevParse() function
1 parent 9c47853 commit 24f15da

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
@@ -76,3 +76,14 @@ func (repo *Repository) GetTreePathLatestCommit(refName, treePath string) (*Comm
7676
}
7777
return repo.GetCommit(strings.TrimSpace(stdout))
7878
}
79+
80+
// rev-parse parses the output of `git rev-parse` command
81+
func (repo *Repository) RevParse(ref string, file string) (string, error) {
82+
stdout, _, err := NewCommand("rev-parse").
83+
AddDynamicArguments(ref+":"+file).
84+
RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path})
85+
if err != nil {
86+
return "", err
87+
}
88+
return strings.TrimSpace(stdout), nil
89+
}

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/unittest"
1415
user_model "code.gitea.io/gitea/models/user"
@@ -159,7 +160,7 @@ func TestCompareCodeExpand(t *testing.T) {
159160
})
160161
}
161162

162-
func TestCompareRawDiff(t *testing.T) {
163+
func TestCompareRawDiffNormal(t *testing.T) {
163164
onGiteaRun(t, func(t *testing.T, u *url.URL) {
164165
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
165166
repo, err := repo_service.CreateRepositoryDirectly(db.DefaultContext, user1, user1, repo_service.CreateRepoOptions{
@@ -170,16 +171,19 @@ func TestCompareRawDiff(t *testing.T) {
170171
}, true)
171172
assert.NoError(t, err)
172173
session := loginUser(t, user1.Name)
174+
173175
r, _ := gitrepo.OpenRepository(db.DefaultContext, repo)
176+
174177
oldRef, _ := r.GetBranchCommit(repo.DefaultBranch)
178+
oldBlobRef, _ := r.RevParse(oldRef.ID.String(), "README.md")
179+
175180
testEditFile(t, session, user1.Name, repo.Name, "main", "README.md", strings.Repeat("a\n", 2))
181+
176182
newRef, _ := r.GetBranchCommit(repo.DefaultBranch)
177-
fmt.Println("oldRef", oldRef.ID.String())
178-
fmt.Println("newRef", newRef.ID.String())
183+
newBlobRef, _ := r.RevParse(newRef.ID.String(), "README.md")
179184

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

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

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

0 commit comments

Comments
 (0)