From 934734a85a1e4cb68d8c1aa68c518b511c7be190 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Mon, 20 Feb 2017 16:16:32 -0500 Subject: [PATCH] diff: able to highlight line with hashtag URL --- gogs.go | 2 +- models/git_diff.go | 29 ++++++++++-------- public/css/gogs.css | 26 ++++++++++++---- public/js/gogs.js | 14 +++++++++ public/less/_repository.less | 38 +++++++++++++----------- templates/.VERSION | 2 +- templates/repo/diff/section_unified.tmpl | 14 +++++---- 7 files changed, 82 insertions(+), 43 deletions(-) diff --git a/gogs.go b/gogs.go index b4827b5ba..76a4a107c 100644 --- a/gogs.go +++ b/gogs.go @@ -16,7 +16,7 @@ import ( "github.com/gogits/gogs/modules/setting" ) -const APP_VER = "0.9.160.0220" +const APP_VER = "0.9.161.0220" func init() { setting.AppVer = APP_VER diff --git a/models/git_diff.go b/models/git_diff.go index c470e4a2c..f61e88d47 100644 --- a/models/git_diff.go +++ b/models/git_diff.go @@ -188,7 +188,7 @@ func (diffSection *DiffSection) GetComputedInlineDiffFor(diffLine *DiffLine) tem type DiffFile struct { Name string OldName string - Index int + Index string // 40-byte SHA, Changed/New: new SHA; Deleted: old SHA Addition, Deletion int Type DiffFileType IsCreated bool @@ -331,7 +331,6 @@ func ParsePatch(maxLines, maxLineCharacteres, maxFiles int, reader io.Reader) (* curFile = &DiffFile{ Name: a, - Index: len(diff.Files) + 1, Type: DIFF_FILE_CHANGE, Sections: make([]*DiffSection, 0, 10), } @@ -343,7 +342,8 @@ func ParsePatch(maxLines, maxLineCharacteres, maxFiles int, reader io.Reader) (* } curFileLinesCount = 0 - // Check file diff type and is submodule. + // Check file diff type and submodule. + CHECK_TYPE: for { line, err := input.ReadString('\n') if err != nil { @@ -358,22 +358,25 @@ func ParsePatch(maxLines, maxLineCharacteres, maxFiles int, reader io.Reader) (* case strings.HasPrefix(line, "new file"): curFile.Type = DIFF_FILE_ADD curFile.IsCreated = true + curFile.IsSubmodule = strings.HasSuffix(line, " 160000\n") case strings.HasPrefix(line, "deleted"): curFile.Type = DIFF_FILE_DEL curFile.IsDeleted = true + curFile.IsSubmodule = strings.HasSuffix(line, " 160000\n") case strings.HasPrefix(line, "index"): - curFile.Type = DIFF_FILE_CHANGE + if curFile.IsDeleted { + curFile.Index = line[6:46] + } else { + curFile.Index = line[49:88] + } + break CHECK_TYPE case strings.HasPrefix(line, "similarity index 100%"): curFile.Type = DIFF_FILE_RENAME curFile.IsRenamed = true curFile.OldName = curFile.Name curFile.Name = b - } - if curFile.Type > 0 { - if strings.HasSuffix(line, " 160000\n") { - curFile.IsSubmodule = true - } - break + curFile.Index = b + break CHECK_TYPE } } } @@ -423,13 +426,13 @@ func GetDiffRange(repoPath, beforeCommitID, afterCommitID string, maxLines, maxL if len(beforeCommitID) == 0 { // First commit of repository. if commit.ParentCount() == 0 { - cmd = exec.Command("git", "show", afterCommitID) + cmd = exec.Command("git", "show", "--full-index", afterCommitID) } else { c, _ := commit.Parent(0) - cmd = exec.Command("git", "diff", "-M", c.ID.String(), afterCommitID) + cmd = exec.Command("git", "diff", "--full-index", "-M", c.ID.String(), afterCommitID) } } else { - cmd = exec.Command("git", "diff", "-M", beforeCommitID, afterCommitID) + cmd = exec.Command("git", "diff", "--full-index", "-M", beforeCommitID, afterCommitID) } cmd.Dir = repoPath cmd.Stderr = os.Stderr diff --git a/public/css/gogs.css b/public/css/gogs.css index 2b044a92b..ed6ef2a9f 100644 --- a/public/css/gogs.css +++ b/public/css/gogs.css @@ -2065,8 +2065,15 @@ footer .ui.language .menu { border-right: 1px solid #d4d4d5; padding: 0 5px; } -.repository .diff-file-box .code-diff tbody tr.tag-code td, -.repository .diff-file-box .code-diff tbody tr.tag-code pre { +.repository .diff-file-box .code-diff .lines-num.lines-num-old, +.repository .diff-file-box .code-diff .lines-num.lines-num-new { + cursor: pointer; +} +.repository .diff-file-box .code-diff .lines-num.lines-num-old:hover, +.repository .diff-file-box .code-diff .lines-num.lines-num-new:hover { + color: #383636; +} +.repository .diff-file-box .code-diff tbody tr.tag-code td { background-color: #F0F0F0 !important; border-color: #D2CECE!important; padding-top: 4px; @@ -2075,6 +2082,9 @@ footer .ui.language .menu { .repository .diff-file-box .code-diff tbody tr.tag-code td.halfwidth { width: 50%; } +.repository .diff-file-box .code-diff tbody tr.same-code td.active { + background-color: #ffffdd !important; +} .repository .diff-file-box .code-diff tbody tr.del-code td.add-code { background-color: #eaffea !important; border-color: #c1e9c1 !important; @@ -2083,22 +2093,26 @@ footer .ui.language .menu { background-color: #eaffea !important; border-color: #c1e9c1 !important; } -.repository .diff-file-box .code-diff tbody tr.del-code td, -.repository .diff-file-box .code-diff tbody tr.del-code pre { +.repository .diff-file-box .code-diff tbody tr.del-code td { background-color: #ffecec !important; border-color: #f1c0c0 !important; } +.repository .diff-file-box .code-diff tbody tr.del-code td.active { + background-color: #ffffdd !important; +} .repository .diff-file-box .code-diff tbody tr.del-code td.halfwidth { width: 50%; } -.repository .diff-file-box .code-diff tbody tr.add-code td, -.repository .diff-file-box .code-diff tbody tr.add-code pre { +.repository .diff-file-box .code-diff tbody tr.add-code td { background-color: #eaffea !important; border-color: #c1e9c1 !important; } .repository .diff-file-box .code-diff tbody tr.add-code td.halfwidth { width: 50%; } +.repository .diff-file-box .code-diff tbody tr.add-code td.active { + background-color: #ffffdd !important; +} .repository .diff-file-box .code-diff tbody tr .removed-code { background-color: #ff9999; } diff --git a/public/js/gogs.js b/public/js/gogs.js index b7770fdee..2cd98d3b2 100644 --- a/public/js/gogs.js +++ b/public/js/gogs.js @@ -542,6 +542,20 @@ function initRepository() { $item.find(".bar .add").css("width", addPercent + "%"); }); } + + $('.diff-file-box .lines-num').click(function () { + if ($(this).attr('id')) { + window.location.href = '#' + $(this).attr('id'); + } + }); + + $(window).on('hashchange', function (e) { + $('.diff-file-box .lines-code.active').removeClass('active'); + var m = window.location.hash.match(/^#diff-.+$/); + if (m) { + $(m[0]).siblings('.lines-code').addClass('active'); + } + }).trigger('hashchange'); } // Quick start and repository home diff --git a/public/less/_repository.less b/public/less/_repository.less index a39d2f75e..f80f9d56a 100644 --- a/public/less/_repository.less +++ b/public/less/_repository.less @@ -1005,12 +1005,19 @@ .lines-num { border-right: 1px solid #d4d4d5; padding: 0 5px; + + &.lines-num-old, &.lines-num-new { + cursor: pointer; + &:hover { + color: #383636; + } + } } tbody { tr { &.tag-code { - td, pre { + td { background-color: #F0F0F0 !important; border-color: #D2CECE!important; padding-top: 4px; @@ -1019,15 +1026,12 @@ td.halfwidth { width: 50%; } - // td.selected-line, td.selected-line pre { - // background-color: #ffffdd !important; - // } } - // &.same-code { - // td.selected-line, td.selected-line pre { - // background-color: #ffffdd !important; - // } - // } + &.same-code { + td.active { + background-color: #ffffdd !important; + } + } &.del-code { // Duplicate here to enforce add code color. td.add-code { @@ -1039,29 +1043,29 @@ } } - td, pre { + td { background-color: #ffecec !important; border-color: #f1c0c0 !important; } + td.active { + background-color: #ffffdd !important; + } td.halfwidth { width: 50%; } - // td.selected-line, td.selected-line pre { - // background-color: #ffffdd !important; - // } } &.add-code { - td, pre { + td { background-color: #eaffea !important; border-color: #c1e9c1 !important; } td.halfwidth { width: 50%; } - // td.selected-line, td.selected-line pre { - // background-color: #ffffdd !important; - // } + td.active { + background-color: #ffffdd !important; + } } .removed-code { diff --git a/templates/.VERSION b/templates/.VERSION index 572fb6cc6..720049a69 100644 --- a/templates/.VERSION +++ b/templates/.VERSION @@ -1 +1 @@ -0.9.160.0220 \ No newline at end of file +0.9.161.0220 \ No newline at end of file diff --git a/templates/repo/diff/section_unified.tmpl b/templates/repo/diff/section_unified.tmpl index ee66b6dc9..f5ea81f17 100644 --- a/templates/repo/diff/section_unified.tmpl +++ b/templates/repo/diff/section_unified.tmpl @@ -8,14 +8,18 @@ {{/* {{if gt $j 0}}{{end}} */}} {{else}} - - {{if $line.LeftIdx}}{{$line.LeftIdx}}{{end}} + + {{if $line.LeftIdx}} + {{$line.LeftIdx}} + {{end}} - - {{if $line.RightIdx}}{{$line.RightIdx}}{{end}} + + {{if $line.RightIdx}} + {{$line.RightIdx}} + {{end}} {{end}} - +
{{$section.GetComputedInlineDiffFor $line}}