commit b4147da52acc7f6e1fb9886e87ccd3acfdd55bcb
parent 219bb57fa0c95190547bf50a63fd41783052d0ca
Author: quantumish <freifeld.david@gmail.com>
Date: Wed, 5 Aug 2026 14:15:43 -0700
Link to project README instead of commit log when possible
Diffstat:
1 file changed, 29 insertions(+), 9 deletions(-)
diff --git a/stagit-index.c b/stagit-index.c
@@ -8,13 +8,17 @@
#include <git2.h>
+#define LEN(s) (sizeof(s)/sizeof(*s))
+
static git_repository *repo;
static const char *relpath = "";
+static char *readmefiles[] = { "HEAD:README", "HEAD:README.md", "HEAD:readme.md", "HEAD:README.org", "HEAD:readme.org" };
static char description[255] = "Repositories";
static char last_development[41] = "";
static char *name = "";
+static char *readme = NULL;
/* Handle read or write errors for a FILE * stream */
void
@@ -50,7 +54,7 @@ percentencode(FILE *fp, const char *s, size_t len)
uc = *s;
/* NOTE: do not encode '/' for paths or ",-." */
if (uc < ',' || uc >= 127 || (uc >= ':' && uc <= '@') ||
- uc == '[' || uc == ']') {
+ uc == '[' || uc == ']') {
putc('%', fp);
putc(tab[(uc >> 4) & 0x0f], fp);
putc(tab[uc & 0x0f], fp);
@@ -105,7 +109,7 @@ writeheader(FILE *fp)
fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
fputs("</head>\n<body>\n", fp);
fprintf(fp, "<table>\n<tr><td><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></td>\n"
- "<td><span class=\"desc\">", relpath);
+ "<td><span class=\"desc\">", relpath);
xmlencode(fp, description, strlen(description));
fputs("</span></td></tr><tr><td></td><td>\n"
"</td></tr>\n</table>\n<hr/>\n<div id=\"content\">\n"
@@ -134,12 +138,12 @@ writelog(FILE *fp)
git_revwalk_new(&w, repo);
git_revwalk_push_head(w);
- if (git_oid_fromstr(&id, last_development) &&
+ if (git_oid_fromstr(&id, last_development) &&
git_revwalk_next(&id, w)) {
ret = -1;
goto err;
}
-
+
if (git_commit_lookup(&commit, repo, &id)) {
ret = -1;
goto err;
@@ -156,7 +160,13 @@ writelog(FILE *fp)
fputs("<tr><td><a href=\"", fp);
percentencode(fp, stripped_name, strlen(stripped_name));
- fputs("/log.html\">", fp);
+
+ if (readme != NULL) {
+ fprintf(fp, "/file/%s.html\">", readme);
+ } else {
+ fputs("/log.html\">", fp);
+ }
+
xmlencode(fp, stripped_name, strlen(stripped_name));
fputs("</a></td><td>", fp);
xmlencode(fp, description, strlen(description));
@@ -179,7 +189,8 @@ main(int argc, char *argv[])
FILE *fp;
char path[PATH_MAX], repodirabs[PATH_MAX + 1];
const char *repodir;
- int i, ret = 0;
+ int i, j, ret = 0;
+ git_object *obj = NULL;
if (argc < 2) {
fprintf(stderr, "usage: %s [repodir...]\n", argv[0]);
@@ -201,13 +212,13 @@ main(int argc, char *argv[])
writeheader(stdout);
- for (i = 1; i < argc; i++) {
+ (((for (i = 1; i < argc; i++) {
repodir = argv[i];
if (!realpath(repodir, repodirabs))
err(1, "realpath");
if (git_repository_open_ext(&repo, repodir,
- GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) {
+ GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) {
fprintf(stderr, "%s: cannot open repository\n", argv[0]);
ret = 1;
continue;
@@ -242,12 +253,21 @@ main(int argc, char *argv[])
last_development[0] = '\0';
if (fp) {
if (!fgets(last_development, sizeof(last_development), fp))
- last_development[0] = '\0';
+ last_development[0] = '\0';
checkfileerror(fp, "last-development", 'r');
fclose(fp);
}
+ for (j = 0; j < LEN(readmefiles) && !readme; j++) {
+ if (!git_revparse_single(&obj, repo, readmefiles[j]) &&
+ git_object_type(obj) == GIT_OBJ_BLOB) {
+ readme = readmefiles[j] + strlen("HEAD:");
+ }
+ git_object_free(obj);
+ }
+
writelog(stdout);
+ readme = NULL;
}
writefooter(stdout);