Skip to content

Commit 87a4b7b

Browse files
authored
Merge branch 'master' into ui_create_tag
2 parents fe1bc21 + dc08195 commit 87a4b7b

File tree

18 files changed

+1106
-361
lines changed

18 files changed

+1106
-361
lines changed

docs/config.yaml

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -313,55 +313,8 @@ languages:
313313
weight: 80
314314
pre: group
315315

316-
pt-pt:
317-
weight: 6
318-
languageName: Português de Portugal
319-
menu:
320-
page:
321-
- name: Página inicial
322-
url: https://gitea.io/pt-pt/
323-
weight: 10
324-
pre: home
325-
- name: Documentação
326-
url: /pt-pt/
327-
weight: 20
328-
pre: question
329-
post: active
330-
- name: API
331-
url: https://try.gitea.io/api/swagger
332-
weight: 45
333-
pre: plug
334-
- name: Blog
335-
url: https://blog.gitea.io/
336-
weight: 30
337-
pre: rss
338-
- name: Código-fonte
339-
url: https://code.gitea.io/
340-
weight: 40
341-
pre: code
342-
- name: Tradução
343-
url: https://crowdin.com/project/gitea
344-
weight: 41
345-
pre: language
346-
- name: Descarregamentos
347-
url: https://dl.gitea.io/
348-
weight: 50
349-
pre: download
350-
- name: GitHub
351-
url: https://github.com/go-gitea/
352-
weight: 60
353-
pre: github
354-
- name: Discussão no Discord
355-
url: https://discord.gg/Gitea
356-
weight: 70
357-
pre: comment
358-
- name: Fórum
359-
url: https://discourse.gitea.io/
360-
weight: 80
361-
pre: group
362-
363316
de-de:
364-
weight: 7
317+
weight: 6
365318
languageName: Deutsch
366319
menu:
367320
page:

