]> git.armaanb.net Git - stagit.git/blobdiff - src/stagit.c
do not simplify the history by first-parent
[stagit.git] / src / stagit.c
index be6d8593c9913d3ac5f02ba7fc2381d46dcaa9ae..24075f6081e2f54024c1babd5ab3e0fa4458d8ad 100644 (file)
@@ -19,8 +19,8 @@
 #include <cmark-gfm.h>
 #endif
 
-#include "cp.h"
 #include "compat.h"
+#include "cp.h"
 
 struct deltainfo {
        git_patch *patch;
@@ -56,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;
 
@@ -71,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;
@@ -279,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)
 {
@@ -366,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 alt=\"Go home\" 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);
@@ -396,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
@@ -426,7 +446,6 @@ call_chroma(const char *filename, FILE *fp, const char *s, size_t len)
        if (strcmp(get_ext(filename), "md") == 0) htmlized = true;
 #endif
 
-
 #ifdef HAS_CHROMA
        if (!htmlized) {
                // Copy STDOUT
@@ -461,6 +480,7 @@ call_chroma(const char *filename, FILE *fp, const char *s, size_t len)
 #else
                fprintf(fp, "<pre>%s</pre>", s);
 #endif
+               free(html);
 }
 
 void
@@ -493,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);
        }
 }
 
@@ -617,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);
                        }
@@ -662,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 = "";
@@ -743,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);
@@ -765,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);
@@ -791,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)))
@@ -849,7 +868,26 @@ 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);
@@ -1114,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);
 }
 
@@ -1147,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)
@@ -1345,6 +1388,7 @@ main(int argc, char *argv[])
        /* cleanup */
        git_repository_free(repo);
        git_libgit2_shutdown();
+       free(strippedname);
 
        return 0;
 }