Browse Source

update git api. fix link... and so on

slene 11 years ago
parent
commit
b27c34f39a

+ 19 - 7
models/git.go

@@ -70,9 +70,12 @@ func GetTargetFile(userName, repoName, branchName, commitId, rpath string) (*Rep
 		return nil, err
 	}
 
-	commit, err := repo.GetCommit(branchName, commitId)
+	commit, err := repo.GetCommitOfBranch(branchName)
 	if err != nil {
-		return nil, err
+		commit, err = repo.GetCommit(commitId)
+		if err != nil {
+			return nil, err
+		}
 	}
 
 	parts := strings.Split(path.Clean(rpath), "/")
@@ -110,13 +113,22 @@ func GetTargetFile(userName, repoName, branchName, commitId, rpath string) (*Rep
 }
 
 // GetReposFiles returns a list of file object in given directory of repository.
-func GetReposFiles(userName, repoName, branchName, commitId, rpath string) ([]*RepoFile, error) {
+// func GetReposFilesOfBranch(userName, repoName, branchName, rpath string) ([]*RepoFile, error) {
+// 	return getReposFiles(userName, repoName, commitId, rpath)
+// }
+
+// GetReposFiles returns a list of file object in given directory of repository.
+func GetReposFiles(userName, repoName, commitId, rpath string) ([]*RepoFile, error) {
+	return getReposFiles(userName, repoName, commitId, rpath)
+}
+
+func getReposFiles(userName, repoName, commitId string, rpath string) ([]*RepoFile, error) {
 	repo, err := git.OpenRepository(RepoPath(userName, repoName))
 	if err != nil {
 		return nil, err
 	}
 
-	commit, err := repo.GetCommit(branchName, commitId)
+	commit, err := repo.GetCommit(commitId)
 	if err != nil {
 		return nil, err
 	}
@@ -216,13 +228,13 @@ func GetReposFiles(userName, repoName, branchName, commitId, rpath string) ([]*R
 	return append(repodirs, repofiles...), nil
 }
 
-func GetCommit(userName, repoName, branchname, commitid string) (*git.Commit, error) {
+func GetCommit(userName, repoName, commitId string) (*git.Commit, error) {
 	repo, err := git.OpenRepository(RepoPath(userName, repoName))
 	if err != nil {
 		return nil, err
 	}
 
-	return repo.GetCommit(branchname, commitid)
+	return repo.GetCommit(commitId)
 }
 
 // GetCommitsByBranch returns all commits of given branch of repository.
@@ -397,7 +409,7 @@ func GetDiff(repoPath, commitid string) (*Diff, error) {
 		return nil, err
 	}
 
-	commit, err := repo.GetCommit("", commitid)
+	commit, err := repo.GetCommit(commitid)
 	if err != nil {
 		return nil, err
 	}

+ 1 - 1
models/repo.go

@@ -364,7 +364,7 @@ func GetRepos(num, offset int) ([]UserRepo, error) {
 }
 
 func RepoPath(userName, repoName string) string {
-	return filepath.Join(UserPath(userName), repoName+".git")
+	return filepath.Join(UserPath(userName), strings.ToLower(repoName)+".git")
 }
 
 func UpdateRepository(repo *Repository) error {

+ 2 - 8
routers/repo/branch.go

@@ -11,21 +11,15 @@ import (
 )
 
 func Branches(ctx *middleware.Context, params martini.Params) {
-	if !ctx.Repo.IsValid {
-		return
-	}
-
-	brs, err := models.GetBranches(params["username"], params["reponame"])
+	brs, err := models.GetBranches(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
 	if err != nil {
-		ctx.Handle(200, "repo.Branches", err)
+		ctx.Handle(404, "repo.Branches", err)
 		return
 	} else if len(brs) == 0 {
 		ctx.Handle(404, "repo.Branches", nil)
 		return
 	}
 
-	ctx.Data["Username"] = params["username"]
-	ctx.Data["Reponame"] = params["reponame"]
 	ctx.Data["Branches"] = brs
 	ctx.Data["IsRepoToolbarBranches"] = true
 

+ 6 - 12
routers/repo/commit.go

@@ -50,16 +50,12 @@ func Commits(ctx *middleware.Context, params martini.Params) {
 }
 
 func Diff(ctx *middleware.Context, params martini.Params) {
-	userName := params["username"]
-	repoName := params["reponame"]
-	branchName := params["branchname"]
-	commitId := params["commitid"]
+	userName := ctx.Repo.Owner.Name
+	repoName := ctx.Repo.Repository.Name
+	branchName := ctx.Repo.BranchName
+	commitId := ctx.Repo.CommitId
 
-	commit, err := models.GetCommit(userName, repoName, branchName, commitId)
-	if err != nil {
-		ctx.Handle(404, "repo.Diff", err)
-		return
-	}
+	commit := ctx.Repo.Commit
 
 	diff, err := models.GetDiff(models.RepoPath(userName, repoName), commitId)
 	if err != nil {
@@ -85,11 +81,9 @@ func Diff(ctx *middleware.Context, params martini.Params) {
 		return isImage
 	}
 
-	shortSha := params["commitid"][:10]
 	ctx.Data["IsImageFile"] = isImageFile
-	ctx.Data["Title"] = commit.Message() + " · " + shortSha
+	ctx.Data["Title"] = commit.Message() + " · " + base.ShortSha(commitId)
 	ctx.Data["Commit"] = commit
-	ctx.Data["ShortSha"] = shortSha
 	ctx.Data["Diff"] = diff
 	ctx.Data["IsRepoToolbarCommits"] = true
 	ctx.Data["SourcePath"] = "/" + path.Join(userName, repoName, "src", commitId)

+ 12 - 39
routers/repo/repo.go

@@ -53,20 +53,20 @@ func Create(ctx *middleware.Context, form auth.CreateRepoForm) {
 }
 
 func Single(ctx *middleware.Context, params martini.Params) {
-	if !ctx.Repo.IsValid {
-		return
-	}
+	branchName := ctx.Repo.BranchName
+	commitId := ctx.Repo.CommitId
+	userName := ctx.Repo.Owner.Name
+	repoName := ctx.Repo.Repository.Name
 
-	branchName := params["branchname"]
-	userName := params["username"]
-	repoName := params["reponame"]
+	repoLink := ctx.Repo.RepoLink
+	branchLink := ctx.Repo.RepoLink + "/src/" + branchName
+	rawLink := ctx.Repo.RepoLink + "/raw/" + branchName
 
 	// Get tree path
 	treename := params["_1"]
 
 	if len(treename) > 0 && treename[len(treename)-1] == '/' {
-		ctx.Redirect("/" + ctx.Repo.Owner.LowerName + "/" +
-			ctx.Repo.Repository.Name + "/src/" + branchName + "/" + treename[:len(treename)-1])
+		ctx.Redirect(repoLink + "/src/" + branchName + "/" + treename[:len(treename)-1])
 		return
 	}
 
@@ -84,23 +84,17 @@ func Single(ctx *middleware.Context, params martini.Params) {
 	}
 	ctx.Data["Branches"] = brs
 
-	var commitId string
-	isViewBranch := models.IsBranchExist(userName, repoName, branchName)
-	if !isViewBranch {
-		commitId = branchName
-	}
+	isViewBranch := ctx.Repo.IsBranch
 	ctx.Data["IsViewBranch"] = isViewBranch
 
 	repoFile, err := models.GetTargetFile(userName, repoName,
 		branchName, commitId, treename)
+
 	if err != nil && err != models.ErrRepoFileNotExist {
 		ctx.Handle(404, "repo.Single(GetTargetFile)", err)
 		return
 	}
 
-	branchLink := "/" + ctx.Repo.Owner.LowerName + "/" + ctx.Repo.Repository.Name + "/src/" + branchName
-	rawLink := "/" + ctx.Repo.Owner.LowerName + "/" + ctx.Repo.Repository.Name + "/raw/" + branchName
-
 	if len(treename) != 0 && repoFile == nil {
 		ctx.Handle(404, "repo.Single", nil)
 		return
@@ -142,8 +136,7 @@ func Single(ctx *middleware.Context, params martini.Params) {
 
 	} else {
 		// Directory and file list.
-		files, err := models.GetReposFiles(userName, repoName,
-			branchName, commitId, treename)
+		files, err := models.GetReposFiles(userName, repoName, ctx.Repo.CommitId, treename)
 		if err != nil {
 			ctx.Handle(404, "repo.Single(GetReposFiles)", err)
 			return
@@ -200,18 +193,7 @@ func Single(ctx *middleware.Context, params martini.Params) {
 		}
 	}
 
-	// Get latest commit according username and repo name.
-	commit, err := models.GetCommit(userName, repoName,
-		branchName, commitId)
-	if err != nil {
-		log.Error("repo.Single(GetCommit): %v", err)
-		ctx.Handle(404, "repo.Single(GetCommit)", err)
-		return
-	}
-	ctx.Data["LastCommit"] = commit
-
-	ctx.Data["CommitId"] = commitId
-
+	ctx.Data["LastCommit"] = ctx.Repo.Commit
 	ctx.Data["Paths"] = Paths
 	ctx.Data["Treenames"] = treenames
 	ctx.Data["BranchLink"] = branchLink
@@ -219,11 +201,6 @@ func Single(ctx *middleware.Context, params martini.Params) {
 }
 
 func SingleDownload(ctx *middleware.Context, params martini.Params) {
-	if !ctx.Repo.IsValid {
-		ctx.Handle(404, "repo.SingleDownload", nil)
-		return
-	}
-
 	// Get tree path
 	treename := params["_1"]
 
@@ -263,10 +240,6 @@ func SingleDownload(ctx *middleware.Context, params martini.Params) {
 }
 
 func Http(ctx *middleware.Context, params martini.Params) {
-	/*if !ctx.Repo.IsValid {
-		return
-	}*/
-
 	// TODO: access check
 
 	username := params["username"]

+ 2 - 2
templates/issue/create.tmpl

@@ -4,7 +4,7 @@
 {{template "repo/toolbar" .}}
 <div id="body" class="container">
     <div id="issue">
-        <form class="form" action="/{{.RepositoryLink}}/issues/new" method="post" id="issue-create-form">
+        <form class="form" action="{{.RepoLink}}/issues/new" method="post" id="issue-create-form">
             {{.CsrfTokenHtml}}
             <div class="col-md-1">
                 <img class="avatar" src="{{.SignedUser.AvatarLink}}" alt=""/>
@@ -40,4 +40,4 @@
         </form>
     </div>
 </div>
-{{template "base/footer" .}}
+{{template "base/footer" .}}

+ 6 - 6
templates/issue/list.tmpl

@@ -6,24 +6,24 @@
     <div id="issue">
         <div class="col-md-3 filter-list">
             <ul class="list-unstyled">
-                <li><a href="/{{.RepositoryLink}}/issues"{{if eq .ViewType "all"}} class="active"{{end}}>All Issues <strong class="pull-right">{{.IssueCount}}</strong></a></li>
+                <li><a href="{{.RepoLink}}/issues"{{if eq .ViewType "all"}} class="active"{{end}}>All Issues <strong class="pull-right">{{.IssueCount}}</strong></a></li>
                 <!-- <li><a href="#">Assigned to you</a></li> -->
-                <li><a href="/{{.RepositoryLink}}/issues?type=created_by"{{if eq .ViewType "created_by"}} class="active"{{end}}>Created by you <strong class="pull-right">{{.IssueCreatedCount}}</strong></a></li>
+                <li><a href="{{.RepoLink}}/issues?type=created_by"{{if eq .ViewType "created_by"}} class="active"{{end}}>Created by you <strong class="pull-right">{{.IssueCreatedCount}}</strong></a></li>
                 <!-- <li><a href="#">Mentioned</a></li> -->
             </ul>
         </div>
         <div class="col-md-9">
             <div class="filter-option">
                 <div class="btn-group">
-                    <a class="btn btn-default issue-open{{if not .IsShowClosed}} active{{end}}" href="/{{.RepositoryLink}}/issues?type={{.ViewType}}">{{.OpenCount}} Open</a>
-                    <a class="btn btn-default issue-close{{if .IsShowClosed}} active{{end}}" href="/{{.RepositoryLink}}/issues?state=closed&type={{.ViewType}}">{{.ClosedCount}} Closed</a>
+                    <a class="btn btn-default issue-open{{if not .IsShowClosed}} active{{end}}" href="{{.RepoLink}}/issues?type={{.ViewType}}">{{.OpenCount}} Open</a>
+                    <a class="btn btn-default issue-close{{if .IsShowClosed}} active{{end}}" href="{{.RepoLink}}/issues?state=closed&type={{.ViewType}}">{{.ClosedCount}} Closed</a>
                 </div>
             </div>
             <div class="issues list-group">
                 {{range .Issues}}
                 <div class="list-group-item issue-item" id="issue-{{.Id}}">
                     <span class="number pull-right">#{{.Index}}</span>
-                    <h5 class="title"><a href="/{{$.RepositoryLink}}/issues/{{.Index}}">{{.Name}}</a></h5>
+                    <h5 class="title"><a href="{{$.RepoLink}}/issues/{{.Index}}">{{.Name}}</a></h5>
                     <p class="info">
                         <span class="author"><img class="avatar" src="{{.Poster.AvatarLink}}" alt="" width="20"/>
                         <a href="/user/{{.Poster.Name}}">{{.Poster.Name}}</a></span>
@@ -37,4 +37,4 @@
         </div>
     </div>
 </div>
-{{template "base/footer" .}}
+{{template "base/footer" .}}

+ 2 - 2
templates/issue/view.tmpl

@@ -64,7 +64,7 @@
                 <hr class="issue-line"/>
                 {{if .SignedUser}}<div class="issue-child issue-reply">
                     <a class="user pull-left" href="/user/{{.SignedUser.Name}}"><img class="avatar" src="{{.SignedUser.AvatarLink}}" alt=""/></a>
-                    <form class="panel panel-default issue-content" action="/{{.RepositoryLink}}/comment/new" method="post">
+                    <form class="panel panel-default issue-content" action="{{.RepoLink}}/comment/new" method="post">
                         {{.CsrfTokenHtml}}
                         <div class="panel-body">
                             <div class="form-group">
@@ -102,4 +102,4 @@
         </div>
     </div>
 </div>
-{{template "base/footer" .}}
+{{template "base/footer" .}}

+ 1 - 2
templates/repo/diff.tmpl

@@ -1,7 +1,6 @@
 {{template "base/head" .}}
 {{template "base/navbar" .}}
 {{template "repo/nav" .}}
-{{template "repo/toolbar" .}}
 <div id="body" class="container" data-page="repo">
     <div id="source">
         <div class="panel panel-info diff-box diff-head-box">
@@ -11,7 +10,7 @@
             </div>
             <div class="panel-body">
                 <span class="pull-right">
-                    commit <span class="label label-default sha">{{.ShortSha}}</span>
+                    commit <span class="label label-default sha">{{ShortSha .CommitId}}</span>
                 </span>
                 <p class="author">
                     <img class="avatar" src="{{AvatarLink .Commit.Author.Email}}" alt=""/>

+ 2 - 2
templates/repo/single.tmpl

@@ -11,11 +11,11 @@
             {{ $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}}
             <div class="dropdown branch-switch">
-                <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}}&nbsp;&nbsp;
+                <a href="#" class="btn btn-success dropdown-toggle" data-toggle="dropdown"><i class="fa fa-chain"></i>{{if .CommitId}}{{ShortSha .CommitId}}{{else}}{{.BranchName}}{{end}}&nbsp;&nbsp;
                     <b class="caret"></b></a>
                 <ul class="dropdown-menu">
                     {{range .Branches}}
-                    <li><a {{if eq . $.Branchname}}class="current" {{end}}href="/{{$.Username}}/{{$.Reponame}}/src/{{.}}">{{.}}</a></li>
+                    <li><a {{if eq . $.BranchName}}class="current" {{end}}href="/{{$.Username}}/{{$.Reponame}}/src/{{.}}">{{.}}</a></li>
                     {{end}}
                 </ul>
             </div>

+ 10 - 10
templates/repo/toolbar.tmpl

@@ -3,24 +3,24 @@
         <nav class="navbar navbar-toolbar navbar-default" role="navigation">
             <div class="collapse navbar-collapse">
                 <ul class="nav navbar-nav">
-                    <li class="{{if .IsRepoToolbarSource}}active{{end}}"><a href="/{{.RepositoryLink}}">Source</a></li>
+                    <li class="{{if .IsRepoToolbarSource}}active{{end}}"><a href="{{.RepoLink}}{{if ne .BranchName `master`}}/src/{{.BranchName}}{{end}}">Source</a></li>
                     {{if not .IsBareRepo}}
-                    <li class="{{if .IsRepoToolbarCommits}}active{{end}}"><a href="/{{.RepositoryLink}}/commits/{{.Branchname}}">Commits</a></li>
-                    <!-- <li class="{{if .IsRepoToolbarBranches}}active{{end}}"><a href="/{{.RepositoryLink}}/branches">Branches</a></li> -->
-                    <!-- <li class="{{if .IsRepoToolbarPulls}}active{{end}}"><a href="/{{.RepositoryLink}}/pulls">Pull Requests</a></li> -->
-                    <li class="{{if .IsRepoToolbarIssues}}active{{end}}"><a href="/{{.RepositoryLink}}/issues">Issues <!--<span class="badge">42</span>--></a></li>
+                    <li class="{{if .IsRepoToolbarCommits}}active{{end}}"><a href="{{.RepoLink}}/commits/{{.BranchName}}">Commits</a></li>
+                    <!-- <li class="{{if .IsRepoToolbarBranches}}active{{end}}"><a href="{{.RepoLink}}/branches">Branches</a></li> -->
+                    <!-- <li class="{{if .IsRepoToolbarPulls}}active{{end}}"><a href="{{.RepoLink}}/pulls">Pull Requests</a></li> -->
+                    <li class="{{if .IsRepoToolbarIssues}}active{{end}}"><a href="{{.RepoLink}}/issues">Issues <!--<span class="badge">42</span>--></a></li>
                     {{if .IsRepoToolbarIssues}}
-                    <li class="tmp">{{if .IsRepoToolbarIssuesList}}<a href="/{{.RepositoryLink}}/issues/new">
+                    <li class="tmp">{{if .IsRepoToolbarIssuesList}}<a href="{{.RepoLink}}/issues/new">
                         <button class="btn btn-primary btn-sm">New Issue</button>
-                    </a>{{else}}<a href="/{{.RepositoryLink}}/issues">
+                    </a>{{else}}<a href="{{.RepoLink}}/issues">
                         <button class="btn btn-primary btn-sm">Issues List</button>
                     </a>{{end}}</li>
                     {{end}}
                     <!-- <li class="dropdown">
                         <a href="#" class="dropdown-toggle" data-toggle="dropdown">More <b class="caret"></b></a>
                         <ul class="dropdown-menu">
-                            <li><a href="/{{.RepositoryLink}}/release">Release</a></li>
-                            <li><a href="//{{.RepositoryLink}}/wiki">Wiki</a></li>
+                            <li><a href="{{.RepoLink}}/release">Release</a></li>
+                            <li><a href="{{.RepoLink}}/wiki">Wiki</a></li>
                         </ul>
                     </li> -->{{end}}
                 </ul>
@@ -34,7 +34,7 @@
                             <li><a href="#">Network</a></li>
                         </ul>
                     </li> -->{{end}}{{if .IsRepositoryOwner}}
-                    <li class="{{if .IsRepoToolbarSetting}}active{{end}}"><a href="/{{.RepositoryLink}}/settings">Settings</a>
+                    <li class="{{if .IsRepoToolbarSetting}}active{{end}}"><a href="{{.RepoLink}}/settings">Settings</a>
                     </li>{{end}}
                 </ul>
             </div>