Skip to content

Commit b0141a0

Browse files
authored
Merge branch 'main' into main
2 parents df520a9 + e0cf3d8 commit b0141a0

File tree

22 files changed

+336
-37
lines changed

22 files changed

+336
-37
lines changed

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ fmt:
239239
.PHONY: vet
240240
vet:
241241
@echo "Running go vet..."
242-
@$(GO) vet $(GO_PACKAGES)
243242
@GOOS= GOARCH= $(GO) build -mod=vendor code.gitea.io/gitea-vet
244243
@$(GO) vet -vettool=gitea-vet $(GO_PACKAGES)
245244

cmd/serv.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ func runServ(c *cli.Context) error {
191191
return fail("Invalid repo name", "Invalid repo name: %s", reponame)
192192
}
193193

194-
if setting.EnablePprof || c.Bool("enable-pprof") {
194+
if c.Bool("enable-pprof") {
195195
if err := os.MkdirAll(setting.PprofDataPath, os.ModePerm); err != nil {
196196
return fail("Error while trying to create PPROF_DATA_PATH", "Error while trying to create PPROF_DATA_PATH: %v", err)
197197
}

integrations/api_repo_git_commits_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,21 @@ func TestDownloadCommitDiffOrPatch(t *testing.T) {
132132
resp.Body.String())
133133

134134
}
135+
136+
func TestGetFileHistory(t *testing.T) {
137+
defer prepareTestEnv(t)()
138+
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
139+
// Login as User2.
140+
session := loginUser(t, user.Name)
141+
token := getTokenForLoggedInUser(t, session)
142+
143+
req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/commits?path=readme.md&token="+token+"&sha=good-sign", user.Name)
144+
resp := session.MakeRequest(t, req, http.StatusOK)
145+
146+
var apiData []api.Commit
147+
DecodeJSON(t, resp, &apiData)
148+
149+
assert.Len(t, apiData, 1)
150+
assert.Equal(t, "f27c2b2b03dcab38beaf89b0ab4ff61f6de63441", apiData[0].CommitMeta.SHA)
151+
compareCommitFiles(t, []string{"readme.md"}, apiData[0].Files)
152+
}

integrations/links_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ func TestRedirectsNoLogin(t *testing.T) {
6161
resp := MakeRequest(t, req, http.StatusFound)
6262
assert.EqualValues(t, path.Join(setting.AppSubURL, redirectLink), test.RedirectURL(resp))
6363
}
64+
65+
var temporaryRedirects = map[string]string{
66+
"/user2/repo1/": "/user2/repo1",
67+
}
68+
for link, redirectLink := range temporaryRedirects {
69+
req := NewRequest(t, "GET", link)
70+
resp := MakeRequest(t, req, http.StatusTemporaryRedirect)
71+
assert.EqualValues(t, path.Join(setting.AppSubURL, redirectLink), test.RedirectURL(resp))
72+
}
73+
6474
}
6575

6676
func TestNoLoginNotExist(t *testing.T) {

integrations/signout_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func TestSignOut(t *testing.T) {
1818
session.MakeRequest(t, req, http.StatusFound)
1919

2020
// try to view a private repo, should fail
21-
req = NewRequest(t, "GET", "/user2/repo2/")
21+
req = NewRequest(t, "GET", "/user2/repo2")
2222
session.MakeRequest(t, req, http.StatusNotFound)
2323

2424
// invalidate cached cookies for user2, for subsequent tests

modules/doctor/dbconsistency.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,13 @@ func checkDBConsistency(logger log.Logger, autofix bool) error {
167167
"lfs_lock", "repository", "lfs_lock.repo_id=repository.id"),
168168
// find collaborations without users
169169
genericOrphanCheck("Collaborations without existing user",
170-
"collaboration", "user", "collaboration.user_id=user.id"),
170+
"collaboration", "user", "collaboration.user_id=`user`.id"),
171171
// find collaborations without repository
172172
genericOrphanCheck("Collaborations without existing repository",
173173
"collaboration", "repository", "collaboration.repo_id=repository.id"),
174174
// find access without users
175175
genericOrphanCheck("Access entries without existing user",
176-
"access", "user", "access.user_id=user.id"),
176+
"access", "user", "access.user_id=`user`.id"),
177177
// find access without repository
178178
genericOrphanCheck("Access entries without existing repository",
179179
"access", "repository", "access.repo_id=repository.id"),

