From 79fab7c6faf78d7bcc0c870359138617aee24c7d Mon Sep 17 00:00:00 2001 From: Quentin Rameau Date: Sun, 10 Jan 2016 00:47:22 +0100 Subject: [PATCH] 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. --- config.def.h | 1 + stagit.c | 25 ++++++++++++++++--------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/config.def.h b/config.def.h index 20a935a..721392f 100644 --- a/config.def.h +++ b/config.def.h @@ -1,2 +1,3 @@ /* See LICENSE file for copyright and license details. */ static const unsigned summarylen = 70; /* summary length in the log */ +static const int showlinecount = 1; /* display line count or file size in file tree index */ diff --git a/stagit.c b/stagit.c index 36041cc..47b6fbc 100644 --- a/stagit.c +++ b/stagit.c @@ -282,11 +282,11 @@ writefooter(FILE *fp) return !fputs("\n\n\n", fp); } -void +int writeblobhtml(FILE *fp, const git_blob *blob) { off_t i; - size_t n = 1; + size_t n = 0; char *nfmt = "%d\n"; const char *s = git_blob_rawcontent(blob); git_off_t len = git_blob_rawsize(blob); @@ -294,6 +294,7 @@ writeblobhtml(FILE *fp, const git_blob *blob) fputs("
\n", fp);
 
 	if (len) {
+		n++;
 		fprintf(fp, nfmt, n, n, n);
 		for (i = 0; i < len - 1; i++) {
 			if (s[i] == '\n') {
@@ -306,6 +307,8 @@ writeblobhtml(FILE *fp, const git_blob *blob)
 	fputs("
\n", fp);
 	xmlencode(fp, s, (size_t)len);
 	fputs("
\n", fp); + + return n; } void @@ -573,12 +576,13 @@ writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t fi char tmp[PATH_MAX] = ""; char *d; const char *p; + int lc = 0; FILE *fp; d = xdirname(fpath); if (mkdirp(d)) { free(d); - return 1; + return -1; } free(d); @@ -600,7 +604,7 @@ writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t fi if (git_blob_is_binary((git_blob *)obj)) { fputs("

Binary file

\n", fp); } else { - writeblobhtml(fp, (git_blob *)obj); + lc = writeblobhtml(fp, (git_blob *)obj); if (ferror(fp)) err(1, "fwrite"); } @@ -609,7 +613,7 @@ writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t fi relpath = ""; - return 0; + return lc; } const char * @@ -663,7 +667,7 @@ writefilestree(FILE *fp, git_tree *tree, const char *branch, const char *path) git_object *obj = NULL; git_off_t filesize; size_t count, i; - int ret; + int lc, ret; count = git_tree_entrycount(tree); for (i = 0; i < count; i++) { @@ -694,15 +698,18 @@ writefilestree(FILE *fp, git_tree *tree, const char *branch, const char *path) filename); filesize = git_blob_rawsize((git_blob *)obj); + lc = writeblob(obj, filepath, filename, filesize); + fputs("", fp); fputs(filemode(git_tree_entry_filemode(entry)), fp); fprintf(fp, "", relpath, filepath); xmlencode(fp, filename, strlen(filename)); fputs("", fp); - fprintf(fp, "%ju", (uintmax_t)filesize); + if (showlinecount && lc > 0) + fprintf(fp, "%dL", lc); + else + fprintf(fp, "%jub", (uintmax_t)filesize); fputs("\n", fp); - - writeblob(obj, filepath, filename, filesize); } return 0; -- 2.30.2