login

<     >

2022-02-26 22:28:46 (UTC-03:00)

Marcel Rodrigues <marcelgmr@gmail.com>

add commit page

diff --git a/app.lua b/app.lua
index f18b2b5..93be67b 100644
--- a/app.lua
+++ b/app.lua
@@ -9,6 +9,22 @@ local path = "/tmp/cogit"
 local limit = 20
 local groups = scan.scanrepos(path)
 
+local function time_fmt(sig)
+    local s = os.date("%Y-%m-%d %H:%M:%S", sig.time_)
+    local offset = sig.offset
+    local sign
+    if offset < 0 then
+        offset = -offset
+        sign = "-"
+    else
+        sign = "+"
+    end
+    local hours = math.floor(offset / 60)
+    local mins = offset % 60
+    s = s .. (" (UTC%s%02d:%02d)"):format(sign, hours, mins)
+    return s
+end
+
 local routes = {
     {"GET", "/", function (req)
         local gnames = {}
@@ -38,6 +54,18 @@ local routes = {
         local env = {gname=gname, rname=rname, bname=bname, commit=commit, limit=limit, prev=prev, first=first}
         return lud.template.render_file("view/history.html", env)
     end},
+    {"GET", "/group/([%w_-]+)/repo/([%w_-]+)/commit/([%w_-]+)", function (req, gname, rname, cid)
+        local repo = groups[gname][rname]
+        local commit = repo:commit(cid)
+        local prev = repo:find_prev(commit:id(), 1)
+        local sig = commit:signature()
+        local time_str = time_fmt(sig)
+        local env = {
+            gname=gname, rname=rname, bname=bname, commit=commit,
+            time_str=time_str, sig=sig, cid=cid, prev=prev,
+        }
+        return lud.template.render_file("view/commit.html", env)
+    end},
 }
 
 local app = lud.app.new_app(routes)

diff --git a/git.lua b/git.lua
index f22edb1..c50422f 100644
--- a/git.lua
+++ b/git.lua
@@ -67,6 +67,7 @@ const git_oid * git_commit_id(const git_commit *commit);
 const char * git_commit_message(const git_commit *commit);
 const char * git_commit_summary(git_commit *commit);
 git_time_t git_commit_time(const git_commit *commit);
+int git_commit_time_offset(const git_commit *commit);
 const git_signature * git_commit_author(const git_commit *commit);
 
 int git_revwalk_new(git_revwalk **out, git_repository *repo);
@@ -105,6 +106,22 @@ function Commit:id(len)
     return ffi.string(oid)
 end
 
+function Commit:time()
+    local time_ = tonumber(C.git_commit_time(self.commit))
+    local offset = C.git_commit_time_offset(self.commit)
+    return time_, offset
+end
+
+function Commit:signature()
+    local sign = C.git_commit_author(self.commit)
+    return {
+        time_=tonumber(sign.when.time),
+        offset=sign.when.offset,
+        name=ffi.string(sign.name),
+        email=ffi.string(sign.email),
+    }
+end
+
 function Commit:message()
     return ffi.string(C.git_commit_message(self.commit))
 end
@@ -180,7 +197,7 @@ function Repo:find_prev(cid, dist)
         end
         if cids[#cids] == cid then
             if #cids > dist then
-                return self:commit(cids[#cids - dist - 1])
+                return self:commit(cids[#cids - dist])
             else
                 return nil
             end

diff --git a/view/commit.html b/view/commit.html
new file mode 100644
index 0000000..ef96789
--- /dev/null
+++ b/view/commit.html
@@ -0,0 +1,30 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <meta charset="utf-8">
+  <title>cogit - {{$gname}} - {{$rname}} - {{$cid}}</title>
+</head>
+<body>
+  <a href="/group/{{$gname}}">{{$gname}}</a>
+  &gt;
+  <a href="/group/{{$gname}}/repo/{{$rname}}">{{$rname}}</a>
+  &gt;
+  {{$cid}}
+  <br/>
+  <p>{{$time_str}}</p>
+  <p>{{$sig.name}} &lt;{{$sig.email}}&gt;</p>
+  <pre>{{$commit:message()}}</pre>
+  <br/>
+  % if $prev then
+  <a href="/group/{{$gname}}/repo/{{$rname}}/commit/{{$prev:id()}}">&lt</a>
+  % else
+  &lt
+  % end
+  &nbsp;&nbsp;&nbsp;
+  % if $commit:parent() then
+  <a href="/group/{{$gname}}/repo/{{$rname}}/commit/{{$commit:parent():id()}}">&gt</a>
+  % else
+  &gt
+  % end
+</body>
+</html>

diff --git a/view/history.html b/view/history.html
index c4976c6..c5c7eee 100644
--- a/view/history.html
+++ b/view/history.html
@@ -9,7 +9,7 @@
   &gt;
   <a href="/group/{{$gname}}/repo/{{$rname}}">{{$rname}}</a>
   &gt;
-  {{$commit:id()}}
+  history
   <p>Commits:</p>
   <ul>
     % set i = 0
@@ -26,8 +26,8 @@
   &lt
   % end
   &nbsp;&nbsp;&nbsp;
-  % if $commit and $commit:parent() then
-  <a href="/group/{{$gname}}/repo/{{$rname}}/history/{{$commit:parent():id()}}">&gt</a>
+  % if $commit then
+  <a href="/group/{{$gname}}/repo/{{$rname}}/history/{{$commit:id()}}">&gt</a>
   % else
   &gt
   % end