Goit

Simple and lightweight Git web server
Mirror of https://github.com/Jamozed/Goit
git clone http://git.omkov.net/Goit
Log | Tree | Refs | README | Download

AuthorJakob Wakeling <[email protected]>
Date2023-11-16 09:50:47
Commit35fbf5f73281709d10188c18c0ffe35025f8213f
Parenta778a391f6d44c4af7d07f1e5784d4fbf2638267

Implement log filtering by file path

Diffstat

M res/repo/tree.html | 3 ++-
M src/main.go | 1 +
M src/repo/log.go | 25 +++++++++++++++++++++++--
M src/repo/tree.go | 15 +++++++--------

4 files changed, 33 insertions, 11 deletions

diff --git a/res/repo/tree.html b/res/repo/tree.html
index 8c2d7d7..5bf72d1 100644
--- a/res/repo/tree.html
+++ b/res/repo/tree.html
@@ -21,7 +21,8 @@
 							<td align="right" {{if .B}}style="padding-right: calc(2ch + 0.4em);"{{end}}>{{.Size}}</td>
 							<td>
 								{{if .RawPath}}
-									log blame
+									<a href="/{{$.Name}}/log/{{.RawPath}}">log</a>
+									blame
 									<a href="/{{$.Name}}/raw/{{.RawPath}}">raw</a>
 									download
 								{{end}}
diff --git a/src/main.go b/src/main.go
index aa662d7..d59bdeb 100644
--- a/src/main.go
+++ b/src/main.go
@@ -95,6 +95,7 @@ func main() {
 	h.Path("/{repo:.+(?:\\.git)$}").Methods("GET").HandlerFunc(redirectDotGit)
 	h.Path("/{repo}").Methods("GET").HandlerFunc(repo.HandleLog)
 	h.Path("/{repo}/log").Methods("GET").HandlerFunc(repo.HandleLog)
+	h.Path("/{repo}/log/{path:.*}").Methods("GET").HandlerFunc(repo.HandleLog)
 	h.Path("/{repo}/commit/{hash}").Methods("GET").HandlerFunc(repo.HandleCommit)
 	h.Path("/{repo}/tree").Methods("GET").HandlerFunc(repo.HandleTree)
 	h.Path("/{repo}/tree/{path:.*}").Methods("GET").HandlerFunc(repo.HandleTree)
diff --git a/src/repo/log.go b/src/repo/log.go
index 39a5932..ec3d2a1 100644
--- a/src/repo/log.go
+++ b/src/repo/log.go
@@ -18,13 +18,20 @@ import (
 )
 
 func HandleLog(w http.ResponseWriter, r *http.Request) {
-	auth, uid := goit.AuthCookie(w, r, true)
+	auth, user, err := goit.Auth(w, r, true)
+	if err != nil {
+		log.Println("[repo/log]", err.Error())
+		goit.HttpError(w, http.StatusInternalServerError)
+		return
+	}
+
+	path := mux.Vars(r)["path"]
 
 	repo, err := goit.GetRepoByName(mux.Vars(r)["repo"])
 	if err != nil {
 		goit.HttpError(w, http.StatusInternalServerError)
 		return
-	} else if repo == nil || (repo.IsPrivate && (!auth || repo.OwnerId != uid)) {
+	} else if repo == nil || (repo.IsPrivate && (!auth || repo.OwnerId != user.Id)) {
 		goit.HttpError(w, http.StatusNotFound)
 		return
 	}
@@ -48,7 +55,7 @@ func HandleLog(w http.ResponseWriter, r *http.Request) {
 	}{
 		Title: repo.Name + " - Log", Name: repo.Name, Description: repo.Description,
 		Url:      util.If(goit.Conf.UsesHttps, "https://", "http://") + r.Host + "/" + repo.Name,
-		Editable: (auth && repo.OwnerId == uid),
+		Editable: (auth && repo.OwnerId == user.Id),
 	}
 
 	gr, err := git.PlainOpen(goit.RepoPath(repo.Name, true))
@@ -83,6 +90,18 @@ func HandleLog(w http.ResponseWriter, r *http.Request) {
 
 		if stats, err := goit.DiffStats(c); err != nil {
 			log.Println("[/repo/log]", err.Error())
+		} else if path != "" {
+			for _, s := range stats {
+				if s.Name == path {
+					files = 1
+					additions += s.Addition
+					deletions += s.Deletion
+				}
+			}
+
+			if files == 0 {
+				return nil
+			}
 		} else {
 			files = len(stats)
 			for _, s := range stats {
diff --git a/src/repo/tree.go b/src/repo/tree.go
index ee6e003..2be34d4 100644
--- a/src/repo/tree.go
+++ b/src/repo/tree.go
@@ -4,7 +4,6 @@ import (
 	"errors"
 	"log"
 	"net/http"
-	"path"
 	"path/filepath"
 	"sort"
 	"strings"
@@ -20,7 +19,7 @@ import (
 
 func HandleTree(w http.ResponseWriter, r *http.Request) {
 	auth, uid := goit.AuthCookie(w, r, true)
-	treepath := mux.Vars(r)["path"]
+	path := mux.Vars(r)["path"]
 
 	repo, err := goit.GetRepoByName(mux.Vars(r)["repo"])
 	if err != nil {
@@ -81,12 +80,12 @@ func HandleTree(w http.ResponseWriter, r *http.Request) {
 			return
 		}
 
-		if treepath != "" {
+		if path != "" {
 			data.Files = append(data.Files, row{
-				Mode: "d---------", Name: "..", Path: filepath.Join("tree", path.Dir(treepath)),
+				Mode: "d---------", Name: "..", Path: filepath.Join("tree", filepath.Dir(path)),
 			})
 
-			tree, err = tree.Tree(treepath)
+			tree, err = tree.Tree(path)
 			if errors.Is(err, object.ErrDirectoryNotFound) {
 				goit.HttpError(w, http.StatusNotFound)
 				return
@@ -116,8 +115,8 @@ func HandleTree(w http.ResponseWriter, r *http.Request) {
 					return
 				}
 
-				fpath = filepath.Join("file", treepath, v.Name)
-				rpath = filepath.Join(treepath, v.Name)
+				fpath = filepath.Join("file", path, v.Name)
+				rpath = filepath.Join(path, v.Name)
 				size = humanize.IBytes(uint64(file.Size))
 			} else {
 				var dirSize uint64
@@ -138,7 +137,7 @@ func HandleTree(w http.ResponseWriter, r *http.Request) {
 					return
 				}
 
-				fpath = filepath.Join("tree", treepath, v.Name)
+				fpath = filepath.Join("tree", path, v.Name)
 				size = humanize.IBytes(dirSize)
 			}