Browse Source

new compare view

pull/376/head
Lunny Xiao 11 years ago
parent
commit
692ba5e5e1
  1. 5
      cmd/web.go
  2. 16
      models/git_diff.go
  3. 32
      routers/repo/commit.go
  4. 32
      templates/repo/diff.tmpl

5
cmd/web.go

@ -108,11 +108,6 @@ func runWeb(*cli.Context) {
// Repositories.
r.Get("/orgs/:org/repos/search", v1.SearchOrgRepositoreis)
m.Group("/:username/:reponame", func(r martini.Router) {
r.Get("/commit/:branchname", repo.DiffAjax)
r.Get("/commit/:branchname/**", repo.DiffAjax)
})
r.Any("**", func(ctx *middleware.Context) {
ctx.JSON(404, &base.ApiJsonErr{"Not Found", v1.DOC_URL})
})

16
models/git_diff.go

@ -209,7 +209,7 @@ func GetDiff(repoPath, commitid string) (*Diff, error) {
return ParsePatch(process.Add(fmt.Sprintf("GetDiff(%s)", repoPath), cmd), cmd, rd)
}
func ParsePatchCallback(pid int64, cmd *exec.Cmd, reader io.Reader, callback DiffCallback) error {
func ParsePatchChan(pid int64, cmd *exec.Cmd, reader io.Reader, file chan *DiffFile) error {
scanner := bufio.NewScanner(reader)
var (
curFile *DiffFile
@ -277,7 +277,8 @@ func ParsePatchCallback(pid int64, cmd *exec.Cmd, reader io.Reader, callback Dif
a := fs[0]
if curFile != nil {
callback(curFile)
fmt.Println("curFile:", curFile)
file <- curFile
}
curFile = &DiffFile{
@ -306,7 +307,8 @@ func ParsePatchCallback(pid int64, cmd *exec.Cmd, reader io.Reader, callback Dif
}
if curFile != nil {
callback(curFile)
fmt.Println("last curFile:", curFile)
file <- curFile
}
// In case process became zombie.
@ -316,9 +318,7 @@ func ParsePatchCallback(pid int64, cmd *exec.Cmd, reader io.Reader, callback Dif
return nil
}
type DiffCallback func(*DiffFile) error
func GetDiffCallback(repoPath, commitid string, callback DiffCallback) error {
func GetDiffChan(repoPath, commitid string, file chan *DiffFile) error {
repo, err := git.OpenRepository(repoPath)
if err != nil {
return err
@ -347,6 +347,6 @@ func GetDiffCallback(repoPath, commitid string, callback DiffCallback) error {
wr.Close()
}()
defer rd.Close()
return ParsePatchCallback(process.Add(fmt.Sprintf("GetDiff(%s)", repoPath), cmd),
cmd, rd, callback)
return ParsePatchChan(process.Add(fmt.Sprintf("GetDiff(%s)", repoPath), cmd),
cmd, rd, file)
}

32
routers/repo/commit.go

@ -5,7 +5,6 @@
package repo
import (
"encoding/json"
"path"
"github.com/go-martini/martini"
@ -106,26 +105,6 @@ func SearchCommits(ctx *middleware.Context, params martini.Params) {
ctx.HTML(200, COMMITS)
}
func DiffAjax(ctx *middleware.Context, params martini.Params) {
userName := params["username"]
repoName := params["reponame"]
commitId := params["branchname"]
err := models.GetDiffCallback(models.RepoPath(userName, repoName), commitId, func(f *models.DiffFile) error {
bs, err := json.Marshal(f)
if err != nil {
return err
}
_, err = ctx.Res.Write(bs)
return err
})
if err != nil {
ctx.Handle(404, "repo.DiffAjax", err)
return
}
}
func Diff(ctx *middleware.Context, params martini.Params) {
ctx.Data["IsRepoToolbarCommits"] = true
@ -135,11 +114,15 @@ func Diff(ctx *middleware.Context, params martini.Params) {
commit := ctx.Repo.Commit
/*diff, err := models.GetDiff(models.RepoPath(userName, repoName), commitId)
var fileChan = make(chan *models.DiffFile)
go func() {
err := models.GetDiffChan(models.RepoPath(userName, repoName), commitId, fileChan)
if err != nil {
ctx.Handle(404, "repo.Diff(GetDiff)", err)
return
}*/
}
close(fileChan)
}()
isImageFile := func(name string) bool {
blob, err := ctx.Repo.Commit.GetBlobByPath(name)
@ -178,9 +161,10 @@ func Diff(ctx *middleware.Context, params martini.Params) {
ctx.Data["Commit"] = commit
//ctx.Data["Diff"] = diff
ctx.Data["Parents"] = parents
//ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
ctx.Data["DiffNotAvailable"] = false
ctx.Data["SourcePath"] = "/" + path.Join(userName, repoName, "src", commitId)
ctx.Data["RawPath"] = "/" + path.Join(userName, repoName, "raw", commitId)
ctx.Data["Diffs"] = fileChan
ctx.HTML(200, DIFF)
}

32
templates/repo/diff.tmpl

@ -32,32 +32,11 @@
<a class="pull-right btn btn-default" data-toggle="collapse" data-target="#diff-files">Show Diff Stats</a>
<p class="showing">
<i class="fa fa-retweet"></i>
<strong> {{.Diff.NumFiles}} changed files</strong> with <strong>{{.Diff.TotalAddition}} additions</strong> and <strong>{{.Diff.TotalDeletion}} deletions</strong>.
<strong> changed files</strong> with <strong> additions</strong> and <strong> deletions</strong>.
</p>
<ol class="detail-files collapse" id="diff-files">
{{range .Diff.Files}}
<li>
<div class="diff-counter count pull-right">
{{if not .IsBin}}
<span class="add" data-line="{{.Addition}}">{{.Addition}}</span>
<span class="bar">
<span class="pull-left add"></span>
<span class="pull-left del"></span>
</span>
<span class="del" data-line="{{.Deletion}}">{{.Deletion}}</span>
{{else}}
<span>BIN</span>
{{end}}
</div>
<!-- todo finish all file status, now modify, add, delete and rename -->
<span class="status {{DiffTypeToStr .Type}}" data-toggle="tooltip" data-placement="right" title="{{DiffTypeToStr .Type}}">&nbsp;</span>
<a class="file" href="#diff-{{.Index}}">{{.Name}}</a>
</li>
{{end}}
</ol>
</div>
{{range .Diff.Files}}
{{range .Diffs}}
<div class="panel panel-default diff-file-box diff-box file-content" id="diff-{{.Index}}">
<div class="panel-heading">
<div class="diff-counter count pull-left">
@ -108,11 +87,4 @@
{{end}}
</div>
</div>
<script>
$.ajax("https://localhost:3000/api/v1/lunny1/cmd/commit/d049c3fe2dd1f6ed8faad2d7151b3145a632098b", {
success:function(){
alert("dddd")
}
})
</script>
{{template "base/footer" .}}

Loading…
Cancel
Save