]> git.armaanb.net Git - stagit.git/blobdiff - src/stagit.c
do not simplify the history by first-parent
[stagit.git] / src / stagit.c
index d7597f90c2b33d747db2410abc1fd001dffc7378..24075f6081e2f54024c1babd5ab3e0fa4458d8ad 100644 (file)
 #include <unistd.h>
 
 #include <git2.h>
+
+#ifdef HAS_CMARK
 #include <cmark-gfm.h>
+#endif
 
-#include "cp.h"
 #include "compat.h"
+#include "cp.h"
 
 struct deltainfo {
        git_patch *patch;
@@ -53,6 +56,7 @@ struct commitinfo {
 
 static git_repository *repo;
 
+static const char *baseurl = ""; /* base URL to make absolute RSS/Atom URI */
 static const char *relpath = "";
 static const char *repodir;
 
@@ -68,6 +72,7 @@ static char *readme;
 static long long nlogcommits = -1; /* < 0 indicates not used */
 
 bool htmlized; /* true if markdoown converted to HTML */
+static char oldfilename[PATH_MAX]; /* filename of the last file */
 
 /* cache */
 static git_oid lastoid;
@@ -276,6 +281,25 @@ xmlencode(FILE *fp, const char *s, size_t len)
        }
 }
 
+/* Escape characters below as HTML 2.0 / XML 1.0, ignore printing '\n', '\r' */
+void
+xmlencodeline(FILE *fp, const char *s, size_t len)
+{
+       size_t i;
+       for (i = 0; *s && i < len; s++, i++) {
+               switch(*s) {
+               case '<':  fputs("&lt;",   fp); break;
+               case '>':  fputs("&gt;",   fp); break;
+               case '\'': fputs("&#39;",  fp); break;
+               case '&':  fputs("&amp;",  fp); break;
+               case '"':  fputs("&quot;", fp); break;
+               case '\r': break; /* ignore CR */
+               case '\n': break; /* ignore LF */
+               default:   putc(*s, fp);
+               }
+       }
+}
+
 int
 mkdirp(const char *path)
 {
@@ -347,7 +371,7 @@ void
 writeheader(FILE *fp, const char *title)
 {
        fputs("<!DOCTYPE html>\n"
-               "<html>\n<head>\n"
+               "<html lang=\"en\">\n<head>\n"
                "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
                "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />\n"
                "<title>", fp);
@@ -363,22 +387,21 @@ writeheader(FILE *fp, const char *title)
                name, relpath);
        fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
        fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%ssyntax.css\" />\n", relpath);
-       fputs("</head>\n<body>\n<table><tr><td>", fp);
-       fprintf(fp, "<a href=\"../%s\"><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></a>",
+       fputs("</head>\n<body>\n", fp);
+       fprintf(fp, "<a href=\"../%s\"><img class=\"logo\" src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></a>",
                relpath, relpath);
-       fputs("</td><td><h1>", fp);
+       fputs("<h1>", fp);
        xmlencode(fp, strippedname, strlen(strippedname));
-       fputs("</h1></td></tr><tr><td></td><td><span class=\"desc\">", fp);
+       fputs("</h1><p class=\"desc\">", fp);
        xmlencode(fp, description, strlen(description));
-       fputs("</span></td></tr>", fp);
+       fputs("</p>", fp);
        if (cloneurl[0]) {
-               fputs("<tr class=\"url\"><td></td><td><span class=\"clone\">git clone <a href=\"", fp);
+               fputs("<p class=\"clone\">git clone <a href=\"", fp);
                xmlencode(fp, cloneurl, strlen(cloneurl));
                fputs("\">", fp);
                xmlencode(fp, cloneurl, strlen(cloneurl));
-               fputs("</a></span></td></tr>", fp);
+               fputs("</a></p>", fp);
        }
-       fputs("<tr><td></td><td>\n", fp);
        fprintf(fp, "<a href=\"%slog.html\">Log</a> | ", relpath);
        fprintf(fp, "<a href=\"%sfiles.html\">Files</a> | ", relpath);
        fprintf(fp, "<a href=\"%srefs.html\">Refs</a>", relpath);
@@ -393,7 +416,7 @@ writeheader(FILE *fp, const char *title)
                        relpath, license);
        fprintf(fp, " | <a href=\"%s%s.tar.gz\">Download</a>",
                                        relpath, strippedname);
-       fputs("</td></tr></table>\n<hr/>\n<div id=\"content\">\n", fp);
+       fputs("<hr/>\n<div id=\"content\">\n", fp);
 }
 
 void
@@ -410,17 +433,20 @@ get_ext(const char *filename)
        return dot + 1;
 }
 
