瀏覽代碼

diff: able to highlight line with hashtag URL

Unknwon 8 年之前
父節點
當前提交
934734a85a
共有 7 個文件被更改,包括 82 次插入43 次删除
  1. 1 1
      gogs.go
  2. 16 13
      models/git_diff.go
  3. 20 6
      public/css/gogs.css
  4. 14 0
      public/js/gogs.js
  5. 21 17
      public/less/_repository.less
  6. 1 1
      templates/.VERSION
  7. 9 5
      templates/repo/diff/section_unified.tmpl

+ 1 - 1
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

+ 16 - 13
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

+ 20 - 6
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;
 }

+ 14 - 0
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

+ 21 - 17
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 {

+ 1 - 1
templates/.VERSION

@@ -1 +1 @@
-0.9.160.0220
+0.9.161.0220

+ 9 - 5
templates/repo/diff/section_unified.tmpl

@@ -8,14 +8,18 @@
 				{{/* {{if gt $j 0}}<span class="fold octicon octicon-fold"></span>{{end}} */}}
 			</td>
 			{{else}}
-			<td class="lines-num lines-num-old">
-				<span rel="{{if $line.LeftIdx}}diff-{{Sha1 $file.Name}}L{{$line.LeftIdx}}{{end}}">{{if $line.LeftIdx}}{{$line.LeftIdx}}{{end}}</span>
+			<td class="lines-num lines-num-old" {{if $line.LeftIdx}}id="diff-{{$file.Index}}L{{$line.LeftIdx}}"{{end}}>
+				{{if $line.LeftIdx}}
+					<span>{{$line.LeftIdx}}</span>
+				{{end}}
 			</td>
-			<td class="lines-num lines-num-new">
-				<span rel="{{if $line.RightIdx}}diff-{{Sha1 $file.Name}}R{{$line.RightIdx}}{{end}}">{{if $line.RightIdx}}{{$line.RightIdx}}{{end}}</span>
+			<td class="lines-num lines-num-new" {{if $line.RightIdx}}id="diff-{{$file.Index}}R{{$line.RightIdx}}"{{end}}>
+				{{if $line.RightIdx}}
+					<span>{{$line.RightIdx}}</span>
+				{{end}}
 			</td>
 			{{end}}
-			<td class="lines-code">
+			<td class="lines-code" rel="">
 				<pre><code class="{{if $highlightClass}}language-{{$highlightClass}}{{else}}nohighlight{{end}}">{{$section.GetComputedInlineDiffFor $line}}</code></pre>
 			</td>
 		</tr>