fix read commit source
This commit is contained in:
		
							parent
							
								
									c796ed3849
								
							
						
					
					
						commit
						677643b812
					
				
					 6 changed files with 86 additions and 36 deletions
				
			
		|  | @ -38,8 +38,8 @@ func (file *RepoFile) LookupBlob() (*git.Blob, error) { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // GetBranches returns all branches of given repository.
 | // GetBranches returns all branches of given repository.
 | ||||||
| func GetBranches(userName, reposName string) ([]string, error) { | func GetBranches(userName, repoName string) ([]string, error) { | ||||||
| 	repo, err := git.OpenRepository(RepoPath(userName, reposName)) | 	repo, err := git.OpenRepository(RepoPath(userName, repoName)) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
|  | @ -56,8 +56,16 @@ func GetBranches(userName, reposName string) ([]string, error) { | ||||||
| 	return brs, nil | 	return brs, nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func GetTargetFile(userName, reposName, branchName, commitId, rpath string) (*RepoFile, error) { | func IsBranchExist(userName, repoName, branchName string) bool { | ||||||
| 	repo, err := git.OpenRepository(RepoPath(userName, reposName)) | 	repo, err := git.OpenRepository(RepoPath(userName, repoName)) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return false | ||||||
|  | 	} | ||||||
|  | 	return repo.IsBranchExist(branchName) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func GetTargetFile(userName, repoName, branchName, commitId, rpath string) (*RepoFile, error) { | ||||||
|  | 	repo, err := git.OpenRepository(RepoPath(userName, repoName)) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
|  | @ -102,8 +110,8 @@ func GetTargetFile(userName, reposName, branchName, commitId, rpath string) (*Re | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // GetReposFiles returns a list of file object in given directory of repository.
 | // GetReposFiles returns a list of file object in given directory of repository.
 | ||||||
| func GetReposFiles(userName, reposName, branchName, commitId, rpath string) ([]*RepoFile, error) { | func GetReposFiles(userName, repoName, branchName, commitId, rpath string) ([]*RepoFile, error) { | ||||||
| 	repo, err := git.OpenRepository(RepoPath(userName, reposName)) | 	repo, err := git.OpenRepository(RepoPath(userName, repoName)) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
|  | @ -217,13 +225,27 @@ func GetCommit(userName, repoName, branchname, commitid string) (*git.Commit, er | ||||||
| 	return repo.GetCommit(branchname, commitid) | 	return repo.GetCommit(branchname, commitid) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // GetCommits returns all commits of given branch of repository.
 | // GetCommitsByBranch returns all commits of given branch of repository.
 | ||||||
| func GetCommits(userName, reposName, branchname string) (*list.List, error) { | func GetCommitsByBranch(userName, repoName, branchName string) (*list.List, error) { | ||||||
| 	repo, err := git.OpenRepository(RepoPath(userName, reposName)) | 	repo, err := git.OpenRepository(RepoPath(userName, repoName)) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
| 	r, err := repo.LookupReference(fmt.Sprintf("refs/heads/%s", branchname)) | 	r, err := repo.LookupReference(fmt.Sprintf("refs/heads/%s", branchName)) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return nil, err | ||||||
|  | 	} | ||||||
|  | 	return r.AllCommits() | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // GetCommitsByCommitId returns all commits of given commitId of repository.
 | ||||||
|  | func GetCommitsByCommitId(userName, repoName, commitId string) (*list.List, error) { | ||||||
|  | 	repo, err := git.OpenRepository(RepoPath(userName, repoName)) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return nil, err | ||||||
|  | 	} | ||||||
|  | 	fmt.Println(userName, repoName, commitId) | ||||||
|  | 	r, err := repo.LookupReference(commitId) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | @ -5,13 +5,22 @@ | ||||||
| package repo | package repo | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
|  | 	"container/list" | ||||||
|  | 	"fmt" | ||||||
|  | 	"path" | ||||||
|  | 
 | ||||||
| 	"github.com/codegangsta/martini" | 	"github.com/codegangsta/martini" | ||||||
|  | 
 | ||||||
| 	"github.com/gogits/gogs/models" | 	"github.com/gogits/gogs/models" | ||||||
| 	"github.com/gogits/gogs/modules/middleware" | 	"github.com/gogits/gogs/modules/middleware" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func Commits(ctx *middleware.Context, params martini.Params) { | func Commits(ctx *middleware.Context, params martini.Params) { | ||||||
| 	brs, err := models.GetBranches(params["username"], params["reponame"]) | 	userName := params["username"] | ||||||
|  | 	repoName := params["reponame"] | ||||||
|  | 	branchName := params["branchname"] | ||||||
|  | 
 | ||||||
|  | 	brs, err := models.GetBranches(userName, repoName) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		ctx.Handle(200, "repo.Commits", err) | 		ctx.Handle(200, "repo.Commits", err) | ||||||
| 		return | 		return | ||||||
|  | @ -20,21 +29,28 @@ func Commits(ctx *middleware.Context, params martini.Params) { | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	ctx.Data["IsRepoToolbarCommits"] = true | 	var commits *list.List | ||||||
| 	commits, err := models.GetCommits(params["username"], | 	if models.IsBranchExist(userName, repoName, branchName) { | ||||||
| 		params["reponame"], params["branchname"]) | 		commits, err = models.GetCommitsByBranch(userName, repoName, branchName) | ||||||
|  | 	} else { | ||||||
|  | 		commits, err = models.GetCommitsByCommitId(userName, repoName, branchName) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		ctx.Handle(404, "repo.Commits", nil) | 		ctx.Handle(404, "repo.Commits", nil) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	ctx.Data["Username"] = params["username"] | 
 | ||||||
| 	ctx.Data["Reponame"] = params["reponame"] | 	ctx.Data["Username"] = userName | ||||||
|  | 	ctx.Data["Reponame"] = repoName | ||||||
| 	ctx.Data["CommitCount"] = commits.Len() | 	ctx.Data["CommitCount"] = commits.Len() | ||||||
| 	ctx.Data["Commits"] = commits | 	ctx.Data["Commits"] = commits | ||||||
|  | 	ctx.Data["IsRepoToolbarCommits"] = true | ||||||
| 	ctx.HTML(200, "repo/commits") | 	ctx.HTML(200, "repo/commits") | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func Diff(ctx *middleware.Context, params martini.Params) { | func Diff(ctx *middleware.Context, params martini.Params) { | ||||||
|  | 	fmt.Println(params["branchname"]) | ||||||
| 	commit, err := models.GetCommit(params["username"], params["reponame"], params["branchname"], params["commitid"]) | 	commit, err := models.GetCommit(params["username"], params["reponame"], params["branchname"], params["commitid"]) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		ctx.Handle(404, "repo.Diff", err) | 		ctx.Handle(404, "repo.Diff", err) | ||||||
|  | @ -47,11 +63,12 @@ func Diff(ctx *middleware.Context, params martini.Params) { | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	shortSha := params["commitid"][:7] | 	shortSha := params["commitid"][:10] | ||||||
| 	ctx.Data["Title"] = commit.Message() + " · " + shortSha | 	ctx.Data["Title"] = commit.Message() + " · " + shortSha | ||||||
| 	ctx.Data["Commit"] = commit | 	ctx.Data["Commit"] = commit | ||||||
| 	ctx.Data["ShortSha"] = shortSha | 	ctx.Data["ShortSha"] = shortSha | ||||||
| 	ctx.Data["Diff"] = diff | 	ctx.Data["Diff"] = diff | ||||||
| 	ctx.Data["IsRepoToolbarCommits"] = true | 	ctx.Data["IsRepoToolbarCommits"] = true | ||||||
|  | 	ctx.Data["SourcePath"] = "/" + path.Join(params["username"], params["reponame"], "src", params["commitid"]) | ||||||
| 	ctx.HTML(200, "repo/diff") | 	ctx.HTML(200, "repo/diff") | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -57,19 +57,23 @@ func Single(ctx *middleware.Context, params martini.Params) { | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	branchName := params["branchname"] | ||||||
|  | 	userName := params["username"] | ||||||
|  | 	repoName := params["reponame"] | ||||||
|  | 
 | ||||||
| 	// Get tree path
 | 	// Get tree path
 | ||||||
| 	treename := params["_1"] | 	treename := params["_1"] | ||||||
| 
 | 
 | ||||||
| 	if len(treename) > 0 && treename[len(treename)-1] == '/' { | 	if len(treename) > 0 && treename[len(treename)-1] == '/' { | ||||||
| 		ctx.Redirect("/" + ctx.Repo.Owner.LowerName + "/" + | 		ctx.Redirect("/" + ctx.Repo.Owner.LowerName + "/" + | ||||||
| 			ctx.Repo.Repository.Name + "/src/" + params["branchname"] + "/" + treename[:len(treename)-1]) | 			ctx.Repo.Repository.Name + "/src/" + branchName + "/" + treename[:len(treename)-1]) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	ctx.Data["IsRepoToolbarSource"] = true | 	ctx.Data["IsRepoToolbarSource"] = true | ||||||
| 
 | 
 | ||||||
| 	// Branches.
 | 	// Branches.
 | ||||||
| 	brs, err := models.GetBranches(params["username"], params["reponame"]) | 	brs, err := models.GetBranches(userName, repoName) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		ctx.Handle(404, "repo.Single(GetBranches)", err) | 		ctx.Handle(404, "repo.Single(GetBranches)", err) | ||||||
| 		return | 		return | ||||||
|  | @ -80,15 +84,20 @@ func Single(ctx *middleware.Context, params martini.Params) { | ||||||
| 	} | 	} | ||||||
| 	ctx.Data["Branches"] = brs | 	ctx.Data["Branches"] = brs | ||||||
| 
 | 
 | ||||||
| 	repoFile, err := models.GetTargetFile(params["username"], params["reponame"], | 	var commitId string | ||||||
| 		params["branchname"], params["commitid"], treename) | 	if !models.IsBranchExist(userName, repoName, branchName) { | ||||||
|  | 		commitId = branchName | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	repoFile, err := models.GetTargetFile(userName, repoName, | ||||||
|  | 		branchName, commitId, treename) | ||||||
| 	if err != nil && err != models.ErrRepoFileNotExist { | 	if err != nil && err != models.ErrRepoFileNotExist { | ||||||
| 		ctx.Handle(404, "repo.Single(GetTargetFile)", err) | 		ctx.Handle(404, "repo.Single(GetTargetFile)", err) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	branchLink := "/" + ctx.Repo.Owner.LowerName + "/" + ctx.Repo.Repository.Name + "/src/" + params["branchname"] | 	branchLink := "/" + ctx.Repo.Owner.LowerName + "/" + ctx.Repo.Repository.Name + "/src/" + branchName | ||||||
| 	rawLink := "/" + ctx.Repo.Owner.LowerName + "/" + ctx.Repo.Repository.Name + "/raw/" + params["branchname"] | 	rawLink := "/" + ctx.Repo.Owner.LowerName + "/" + ctx.Repo.Repository.Name + "/raw/" + branchName | ||||||
| 
 | 
 | ||||||
| 	if len(treename) != 0 && repoFile == nil { | 	if len(treename) != 0 && repoFile == nil { | ||||||
| 		ctx.Handle(404, "repo.Single", nil) | 		ctx.Handle(404, "repo.Single", nil) | ||||||
|  | @ -126,8 +135,8 @@ func Single(ctx *middleware.Context, params martini.Params) { | ||||||
| 
 | 
 | ||||||
| 	} else { | 	} else { | ||||||
| 		// Directory and file list.
 | 		// Directory and file list.
 | ||||||
| 		files, err := models.GetReposFiles(params["username"], params["reponame"], | 		files, err := models.GetReposFiles(userName, repoName, | ||||||
| 			params["branchname"], params["commitid"], treename) | 			branchName, commitId, treename) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			ctx.Handle(404, "repo.Single(GetReposFiles)", err) | 			ctx.Handle(404, "repo.Single(GetReposFiles)", err) | ||||||
| 			return | 			return | ||||||
|  | @ -166,8 +175,8 @@ func Single(ctx *middleware.Context, params martini.Params) { | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	ctx.Data["Username"] = params["username"] | 	ctx.Data["Username"] = userName | ||||||
| 	ctx.Data["Reponame"] = params["reponame"] | 	ctx.Data["Reponame"] = repoName | ||||||
| 
 | 
 | ||||||
| 	var treenames []string | 	var treenames []string | ||||||
| 	Paths := make([]string, 0) | 	Paths := make([]string, 0) | ||||||
|  | @ -185,8 +194,8 @@ func Single(ctx *middleware.Context, params martini.Params) { | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// Get latest commit according username and repo name.
 | 	// Get latest commit according username and repo name.
 | ||||||
| 	commit, err := models.GetCommit(params["username"], params["reponame"], | 	commit, err := models.GetCommit(userName, repoName, | ||||||
| 		params["branchname"], params["commitid"]) | 		branchName, commitId) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Error("repo.Single(GetCommit): %v", err) | 		log.Error("repo.Single(GetCommit): %v", err) | ||||||
| 		ctx.Handle(404, "repo.Single(GetCommit)", err) | 		ctx.Handle(404, "repo.Single(GetCommit)", err) | ||||||
|  | @ -194,6 +203,8 @@ func Single(ctx *middleware.Context, params martini.Params) { | ||||||
| 	} | 	} | ||||||
| 	ctx.Data["LastCommit"] = commit | 	ctx.Data["LastCommit"] = commit | ||||||
| 
 | 
 | ||||||
|  | 	ctx.Data["CommitId"] = commitId | ||||||
|  | 
 | ||||||
| 	ctx.Data["Paths"] = Paths | 	ctx.Data["Paths"] = Paths | ||||||
| 	ctx.Data["Treenames"] = treenames | 	ctx.Data["Treenames"] = treenames | ||||||
| 	ctx.Data["BranchLink"] = branchLink | 	ctx.Data["BranchLink"] = branchLink | ||||||
|  |  | ||||||
|  | @ -27,7 +27,7 @@ | ||||||
|                 {{range $r}} |                 {{range $r}} | ||||||
|                 <tr> |                 <tr> | ||||||
|                     <td class="author"><img class="avatar" src="{{AvatarLink .Committer.Email}}" alt=""/><a href="/user/{{.Committer.Name}}">{{.Committer.Name}}</a></td> |                     <td class="author"><img class="avatar" src="{{AvatarLink .Committer.Email}}" alt=""/><a href="/user/{{.Committer.Name}}">{{.Committer.Name}}</a></td> | ||||||
|                     <td class="sha"><a class="label label-success" href="/{{$username}}/{{$reponame}}/commit/{{.Id}} ">{{SubStr .Id.String 0 7}} </a></td> |                     <td class="sha"><a class="label label-success" href="/{{$username}}/{{$reponame}}/commit/{{.Id}} ">{{SubStr .Id.String 0 10}} </a></td> | ||||||
|                     <td class="message">{{.Message}} </td> |                     <td class="message">{{.Message}} </td> | ||||||
|                     <td class="date">{{TimeSince .Committer.When}}</td> |                     <td class="date">{{TimeSince .Committer.When}}</td> | ||||||
|                 </tr> |                 </tr> | ||||||
|  | @ -37,4 +37,4 @@ | ||||||
|         </div> |         </div> | ||||||
|     </div> |     </div> | ||||||
| </div> | </div> | ||||||
| {{template "base/footer" .}} | {{template "base/footer" .}} | ||||||
|  |  | ||||||
|  | @ -6,7 +6,7 @@ | ||||||
|     <div id="source"> |     <div id="source"> | ||||||
|         <div class="panel panel-info diff-box diff-head-box"> |         <div class="panel panel-info diff-box diff-head-box"> | ||||||
|             <div class="panel-heading"> |             <div class="panel-heading"> | ||||||
|                 <a class="pull-right btn btn-primary btn-sm" href="#commit-source">Browse Source</a> |                 <a class="pull-right btn btn-primary btn-sm" href="{{.SourcePath}}">Browse Source</a> | ||||||
|                 <h4>{{.Commit.Message}}</h4> |                 <h4>{{.Commit.Message}}</h4> | ||||||
|             </div> |             </div> | ||||||
|             <div class="panel-body"> |             <div class="panel-body"> | ||||||
|  | @ -57,7 +57,7 @@ | ||||||
|                     </span> |                     </span> | ||||||
|                     <span class="del" data-line="{{.Deletion}}">- {{.Deletion}}</span> |                     <span class="del" data-line="{{.Deletion}}">- {{.Deletion}}</span> | ||||||
|                 </div> |                 </div> | ||||||
|                 <a class="btn btn-default btn-sm pull-right" href="#">View File</a> |                 <a class="btn btn-default btn-sm pull-right" href="{{$.SourcePath}}/{{.Name}}">View File</a> | ||||||
|                 <span class="file">{{.Name}}</span> |                 <span class="file">{{.Name}}</span> | ||||||
|             </div> |             </div> | ||||||
|             <div class="panel-body file-body file-code code-view code-diff"> |             <div class="panel-body file-body file-code code-view code-diff"> | ||||||
|  | @ -411,4 +411,4 @@ | ||||||
|         </div> --> |         </div> --> | ||||||
|     </div> |     </div> | ||||||
| </div> | </div> | ||||||
| {{template "base/footer" .}} | {{template "base/footer" .}} | ||||||
|  |  | ||||||
|  | @ -11,7 +11,7 @@ | ||||||
|             {{ $n := len .Treenames}} |             {{ $n := len .Treenames}} | ||||||
|             {{if not .IsFile}}<button class="btn btn-default pull-right hidden"><i class="fa fa-plus-square"></i>Add File</button>{{end}} |             {{if not .IsFile}}<button class="btn btn-default pull-right hidden"><i class="fa fa-plus-square"></i>Add File</button>{{end}} | ||||||
|             <div class="dropdown branch-switch"> |             <div class="dropdown branch-switch"> | ||||||
|                 <a href="#" class="btn btn-success dropdown-toggle" data-toggle="dropdown"><i class="fa fa-chain"></i>{{.Branchname}}   |                 <a href="#" class="btn btn-success dropdown-toggle" data-toggle="dropdown"><i class="fa fa-chain"></i>{{if .CommitId}}{{SubStr .CommitId 0 10}}{{else}}{{.Branchname}}{{end}}   | ||||||
|                     <b class="caret"></b></a> |                     <b class="caret"></b></a> | ||||||
|                 <ul class="dropdown-menu"> |                 <ul class="dropdown-menu"> | ||||||
|                     {{range .Branches}} |                     {{range .Branches}} | ||||||
|  | @ -41,4 +41,4 @@ | ||||||
|         {{end}} |         {{end}} | ||||||
|     </div> |     </div> | ||||||
| </div> | </div> | ||||||
| {{template "base/footer" .}} | {{template "base/footer" .}} | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue