]> git.armaanb.net Git - stagit.git/blobdiff - stagit.c
README: advertise text-browser improvements
[stagit.git] / stagit.c
index d4dd7a5dda9c7a3258d8be3308fcc930a9c63b3e..d9f66048f2fcc99675df668746d4ef11f6e50629 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,11 +69,7 @@ static FILE *rcachefp, *wcachefp;
 static const char *cachefile;
 
 #ifndef USE_PLEDGE
-int
-pledge(const char *promises, const char *paths[])
-{
-       return 0;
-}
+#define pledge(p1,p2) 0
 #endif
 
 void
@@ -118,6 +114,7 @@ commitinfo_getstats(struct commitinfo *ci)
                        err(1, "calloc");
                if (git_patch_from_diff(&patch, ci->diff, i)) {
                        git_patch_free(patch);
+                       free(di);
                        goto err;
                }
                di->patch = patch;
@@ -125,7 +122,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;
 
@@ -181,6 +178,8 @@ commitinfo_free(struct commitinfo *ci)
        git_tree_free(ci->commit_tree);
        git_tree_free(ci->parent_tree);
        git_commit_free(ci->commit);
+       git_commit_free(ci->parent);
+       free(ci);
 }
 
 struct commitinfo *
@@ -224,7 +223,6 @@ commitinfo_getbyoid(const git_oid *id)
 
 err:
        commitinfo_free(ci);
-       free(ci);
 
        return NULL;
 }
@@ -250,7 +248,7 @@ xmlencode(FILE *fp, const char *s, size_t len)
                switch(*s) {
                case '<':  fputs("&lt;",   fp); break;
                case '>':  fputs("&gt;",   fp); break;
-               case '\'': fputs("&apos;", fp); break;
+               case '\'': fputs("&#39;",  fp); break;
                case '&':  fputs("&amp;",  fp); break;
                case '"':  fputs("&quot;", fp); break;
                default:   fputc(*s, fp);
@@ -329,9 +327,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);
@@ -382,7 +380,7 @@ writeblobhtml(FILE *fp, const git_blob *blob)
 {
        off_t i;
        size_t n = 0;
-       char *nfmt = "<a href=\"#l%d\" id=\"l%d\">%d</a>\n";
+       const char *nfmt = "<a href=\"#l%d\" id=\"l%d\">%d</a>\n";
        const char *s = git_blob_rawcontent(blob);
        git_off_t len = git_blob_rawsize(blob);
 
@@ -453,7 +451,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;
        }
 
@@ -504,7 +502,7 @@ printshowfile(FILE *fp, struct commitinfo *ci)
 
                /* check binary data */
                if (delta->flags & GIT_DIFF_FLAG_BINARY) {
-                       fputs("Binary files differ\n", fp);
+                       fputs("Binary files differ.\n", fp);
                        continue;
                }
 
@@ -699,7 +697,7 @@ writeatom(FILE *fp)
        }
        git_revwalk_free(w);
 
-       fputs("</feed>", fp);
+       fputs("</feed>\n", fp);
 
        return 0;
 }
@@ -722,7 +720,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;
 
@@ -734,7 +731,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))
@@ -791,7 +788,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;
@@ -820,7 +817,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)
@@ -844,6 +841,7 @@ writefilestree(FILE *fp, git_tree *tree, const char *branch, const char *path)
                        else
                                fprintf(fp, "%juB", (uintmax_t)filesize);
                        fputs("</td></tr>\n", fp);
+                       git_object_free(obj);
                } else if (!git_submodule_lookup(&module, repo, entryname)) {
                        fprintf(fp, "<tr><td>m---------</td><td><a href=\"%sfile/.gitmodules.html\">",
                                relpath);
@@ -857,7 +855,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;
@@ -867,12 +865,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);
@@ -886,13 +882,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));
@@ -943,9 +936,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;
@@ -1045,11 +1037,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++;
@@ -1089,12 +1084,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 */
@@ -1140,7 +1140,8 @@ main(int argc, char *argv[])
                }
                fclose(wcachefp);
        } else {
-               writelog(fp, head);
+               if (head)
+                       writelog(fp, head);
        }
 
        fputs("</tbody></table>", fp);
@@ -1150,7 +1151,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);