Skip to content

Commit 78d8ea1

Browse files
committed
fix(range-diff): properly handle removed files from a patchset
1 parent 09798a4 commit 78d8ea1

File tree

5 files changed

+107
-25
lines changed

5 files changed

+107
-25
lines changed

range_diff.go

+49-1
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,16 @@ type RangeDiffFile struct {
159159

160160
func outputDiff(patchA, patchB *PatchRange) []*RangeDiffFile {
161161
diffs := []*RangeDiffFile{}
162+
162163
for _, fileA := range patchA.Files {
164+
found := false
163165
for _, fileB := range patchB.Files {
164166
if fileA.NewName == fileB.NewName {
167+
found = true
168+
// this means both files have been deleted so we should skip
169+
if fileA.NewName == "" {
170+
continue
171+
}
165172
strA := ""
166173
for _, frag := range fileA.TextFragments {
167174
for _, line := range frag.Lines {
@@ -185,7 +192,6 @@ func outputDiff(patchA, patchB *PatchRange) []*RangeDiffFile {
185192
if !hasDiff {
186193
continue
187194
}
188-
// curDiff := DoDiff(fileA.String(), fileB.String())
189195
fp := &RangeDiffFile{
190196
OldFile: fileA,
191197
NewFile: fileB,
@@ -194,6 +200,48 @@ func outputDiff(patchA, patchB *PatchRange) []*RangeDiffFile {
194200
diffs = append(diffs, fp)
195201
}
196202
}
203+
204+
// find files in patchA but not in patchB
205+
if !found {
206+
strA := ""
207+
for _, frag := range fileA.TextFragments {
208+
for _, line := range frag.Lines {
209+
strA += line.String()
210+
}
211+
}
212+
fp := &RangeDiffFile{
213+
OldFile: fileA,
214+
NewFile: nil,
215+
Diff: DoDiff(strA, ""),
216+
}
217+
diffs = append(diffs, fp)
218+
}
219+
}
220+
221+
// find files in patchB not in patchA
222+
for _, fileB := range patchB.Files {
223+
found := false
224+
for _, fileA := range patchA.Files {
225+
if fileA.NewName == fileB.NewName {
226+
found = true
227+
break
228+
}
229+
}
230+
231+
if !found {
232+
strB := ""
233+
for _, frag := range fileB.TextFragments {
234+
for _, line := range frag.Lines {
235+
strB += line.String()
236+
}
237+
}
238+
fp := &RangeDiffFile{
239+
OldFile: nil,
240+
NewFile: fileB,
241+
Diff: DoDiff("", strB),
242+
}
243+
diffs = append(diffs, fp)
244+
}
197245
}
198246

199247
return diffs

tmpl/patchset.html

+18
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ <h3 class="text-lg text-transform-none m-0 p-0 mb mono">
2424

2525
{{range $patch.PatchFiles}}
2626
<div class="flex justify-between items-center">
27+
{{if .NewName}}
2728
<a class="flex-1 word-break-word mono" href="#patch-{{$patch.ID}}-{{.NewName}}">{{.NewName}}</a>
29+
{{else}}
30+
<a class="flex-1 word-break-word mono" href="#patch-{{$patch.ID}}-{{.OldName}}">{{.OldName}}</a>
31+
{{end}}
2832
<div class="flex gap">
2933
<code class="pill-success">+{{.Adds}}</code>
3034
<code class="pill-admin">-{{.Dels}}</code>
@@ -49,6 +53,7 @@ <h3 class="text-lg text-transform-none mono"><a href="#{{$patch.Url}}">{{$patch.
4953
{{if $patch.Body}}<pre class="w-full">{{$patch.Body}}</pre>{{end}}
5054

5155
{{range $patch.PatchFiles}}
56+
{{if .NewName}}
5257
<details class="details-min" open="true" id="patch-{{$patch.ID}}-{{.NewName}}">
5358
<summary class="group-h patch-file">
5459
<span class="mono">{{.NewName}}</span>
@@ -60,6 +65,19 @@ <h3 class="text-lg text-transform-none mono"><a href="#{{$patch.Url}}">{{$patch.
6065
</summary>
6166
<div>{{.DiffText}}</div>
6267
</details>
68+
{{else}}
69+
<details class="details-min" open="true" id="patch-{{$patch.ID}}-{{.OldName}}">
70+
<summary class="group-h patch-file">
71+
<span class="mono">{{.OldName}}</span>
72+
<a href="#patch-{{$patch.ID}}-{{.OldName}}" class="word-break-word">link</a>
73+
<div class="flex gap">
74+
<code class="pill-success">+{{.Adds}}</code>
75+
<code class="pill-admin">-{{.Dels}}</code>
76+
</div>
77+
</summary>
78+
<div>{{.DiffText}}</div>
79+
</details>
80+
{{end}}
6381
{{end}}
6482
</div>
6583
<hr class="my" />

tmpl/pr-table.html

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<th class="text-left">Status</th>
77
<th class="text-left">User</th>
88
<th class="text-left">Title</th>
9+
<th class="text-left">Patchsets</th>
910
<th class="text-left">Created At</th>
1011
</tr>
1112
</thead>
@@ -22,6 +23,7 @@
2223
<code>#{{.ID}}</code>
2324
<a href="{{.PrLink.Url}}">{{.PrLink.Text}}</a>
2425
</td>
26+
<td><code>{{.NumPatchsets}}</code></td>
2527
<td><date>{{.Date}}</date></td>
2628
</tr>
2729
{{else}}

tmpl/range-diff.html

+19-13
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ <h2 class="text-xl mt">
4747
<div class="group">
4848
{{range .PatchsetData.RangeDiff}}
4949
<div id="{{.Header.OldIdx}}-{{.Header.NewIdx}}">
50-
<div>
50+
<div class="mb">
5151
<code class='{{if eq .Type "rm"}}pill-admin{{else if eq .Type "add"}}pill-success{{else if eq .Type "diff"}}pill-review{{end}}'>
5252
{{.Header}}
5353
</code>
@@ -58,31 +58,37 @@ <h2 class="text-xl mt">
5858
{{range .Files}}
5959
<div class="flex gap">
6060
<div class="flex-1" style="width: 48%;">
61-
<div>{{.OldFile.NewName}}</div>
62-
<pre>{{- range .Diff -}}
63-
{{- if eq .InnerType "insert" -}}
61+
<div>
62+
{{if .OldFile.OldName}}<code>{{.OldFile.OldName}}</code>{{end}}
63+
{{if .OldFile.NewName}}<code>{{.OldFile.NewName}}</code>{{end}}
64+
</div>
65+
<pre class="m-0">{{- range .Diff -}}
66+
{{- if eq .OuterType "delete" -}}
67+
<span style="background-color: tomato;">{{.Text}}</span>
68+
{{- else if eq .OuterType "insert" -}}
69+
{{- else if eq .InnerType "insert" -}}
6470
<span style="color: limegreen;">{{.Text}}</span>
6571
{{- else if eq .InnerType "delete" -}}
6672
<span style="color: tomato;">{{.Text}}</span>
67-
{{- else if eq .OuterType "delete" -}}
68-
<span style="background-color: tomato;">{{.Text}}</span>
69-
{{- else if eq .OuterType "insert" -}}
7073
{{- else -}}
7174
<span>{{.Text}}</span>
7275
{{- end -}}
7376
{{- end -}}</pre>
7477
</div>
7578

7679
<div class="flex-1" style="width: 48%;">
77-
<div>{{.NewFile.NewName}}</div>
78-
<pre>{{- range .Diff -}}
79-
{{- if eq .InnerType "insert" -}}
80+
<div>
81+
{{if .NewFile.OldName}}<code>{{.NewFile.OldName}}</code>{{end}}
82+
{{if .NewFile.NewName}}<code>{{.NewFile.NewName}}</code>{{end}}
83+
</div>
84+
<pre class="m-0">{{- range .Diff -}}
85+
{{- if eq .OuterType "insert" -}}
86+
<span style="background-color: limegreen;">{{.Text}}</span>
87+
{{- else if eq .OuterType "delete" -}}
88+
{{- else if eq .InnerType "insert" -}}
8089
<span style="color: limegreen;">{{.Text}}</span>
8190
{{- else if eq .InnerType "delete" -}}
8291
<span style="color: tomato;">{{.Text}}</span>
83-
{{- else if eq .OuterType "insert" -}}
84-
<span style="background-color: limegreen;">{{.Text}}</span>
85-
{{- else if eq .OuterType "delete" -}}
8692
{{- else -}}
8793
<span>{{.Text}}</span>
8894
{{- end -}}

web.go

+19-11
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,12 @@ func getPrTableData(web *WebCtx, prs []*PatchRequest, query url.Values) ([]*PrLi
203203
continue
204204
}
205205

206+
ps, err := web.Pr.GetPatchsetsByPrID(curpr.ID)
207+
if err != nil {
208+
web.Logger.Error("cannot get patchsets for pr", "err", err)
209+
continue
210+
}
211+
206212
if hasFilter {
207213
if status != "" {
208214
if status != curpr.Status {
@@ -241,9 +247,10 @@ func getPrTableData(web *WebCtx, prs []*PatchRequest, query url.Values) ([]*PrLi
241247
Url: template.URL(fmt.Sprintf("/prs/%d", curpr.ID)),
242248
Text: curpr.Name,
243249
},
244-
DateOrig: curpr.CreatedAt,
245-
Date: curpr.CreatedAt.Format(web.Backend.Cfg.TimeFormat),
246-
Status: curpr.Status,
250+
NumPatchsets: len(ps),
251+
DateOrig: curpr.CreatedAt,
252+
Date: curpr.CreatedAt.Format(web.Backend.Cfg.TimeFormat),
253+
Status: curpr.Status,
247254
}
248255
prdata = append(prdata, prls)
249256
}
@@ -306,14 +313,15 @@ type MetaData struct {
306313

307314
type PrListData struct {
308315
UserData
309-
RepoNs string
310-
RepoLink LinkData
311-
PrLink LinkData
312-
Title string
313-
ID int64
314-
DateOrig time.Time
315-
Date string
316-
Status string
316+
RepoNs string
317+
RepoLink LinkData
318+
PrLink LinkData
319+
Title string
320+
NumPatchsets int
321+
ID int64
322+
DateOrig time.Time
323+
Date string
324+
Status string
317325
}
318326

319327
func userDetailHandler(w http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)