]> git.armaanb.net Git - stagit.git/blobdiff - stagit.c
simplify/shorten pledge stub
[stagit.git] / stagit.c
index 85b782aa9f0762717baac13aeb9e2a936b98f519..c3693c70d769c8a428f8400d009e2f546a293864 100644 (file)
--- a/stagit.c
+++ b/stagit.c
@@ -57,7 +57,7 @@ static const char *relpath = "";
 static const char *repodir;
 
 static char *name = "";
-static char *strippedname;
+static char *strippedname = "";
 static char description[255];
 static char cloneurl[1024];
 static int haslicense, hasreadme, hassubmodules;
@@ -69,12 +69,20 @@ static FILE *rcachefp, *wcachefp;
 static const char *cachefile;
 
 #ifndef USE_PLEDGE
-int
-pledge(const char *promises, const char *paths[])
+#define pledge(p1,p2) 0
+#endif
+
+void
+joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
 {
-       return 0;
+       int r;
+
+       r = snprintf(buf, bufsiz, "%s%s%s",
+               path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
+       if (r == -1 || (size_t)r >= bufsiz)
+               errx(1, "path truncated: '%s%s%s'",
+                       path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
 }
-#endif
 
 void
 deltainfo_free(struct deltainfo *di)
@@ -113,7 +121,7 @@ commitinfo_getstats(struct commitinfo *ci)
 
                delta = git_patch_get_delta(patch);
 
-               /* check binary data */
+               /* skip stats for binary data */
                if (delta->flags & GIT_DIFF_FLAG_BINARY)
                        continue;
 
@@ -285,19 +293,18 @@ printtime(FILE *fp, const git_time *intime)
 {
        struct tm *intm;
        time_t t;
-       int offset, sign = '+';
        char out[32];
 
-       offset = intime->offset * 60;
-       t = (time_t)intime->time + offset;
+       t = (time_t)intime->time + (intime->offset * 60);
        if (!(intm = gmtime(&t)))
                return;
        strftime(out, sizeof(out), "%a %b %e %H:%M:%S", intm);
-       if (offset < 0) {
-               offset = -offset;
-               sign = '-';
-       }
-       fprintf(fp, "%s %c%02d%02d", out, sign, offset / 60, offset % 60);
+       if (intime->offset < 0)
+               fprintf(fp, "%s -%02d%02d", out,
+                           -(intime->offset) / 60, -(intime->offset) % 60);
+       else
+               fprintf(fp, "%s +%02d%02d", out,
+                           intime->offset / 60, intime->offset % 60);
 }
 
 void
@@ -318,9 +325,9 @@ void
 writeheader(FILE *fp, const char *title)
 {
        fputs("<!DOCTYPE html>\n"
-               "<html dir=\"ltr\" lang=\"en\">\n<head>\n"
+               "<html>\n<head>\n"
                "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
-               "<meta http-equiv=\"Content-Language\" content=\"en\" />\n<title>", fp);
+               "<title>", fp);
        xmlencode(fp, title, strlen(title));
        if (title[0] && strippedname[0])
                fputs(" - ", fp);
@@ -442,7 +449,7 @@ printshowfile(FILE *fp, struct commitinfo *ci)
            ci->ndeltas   > 1000   ||
            ci->addcount  > 100000 ||
            ci->delcount  > 100000) {
-               fprintf(fp, "(diff is too large, output suppressed)");
+               fputs("Diff is too large, output suppressed.\n", fp);
                return;
        }
 
@@ -450,7 +457,7 @@ printshowfile(FILE *fp, struct commitinfo *ci)
        fputs("<b>Diffstat:</b>\n<table>", fp);
        for (i = 0; i < ci->ndeltas; i++) {
                delta = git_patch_get_delta(ci->deltas[i]->patch);
-               fputs("<tr><td>", fp);
+               fprintf(fp, "<tr><td><a href=\"#h%zu\">", i);
                xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
                if (strcmp(delta->old_file.path, delta->new_file.path)) {
                        fputs(" -&gt; ", fp);
@@ -470,7 +477,7 @@ printshowfile(FILE *fp, struct commitinfo *ci)
                memset(&linestr, '+', add);
                memset(&linestr[add], '-', del);
 
-               fprintf(fp, "</td><td> | </td><td class=\"num\">%zu</td><td><span class=\"i\">",
+               fprintf(fp, "</a></td><td> | </td><td class=\"num\">%zu</td><td><span class=\"i\">",
                        ci->deltas[i]->addcount + ci->deltas[i]->delcount);
                fwrite(&linestr, 1, add, fp);
                fputs("</span><span class=\"d\">", fp);
@@ -487,13 +494,13 @@ printshowfile(FILE *fp, struct commitinfo *ci)
        for (i = 0; i < ci->ndeltas; i++) {
                patch = ci->deltas[i]->patch;
                delta = git_patch_get_delta(patch);
-               fprintf(fp, "<b>diff --git a/<a href=\"%sfile/%s.html\">%s</a> b/<a href=\"%sfile/%s.html\">%s</a></b>\n",
-                       relpath, delta->old_file.path, delta->old_file.path,
+               fprintf(fp, "<b>diff --git a/<a id=\"h%zu\" href=\"%sfile/%s.html\">%s</a> b/<a href=\"%sfile/%s.html\">%s</a></b>\n",
+                       i, relpath, delta->old_file.path, delta->old_file.path,
                        relpath, delta->new_file.path, delta->new_file.path);
 
                /* check binary data */
                if (delta->flags & GIT_DIFF_FLAG_BINARY) {
-                       fputs("Binary files differ\n", fp);
+                       fputs("Binary files differ.\n", fp);
                        continue;
                }
 
@@ -711,7 +718,6 @@ writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t fi
        for (p = fpath, tmp[0] = '\0'; *p; p++) {
                if (*p == '/' && strlcat(tmp, "../", sizeof(tmp)) >= sizeof(tmp))
                        errx(1, "path truncated: '../%s'", tmp);
-               p++;
        }
        relpath = tmp;
 
@@ -723,7 +729,7 @@ writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t fi
        fputs("</p><hr/>", fp);
 
        if (git_blob_is_binary((git_blob *)obj)) {
-               fputs("<p>Binary file</p>\n", fp);
+               fputs("<p>Binary file.</p>\n", fp);
        } else {
                lc = writeblobhtml(fp, (git_blob *)obj);
                if (ferror(fp))
@@ -780,7 +786,7 @@ filemode(git_filemode_t m)
 }
 
 int
-writefilestree(FILE *fp, git_tree *tree, const char *branch, const char *path)
+writefilestree(FILE *fp, git_tree *tree, const char *path)
 {
        const git_tree_entry *entry = NULL;
        git_submodule *module = NULL;
@@ -796,17 +802,12 @@ writefilestree(FILE *fp, git_tree *tree, const char *branch, const char *path)
                if (!(entry = git_tree_entry_byindex(tree, i)) ||
                    !(entryname = git_tree_entry_name(entry)))
                        return -1;
-               r = snprintf(entrypath, sizeof(entrypath), "%s%s%s",
-                        path, path[0] ? "/" : "", entryname);
-               if (r == -1 || (size_t)r >= sizeof(entrypath))
-                       errx(1, "path truncated: '%s%s%s'",
-                               path, path[0] ? "/" : "", entryname);
-
-               r = snprintf(filepath, sizeof(filepath), "file/%s%s%s.html",
-                        path, path[0] ? "/" : "", entryname);
+               joinpath(entrypath, sizeof(entrypath), path, entryname);
+
+               r = snprintf(filepath, sizeof(filepath), "file/%s.html",
+                        entrypath);
                if (r == -1 || (size_t)r >= sizeof(filepath))
-                       errx(1, "path truncated: 'file/%s%s%s.html'",
-                               path, path[0] ? "/" : "", entryname);
+                       errx(1, "path truncated: 'file/%s.html'", entrypath);
 
                if (!git_tree_entry_to_object(&obj, repo, entry)) {
                        switch (git_object_type(obj)) {
@@ -814,7 +815,7 @@ writefilestree(FILE *fp, git_tree *tree, const char *branch, const char *path)
                                break;
                        case GIT_OBJ_TREE:
                                /* NOTE: recurses */
-                               ret = writefilestree(fp, (git_tree *)obj, branch,
+                               ret = writefilestree(fp, (git_tree *)obj,
                                                     entrypath);
                                git_object_free(obj);
                                if (ret)
@@ -851,7 +852,7 @@ writefilestree(FILE *fp, git_tree *tree, const char *branch, const char *path)
 }
 
 int
-writefiles(FILE *fp, const git_oid *id, const char *branch)
+writefiles(FILE *fp, const git_oid *id)
 {
        git_tree *tree = NULL;
        git_commit *commit = NULL;
@@ -861,12 +862,10 @@ writefiles(FILE *fp, const git_oid *id, const char *branch)
              "<td>Mode</td><td>Name</td><td class=\"num\">Size</td>"
              "</tr>\n</thead><tbody>\n", fp);
 
-       if (git_commit_lookup(&commit, repo, id) ||
-           git_commit_tree(&tree, commit))
-               goto err;
-       ret = writefilestree(fp, tree, branch, "");
+       if (!git_commit_lookup(&commit, repo, id) &&
+           !git_commit_tree(&tree, commit))
+               ret = writefilestree(fp, tree, "");
 
-err:
        fputs("</tbody></table>", fp);
 
        git_commit_free(commit);
@@ -880,13 +879,10 @@ refs_cmp(const void *v1, const void *v2)
 {
        git_reference *r1 = (*(git_reference **)v1);
        git_reference *r2 = (*(git_reference **)v2);
-       int t1, t2;
-
-       t1 = git_reference_is_branch(r1);
-       t2 = git_reference_is_branch(r2);
+       int r;
 
-       if (t1 != t2)
-               return t1 - t2;
+       if ((r = git_reference_is_branch(r1) - git_reference_is_branch(r2)))
+               return r;
 
        return strcmp(git_reference_shorthand(r1),
                      git_reference_shorthand(r2));
@@ -937,9 +933,8 @@ writerefs(FILE *fp)
                        default:
                                continue;
                        }
-                       if (!(id = git_reference_target(r)))
-                               goto err;
-                       if (git_reference_peel(&obj, r, GIT_OBJ_ANY))
+                       if (!git_reference_target(r) ||
+                           git_reference_peel(&obj, r, GIT_OBJ_ANY))
                                goto err;
                        if (!(id = git_object_id(obj)))
                                goto err;
@@ -990,18 +985,6 @@ err:
        return 0;
 }
 
-void
-joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
-{
-       int r;
-
-       r = snprintf(buf, bufsiz, "%s%s%s",
-               repodir, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
-       if (r == -1 || (size_t)r >= bufsiz)
-               errx(1, "path truncated: '%s%s%s'",
-                       path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
-}
-
 void
 usage(char *argv0)
 {
@@ -1051,11 +1034,14 @@ main(int argc, char *argv[])
        }
 
        /* find HEAD */
-       if (git_revparse_single(&obj, repo, "HEAD"))
-               return 1;
-       head = git_object_id(obj);
+       if (!git_revparse_single(&obj, repo, "HEAD"))
+               head = git_object_id(obj);
        git_object_free(obj);
 
+       /* don't cache if there is no HEAD */
+       if (!head)
+               cachefile = NULL;
+
        /* use directory name as name */
        if ((name = strrchr(repodirabs, '/')))
                name++;
@@ -1095,12 +1081,17 @@ main(int argc, char *argv[])
        }
 
        /* check LICENSE */
-       haslicense = !git_revparse_single(&obj, repo, "HEAD:LICENSE");
+       haslicense = (!git_revparse_single(&obj, repo, "HEAD:LICENSE") &&
+               git_object_type(obj) == GIT_OBJ_BLOB);
        git_object_free(obj);
+
        /* check README */
-       hasreadme = !git_revparse_single(&obj, repo, "HEAD:README");
+       hasreadme = (!git_revparse_single(&obj, repo, "HEAD:README") &&
+               git_object_type(obj) == GIT_OBJ_BLOB);
        git_object_free(obj);
-       hassubmodules = !git_revparse_single(&obj, repo, "HEAD:.gitmodules");
+
+       hassubmodules = (!git_revparse_single(&obj, repo, "HEAD:.gitmodules") &&
+               git_object_type(obj) == GIT_OBJ_BLOB);
        git_object_free(obj);
 
        /* log for HEAD */
@@ -1146,7 +1137,8 @@ main(int argc, char *argv[])
                }
                fclose(wcachefp);
        } else {
-               writelog(fp, head);
+               if (head)
+                       writelog(fp, head);
        }
 
        fputs("</tbody></table>", fp);
@@ -1156,7 +1148,8 @@ main(int argc, char *argv[])
        /* files for HEAD */
        fp = efopen("files.html", "w");
        writeheader(fp, "Files");
-       writefiles(fp, head, "HEAD");
+       if (head)
+               writefiles(fp, head);
        writefooter(fp);
        fclose(fp);