]> source.dussan.org Git - gitea.git/commitdiff
add commit view
authorLunny Xiao <xiaolunwen@gmail.com>
Tue, 18 Mar 2014 03:22:19 +0000 (11:22 +0800)
committerLunny Xiao <xiaolunwen@gmail.com>
Tue, 18 Mar 2014 03:22:19 +0000 (11:22 +0800)
models/repo.go
routers/repo/single.go
templates/repo/single.tmpl
web.go

index 6e3754acf5dcf4a2d616eaa0b7d600778bfc95a7..c37fb4de20bac09d3e547d0a2540966448f8d5a1 100644 (file)
@@ -416,12 +416,10 @@ var (
 // RepoFile represents a file object in git repository.
 type RepoFile struct {
        *git.TreeEntry
-       Path       string
-       Message    string
-       Created    time.Time
-       Size       int64
-       Repo       *git.Repository
-       LastCommit string
+       Path   string
+       Size   int64
+       Repo   *git.Repository
+       Commit *git.Commit
 }
 
 // LookupBlob returns the content of an object.
@@ -453,32 +451,28 @@ func GetBranches(userName, reposName string) ([]string, error) {
 }
 
 // GetReposFiles returns a list of file object in given directory of repository.
-func GetReposFiles(userName, reposName, branchName, rpath string) ([]*RepoFile, error) {
+func GetReposFiles(userName, reposName, branchName, commitId, rpath string) ([]*RepoFile, error) {
        repo, err := git.OpenRepository(RepoPath(userName, reposName))
        if err != nil {
                return nil, err
        }
 
-       ref, err := repo.LookupReference("refs/heads/" + branchName)
-       if err != nil {
-               return nil, err
-       }
-
-       lastCommit, err := repo.LookupCommit(ref.Oid)
+       commit, err := GetCommit(userName, reposName, branchName, commitId)
        if err != nil {
                return nil, err
        }
 
        var repodirs []*RepoFile
        var repofiles []*RepoFile
-       lastCommit.Tree.Walk(func(dirname string, entry *git.TreeEntry) int {
+       commit.Tree.Walk(func(dirname string, entry *git.TreeEntry) int {
                if dirname == rpath {
+                       // TODO: size get method shoule be improved
                        size, err := repo.ObjectSize(entry.Id)
                        if err != nil {
                                return 0
                        }
 
-                       var cm = lastCommit
+                       var cm = commit
 
                        for {
                                if cm.ParentCount() == 0 {
@@ -533,11 +527,9 @@ func GetReposFiles(userName, reposName, branchName, rpath string) ([]*RepoFile,
                        rp := &RepoFile{
                                entry,
                                path.Join(dirname, entry.Name),
-                               cm.Message(),
-                               cm.Committer.When,
                                size,
                                repo,
-                               cm.Id().String(),
+                               cm,
                        }
 
                        if entry.IsFile() {
@@ -552,6 +544,31 @@ func GetReposFiles(userName, reposName, branchName, rpath string) ([]*RepoFile,
        return append(repodirs, repofiles...), nil
 }
 
+func GetCommit(userName, repoName, branchname, commitid string) (*git.Commit, error) {
+       repo, err := git.OpenRepository(RepoPath(userName, repoName))
+       if err != nil {
+               return nil, err
+       }
+
+       if commitid != "" {
+               oid, err := git.NewOidFromString(commitid)
+               if err != nil {
+                       return nil, err
+               }
+               return repo.LookupCommit(oid)
+       }
+       if branchname == "" {
+               return nil, errors.New("no branch name and no commit id")
+       }
+
+       r, err := repo.LookupReference(fmt.Sprintf("refs/heads/%s", branchname))
+       if err != nil {
+               return nil, err
+       }
+       return r.LastCommit()
+}
+
+/*
 // GetLastestCommit returns the latest commit of given repository.
 func GetLastestCommit(userName, repoName string) (*Commit, error) {
        stdout, _, err := com.ExecCmd("git", "--git-dir="+RepoPath(userName, repoName), "log", "-1")
@@ -581,7 +598,7 @@ func GetLastestCommit(userName, repoName string) (*Commit, error) {
                }
        }
        return commit, nil
-}
+}*/
 
 // GetCommits returns all commits of given branch of repository.
 func GetCommits(userName, reposName, branchname string) ([]*git.Commit, error) {
index 1b5da9d30732d994cc6efdcb13007a65b020b07b..2ed5cfed23f733600f99419f0379f12e67b51cd9 100644 (file)
@@ -69,7 +69,7 @@ func Single(ctx *middleware.Context, params martini.Params) {
 
        // Directory and file list.
        files, err := models.GetReposFiles(params["username"], params["reponame"],
-               params["branchname"], treename)
+               params["branchname"], params["commitid"], treename)
        if err != nil {
                log.Error("repo.Single(GetReposFiles): %v", err)
                ctx.Render.Error(404)
@@ -90,13 +90,14 @@ func Single(ctx *middleware.Context, params martini.Params) {
        }
 
        // Get latest commit according username and repo name
-       commit, err := models.GetLastestCommit(params["username"], params["reponame"])
+       commit, err := models.GetCommit(params["username"], params["reponame"],
+               params["branchname"], params["commitid"])
        if err != nil {
-               log.Error("repo.Single(GetLastestCommit): %v", err)
+               log.Error("repo.Single(GetCommit): %v", err)
                ctx.Render.Error(404)
                return
        }
-       ctx.Data["LatestCommit"] = commit
+       ctx.Data["CurrentCommit"] = commit
 
        var readmeFile *models.RepoFile
 
index 019ec8eaaa90a7212ee9e4766f53c5a62907abe3..c19ade7781208d58cbebe4404f3744c327c0b8ee 100644 (file)
@@ -45,7 +45,7 @@
                 <a href="/{{$username}}/{{$reponame}}/commit/{{.LatestCommit.SHA}}">{{.LatestCommit.Message}}</a>
             </div>
             <div class="panel-body info-content">
-                <a href="/user/{{.LatestCommit.Author}}">{{.LatestCommit.Author}}</a> <span class="text-muted">{{TimeSince .LatestCommit.Date}}</span>
+                <a href="/user/{{.LatestCommit.Author}}">{{.LatestCommit.Author}}</a> <span class="text-muted">{{TimeSince .CurrentCommit.Committer.When}}</span>
             </div>
             <table class="panel-footer table file-list">
                 <thead class="hidden">
@@ -57,6 +57,7 @@
                 </tr>
                 </thead>
                 <tbody>
+                {{$currentCommit := .CurrentCommit}}
                 {{range .Files}}
                 <tr
                 {{if .IsDir}}class="is-dir"{{end}}>
                     </span>
                 </td>
                 <td class="text">
-                    <span class="wrap"><a href="/{{$username}}/{{$reponame}}/commit/{{.LastCommit}}">{{.Message}}</a></span>
+                    <span class="wrap"><a href="/{{$username}}/{{$reponame}}/commit/{{.Commit.Oid}}">{{.Commit.Message}}</a></span>
                 </td>
                 <td class="date">
-                    <span class="wrap">{{TimeSince .Created}}</span>
+                    <span class="wrap">{{TimeSince .Commit.Committer.When}}</span>
                 </td>
                 </tr>
                 {{end}}
diff --git a/web.go b/web.go
index 2177d5662f9b7d0f1185ed0615eb48e81dc2e6de..ca504ea56013d72959fc2af9db2480c2aefa2eab 100644 (file)
--- a/web.go
+++ b/web.go
@@ -82,6 +82,9 @@ func runWeb(*cli.Context) {
                middleware.SignInRequire(false), middleware.RepoAssignment(true), repo.Single)
        m.Get("/:username/:reponame/tree/:branchname",
                middleware.SignInRequire(false), middleware.RepoAssignment(true), repo.Single)
+       m.Get("/:username/:reponame/commit/:commitid/**", middleware.SignInRequire(false), middleware.RepoAssignment(true), repo.Single)
+       m.Get("/:username/:reponame/commit/:commitid", middleware.SignInRequire(false), middleware.RepoAssignment(true), repo.Single)
+
        m.Get("/:username/:reponame", middleware.SignInRequire(false), middleware.RepoAssignment(true), repo.Single)
 
        listenAddr := fmt.Sprintf("%s:%s",