-int
+void
 call_chroma(const char *filename, FILE *fp, const char *s, size_t len)
 {
        htmlized = false;
+       char *html = "";
        // Flush HTML-file
        fflush(fp);
 
-       char *html = cmark_markdown_to_html(s, len, CMARK_OPT_DEFAULT);
+#ifdef HAS_CMARK
+       html = cmark_markdown_to_html(s, len, CMARK_OPT_DEFAULT);
        if (strcmp(get_ext(filename), "md") == 0) htmlized = true;
+#endif
 
-       int lc;
+#ifdef HAS_CHROMA
        if (!htmlized) {
                // Copy STDOUT
                int stdout_copy = dup(1);
@@ -439,7 +465,6 @@ call_chroma(const char *filename, FILE *fp, const char *s, size_t len)
                // Give code to highlight through STDIN:
                size_t i;
                for (i = 0; *s && i < len; s++, i++) {
-                       if (*s == '\n') lc++;
                        fprintf(child, "%c", *s);
                }
 
@@ -452,22 +477,21 @@ call_chroma(const char *filename, FILE *fp, const char *s, size_t len)
        } else {
                fprintf(fp, "%s", html);
        }
-
-       return lc;
+#else
+               fprintf(fp, "<pre>%s</pre>", s);
+#endif
+               free(html);
 }
 
-int
+void
 writeblobhtml(const char *filename, FILE *fp, const git_blob *blob)
 {
-       int lc = 0;
        const char *s = git_blob_rawcontent(blob);
        git_off_t len = git_blob_rawsize(blob);
 
        if (len > 0) {
-               lc = call_chroma(filename, fp, s, len);
+               call_chroma(filename, fp, s, len);
        }
-
-       return lc;
 }
 
 void
@@ -489,12 +513,12 @@ printcommit(FILE *fp, struct commitinfo *ci)
                xmlencode(fp, ci->author->email, strlen(ci->author->email));
                fputs("</a>&gt;\n<b>Date:</b>   ", fp);
                printtime(fp, &(ci->author->when));
-               fputc('\n', fp);
+               putc('\n', fp);
        }
        if (ci->msg) {
-               fputc('\n', fp);
+               putc('\n', fp);
                xmlencode(fp, ci->msg, strlen(ci->msg));
-               fputc('\n', fp);
+               putc('\n', fp);
        }
 }
 
@@ -613,8 +637,9 @@ printshowfile(FILE *fp, struct commitinfo *ci)
                                        fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"d\">-",
                                                i, j, k, i, j, k);
                                else
-                                       fputc(' ', fp);
-                               xmlencode(fp, line->content, line->content_len);
+                                       putc(' ', fp);
+                               xmlencodeline(fp, line->content, line->content_len);
+                               putc('\n', fp);
                                if (line->old_lineno == -1 || line->new_lineno == -1)
                                        fputs("</a>", fp);
                        }
@@ -658,7 +683,6 @@ writelog(FILE *fp, const git_oid *oid)
 
        git_revwalk_new(&w, repo);
        git_revwalk_push(w, oid);
-       git_revwalk_simplify_first_parent(w);
 
        while (!git_revwalk_next(&id, w)) {
                relpath = "";
@@ -739,8 +763,8 @@ printcommitatom(FILE *fp, struct commitinfo *ci)
                xmlencode(fp, ci->summary, strlen(ci->summary));
                fputs("</title>\n", fp);
        }
-       fprintf(fp, "<link rel=\"alternate\" type=\"text/html\" href=\"commit/%s.html\" />\n",
-               ci->oid);
+       fprintf(fp, "<link rel=\"alternate\" type=\"text/html\" href=\"%scommit/%s.html\" />\n",
+               baseurl, ci->oid);
 
        if (ci->author) {
                fputs("<author>\n<name>", fp);
@@ -761,10 +785,10 @@ printcommitatom(FILE *fp, struct commitinfo *ci)
                xmlencode(fp, ci->author->email, strlen(ci->author->email));
                fputs("&gt;\nDate:   ", fp);
                printtime(fp, &(ci->author->when));
-               fputc('\n', fp);
+               putc('\n', fp);
        }
        if (ci->msg) {
-               fputc('\n', fp);
+               putc('\n', fp);
                xmlencode(fp, ci->msg, strlen(ci->msg));
        }
        fputs("\n</content>\n</entry>\n", fp);
@@ -787,7 +811,6 @@ writeatom(FILE *fp)
 
        git_revwalk_new(&w, repo);
        git_revwalk_push_head(w);
-       git_revwalk_simplify_first_parent(w);
 
        for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
                if (!(ci = commitinfo_getbyoid(&id)))
@@ -805,35 +828,33 @@ writeatom(FILE *fp)
 float
 rounder(float var)
 {
-    float value = (int)(var * 10 + .5);
-    return (float)value / 10;
+    int value = var * 10 + .5;
+    return value / 10.0;
 }
 
 const char *
 convertbytes(int bytes)
 {
-       (float)bytes;
-       static char outp[255] = "hi";
+       bytes = (float)bytes;
+       static char outp[25];
        if (bytes < 1024) sprintf(outp, "%u %s", bytes, "B");
        else if (bytes < 1048576) sprintf(outp, "%0.1f %s", rounder(bytes/1024.0), "K");
        else sprintf(outp, "%0.1f %s", rounder(bytes/1048576.0), "M");
        return outp;
 }
 