modules/git/diff.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,30 +125,39 @@ var hunkRegex = regexp.MustCompile(`^@@ -(?P<beginOld>[0-9]+)(,(?P<endOld>[0-9]+
125125

126126
const cmdDiffHead = "diff --git "
127127

128-
func isHeader(lof string) bool {
129-
return strings.HasPrefix(lof, cmdDiffHead) || strings.HasPrefix(lof, "---") || strings.HasPrefix(lof, "+++")
128+
func isHeader(lof string, inHunk bool) bool {
129+
return strings.HasPrefix(lof, cmdDiffHead) || (!inHunk && (strings.HasPrefix(lof, "---") || strings.HasPrefix(lof, "+++")))
130130
}
131131

132132
// CutDiffAroundLine cuts a diff of a file in way that only the given line + numberOfLine above it will be shown
133133
// it also recalculates hunks and adds the appropriate headers to the new diff.
134134
// Warning: Only one-file diffs are allowed.
135-
func CutDiffAroundLine(originalDiff io.Reader, line int64, old bool, numbersOfLine int) string {
135+
func CutDiffAroundLine(originalDiff io.Reader, line int64, old bool, numbersOfLine int) (string, error) {
136136
if line == 0 || numbersOfLine == 0 {
137137
// no line or num of lines => no diff
138-
return ""
138+
return "", nil
139139
}
140+
140141
scanner := bufio.NewScanner(originalDiff)
141142
hunk := make([]string, 0)
143+
142144
// begin is the start of the hunk containing searched line
143145
// end is the end of the hunk ...
144146
// currentLine is the line number on the side of the searched line (differentiated by old)
145147
// otherLine is the line number on the opposite side of the searched line (differentiated by old)
146148
var begin, end, currentLine, otherLine int64
147149
var headerLines int
150+
151+
inHunk := false
152+
148153
for scanner.Scan() {
149154
lof := scanner.Text()
150155
// Add header to enable parsing
151-
if isHeader(lof) {
156+
157+
if isHeader(lof, inHunk) {
158+
if strings.HasPrefix(lof, cmdDiffHead) {
159+
inHunk = false
160+
}
152161
hunk = append(hunk, lof)
153162
headerLines++
154163
}
@@ -157,6 +166,7 @@ func CutDiffAroundLine(originalDiff io.Reader, line int64, old bool, numbersOfLi
157166
}
158167
// Detect "hunk" with contains commented lof
159168
if strings.HasPrefix(lof, "@@") {
169+
inHunk = true
160170
// Already got our hunk. End of hunk detected!
161171
if len(hunk) > headerLines {
162172
break
@@ -213,15 +223,19 @@ func CutDiffAroundLine(originalDiff io.Reader, line int64, old bool, numbersOfLi
213223
}
214224
}
215225
}
226+
err := scanner.Err()
227+
if err != nil {
228+
return "", err
229+
}
216230

217231
// No hunk found
218232
if currentLine == 0 {
219-
return ""
233+
return "", nil
220234
}
221235
// headerLines + hunkLine (1) = totalNonCodeLines
222236
if len(hunk)-headerLines-1 <= numbersOfLine {
223237
// No need to cut the hunk => return existing hunk
224-
return strings.Join(hunk, "\n")
238+
return strings.Join(hunk, "\n"), nil
225239
}
226240
var oldBegin, oldNumOfLines, newBegin, newNumOfLines int64
227241
if old {
@@ -256,5 +270,5 @@ func CutDiffAroundLine(originalDiff io.Reader, line int64, old bool, numbersOfLi
256270
// construct the new hunk header
257271
newHunk[headerLines] = fmt.Sprintf("@@ -%d,%d +%d,%d @@",
258272
oldBegin, oldNumOfLines, newBegin, newNumOfLines)
259-
return strings.Join(newHunk, "\n")
273+
return strings.Join(newHunk, "\n"), nil
260274
}

modules/git/diff_test.go

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,28 @@ const exampleDiff = `diff --git a/README.md b/README.md
2323
+ cut off
2424
+ cut off`
2525

26+
const breakingDiff = `diff --git a/aaa.sql b/aaa.sql
27+
index d8e4c92..19dc8ad 100644
28+
--- a/aaa.sql
29+
+++ b/aaa.sql
30+
@@ -1,9 +1,10 @@
31+
--some comment
32+
--- some comment 5
33+
+--some coment 2
34+
+-- some comment 3
35+
create or replace procedure test(p1 varchar2)
36+
is
37+
begin
38+
---new comment
39+
dbms_output.put_line(p1);
40+
+--some other comment
41+
end;
42+
/
43+
`
44+
2645
func TestCutDiffAroundLine(t *testing.T) {
27-
result := CutDiffAroundLine(strings.NewReader(exampleDiff), 4, false, 3)
46+
result, err := CutDiffAroundLine(strings.NewReader(exampleDiff), 4, false, 3)
47+
assert.NoError(t, err)
2848
resultByLine := strings.Split(result, "\n")
2949
assert.Len(t, resultByLine, 7)
3050
// Check if headers got transferred
@@ -37,18 +57,50 @@ func TestCutDiffAroundLine(t *testing.T) {
3757
assert.Equal(t, "+ Build Status", resultByLine[4])
3858

3959
// Must be same result as before since old line 3 == new line 5
40-
newResult := CutDiffAroundLine(strings.NewReader(exampleDiff), 3, true, 3)
60+
newResult, err := CutDiffAroundLine(strings.NewReader(exampleDiff), 3, true, 3)
61+
assert.NoError(t, err)
4162
assert.Equal(t, result, newResult, "Must be same result as before since old line 3 == new line 5")
4263

43-
newResult = CutDiffAroundLine(strings.NewReader(exampleDiff), 6, false, 300)
64+
newResult, err = CutDiffAroundLine(strings.NewReader(exampleDiff), 6, false, 300)
65+
assert.NoError(t, err)
4466
assert.Equal(t, exampleDiff, newResult)
4567

46-
emptyResult := CutDiffAroundLine(strings.NewReader(exampleDiff), 6, false, 0)
68+
emptyResult, err := CutDiffAroundLine(strings.NewReader(exampleDiff), 6, false, 0)
69+
assert.NoError(t, err)
4770
assert.Empty(t, emptyResult)
4871

4972
// Line is out of scope
50-
emptyResult = CutDiffAroundLine(strings.NewReader(exampleDiff), 434, false, 0)
73+
emptyResult, err = CutDiffAroundLine(strings.NewReader(exampleDiff), 434, false, 0)
74+
assert.NoError(t, err)
5175
assert.Empty(t, emptyResult)
76+
77+
// Handle minus diffs properly
78+
minusDiff, err := CutDiffAroundLine(strings.NewReader(breakingDiff), 2, false, 4)
79+
assert.NoError(t, err)
80+
81+
expected := `diff --git a/aaa.sql b/aaa.sql
82+
--- a/aaa.sql
83+
+++ b/aaa.sql
84+
@@ -1,9 +1,10 @@
85+
--some comment
86+
--- some comment 5
87+
+--some coment 2`
88+
assert.Equal(t, expected, minusDiff)
89+
90+
// Handle minus diffs properly
91+
minusDiff, err = CutDiffAroundLine(strings.NewReader(breakingDiff), 3, false, 4)
92+
assert.NoError(t, err)
93+
94+
expected = `diff --git a/aaa.sql b/aaa.sql
95+
--- a/aaa.sql
96+
+++ b/aaa.sql
97+
@@ -1,9 +1,10 @@
98+
--some comment
99+
--- some comment 5
100+
+--some coment 2
101+
+-- some comment 3`
102+
103+
assert.Equal(t, expected, minusDiff)
52104
}
53105

54106
func BenchmarkCutDiffAroundLine(b *testing.B) {
@@ -69,7 +121,7 @@ func ExampleCutDiffAroundLine() {
69121
Docker Pulls
70122
+ cut off
71123
+ cut off`
72-
result := CutDiffAroundLine(strings.NewReader(diff), 4, false, 3)
124+
result, _ := CutDiffAroundLine(strings.NewReader(diff), 4, false, 3)
73125
println(result)
74126
}
75127