modules/git/repo_compare.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@
66
package git
77

88
import (
9+
"bufio"
910
"bytes"
11+
"errors"
1012
"fmt"
1113
"io"
14+
"io/ioutil"
15+
"os"
16+
"path/filepath"
1217
"regexp"
1318
"strconv"
1419
"strings"
@@ -188,6 +193,8 @@ func GetDiffShortStat(repoPath string, args ...string) (numFiles, totalAdditions
188193
var shortStatFormat = regexp.MustCompile(
189194
`\s*(\d+) files? changed(?:, (\d+) insertions?\(\+\))?(?:, (\d+) deletions?\(-\))?`)
190195

196+
var patchCommits = regexp.MustCompile(`^From\s(\w+)\s`)
197+
191198
func parseDiffStat(stdout string) (numFiles, totalAdditions, totalDeletions int, err error) {
192199
if len(stdout) == 0 || stdout == "\n" {
193200
return 0, 0, 0, nil
@@ -267,3 +274,57 @@ func (repo *Repository) GetDiffFromMergeBase(base, head string, w io.Writer) err
267274
}
268275
return err
269276
}
277+
278+
// ReadPullHead will fetch a pull ref if possible or return an error
279+
func (repo *Repository) ReadPullHead(prID int64) (commitSHA string, err error) {
280+
headPath := fmt.Sprintf("refs/pull/%d/head", prID)
281+
fullHeadPath := filepath.Join(repo.Path, headPath)
282+
loadHead, err := os.Open(fullHeadPath)
283+
if err != nil {
284+
return "", err
285+
}
286+
defer loadHead.Close()
287+
// Read only the first line of the patch - usually it contains the first commit made in patch
288+
scanner := bufio.NewScanner(loadHead)
289+
scanner.Scan()
290+
commitHead := scanner.Text()
291+
if len(commitHead) != 40 {
292+
return "", errors.New("head file doesn't contain valid commit ID")
293+
}
294+
return commitHead, nil
295+
}
296+
297+
// ReadPatchCommit will check if a diff patch exists and return stats
298+
func (repo *Repository) ReadPatchCommit(prID int64) (commitSHA string, err error) {
299+
// Migrated repositories download patches to "pulls" location
300+
patchFile := fmt.Sprintf("pulls/%d.patch", prID)
301+
loadPatch, err := os.Open(filepath.Join(repo.Path, patchFile))
302+
if err != nil {
303+
return "", err
304+
}
305+
defer loadPatch.Close()
306+
// Read only the first line of the patch - usually it contains the first commit made in patch
307+
scanner := bufio.NewScanner(loadPatch)
308+
scanner.Scan()
309+
// Parse the Patch stats, sometimes Migration returns a 404 for the patch file
310+
commitSHAGroups := patchCommits.FindStringSubmatch(scanner.Text())
311+
if len(commitSHAGroups) != 0 {
312+
commitSHA = commitSHAGroups[1]
313+
} else {
314+
return "", errors.New("patch file doesn't contain valid commit ID")
315+
}
316+
return commitSHA, nil
317+
}
318+
319+
// WritePullHead will populate a PR head retrieved from patch file
320+
func (repo *Repository) WritePullHead(prID int64, commitSHA string) error {
321+
headPath := fmt.Sprintf("refs/pull/%d", prID)
322+
fullHeadPath := filepath.Join(repo.Path, headPath)
323+
// Create missing directory just in case
324+
if err := os.MkdirAll(fullHeadPath, os.ModePerm); err != nil {
325+
return err
326+
}
327+
commitBytes := []byte(commitSHA)
328+
pullPath := filepath.Join(fullHeadPath, "head")
329+
return ioutil.WriteFile(pullPath, commitBytes, os.ModePerm)
330+
}

modules/git/repo_compare_test.go

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"bytes"
99
"io"
1010
"path/filepath"
11+
"strings"
1112
"testing"
1213

1314
"code.gitea.io/gitea/modules/util"
@@ -18,11 +19,11 @@ import (
1819
func TestGetFormatPatch(t *testing.T) {
1920
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
2021
clonedPath, err := cloneRepo(bareRepo1Path, testReposDir, "repo1_TestGetFormatPatch")
21-
assert.NoError(t, err)
2222
defer util.RemoveAll(clonedPath)
23-
repo, err := OpenRepository(clonedPath)
2423
assert.NoError(t, err)
24+
repo, err := OpenRepository(clonedPath)
2525
defer repo.Close()
26+
assert.NoError(t, err)
2627
rd := &bytes.Buffer{}
2728
err = repo.GetPatch("8d92fc95^", "8d92fc95", rd)
2829
assert.NoError(t, err)
@@ -32,3 +33,49 @@ func TestGetFormatPatch(t *testing.T) {
3233
assert.Regexp(t, "^From 8d92fc95", patch)
3334
assert.Contains(t, patch, "Subject: [PATCH] Add file2.txt")
3435
}
36+
37+
func TestReadPatch(t *testing.T) {
38+
// Ensure we can read the patch files
39+
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
40+
repo, err := OpenRepository(bareRepo1Path)
41+
defer repo.Close()
42+
assert.NoError(t, err)
43+
// This patch doesn't exist
44+
noFile, err := repo.ReadPatchCommit(0)
45+
assert.Error(t, err)
46+
// This patch is an empty one (sometimes it's a 404)
47+
noCommit, err := repo.ReadPatchCommit(1)
48+
assert.Error(t, err)
49+
// This patch is legit and should return a commit
50+
oldCommit, err := repo.ReadPatchCommit(2)
51+
assert.NoError(t, err)
52+
53+
assert.Empty(t, noFile)
54+
assert.Empty(t, noCommit)
55+
assert.Len(t, oldCommit, 40)
56+
assert.True(t, oldCommit == "6e8e2a6f9efd71dbe6917816343ed8415ad696c3")
57+
}
58+
59+
func TestReadWritePullHead(t *testing.T) {
60+
// Ensure we can write SHA1 head corresponding to PR and open them
61+
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
62+
repo, err := OpenRepository(bareRepo1Path)
63+
assert.NoError(t, err)
64+
defer repo.Close()
65+
// Try to open non-existing Pull
66+
_, err = repo.ReadPullHead(0)
67+
assert.Error(t, err)
68+
// Write a fake sha1 with only 40 zeros
69+
newCommit := strings.Repeat("0", 40)
70+
err = repo.WritePullHead(1, newCommit)
71+
assert.NoError(t, err)
72+
headFile := filepath.Join(repo.Path, "refs/pull/1/head")
73+
// Remove file after the test
74+
defer util.Remove(headFile)
75+
assert.FileExists(t, headFile)
76+
// Read the file created
77+
headContents, err := repo.ReadPullHead(1)
78+
assert.NoError(t, err)
79+
assert.Len(t, string(headContents), 40)
80+
assert.True(t, string(headContents) == newCommit)
81+
}

modules/git/tests/repos/repo1_bare/pulls/1.patch

Whitespace-only changes.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
From 6e8e2a6f9efd71dbe6917816343ed8415ad696c3 Mon Sep 17 00:00:00 2001
2+
From: 99rgosse <[email protected]>
3+
Date: Fri, 26 Mar 2021 12:44:22 +0000
4+
Subject: [PATCH] Update gitea_import_actions.py
5+
6+
---
7+
gitea_import_actions.py | 6 +++---
8+
1 file changed, 3 insertions(+), 3 deletions(-)
9+
10+
diff --git a/gitea_import_actions.py b/gitea_import_actions.py
11+
index f0d72cd..7b31963 100644
12+
--- a/gitea_import_actions.py
13+
+++ b/gitea_import_actions.py
14+
@@ -3,14 +3,14 @@
15+
# git log --pretty=format:'%H,%at,%s' --date=default > /tmp/commit.log
16+
# to get the commits logfile for a repository
17+
18+
-import mysql.connector as mariadb
19+
+import psycopg2
20+
21+
# set the following variables to fit your need...
22+
USERID = 1
23+
REPOID = 1
24+
BRANCH = "master"
25+
26+
-mydb = mariadb.connect(
27+
+mydb = psycopg2.connect(
28+
host="localhost",
29+
user="user",
30+
passwd="password",
31+
@@ -31,4 +31,4 @@ with open("/tmp/commit.log") as f:
32+
33+
mydb.commit()
34+
35+
-print("actions inserted.")
36+
\ No newline at end of file
37+
+print("actions inserted.")
38+
--
39+
GitLab

0 commit comments

Comments
 (0)