Diff between 5b4d60a48d19f33fde80797f010d88e11c926432 and 8b9c6ecd6e6d81f3832a11542aa460d46e1b68ef

Changed Files

File Additions Deletions Status
app.py +11 -2 modified
git/commit.py +40 -0 modified
templates/commits.html +10 -0 added
templates/index.html +1 -1 modified

Full Patch

diff --git a/app.py b/app.py
index df03977..da8d209 100644
--- a/app.py
+++ b/app.py
@@ -1,13 +1,22 @@
-from flask import Flask, render_template
+from flask import Flask, render_template, request
 
 from git.repo import get_bare_repos
+from git.commit import get_commits
 
 app = Flask(__name__)
 
+repo_path = "/home/lhietala/git-webview/repos-example"
 @app.route("/")
 def index():
-    repos = get_bare_repos("/home/lhietala/git-webview/repos-example")
+    repos = get_bare_repos(repo_path)
     return render_template("index.html", repos=repos)
 
+@app.route("/<repo_name>/commits")
+def repo_commits(repo_name):
+    ref = request.args.get('ref', 'HEAD')
+
+    commits = get_commits(f"{repo_path}/{repo_name}", ref=ref, max_count=50)
+    return render_template("commits.html", repo_name=repo_name, commits=commits)
+
 if __name__ == "__main__":
     app.run(debug=True)
\ No newline at end of file
diff --git a/git/commit.py b/git/commit.py
index e69de29..acb0101 100644
--- a/git/commit.py
+++ b/git/commit.py
@@ -0,0 +1,40 @@
+import pygit2 as git
+
+# retrieves commit history for given repo path and reference
+def get_commits(path, ref="HEAD", max_count=None):
+    repo = git.Repository(path)
+    commits = []
+    walker = repo.walk(repo.revparse_single(ref).id, git.GIT_SORT_TIME)
+
+    n = 0
+    for commit in walker:
+        if max_count is not None and n >= max_count:
+            break
+        if len(commit.parents) > 0:
+            # get diif stats against first parent
+            # libgit2 has very fast diff stats calculation, using that here
+            diff = repo.diff(commit.parents[0], commit)
+            stats = diff.stats
+            diff_stats = {
+                'insertions': stats.insertions,
+                'deletions': stats.deletions,
+                'files_changed': stats.files_changed
+            }
+        else:
+            # TODO: compare to NULL_TREE
+            diff_stats = {
+                'insertions': 0,
+                'deletions': 0,
+                'files_changed': 0
+            }
+        commit_info = {
+            'id': str(commit.id),
+            'message': commit.message.strip(),
+            'author': commit.author,
+            'committer': commit.committer,
+            'date': commit.commit_time,
+            'diff_stats': diff_stats
+        }
+        commits.append(commit_info)
+        n += 1
+    return commits
\ No newline at end of file
diff --git a/templates/commits.html b/templates/commits.html
new file mode 100644
index 0000000..18e7498
--- /dev/null
+++ b/templates/commits.html
@@ -0,0 +1,10 @@
+{% block content %}
+    <h1>Commits for {{ repo_name }}</h1>
+    <ul>
+        {% for commit in commits %}
+            <li>
+            {{ commit.id }} - {{ commit.message }} - {{ commit.author.name }} - {{ commit.date }} {{ commit.diff_stats.files_changed }} files changed, {{ commit.diff_stats.insertions }} insertions, {{ commit.diff_stats.deletions }} deletions
+            </li>
+        {% endfor %}
+    </ul>
+{% endblock %}
\ No newline at end of file
diff --git a/templates/index.html b/templates/index.html
index c3cabf8..8c77402 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -2,7 +2,7 @@
     <h1>Repositories</h1>
     <ul>
         {% for repo in repos %}
-            <li><a href="{{ repo.name }}">{{ repo.name }}</a></li>
+            <li><a href="{{ repo.name }}/commits">{{ repo.name }}</a></li>
         {% endfor %}
     </ul>
 {% endblock %}
\ No newline at end of file