implify and restructure code

- remove commit message and lines changed from refs page.
- resolve HEAD once.
- dont make filepath in writeblob()
This commit is contained in:
Hiltjo Posthuma 2016-01-08 20:08:40 +01:00
parent efe16db1e5
commit 934980659a

107
stagit.c
View file

@ -329,12 +329,11 @@ printcommit(FILE *fp, struct commitinfo *ci)
printtime(fp, &(ci->author->when)); printtime(fp, &(ci->author->when));
fputc('\n', fp); fputc('\n', fp);
} }
fputc('\n', fp); if (ci->msg) {
fputc('\n', fp);
if (ci->msg)
xmlencode(fp, ci->msg, strlen(ci->msg)); xmlencode(fp, ci->msg, strlen(ci->msg));
fputc('\n', fp);
fputc('\n', fp); }
} }
void void
@ -357,19 +356,18 @@ printshowfile(struct commitinfo *ci)
fp = efopen(path, "w"); fp = efopen(path, "w");
writeheader(fp); writeheader(fp);
fputs("<pre>\n", fp); fputs("<pre>", fp);
printcommit(fp, ci); printcommit(fp, ci);
memset(&statsbuf, 0, sizeof(statsbuf)); memset(&statsbuf, 0, sizeof(statsbuf));
/* diff stat */ /* diff stat */
if (ci->stats) { if (ci->stats &&
if (!git_diff_stats_to_buf(&statsbuf, ci->stats, !git_diff_stats_to_buf(&statsbuf, ci->stats,
GIT_DIFF_STATS_FULL | GIT_DIFF_STATS_SHORT, 80)) { GIT_DIFF_STATS_FULL | GIT_DIFF_STATS_SHORT, 80)) {
if (statsbuf.ptr && statsbuf.ptr[0]) { if (statsbuf.ptr && statsbuf.ptr[0]) {
fputs("<b>Diffstat:</b>\n", fp); fputs("<b>Diffstat:</b>\n", fp);
fputs(statsbuf.ptr, fp); xmlencode(fp, statsbuf.ptr, strlen(statsbuf.ptr));
}
} }
} }
@ -430,21 +428,13 @@ printshowfile(struct commitinfo *ci)
} }
int int
writelog(FILE *fp, const char *branch) writelog(FILE *fp, const git_oid *oid)
{ {
struct commitinfo *ci; struct commitinfo *ci;
const git_oid *oid;
git_revwalk *w = NULL; git_revwalk *w = NULL;
git_object *obj = NULL;
git_oid id; git_oid id;
size_t len; size_t len;
mkdir("commit", 0755);
if (git_revparse_single(&obj, repo, branch))
return -1;
oid = git_object_id(obj);
git_revwalk_new(&w, repo); git_revwalk_new(&w, repo);
git_revwalk_push(w, oid); git_revwalk_push(w, oid);
git_revwalk_sorting(w, GIT_SORT_TIME); git_revwalk_sorting(w, GIT_SORT_TIME);
@ -493,7 +483,6 @@ writelog(FILE *fp, const char *branch)
fputs("</tbody></table>", fp); fputs("</tbody></table>", fp);
git_revwalk_free(w); git_revwalk_free(w);
git_object_free(obj);
relpath = ""; relpath = "";
@ -579,14 +568,13 @@ writeatom(FILE *fp)
} }
int int
writeblob(git_object *obj, const char *filename, git_off_t filesize) writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t filesize)
{ {
char fpath[PATH_MAX];
char tmp[PATH_MAX] = ""; char tmp[PATH_MAX] = "";
char *d, *p; char *d;
const char *p;
FILE *fp; FILE *fp;
snprintf(fpath, sizeof(fpath), "file/%s.html", filename);
d = xdirname(fpath); d = xdirname(fpath);
if (mkdirp(d)) { if (mkdirp(d)) {
free(d); free(d);
@ -698,35 +686,32 @@ writefilestree(FILE *fp, git_tree *tree, const char *branch, const char *path)
git_object_free(obj); git_object_free(obj);
continue; continue;
} }
if (path[0]) { if (path[0])
snprintf(filepath, sizeof(filepath), "%s/%s", snprintf(filepath, sizeof(filepath), "file/%s/%s.html",
path, filename); path, filename);
filename = filepath; else
} snprintf(filepath, sizeof(filepath), "file/%s.html",
filename);
filesize = git_blob_rawsize((git_blob *)obj); filesize = git_blob_rawsize((git_blob *)obj);
fputs("<tr><td>", fp); fputs("<tr><td>", fp);
fprintf(fp, "%s", filemode(git_tree_entry_filemode(entry))); fputs(filemode(git_tree_entry_filemode(entry)), fp);
fprintf(fp, "</td><td><a href=\"%sfile/", relpath); fprintf(fp, "</td><td><a href=\"%s%s\">", relpath, filepath);
xmlencode(fp, filename, strlen(filename));
fputs(".html\">", fp);
xmlencode(fp, filename, strlen(filename)); xmlencode(fp, filename, strlen(filename));
fputs("</a></td><td class=\"num\">", fp); fputs("</a></td><td class=\"num\">", fp);
fprintf(fp, "%ju", (uintmax_t)filesize); fprintf(fp, "%ju", (uintmax_t)filesize);
fputs("</td></tr>\n", fp); fputs("</td></tr>\n", fp);
writeblob(obj, filename, filesize); writeblob(obj, filepath, filename, filesize);
} }
return 0; return 0;
} }
int int
writefiles(FILE *fp, const char *branch) writefiles(FILE *fp, const git_oid *id, const char *branch)
{ {
const git_oid *id;
git_tree *tree = NULL; git_tree *tree = NULL;
git_object *obj = NULL;
git_commit *commit = NULL; git_commit *commit = NULL;
int ret = -1; int ret = -1;
@ -734,9 +719,6 @@ writefiles(FILE *fp, const char *branch)
"<td>Mode</td><td>Name</td><td class=\"num\">Size</td>" "<td>Mode</td><td>Name</td><td class=\"num\">Size</td>"
"</tr>\n</thead><tbody>\n", fp); "</tr>\n</thead><tbody>\n", fp);
if (git_revparse_single(&obj, repo, branch))
goto err;
id = git_object_id(obj);
if (git_commit_lookup(&commit, repo, id) || if (git_commit_lookup(&commit, repo, id) ||
git_commit_tree(&tree, commit)) git_commit_tree(&tree, commit))
goto err; goto err;
@ -745,7 +727,6 @@ writefiles(FILE *fp, const char *branch)
err: err:
fputs("</tbody></table>", fp); fputs("</tbody></table>", fp);
git_object_free(obj);
git_commit_free(commit); git_commit_free(commit);
git_tree_free(tree); git_tree_free(tree);
@ -778,7 +759,7 @@ writerefs(FILE *fp)
git_reference *dref = NULL, *r, *ref = NULL; git_reference *dref = NULL, *r, *ref = NULL;
git_reference_iterator *it = NULL; git_reference_iterator *it = NULL;
git_reference **refs = NULL; git_reference **refs = NULL;
size_t count, i, j, len, refcount = 0; size_t count, i, j, refcount = 0;
const char *cols[] = { "Branch", "Tag" }; /* first column title */ const char *cols[] = { "Branch", "Tag" }; /* first column title */
const char *titles[] = { "Branches", "Tags" }; const char *titles[] = { "Branches", "Tags" };
const char *ids[] = { "branches", "tags" }; const char *ids[] = { "branches", "tags" };
@ -827,9 +808,7 @@ writerefs(FILE *fp)
/* print header if it has an entry (first). */ /* print header if it has an entry (first). */
if (++count == 1) { if (++count == 1) {
fprintf(fp, "<h2>%s</h2><table id=\"%s\"><thead>\n<tr><td>%s</td>" fprintf(fp, "<h2>%s</h2><table id=\"%s\"><thead>\n<tr><td>%s</td>"
"<td>Age</td><td>Commit message</td>" "<td>Age</td><td>Author</td>\n</tr>\n</thead><tbody>\n",
"<td>Author</td><td>Files</td><td class=\"num\">+</td>"
"<td class=\"num\">-</td></tr>\n</thead><tbody>\n",
titles[j], ids[j], cols[j]); titles[j], ids[j], cols[j]);
} }
@ -842,28 +821,8 @@ writerefs(FILE *fp)
if (ci->author) if (ci->author)
printtimeshort(fp, &(ci->author->when)); printtimeshort(fp, &(ci->author->when));
fputs("</td><td>", fp); fputs("</td><td>", fp);
if (ci->summary) {
if (j)
fprintf(fp, "<a href=\"%scommit/%s.html\">",
relpath, ci->oid);
if ((len = strlen(ci->summary)) > summarylen) {
xmlencode(fp, ci->summary, summarylen - 1);
fputs("", fp);
} else {
xmlencode(fp, ci->summary, len);
}
if (j)
fputs("</a>", fp);
}
fputs("</td><td>", fp);
if (ci->author) if (ci->author)
xmlencode(fp, ci->author->name, strlen(ci->author->name)); xmlencode(fp, ci->author->name, strlen(ci->author->name));
fputs("</td><td class=\"num\">", fp);
fprintf(fp, "%zu", ci->filecount);
fputs("</td><td class=\"num\">", fp);
fprintf(fp, "+%zu", ci->addcount);
fputs("</td><td class=\"num\">", fp);
fprintf(fp, "-%zu", ci->delcount);
fputs("</td></tr>\n", fp); fputs("</td></tr>\n", fp);
relpath = "../"; relpath = "../";
@ -876,7 +835,7 @@ writerefs(FILE *fp)
} }
/* table footer */ /* table footer */
if (count) if (count)
fputs("</tbody></table>", fp); fputs("</tbody></table><br/>", fp);
} }
err: err:
@ -894,6 +853,7 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
git_object *obj = NULL; git_object *obj = NULL;
const git_oid *head = NULL;
const git_error *e = NULL; const git_error *e = NULL;
FILE *fp, *fpread; FILE *fp, *fpread;
char path[PATH_MAX], *p; char path[PATH_MAX], *p;
@ -914,6 +874,12 @@ main(int argc, char *argv[])
return status; return status;
} }
/* find HEAD */
if (git_revparse_single(&obj, repo, "HEAD"))
return 1;
head = git_object_id(obj);
git_object_free(obj);
/* use directory name as name */ /* use directory name as name */
name = xbasename(repodir); name = xbasename(repodir);
@ -964,14 +930,15 @@ main(int argc, char *argv[])
fp = efopen("log.html", "w"); fp = efopen("log.html", "w");
relpath = ""; relpath = "";
writeheader(fp); writeheader(fp);
writelog(fp, "HEAD"); mkdir("commit", 0755);
writelog(fp, head);
writefooter(fp); writefooter(fp);
fclose(fp); fclose(fp);
/* files for HEAD */ /* files for HEAD */
fp = efopen("files.html", "w"); fp = efopen("files.html", "w");
writeheader(fp); writeheader(fp);
writefiles(fp, "HEAD"); writefiles(fp, head, "HEAD");
writefooter(fp); writefooter(fp);
fclose(fp); fclose(fp);