replace file size with line count for text files
Keep the number of line when writing text blobs and print it in the index file tree instead of the size. Still print the size otherwise.
2 files changed, 17 insertions(+), 9 deletions(-) | |||
---|---|---|---|
M | config.def.h | +1 | -0 |
M | stagit.c | +16 | -9 |
1@@ -1,2 +1,3 @@
2 /* See LICENSE file for copyright and license details. */
3 static const unsigned summarylen = 70; /* summary length in the log */
4+static const int showlinecount = 1; /* display line count or file size in file tree index */
M · stagit.c
+16, -9 1@@ -282,11 +282,11 @@ writefooter(FILE *fp)
2 return !fputs("</div>\n</body>\n</html>\n", fp);
3 }
4
5-void
6+int
7 writeblobhtml(FILE *fp, const git_blob *blob)
8 {
9 off_t i;
10- size_t n = 1;
11+ size_t n = 0;
12 char *nfmt = "<a href=\"#l%d\" id=\"l%d\">%d</a>\n";
13 const char *s = git_blob_rawcontent(blob);
14 git_off_t len = git_blob_rawsize(blob);
15@@ -294,6 +294,7 @@ writeblobhtml(FILE *fp, const git_blob *blob)
16 fputs("<table id=\"blob\"><tr><td class=\"num\"><pre>\n", fp);
17
18 if (len) {
19+ n++;
20 fprintf(fp, nfmt, n, n, n);
21 for (i = 0; i < len - 1; i++) {
22 if (s[i] == '\n') {
23@@ -306,6 +307,8 @@ writeblobhtml(FILE *fp, const git_blob *blob)
24 fputs("</pre></td><td><pre>\n", fp);
25 xmlencode(fp, s, (size_t)len);
26 fputs("</pre></td></tr></table>\n", fp);
27+
28+ return n;
29 }
30
31 void
32@@ -573,12 +576,13 @@ writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t fi
33 char tmp[PATH_MAX] = "";
34 char *d;
35 const char *p;
36+ int lc = 0;
37 FILE *fp;
38
39 d = xdirname(fpath);
40 if (mkdirp(d)) {
41 free(d);
42- return 1;
43+ return -1;
44 }
45 free(d);
46
47@@ -600,7 +604,7 @@ writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t fi
48 if (git_blob_is_binary((git_blob *)obj)) {
49 fputs("<p>Binary file</p>\n", fp);
50 } else {
51- writeblobhtml(fp, (git_blob *)obj);
52+ lc = writeblobhtml(fp, (git_blob *)obj);
53 if (ferror(fp))
54 err(1, "fwrite");
55 }
56@@ -609,7 +613,7 @@ writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t fi
57
58 relpath = "";
59
60- return 0;
61+ return lc;
62 }
63
64 const char *
65@@ -663,7 +667,7 @@ writefilestree(FILE *fp, git_tree *tree, const char *branch, const char *path)
66 git_object *obj = NULL;
67 git_off_t filesize;
68 size_t count, i;
69- int ret;
70+ int lc, ret;
71
72 count = git_tree_entrycount(tree);
73 for (i = 0; i < count; i++) {
74@@ -694,15 +698,18 @@ writefilestree(FILE *fp, git_tree *tree, const char *branch, const char *path)
75 filename);
76 filesize = git_blob_rawsize((git_blob *)obj);
77
78+ lc = writeblob(obj, filepath, filename, filesize);
79+
80 fputs("<tr><td>", fp);
81 fputs(filemode(git_tree_entry_filemode(entry)), fp);
82 fprintf(fp, "</td><td><a href=\"%s%s\">", relpath, filepath);
83 xmlencode(fp, filename, strlen(filename));
84 fputs("</a></td><td class=\"num\">", fp);
85- fprintf(fp, "%ju", (uintmax_t)filesize);
86+ if (showlinecount && lc > 0)
87+ fprintf(fp, "%dL", lc);
88+ else
89+ fprintf(fp, "%jub", (uintmax_t)filesize);
90 fputs("</td></tr>\n", fp);
91-
92- writeblob(obj, filepath, filename, filesize);
93 }
94
95 return 0;