modules/migrations/gitea_uploader.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package migrations
77

88
import (
9-
"bytes"
109
"context"
1110
"fmt"
1211
"io"
@@ -802,13 +801,20 @@ func (g *GiteaLocalUploader) CreateReviews(reviews ...*base.Review) error {
802801
}
803802

804803
var patch string
805-
patchBuf := new(bytes.Buffer)
806-
if err := git.GetRepoRawDiffForFile(g.gitRepo, pr.MergeBase, headCommitID, git.RawDiffNormal, comment.TreePath, patchBuf); err != nil {
807-
// We should ignore the error since the commit maybe removed when force push to the pull request
808-
log.Warn("GetRepoRawDiffForFile failed when migrating [%s, %s, %s, %s]: %v", g.gitRepo.Path, pr.MergeBase, headCommitID, comment.TreePath, err)
809-
} else {
810-
patch = git.CutDiffAroundLine(patchBuf, int64((&models.Comment{Line: int64(line + comment.Position - 1)}).UnsignedLine()), line < 0, setting.UI.CodeCommentLines)
811-
}
804+
reader, writer := io.Pipe()
805+
defer func() {
806+
_ = reader.Close()
807+
_ = writer.Close()
808+
}()
809+
go func() {
810+
if err := git.GetRepoRawDiffForFile(g.gitRepo, pr.MergeBase, headCommitID, git.RawDiffNormal, comment.TreePath, writer); err != nil {
811+
// We should ignore the error since the commit maybe removed when force push to the pull request
812+
log.Warn("GetRepoRawDiffForFile failed when migrating [%s, %s, %s, %s]: %v", g.gitRepo.Path, pr.MergeBase, headCommitID, comment.TreePath, err)
813+
}
814+
_ = writer.Close()
815+
}()
816+
817+
patch, _ = git.CutDiffAroundLine(reader, int64((&models.Comment{Line: int64(line + comment.Position - 1)}).UnsignedLine()), line < 0, setting.UI.CodeCommentLines)
812818

813819
var c = models.Comment{
814820
Type: models.CommentTypeCode,

modules/setting/setting.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,7 @@ func MakeAbsoluteAssetURL(appURL string, staticURLPrefix string) string {
10901090
}
10911091

10921092
// StaticURLPrefix is just a path
1093-
return strings.TrimSuffix(appURL, "/") + strings.TrimSuffix(staticURLPrefix, "/")
1093+
return util.URLJoin(appURL, strings.TrimSuffix(staticURLPrefix, "/"))
10941094
}
10951095

10961096
return strings.TrimSuffix(staticURLPrefix, "/")

modules/setting/setting_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ func TestMakeAbsoluteAssetURL(t *testing.T) {
1818
assert.Equal(t, "https://localhost:1234/foo", MakeAbsoluteAssetURL("https://localhost:1234", "/foo"))
1919
assert.Equal(t, "https://localhost:1234/foo", MakeAbsoluteAssetURL("https://localhost:1234/", "/foo"))
2020
assert.Equal(t, "https://localhost:1234/foo", MakeAbsoluteAssetURL("https://localhost:1234/", "/foo/"))
21-
assert.Equal(t, "https://localhost:1234/foo/bar", MakeAbsoluteAssetURL("https://localhost:1234/foo", "/bar"))
22-
assert.Equal(t, "https://localhost:1234/foo/bar", MakeAbsoluteAssetURL("https://localhost:1234/foo/", "/bar"))
23-
assert.Equal(t, "https://localhost:1234/foo/bar", MakeAbsoluteAssetURL("https://localhost:1234/foo/", "/bar/"))
21+
assert.Equal(t, "https://localhost:1234/foo", MakeAbsoluteAssetURL("https://localhost:1234/foo", "/foo"))
22+
assert.Equal(t, "https://localhost:1234/foo", MakeAbsoluteAssetURL("https://localhost:1234/foo/", "/foo"))
23+
assert.Equal(t, "https://localhost:1234/foo", MakeAbsoluteAssetURL("https://localhost:1234/foo/", "/foo/"))
24+
assert.Equal(t, "https://localhost:1234/bar", MakeAbsoluteAssetURL("https://localhost:1234/foo", "/bar"))
25+
assert.Equal(t, "https://localhost:1234/bar", MakeAbsoluteAssetURL("https://localhost:1234/foo/", "/bar"))
26+
assert.Equal(t, "https://localhost:1234/bar", MakeAbsoluteAssetURL("https://localhost:1234/foo/", "/bar/"))
2427
}
2528

2629
func TestMakeManifestData(t *testing.T) {

0 commit comments

Comments
 (0)