From 31392c13cf805602c6e4a46695a27325d908f3fb Mon Sep 17 00:00:00 2001 From: Hiltjo Posthuma Date: Sun, 21 Jan 2018 16:47:10 +0100 Subject: [PATCH] stagit: add -l option: limit the amount of commits for the log.html file --- stagit.1 | 14 +++++++++++++- stagit.c | 49 +++++++++++++++++++++++++++++++++++++------------ 2 files changed, 50 insertions(+), 13 deletions(-) diff --git a/stagit.1 b/stagit.1 index 18698d8..3e9a77a 100644 --- a/stagit.1 +++ b/stagit.1 @@ -1,4 +1,4 @@ -.Dd May 1, 2016 +.Dd Januari 21, 2018 .Dt STAGIT 1 .Os .Sh NAME @@ -7,6 +7,7 @@ .Sh SYNOPSIS .Nm .Op Fl c Ar cachefile +.Op Fl l Ar commits .Ar repodir .Sh DESCRIPTION .Nm @@ -25,8 +26,19 @@ will store the last commit id and the entries in the HTML table. It is up to the user to make sure the state of the .Ar cachefile is in sync with the history of the repository. +.It Fl l Ar commits +Write a maximum number of +.Ar commits +to the log.html file only. +However the commit files are written as usual. .El .Pp +The options +.Fl c +and +.Fl l +cannot be used at the same time. +.Pp The following files will be written: .Bl -tag -width Ds .It atom.xml diff --git a/stagit.c b/stagit.c index 1bc57e2..81fdd1d 100644 --- a/stagit.c +++ b/stagit.c @@ -57,6 +57,7 @@ static char *strippedname = ""; static char description[255]; static char cloneurl[1024]; static int haslicense, hasreadme, hassubmodules; +static long long nlogcommits = -1; /* < 0 indicates not used */ /* cache */ static git_oid lastoid; @@ -558,7 +559,7 @@ writelog(FILE *fp, const git_oid *oid) struct commitinfo *ci; git_revwalk *w = NULL; git_oid id; - char path[PATH_MAX]; + char path[PATH_MAX], oidstr[GIT_OID_HEXSZ + 1]; FILE *fpfile; int r; @@ -572,24 +573,37 @@ writelog(FILE *fp, const git_oid *oid) if (cachefile && !memcmp(&id, &lastoid, sizeof(id))) break; + + git_oid_tostr(oidstr, sizeof(oidstr), &id); + r = snprintf(path, sizeof(path), "commit/%s.html", oidstr); + if (r == -1 || (size_t)r >= sizeof(path)) + errx(1, "path truncated: 'commit/%s.html'", oidstr); + r = access(path, F_OK); + + /* optimization: if there are no log lines to write and + the commit file already exists: skip the diffstat */ + if (!nlogcommits && !r) + continue; + if (!(ci = commitinfo_getbyoid(&id))) break; - /* lookup stats: only required here */ + /* diffstat: for stagit HTML required for the log.html line */ if (commitinfo_getstats(ci) == -1) goto err; - writelogline(fp, ci); + if (nlogcommits < 0) { + writelogline(fp, ci); + } else if (nlogcommits > 0) { + writelogline(fp, ci); + nlogcommits--; + } + if (cachefile) writelogline(wcachefp, ci); - relpath = "../"; - - r = snprintf(path, sizeof(path), "commit/%s.html", ci->oid); - if (r == -1 || (size_t)r >= sizeof(path)) - errx(1, "path truncated: 'commit/%s.html'", ci->oid); - /* check if file exists if so skip it */ - if (access(path, F_OK)) { + if (r) { + relpath = "../"; fpfile = efopen(path, "w"); writeheader(fpfile, ci->summary); fputs("
", fpfile);
@@ -986,7 +1000,7 @@ err:
 void
 usage(char *argv0)
 {
-	fprintf(stderr, "%s [-c cachefile] repodir\n", argv0);
+	fprintf(stderr, "%s [-c cachefile] [-l commits] repodir\n", argv0);
 	exit(1);
 }
 
@@ -1012,9 +1026,20 @@ main(int argc, char *argv[])
 				usage(argv[0]);
 			repodir = argv[i];
 		} else if (argv[i][1] == 'c') {
-			if (i + 1 >= argc)
+			if (nlogcommits > 0 || i + 1 >= argc)
 				usage(argv[0]);
 			cachefile = argv[++i];
+		} else if (argv[i][1] == 'l') {
+			if (cachefile || i + 1 >= argc)
+				usage(argv[0]);
+			errno = 0;
+			nlogcommits = strtoll(argv[++i], &p, 10);
+			if (argv[i][0] == '\0' || *p != '\0' ||
+			    nlogcommits <= 0)
+				usage(argv[0]);
+			if (errno == ERANGE && (nlogcommits == LLONG_MAX ||
+			    nlogcommits == LLONG_MIN))
+				usage(argv[0]);
 		}
 	}
 	if (!cachefile && pledge("stdio rpath wpath cpath", NULL) == -1)
-- 
2.39.2