-int
+void
 writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t filesize)
 {
        char tmp[PATH_MAX] = "", *d;
        const char *p;
-       int lc = 0;
        FILE *fp;
 
        if (strlcpy(tmp, fpath, sizeof(tmp)) >= sizeof(tmp))
                errx(1, "path truncated: '%s'", fpath);
        if (!(d = dirname(tmp)))
                err(1, "dirname");
-       if (mkdirp(d))
-               return -1;
+       mkdirp(d);
 
        for (p = fpath, tmp[0] = '\0'; *p; p++) {
                if (*p == '/' && strlcat(tmp, "../", sizeof(tmp)) >= sizeof(tmp))
@@ -847,12 +868,31 @@ writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t fi
        xmlencode(fp, filename, strlen(filename));
        fprintf(fp, " (%s)", convertbytes((int)filesize));
 
-       fputs("</p><hr/>", fp);
+#ifdef HAS_CMARK
+       char newfpath[PATH_MAX];
+       char newfilename[PATH_MAX];
+       if (strcmp(get_ext(filename), "md") == 0) {
+               fprintf(fp, " <a href=\"%s.html-raw\">View raw</a>", filename);
+               strcpy(newfpath, fpath);
+               strcat(newfpath, "-raw");
+
+               strcpy(newfilename, filename);
+               strcat(newfilename, "-raw");
+               strcpy(oldfilename, filename);
+
+               /* NOTE: recurses */
+               writeblob(obj, newfpath, newfilename, filesize);
+       } else if (strcmp(get_ext(filename), "md-raw" ) == 0) {
+               fprintf(fp, " <a href=\"%s.html\">View rendered</a>", oldfilename);
+       }
+#endif
+
+       fputs(".</p><hr/>", fp);
 
        if (git_blob_is_binary((git_blob *)obj)) {
                fputs("<p>Binary file.</p>\n", fp);
        } else {
-               lc = writeblobhtml(filename, fp, (git_blob *)obj);
+               writeblobhtml(filename, fp, (git_blob *)obj);
                if (ferror(fp))
                        err(1, "fwrite");
        }
@@ -861,8 +901,6 @@ writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t fi
        fclose(fp);
 
        relpath = "";
-
-       return lc;
 }
 
 const char *
@@ -917,7 +955,7 @@ writefilestree(FILE *fp, git_tree *tree, const char *path)
        const char *entryname;
        char filepath[PATH_MAX], entrypath[PATH_MAX];
        size_t count, i;
-       int lc, r, ret;
+       int r, ret;
 
        count = git_tree_entrycount(tree);
        for (i = 0; i < count; i++) {
@@ -949,7 +987,7 @@ writefilestree(FILE *fp, git_tree *tree, const char *path)
                        }
 
                        filesize = git_blob_rawsize((git_blob *)obj);
-                       lc = writeblob(obj, filepath, entryname, filesize);
+                       writeblob(obj, filepath, entryname, filesize);
 
                        fputs("<tr><td>", fp);
                        fputs(filemode(git_tree_entry_filemode(entry)), fp);
@@ -958,10 +996,7 @@ writefilestree(FILE *fp, git_tree *tree, const char *path)
                        fputs("\">", fp);
                        xmlencode(fp, entrypath, strlen(entrypath));
                        fputs("</a></td><td class=\"num\" align=\"right\">", fp);
-                       if (lc > 0)
-                               fprintf(fp, "%dL", lc);
-                       else
-                               fprintf(fp, "%s", convertbytes((int)filesize));
+                       fprintf(fp, "%s", convertbytes((int)filesize));
                        fputs("</td></tr>\n", fp);
                        git_object_free(obj);
                } else if (!git_submodule_lookup(&module, repo, entryname)) {
@@ -1117,7 +1152,8 @@ err:
 void
 usage(char *argv0)
 {
-       fprintf(stderr, "%s [-c cachefile | -l commits] repodir\n", argv0);
+       fprintf(stderr, "%s [-c cachefile | -l commits] "
+               "[-u baseurl] repodir\n", argv0);
        exit(1);
 }
 
@@ -1150,6 +1186,10 @@ main(int argc, char *argv[])
                        if (argv[i][0] == '\0' || *p != '\0' ||
                            nlogcommits <= 0 || errno)
                                usage(argv[0]);
+               } else if (argv[i][1] == 'u') {
+                       if (i + 1 >= argc)
+                               usage(argv[0]);
+                       baseurl = argv[++i];
                }
        }
        if (!repodir)
@@ -1348,6 +1388,7 @@ main(int argc, char *argv[])
        /* cleanup */
        git_repository_free(repo);
        git_libgit2_shutdown();
+       free(strippedname);
 
        return 0;
 }