Skip to content

Commit a95ebbb

Browse files
committed
fix
1 parent fd2fc61 commit a95ebbb

File tree

8 files changed

+62
-75
lines changed

8 files changed

+62
-75
lines changed

routers/web/repo/compare.go

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,11 @@ func PrepareCompareDiff(
640640
}
641641
ctx.Data["DiffShortStat"] = diffShortStat
642642
ctx.Data["Diff"] = diff
643+
ctx.Data["DiffBlobExcerptData"] = &gitdiff.DiffBlobExcerptData{
644+
BaseLink: ctx.Repo.RepoLink + "/blob_excerpt",
645+
DiffStyle: ctx.FormString("style"),
646+
AfterCommitID: headCommitID,
647+
}
643648
ctx.Data["DiffNotAvailable"] = diffShortStat.NumFiles == 0
644649

645650
if !fileOnly {
@@ -979,32 +984,29 @@ func ExcerptBlob(ctx *context.Context) {
979984
section.Lines = append(section.Lines, lineSection)
980985
}
981986
}
982-
if ctx.Doer != nil {
983-
ctx.Data["SignedUserID"] = ctx.Doer.ID
984-
}
985-
isPull := ctx.FormBool("is_pull")
986-
ctx.Data["PageIsPullFiles"] = isPull
987987

