From f11b40e9e1d60dd3e18222db14522413fa54cc74 Mon Sep 17 00:00:00 2001 From: rogerluo410 Date: Fri, 26 Mar 2021 16:54:19 +0800 Subject: [PATCH 1/8] Fix blame bottom line and add blame prior button --- options/locale/locale_en-US.ini | 1 + routers/repo/blame.go | 63 +++++++++++++++++---------------- templates/repo/blame.tmpl | 42 +++++++++++++++++++--- web_src/js/index.js | 44 ++++++++++++++--------- web_src/less/_base.less | 12 +++++++ 5 files changed, 111 insertions(+), 51 deletions(-) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index e679e1e874979..ff28e17feabc6 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -735,6 +735,7 @@ delete_preexisting_label = Delete delete_preexisting = Delete pre-existing files delete_preexisting_content = Delete files in %s delete_preexisting_success = Deleted unadopted files in %s +blame_prior = View blame prior to this change transfer.accept = Accept Transfer transfer.accept_desc = Transfer to "%s" diff --git a/routers/repo/blame.go b/routers/repo/blame.go index 9be1ea05af9b4..3d817cfeb49a4 100644 --- a/routers/repo/blame.go +++ b/routers/repo/blame.go @@ -5,7 +5,6 @@ package repo import ( - "bytes" "container/list" "fmt" "html" @@ -26,6 +25,17 @@ const ( tplBlame base.TplName = "repo/home" ) +type BlameRow struct { + RowNumber int + Avatar gotemplate.HTML + RepoLink string + PartSha string + CommitUrl string + CommitMessage string + CommitSince gotemplate.HTML + Code gotemplate.HTML +} + // RefBlame render blame page func RefBlame(ctx *context.Context) { fileName := ctx.Repo.TreePath @@ -191,23 +201,24 @@ func renderBlame(ctx *context.Context, blameParts []git.BlamePart, commitNames m repoLink := ctx.Repo.RepoLink var lines = make([]string, 0) - - var commitInfo bytes.Buffer - var lineNumbers bytes.Buffer - var codeLines bytes.Buffer + rows := make([]*BlameRow, 0) var i = 0 - for pi, part := range blameParts { + var commitCnt = 0 + for _, part := range blameParts { for index, line := range part.Lines { i++ lines = append(lines, line) - - var attr = "" - if len(part.Lines)-1 == index && len(blameParts)-1 != pi { - attr = " bottom-line" + + br := &BlameRow{ + RowNumber: i, } + commit := commitNames[part.Sha] if index == 0 { + // Count commit number + commitCnt++ + // User avatar image commitSince := timeutil.TimeSinceUnix(timeutil.TimeStamp(commit.Author.When.Unix()), ctx.Data["Lang"].(string)) @@ -217,17 +228,13 @@ func renderBlame(ctx *context.Context, blameParts []git.BlamePart, commitNames m } else { avatar = string(templates.AvatarByEmail(commit.Author.Email, commit.Author.Name, 18, "mr-3")) } - - commitInfo.WriteString(fmt.Sprintf(`
%s
%s
`, attr, avatar, repoLink, part.Sha, html.EscapeString(commit.CommitMessage), commitSince)) - } else { - commitInfo.WriteString(fmt.Sprintf(`
`, attr)) - } - - //Line number - if len(part.Lines)-1 == index && len(blameParts)-1 != pi { - lineNumbers.WriteString(fmt.Sprintf(``, i, i)) - } else { - lineNumbers.WriteString(fmt.Sprintf(``, i, i)) + + br.Avatar = gotemplate.HTML(avatar) + br.RepoLink = repoLink + br.PartSha = part.Sha + br.CommitUrl = fmt.Sprintf("%s/commit/%s", repoLink, part.Sha) + br.CommitMessage = html.EscapeString(commit.CommitMessage) + br.CommitSince = commitSince } if i != len(lines)-1 { @@ -235,16 +242,12 @@ func renderBlame(ctx *context.Context, blameParts []git.BlamePart, commitNames m } fileName := fmt.Sprintf("%v", ctx.Data["FileName"]) line = highlight.Code(fileName, line) - line = `` + line + `` - if len(part.Lines)-1 == index && len(blameParts)-1 != pi { - codeLines.WriteString(fmt.Sprintf(`
  • %s
  • `, i, i, line)) - } else { - codeLines.WriteString(fmt.Sprintf(`
  • %s
  • `, i, i, line)) - } + + br.Code = gotemplate.HTML(line) + rows = append(rows, br) } } - ctx.Data["BlameContent"] = gotemplate.HTML(codeLines.String()) - ctx.Data["BlameCommitInfo"] = gotemplate.HTML(commitInfo.String()) - ctx.Data["BlameLineNums"] = gotemplate.HTML(lineNumbers.String()) + ctx.Data["Codes"] = rows + ctx.Data["CommitCnt"] = commitCnt } diff --git a/templates/repo/blame.tmpl b/templates/repo/blame.tmpl index 5dd93d3d4611a..0d8b144b8784f 100644 --- a/templates/repo/blame.tmpl +++ b/templates/repo/blame.tmpl @@ -23,11 +23,43 @@
    - - - - - + {{range $row := .Codes}} + + + + + + {{/* */}} + + {{end}}
    {{.BlameCommitInfo}}{{.BlameLineNums}}
      {{.BlameContent}}
    +
    +
    +
    + {{$row.Avatar}} +
    + +
    + {{$row.CommitSince}} +
    +
    +
    +
    + + {{svg "octicon-calendar"}} + + + + + {{$row.Code}} + +
    + {{$row.Code}} +
    +
    diff --git a/web_src/js/index.js b/web_src/js/index.js index de9b99d4efaad..08e1696f0c5fe 100644 --- a/web_src/js/index.js +++ b/web_src/js/index.js @@ -2219,7 +2219,7 @@ function initCodeView() { const $select = $(this); let $list; if ($('div.blame').length) { - $list = $('.code-view td.lines-code li'); + $list = $('.code-view td.lines-code.blame-code'); } else { $list = $('.code-view td.lines-code'); } @@ -2227,14 +2227,17 @@ function initCodeView() { deSelect(); // show code view menu marker - showCodeViewMenu(); + // not show in blame page + if ($('div.blame').length === 0) { + showCodeViewMenu(); + } }); $(window).on('hashchange', () => { let m = window.location.hash.match(/^#(L\d+)-(L\d+)$/); let $list; if ($('div.blame').length) { - $list = $('.code-view td.lines-code li'); + $list = $('.code-view td.lines-code.blame-code'); } else { $list = $('.code-view td.lines-code'); } @@ -2244,7 +2247,10 @@ function initCodeView() { selectRange($list, $first, $list.filter(`[rel=${m[2]}]`)); // show code view menu marker - showCodeViewMenu(); + // not show in blame page + if ($('div.blame').length === 0) { + showCodeViewMenu(); + } $('html, body').scrollTop($first.offset().top - 200); return; @@ -2255,7 +2261,10 @@ function initCodeView() { selectRange($list, $first); // show code view menu marker - showCodeViewMenu(); + // not show in blame page + if ($('div.blame').length === 0) { + showCodeViewMenu(); + } $('html, body').scrollTop($first.offset().top - 200); } @@ -2824,13 +2833,14 @@ function selectRange($list, $select, $from) { // add hashchange to permalink const $issue = $('a.ref-in-new-issue'); - const matched = $issue.attr('href').match(/%23L\d+$|%23L\d+-L\d+$/); - if (matched) { - $issue.attr('href', $issue.attr('href').replace($issue.attr('href').substr(matched.index), `%23L${a}-L${b}`)); - } else { - $issue.attr('href', `${$issue.attr('href')}%23L${a}-L${b}`); + if ($issue && $issue.attr('href')) { + const matched = $issue.attr('href').match(/%23L\d+$|%23L\d+-L\d+$/); + if (matched) { + $issue.attr('href', $issue.attr('href').replace($issue.attr('href').substr(matched.index), `%23L${a}-L${b}`)); + } else { + $issue.attr('href', `${$issue.attr('href')}%23L${a}-L${b}`); + } } - return; } } @@ -2839,11 +2849,13 @@ function selectRange($list, $select, $from) { // add hashchange to permalink const $issue = $('a.ref-in-new-issue'); - const matched = $issue.attr('href').match(/%23L\d+$|%23L\d+-L\d+$/); - if (matched) { - $issue.attr('href', $issue.attr('href').replace($issue.attr('href').substr(matched.index), `%23${$select.attr('rel')}`)); - } else { - $issue.attr('href', `${$issue.attr('href')}%23${$select.attr('rel')}`); + if ($issue && $issue.attr('href')) { + const matched = $issue.attr('href').match(/%23L\d+$|%23L\d+-L\d+$/); + if (matched) { + $issue.attr('href', $issue.attr('href').replace($issue.attr('href').substr(matched.index), `%23${$select.attr('rel')}`)); + } else { + $issue.attr('href', `${$issue.attr('href')}%23${$select.attr('rel')}`); + } } } diff --git a/web_src/less/_base.less b/web_src/less/_base.less index 1ce5e9d0ad9d3..f418191fdb9a5 100644 --- a/web_src/less/_base.less +++ b/web_src/less/_base.less @@ -1382,6 +1382,14 @@ a.ui.label:hover { margin-right: 0; } +.lines-blame-btn { + padding-left: 10px; + padding-right: 10px; + text-align: right !important; + background-color: #f5f5f5; + width: 2%; +} + .lines-num { padding-left: 10px; padding-right: 10px; @@ -1510,6 +1518,10 @@ a.ui.label:hover { } } +.bottom-line-blame { + border-bottom: 1px solid var(--color-secondary); +} + .lines-code, .lines-commit { .bottom-line { From 04990824984e31809863675cbb2b92d9d2d1897e Mon Sep 17 00:00:00 2001 From: rogerluo410 Date: Fri, 26 Mar 2021 18:58:48 +0800 Subject: [PATCH 2/8] Jump to previous parent commit from the commit. --- routers/repo/blame.go | 16 ++++++++++++++-- templates/repo/blame.tmpl | 13 +++++-------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/routers/repo/blame.go b/routers/repo/blame.go index 3d817cfeb49a4..addc9c0b7d5aa 100644 --- a/routers/repo/blame.go +++ b/routers/repo/blame.go @@ -30,6 +30,8 @@ type BlameRow struct { Avatar gotemplate.HTML RepoLink string PartSha string + PreviousSha string + PreviousShaUrl string CommitUrl string CommitMessage string CommitSince gotemplate.HTML @@ -155,6 +157,7 @@ func RefBlame(ctx *context.Context) { } commitNames := make(map[string]models.UserCommit) + previousCommits := make(map[string]string) commits := list.New() for _, part := range blameParts { @@ -173,6 +176,12 @@ func RefBlame(ctx *context.Context) { return } + // Get previous closest commit sha from the commit + previousSha, err := commit.ParentID(0) + if err == nil { + previousCommits[commit.ID.String()] = previousSha.String() + } + commits.PushBack(commit) commitNames[commit.ID.String()] = models.UserCommit{} @@ -192,12 +201,12 @@ func RefBlame(ctx *context.Context) { return } - renderBlame(ctx, blameParts, commitNames) + renderBlame(ctx, blameParts, commitNames, previousCommits) ctx.HTML(200, tplBlame) } -func renderBlame(ctx *context.Context, blameParts []git.BlamePart, commitNames map[string]models.UserCommit) { +func renderBlame(ctx *context.Context, blameParts []git.BlamePart, commitNames map[string]models.UserCommit, previousCommits map[string]string) { repoLink := ctx.Repo.RepoLink var lines = make([]string, 0) @@ -215,6 +224,7 @@ func renderBlame(ctx *context.Context, blameParts []git.BlamePart, commitNames m } commit := commitNames[part.Sha] + previousSha := previousCommits[part.Sha] if index == 0 { // Count commit number commitCnt++ @@ -232,6 +242,8 @@ func renderBlame(ctx *context.Context, blameParts []git.BlamePart, commitNames m br.Avatar = gotemplate.HTML(avatar) br.RepoLink = repoLink br.PartSha = part.Sha + br.PreviousSha = previousSha + br.PreviousShaUrl = fmt.Sprintf("%s/blame/commit/%s/%s", repoLink, previousSha, ctx.Data["FileName"]) br.CommitUrl = fmt.Sprintf("%s/commit/%s", repoLink, part.Sha) br.CommitMessage = html.EscapeString(commit.CommitMessage) br.CommitSince = commitSince diff --git a/templates/repo/blame.tmpl b/templates/repo/blame.tmpl index 0d8b144b8784f..b3851d5710d70 100644 --- a/templates/repo/blame.tmpl +++ b/templates/repo/blame.tmpl @@ -43,9 +43,11 @@ - - {{svg "octicon-calendar"}} - + {{if $row.PreviousSha}} + + {{svg "octicon-calendar"}} + + {{end}} @@ -53,11 +55,6 @@ {{$row.Code}} - {{/* -
    - {{$row.Code}} -
    - */}} {{end}} From 7ee46f012891984987a454d10a437c96a0b62832 Mon Sep 17 00:00:00 2001 From: rogerluo410 Date: Fri, 26 Mar 2021 19:01:32 +0800 Subject: [PATCH 3/8] Lint code --- routers/repo/blame.go | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/routers/repo/blame.go b/routers/repo/blame.go index addc9c0b7d5aa..d78d2cc0153dc 100644 --- a/routers/repo/blame.go +++ b/routers/repo/blame.go @@ -26,16 +26,16 @@ const ( ) type BlameRow struct { - RowNumber int - Avatar gotemplate.HTML - RepoLink string - PartSha string - PreviousSha string - PreviousShaUrl string - CommitUrl string - CommitMessage string - CommitSince gotemplate.HTML - Code gotemplate.HTML + RowNumber int + Avatar gotemplate.HTML + RepoLink string + PartSha string + PreviousSha string + PreviousShaUrl string + CommitUrl string + CommitMessage string + CommitSince gotemplate.HTML + Code gotemplate.HTML } // RefBlame render blame page @@ -218,7 +218,7 @@ func renderBlame(ctx *context.Context, blameParts []git.BlamePart, commitNames m for index, line := range part.Lines { i++ lines = append(lines, line) - + br := &BlameRow{ RowNumber: i, } @@ -238,7 +238,7 @@ func renderBlame(ctx *context.Context, blameParts []git.BlamePart, commitNames m } else { avatar = string(templates.AvatarByEmail(commit.Author.Email, commit.Author.Name, 18, "mr-3")) } - + br.Avatar = gotemplate.HTML(avatar) br.RepoLink = repoLink br.PartSha = part.Sha @@ -256,10 +256,10 @@ func renderBlame(ctx *context.Context, blameParts []git.BlamePart, commitNames m line = highlight.Code(fileName, line) br.Code = gotemplate.HTML(line) - rows = append(rows, br) + rows = append(rows, br) } } - ctx.Data["Codes"] = rows + ctx.Data["Codes"] = rows ctx.Data["CommitCnt"] = commitCnt } From 93531202663584c1a3be7255162487fef8034a09 Mon Sep 17 00:00:00 2001 From: rogerluo410 Date: Fri, 26 Mar 2021 19:28:42 +0800 Subject: [PATCH 4/8] Fix previous commit link --- routers/repo/blame.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/repo/blame.go b/routers/repo/blame.go index d78d2cc0153dc..4e78ff920b2e9 100644 --- a/routers/repo/blame.go +++ b/routers/repo/blame.go @@ -243,7 +243,7 @@ func renderBlame(ctx *context.Context, blameParts []git.BlamePart, commitNames m br.RepoLink = repoLink br.PartSha = part.Sha br.PreviousSha = previousSha - br.PreviousShaUrl = fmt.Sprintf("%s/blame/commit/%s/%s", repoLink, previousSha, ctx.Data["FileName"]) + br.PreviousShaUrl = fmt.Sprintf("%s/blame/commit/%s/%s", repoLink, previousSha, ctx.Repo.TreePath) br.CommitUrl = fmt.Sprintf("%s/commit/%s", repoLink, part.Sha) br.CommitMessage = html.EscapeString(commit.CommitMessage) br.CommitSince = commitSince From caeffdb511c05da133a54f1b97c872de167169d3 Mon Sep 17 00:00:00 2001 From: rogerluo410 Date: Fri, 26 Mar 2021 21:22:47 +0800 Subject: [PATCH 5/8] Fix previous blame link --- routers/repo/blame.go | 6 +++++- templates/repo/blame.tmpl | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/routers/repo/blame.go b/routers/repo/blame.go index 4e78ff920b2e9..69712a500cb9f 100644 --- a/routers/repo/blame.go +++ b/routers/repo/blame.go @@ -179,7 +179,11 @@ func RefBlame(ctx *context.Context) { // Get previous closest commit sha from the commit previousSha, err := commit.ParentID(0) if err == nil { - previousCommits[commit.ID.String()] = previousSha.String() + // Verify the commit + haz, _ := commit.HasPreviousCommit(previousSha) + if haz { + previousCommits[commit.ID.String()] = previousSha.String() + } } commits.PushBack(commit) diff --git a/templates/repo/blame.tmpl b/templates/repo/blame.tmpl index b3851d5710d70..f4b5442716836 100644 --- a/templates/repo/blame.tmpl +++ b/templates/repo/blame.tmpl @@ -43,7 +43,7 @@
    - {{if $row.PreviousSha}} + {{if and (gt $.CommitCnt 1) ($row.PreviousSha)}} {{svg "octicon-calendar"}} From c6170cdcbf74f7ad8ba43257e47fb9458d961bb0 Mon Sep 17 00:00:00 2001 From: rogerluo410 Date: Sat, 27 Mar 2021 18:14:11 +0800 Subject: [PATCH 6/8] Update svg. --- routers/repo/blame.go | 8 ++++---- templates/repo/blame.tmpl | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/routers/repo/blame.go b/routers/repo/blame.go index 69712a500cb9f..0f04256220f9c 100644 --- a/routers/repo/blame.go +++ b/routers/repo/blame.go @@ -31,8 +31,8 @@ type BlameRow struct { RepoLink string PartSha string PreviousSha string - PreviousShaUrl string - CommitUrl string + PreviousShaURL string + CommitURL string CommitMessage string CommitSince gotemplate.HTML Code gotemplate.HTML @@ -247,8 +247,8 @@ func renderBlame(ctx *context.Context, blameParts []git.BlamePart, commitNames m br.RepoLink = repoLink br.PartSha = part.Sha br.PreviousSha = previousSha - br.PreviousShaUrl = fmt.Sprintf("%s/blame/commit/%s/%s", repoLink, previousSha, ctx.Repo.TreePath) - br.CommitUrl = fmt.Sprintf("%s/commit/%s", repoLink, part.Sha) + br.PreviousShaURL = fmt.Sprintf("%s/blame/commit/%s/%s", repoLink, previousSha, ctx.Repo.TreePath) + br.CommitURL = fmt.Sprintf("%s/commit/%s", repoLink, part.Sha) br.CommitMessage = html.EscapeString(commit.CommitMessage) br.CommitSince = commitSince } diff --git a/templates/repo/blame.tmpl b/templates/repo/blame.tmpl index f4b5442716836..dc82ad265d147 100644 --- a/templates/repo/blame.tmpl +++ b/templates/repo/blame.tmpl @@ -32,7 +32,7 @@ {{$row.Avatar}}
    @@ -44,8 +44,8 @@ {{if and (gt $.CommitCnt 1) ($row.PreviousSha)}} - - {{svg "octicon-calendar"}} + + {{svg "octicon-versions"}} {{end}} From 45b83dc25c5301da454b977498f3d06400455a38 Mon Sep 17 00:00:00 2001 From: rogerluo410 Date: Sat, 27 Mar 2021 18:36:23 +0800 Subject: [PATCH 7/8] Fix the given file not exist in the previous commit. --- routers/repo/blame.go | 10 ++++++---- templates/repo/blame.tmpl | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/routers/repo/blame.go b/routers/repo/blame.go index 0f04256220f9c..f0c6070dc0da8 100644 --- a/routers/repo/blame.go +++ b/routers/repo/blame.go @@ -32,6 +32,7 @@ type BlameRow struct { PartSha string PreviousSha string PreviousShaURL string + IsFirstCommit bool CommitURL string CommitMessage string CommitSince gotemplate.HTML @@ -177,12 +178,13 @@ func RefBlame(ctx *context.Context) { } // Get previous closest commit sha from the commit - previousSha, err := commit.ParentID(0) + previousCommit, err := commit.Parent(0) if err == nil { // Verify the commit - haz, _ := commit.HasPreviousCommit(previousSha) - if haz { - previousCommits[commit.ID.String()] = previousSha.String() + if haz, _ := commit.HasPreviousCommit(previousCommit.ID); haz { + if haz1, _ := previousCommit.HasFile(ctx.Repo.TreePath); haz1 { + previousCommits[commit.ID.String()] = previousCommit.ID.String() + } } } diff --git a/templates/repo/blame.tmpl b/templates/repo/blame.tmpl index dc82ad265d147..c0a98ef46afe7 100644 --- a/templates/repo/blame.tmpl +++ b/templates/repo/blame.tmpl @@ -43,7 +43,7 @@ - {{if and (gt $.CommitCnt 1) ($row.PreviousSha)}} + {{if $row.PreviousSha}} {{svg "octicon-versions"}} From 78ed60ad548311a2e854270171563fe24a97c1ab Mon Sep 17 00:00:00 2001 From: rogerluo410 Date: Sat, 27 Mar 2021 18:58:22 +0800 Subject: [PATCH 8/8] Fix blameRow struct not export --- routers/repo/blame.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/routers/repo/blame.go b/routers/repo/blame.go index f0c6070dc0da8..d8e8c93da979b 100644 --- a/routers/repo/blame.go +++ b/routers/repo/blame.go @@ -25,7 +25,7 @@ const ( tplBlame base.TplName = "repo/home" ) -type BlameRow struct { +type blameRow struct { RowNumber int Avatar gotemplate.HTML RepoLink string @@ -216,7 +216,7 @@ func renderBlame(ctx *context.Context, blameParts []git.BlamePart, commitNames m repoLink := ctx.Repo.RepoLink var lines = make([]string, 0) - rows := make([]*BlameRow, 0) + rows := make([]*blameRow, 0) var i = 0 var commitCnt = 0 @@ -225,7 +225,7 @@ func renderBlame(ctx *context.Context, blameParts []git.BlamePart, commitNames m i++ lines = append(lines, line) - br := &BlameRow{ + br := &blameRow{ RowNumber: i, }