forked from go-gitea/gitea
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
288 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package models | ||
|
||
import ( | ||
"code.gitea.io/git" | ||
"fmt" | ||
"github.com/Unknwon/com" | ||
"time" | ||
"bytes" | ||
"regexp" | ||
) | ||
|
||
// Blame represents a Git object. | ||
type BlameLine struct { | ||
Commit *git.Commit | ||
Author string // Signature? | ||
When time.Time | ||
Num string | ||
Content string | ||
} | ||
|
||
// Blame represents a Git object. | ||
type Blame struct { | ||
File string | ||
Lines []*BlameLine | ||
} | ||
|
||
// GitBlame | ||
func (repo *Repository) GitBlame(branch, fileName string) (*Blame, error) { | ||
repoWorkingPool.CheckIn(com.ToStr(repo.ID)) | ||
defer repoWorkingPool.CheckOut(com.ToStr(repo.ID)) | ||
|
||
if err := repo.DiscardLocalRepoBranchChanges(branch); err != nil { | ||
return nil, fmt.Errorf("DiscardLocalRepoBranchChanges [branch: %s]: %v", branch, err) | ||
} else if err = repo.UpdateLocalCopyBranch(branch); err != nil { | ||
return nil, fmt.Errorf("UpdateLocalCopyBranch [branch: %s]: %v", branch, err) | ||
} | ||
|
||
localPath := repo.LocalCopyPath() | ||
|
||
gitRepo, err := git.OpenRepository(repo.RepoPath()) | ||
if err != nil { | ||
return nil, fmt.Errorf("git.OpenRepository [branch: %s]: %v", branch, err) | ||
} | ||
|
||
blameBytes, err := gitRepo.FileBlame(branch, localPath, fileName) | ||
if err != nil { | ||
return nil, fmt.Errorf("git.FileBlame [branch: %s, file %s]: %v", branch, fileName, err) | ||
} | ||
|
||
return repo.parseBlameOutput(blameBytes) | ||
} | ||
|
||
func (repo *Repository) parseBlameOutput(blameBytes []byte) (*Blame, error) { | ||
b := new(Blame) | ||
if len(blameBytes) == 0 { | ||
return b, nil | ||
} | ||
|
||
gitRepo, err := git.OpenRepository(repo.RepoPath()) | ||
if err != nil { | ||
return nil, fmt.Errorf("git.OpenRepository: %v", err) | ||
} | ||
|
||
parts := bytes.Split(blameBytes, []byte{'\n'}) | ||
|
||
r := regexp.MustCompile(`^(.{8})\s\((.+)\s(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\s\+\d{4})\s(\d+)\)\s(.*)`) | ||
for _, part := range parts[0:len(parts) - 1] { // last one is empty string | ||
matches := r.FindAllStringSubmatch(string(part), -1) | ||
if len(matches) > 0 { | ||
commitID := matches[0][1] | ||
author := matches[0][2] | ||
dateString := matches[0][3] | ||
lineNum := matches[0][4] | ||
content := matches[0][5] | ||
|
||
date, err := time.Parse("2006-01-02 15:04:05 -0700", dateString) | ||
if err != nil { | ||
return nil, err | ||
} | ||
commit, err := gitRepo.GetCommit(commitID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
bl := &BlameLine{ | ||
Commit: commit, | ||
Author: author, | ||
When: date, | ||
Num: lineNum, | ||
Content: content, | ||
} | ||
|
||
b.Lines = append(b.Lines, bl) | ||
} | ||
} | ||
|
||
return b, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright 2017 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package repo | ||
|
||
import ( | ||
//"code.gitea.io/git" | ||
gotemplate "html/template" | ||
|
||
"code.gitea.io/gitea/modules/base" | ||
"code.gitea.io/gitea/modules/context" | ||
"code.gitea.io/git" | ||
"fmt" | ||
"bytes" | ||
"code.gitea.io/gitea/modules/highlight" | ||
"strings" | ||
) | ||
|
||
const ( | ||
tplBlame base.TplName = "repo/blame" | ||
) | ||
|
||
// Blame shows git-blame | ||
func Blame(ctx *context.Context) { | ||
fileName := ctx.Repo.TreePath | ||
|
||
_, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath) | ||
if err != nil { | ||
ctx.NotFoundOrServerError("Repo.Commit.GetTreeEntryByPath", git.IsErrNotExist, err) | ||
return | ||
} | ||
|
||
if len(fileName) == 0 { | ||
ctx.Handle(404, "TreePath not foung", nil) | ||
return | ||
} | ||
branchName := ctx.Repo.BranchName | ||
blame, err := ctx.Repo.Repository.GitBlame(branchName, fileName) | ||
|
||
ctx.Data["RequireHighlightJS"] = true | ||
ctx.Data["HighlightClass"] = highlight.FileNameToHighlightClass(fileName) | ||
if err != nil { | ||
ctx.Handle(500, "FileBlame", err) | ||
return | ||
} | ||
var output bytes.Buffer | ||
for index, line := range blame.Lines { | ||
output.WriteString(fmt.Sprintf(`<li class="L%d" rel="L%d">%s</li>`, index+1, index+1, gotemplate.HTMLEscapeString(line.Content)) + "\n") | ||
} | ||
|
||
var treeNames []string | ||
paths := make([]string, 0, 5) | ||
if len(ctx.Repo.TreePath) > 0 { | ||
treeNames = strings.Split(ctx.Repo.TreePath, "/") | ||
for i := range treeNames { | ||
paths = append(paths, strings.Join(treeNames[:i+1], "/")) | ||
} | ||
} | ||
|
||
ctx.Data["TreeNames"] = treeNames | ||
ctx.Data["FileContent"] = gotemplate.HTML(output.String()) | ||
ctx.Data["Blame"] = blame | ||
ctx.Data["FileName"] = fileName | ||
ctx.HTML(200, tplBlame) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
{{template "base/head" .}} | ||
<div class="repository file list blame"> | ||
{{template "repo/header" .}} | ||
<div class="ui container"> | ||
<div class="ui secondary menu"> | ||
{{template "repo/branch_dropdown" .}} | ||
<div class="fitted item"> | ||
<div class="ui breadcrumb"> | ||
<a class="section" href="{{.RepoLink}}/src/{{EscapePound .BranchName}}">{{EllipsisString .Repository.Name 25}}</a> | ||
{{ $n := len .TreeNames}} | ||
{{ $l := Subtract $n 1}} | ||
{{range $i, $v := .TreeNames}} | ||
<div class="divider"> / </div> | ||
{{if eq $i $l}} | ||
<span class="active section">{{EllipsisString $v 15}}</span> | ||
{{else}} | ||
{{ $p := index $.Paths $i}} | ||
<span class="section"><a href="{{EscapePound $.BranchLink}}/{{EscapePound $p}}">{{EllipsisString $v 15}}</a></span> | ||
{{end}} | ||
{{end}} | ||
</div> | ||
</div> | ||
</div> | ||
<div id="file-content" class="{{TabSizeClass .Editorconfig .FileName}}"> | ||
<h4 class="ui top attached header"> | ||
<i class="file text outline icon ui left"></i> | ||
<strong>{{.FileName}}</strong> <span class="text grey normal"></span> | ||
<div class="ui right file-actions"> | ||
<div class="ui buttons"> | ||
<a class="ui button" | ||
href="{{.RepoLink}}/commits/{{EscapePound .BranchName}}/{{EscapePound .TreePath}}">{{.i18n.Tr "repo.file_history"}}</a> | ||
<a class="ui button" href="{{.RepoLink}}/src/{{EscapePound .BranchName}}/{{EscapePound .TreePath}}">{{.i18n.Tr "repo.file_normal"}}</a> | ||
</div> | ||
</div> | ||
</h4> | ||
|
||
<div class="ui attached table segment"> | ||
<div class="file-view code-view has-emoji"> | ||
<table> | ||
<tbody> | ||
<tr> | ||
{{if .IsFileTooLarge}} | ||
<td><strong>{{.i18n.Tr "repo.file_too_large"}}</strong></td> | ||
{{else}} | ||
<td class="lines-blame"> | ||
<ol class="linenums"> | ||
{{range .Blame.Lines}} | ||
<li> | ||
<a href="{{$.RepoLink}}/commit/{{.Commit.ID.String}}"> | ||
{{ShortSha .Commit.ID.String}} | ||
</a> | ||
({{.Commit.Author.Name}}) | ||
{{TimeSince .Commit.Committer.When $.Lang}} | ||
</li> | ||
{{end}} | ||
{{.Commits}} | ||
</ol> | ||
</td> | ||
<td class="lines-num"> | ||
{{range .Blame.Lines}} | ||
<span id="L{{.Num}}">{{.Num}}</span> | ||
{{end}} | ||
</td> | ||
<td class="lines-code"><pre><code class="{{.HighlightClass}} hljs"><ol class="linenums">{{.FileContent}}</ol></code></pre></td> | ||
{{end}} | ||
</tr> | ||
</tbody> | ||
</table> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
|
||
</div> | ||
</div> | ||
{{template "base/footer" .}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
995a280
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the status of this branch?