988-
if isPull {
988+
diffBlobExcerptData.PullIssueIndex = ctx.FormInt64("pull_issue_index")
989+
if diffBlobExcerptData.PullIssueIndex > 0 {
989990
if !ctx.Repo.CanRead(unit.TypePullRequests) {
990991
ctx.NotFound(nil)
991992
return
992993
}
993-
issueIndex := ctx.FormInt64("issue_index")
994-
diffBlobExcerptData.PullIndex = issueIndex
995-
ctx.Data["IssueIndex"] = issueIndex
996-
if issueIndex > 0 {
997-
if issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, issueIndex); err == nil && issue.IsPull {
998-
ctx.Data["Issue"] = issue
999-
ctx.Data["CanBlockUser"] = func(blocker, blockee *user_model.User) bool {
1000-
return user_service.CanBlockUser(ctx, ctx.Doer, blocker, blockee)
1001-
}
1002994

1003-
if allComments, err := issues_model.FetchCodeComments(ctx, issue, ctx.Doer, ctx.FormBool("show_outdated")); err == nil {
1004-
if lineComments, ok := allComments[filePath]; ok {
1005-
attachCommentsToLines(section, lineComments)
1006-
attachHiddenCommentIDs(section, lineComments)
1007-
}
995+
issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, diffBlobExcerptData.PullIssueIndex)
996+
if err != nil {
997+
log.Error("GetIssueByIndex error: %v", err)
998+
} else if issue.IsPull {
999+
ctx.Data["Issue"] = issue
1000+
ctx.Data["CanBlockUser"] = func(blocker, blockee *user_model.User) bool {
1001+
return user_service.CanBlockUser(ctx, ctx.Doer, blocker, blockee)
1002+
}
1003+
allComments, err := issues_model.FetchCodeComments(ctx, issue, ctx.Doer, ctx.FormBool("show_outdated"))
1004+
if err != nil {
1005+
log.Error("FetchCodeComments error: %v", err)
1006+
} else {
1007+
if lineComments, ok := allComments[filePath]; ok {
1008+
attachCommentsToLines(section, lineComments)
1009+
attachHiddenCommentIDs(section, lineComments)
10081010
}
10091011
}
10101012
}

routers/web/repo/compare_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ func TestAttachHiddenCommentIDs(t *testing.T) {
4343

4444
attachHiddenCommentIDs(section, lineComments)
4545

46-
assert.Equal(t, []int64{100}, section.Lines[0].HiddenCommentIDs)
47-
assert.Nil(t, section.Lines[1].HiddenCommentIDs)
48-
assert.Equal(t, []int64{200}, section.Lines[2].HiddenCommentIDs)
46+
assert.Equal(t, []int64{100}, section.Lines[0].SectionInfo.HiddenCommentIDs)
47+
assert.Nil(t, section.Lines[1].SectionInfo.HiddenCommentIDs)
48+
assert.Equal(t, []int64{200}, section.Lines[2].SectionInfo.HiddenCommentIDs)
4949
}
5050

5151
func TestAttachCommentsToLines(t *testing.T) {

routers/web/repo/pull.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -828,10 +828,10 @@ func viewPullFiles(ctx *context.Context, beforeCommitID, afterCommitID string) {
828828

829829
ctx.Data["Diff"] = diff
830830
ctx.Data["DiffBlobExcerptData"] = &gitdiff.DiffBlobExcerptData{
831-
BaseLink: ctx.Repo.RepoLink + "/blob_excerpt",
832-
PullIndex: pull.Index,
833-
DiffStyle: ctx.FormString("style"),
834-
AfterCommitID: afterCommitID,
831+
BaseLink: ctx.Repo.RepoLink + "/blob_excerpt",
832+
PullIssueIndex: pull.Index,
833+
DiffStyle: ctx.FormString("style"),
834+
AfterCommitID: afterCommitID,
835835
}
836836
ctx.Data["DiffNotAvailable"] = diffShortStat.NumFiles == 0
837837

services/gitdiff/gitdiff.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ type DiffLineSectionInfo struct {
9191
RightHunkSize int
9292

9393
HiddenCommentIDs []int64 // IDs of hidden comments in this section
94-
9594
}
9695

9796
// DiffHTMLOperation is the HTML version of diffmatchpatch.Diff
@@ -174,11 +173,11 @@ func (d *DiffLine) getExpandDirection() string {
174173
}
175174

176175
type DiffBlobExcerptData struct {
177-
BaseLink string
178-
IsWikiRepo bool
179-
PullIndex int64
180-
DiffStyle string
181-
AfterCommitID string
176+
BaseLink string
177+
IsWikiRepo bool
178+
PullIssueIndex int64
179+
DiffStyle string
180+
AfterCommitID string
182181
}
183182

184183
func (d *DiffLine) RenderBlobExcerptButtons(fileNameHash string, data *DiffBlobExcerptData) template.HTML {
@@ -187,16 +186,13 @@ func (d *DiffLine) RenderBlobExcerptButtons(fileNameHash string, data *DiffBlobE
187186

188187
makeButton := func(direction, svgName string) template.HTML {
189188
style := util.IfZero(data.DiffStyle, "unified")
190-
link := data.BaseLink + "/" + data.AfterCommitID + fmt.Sprintf("?style=%s&direction=%s&anchor=%s", url.QueryEscape(style), direction, url.QueryEscape(anchor))
191-
if data.PullIndex > 0 {
192-
link += fmt.Sprintf("&is_pull=1&issue_index=%d", data.PullIndex)
189+
link := data.BaseLink + "/" + data.AfterCommitID + fmt.Sprintf("?style=%s&direction=%s&anchor=%s", url.QueryEscape(style), direction, url.QueryEscape(anchor)) + "&" + d.getBlobExcerptQuery()
190+
if data.PullIssueIndex > 0 {
191+
link += fmt.Sprintf("&pull_issue_index=%d", data.PullIssueIndex)
193192
}
194-
link += "&" + d.getBlobExcerptQuery()
195-
196-
svgContent := svg.RenderHTML(svgName)
197193
return htmlutil.HTMLFormat(
198194
`<button class="code-expander-button" hx-target="closest tr" hx-get="%s" data-hidden-comment-ids="%s">%s</button>`,
199-
link, dataHiddenCommentIDs, svgContent,
195+
link, dataHiddenCommentIDs, svg.RenderHTML(svgName),
200196
)
201197
}
202198
var content template.HTML

services/pull/review.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,11 @@ func createCodeComment(ctx context.Context, doer *user_model.User, repo *repo_mo
286286
}
287287

288288
// If patch is still empty (unchanged line), generate code context
289-
if len(patch) == 0 && len(commitID) > 0 {
289+
if patch == "" && commitID != "" {
290290
patch, err = gitdiff.GeneratePatchForUnchangedLine(gitRepo, commitID, treePath, line, setting.UI.CodeCommentLines)
291291
if err != nil {
292292
// Log the error but don't fail comment creation
293-
log.Warn("Unable to generate patch for unchanged line (file=%s, line=%d, commit=%s): %v", treePath, line, commitID, err)
293+
log.Debug("Unable to generate patch for unchanged line (file=%s, line=%d, commit=%s): %v", treePath, line, commitID, err)
294294
}
295295
}
296296
}

templates/repo/diff/blob_excerpt.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
<td class="lines-escape">{{if $inlineDiff.EscapeStatus.Escaped}}<button class="toggle-escape-button btn interact-bg" title="{{template "repo/diff/escape_title" dict "diff" $inlineDiff}}"></button>{{end}}</td>
7171
<td class="lines-type-marker"><span class="tw-font-mono" data-type-marker="{{$line.GetLineTypeMarker}}"></span></td>
7272
<td class="lines-code{{if (not $line.RightIdx)}} lines-code-old{{end}}">
73-
{{- if and $.SignedUserID $.PageIsPullFiles -}}
73+
{{- if and ctx.RootData.SignedUserID $diffBlobExcerptData.PullIssueIndex -}}
7474
<button type="button" aria-label="{{ctx.Locale.Tr "repo.diff.comment.add_line_comment"}}" class="ui primary button add-code-comment add-code-comment-{{if $line.RightIdx}}right{{else}}left{{end}}{{if (not $line.CanComment)}} tw-invisible{{end}}" data-side="{{if $line.RightIdx}}right{{else}}left{{end}}" data-idx="{{if $line.RightIdx}}{{$line.RightIdx}}{{else}}{{$line.LeftIdx}}{{end}}">
7575
{{- svg "octicon-plus" -}}
7676
</button>

web_src/css/review.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@
128128
color: var(--color-primary-contrast);
129129
border-radius: 8px !important;
130130
left: 4px;
131-
top: 4px;
131+
top: 6px;
132132
text-align: center;
133133
}
134134

web_src/js/features/repo-diff.ts

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {submitEventSubmitter, queryElemSiblings, hideElem, showElem, animateOnce
99
import {POST, GET} from '../modules/fetch.ts';
1010
import {createTippy} from '../modules/tippy.ts';
1111
import {invertFileFolding} from './file-fold.ts';
12-
import {parseDom} from '../utils.ts';
12+
import {parseDom, sleep} from '../utils.ts';
1313
import {registerGlobalSelectorFunc} from '../modules/observer.ts';
1414

1515
const {i18n} = window.config;
@@ -219,40 +219,32 @@ function initRepoDiffShowMore() {
219219
}
220220

221221
async function loadUntilFound() {
222-
const hashTargetSelector = window.location.hash;
223-
if (!hashTargetSelector.startsWith('#diff-') && !hashTargetSelector.startsWith('#issuecomment-')) {
222+
const currentHash = window.location.hash;
223+
if (!currentHash.startsWith('#diff-') && !currentHash.startsWith('#issuecomment-')) {
224224
return;
225225
}
226226

227-
let wasExpanded = false;
228-
229-
while (true) {
227+
while (window.location.hash === currentHash) {
230228
// use getElementById to avoid querySelector throws an error when the hash is invalid
231229
// eslint-disable-next-line unicorn/prefer-query-selector
232-
const targetElement = document.getElementById(hashTargetSelector.substring(1));
230+
const targetElement = document.getElementById(currentHash.substring(1));
233231
if (targetElement) {
234-
// If we just expanded content, we need to re-trigger :target CSS
235-
if (wasExpanded) {
236-
const currentHash = window.location.hash;
237-
window.location.hash = '';
238-
setTimeout(() => {
239-
window.location.hash = currentHash;
240-
}, 10);
241-
} else {
242-
targetElement.scrollIntoView();
243-
}
232+
// re-trigger :target CSS and browser will scroll to the element
233+
window.location.hash = '';
234+
setTimeout(() => { window.location.hash = currentHash }, 0);
244235
return;
245236
}
246237

247-
// If looking for a comment, try to expand the section that contains it
248-
if (hashTargetSelector.startsWith('#issuecomment-')) {
249-
const commentId = hashTargetSelector.substring('#issuecomment-'.length);
238+
// If looking for a hidden comment, try to expand the section that contains it
239+
if (currentHash.startsWith('#issuecomment-')) {
240+
const commentId = currentHash.substring('#issuecomment-'.length);
250241
const expandButton = findExpandButtonForComment(commentId);
251242
if (expandButton) {
243+
// Avoid infinite loop, do not re-click the button if already clicked
244+
if (expandButton.hasAttribute('data-auto-load-clicked')) return;
245+
expandButton.setAttribute('data-auto-load-clicked', 'true');
252246
expandButton.click();
253-
wasExpanded = true;
254-
// Wait for HTMX to load the content
255-
await new Promise((resolve) => setTimeout(resolve, 500));
247+
await sleep(500); // Wait for HTMX to load the content. FIXME: need to drop htmx in the future
256248
continue; // Try again to find the element
257249
}
258250
}
@@ -273,16 +265,13 @@ async function loadUntilFound() {
273265
function findExpandButtonForComment(commentId: string): HTMLElement | null {
274266
const expandButtons = document.querySelectorAll('.code-expander-button[data-hidden-comment-ids]');
275267
for (const button of expandButtons) {
276-
const hiddenIds = button.getAttribute('data-hidden-comment-ids');
277-
if (hiddenIds) {
278-
// Parse the comma-separated list of IDs
279-
const ids = hiddenIds.replace(/[[\]]/g, '').split(' ').filter(Boolean);
280-
if (ids.includes(commentId)) {
281-
return button as HTMLElement;
282-
}
268+
const hiddenIdsAttr = button.getAttribute('data-hidden-comment-ids');
269+
if (!hiddenIdsAttr) continue;
270+
const hiddenIds = hiddenIdsAttr.split(',').filter(Boolean);
271+
if (hiddenIds.includes(commentId)) {
272+
return button as HTMLElement;
283273
}
284274
}
285-
286275
return null;
287276
}
288277

0 commit comments

Comments
 (0)