stagit

stagit: add -l option: limit the amount of commits for the log.html file

Hiltjo Posthuma contact@arjunchoudhary.com

commit: 865be58 parent: a9d441f
2 files changed, 50 insertions(+), 13 deletions(-)
Mstagit.1+13-1
Mstagit.c+37-12
M · stagit.1 +13, -1
 1@@ -1,4 +1,4 @@
 2-.Dd May 1, 2016
 3+.Dd Januari 21, 2018
 4 .Dt STAGIT 1
 5 .Os
 6 .Sh NAME
 7@@ -7,6 +7,7 @@
 8 .Sh SYNOPSIS
 9 .Nm
10 .Op Fl c Ar cachefile
11+.Op Fl l Ar commits
12 .Ar repodir
13 .Sh DESCRIPTION
14 .Nm
15@@ -25,8 +26,19 @@ will store the last commit id and the entries in the HTML table.
16 It is up to the user to make sure the state of the
17 .Ar cachefile
18 is in sync with the history of the repository.
19+.It Fl l Ar commits
20+Write a maximum number of
21+.Ar commits
22+to the log.html file only.
23+However the commit files are written as usual.
24 .El
25 .Pp
26+The options
27+.Fl c
28+and
29+.Fl l
30+cannot be used at the same time.
31+.Pp
32 The following files will be written:
33 .Bl -tag -width Ds
34 .It atom.xml
M · stagit.c +37, -12
 1@@ -57,6 +57,7 @@ static char *strippedname = "";
 2 static char description[255];
 3 static char cloneurl[1024];
 4 static int haslicense, hasreadme, hassubmodules;
 5+static long long nlogcommits = -1; /* < 0 indicates not used */
 6 
 7 /* cache */
 8 static git_oid lastoid;
 9@@ -558,7 +559,7 @@ writelog(FILE *fp, const git_oid *oid)
10 	struct commitinfo *ci;
11 	git_revwalk *w = NULL;
12 	git_oid id;
13-	char path[PATH_MAX];
14+	char path[PATH_MAX], oidstr[GIT_OID_HEXSZ + 1];
15 	FILE *fpfile;
16 	int r;
17 
18@@ -572,24 +573,37 @@ writelog(FILE *fp, const git_oid *oid)
19 
20 		if (cachefile && !memcmp(&id, &lastoid, sizeof(id)))
21 			break;
22+
23+		git_oid_tostr(oidstr, sizeof(oidstr), &id);
24+		r = snprintf(path, sizeof(path), "commit/%s.html", oidstr);
25+		if (r == -1 || (size_t)r >= sizeof(path))
26+			errx(1, "path truncated: 'commit/%s.html'", oidstr);
27+		r = access(path, F_OK);
28+
29+		/* optimization: if there are no log lines to write and
30+		   the commit file already exists: skip the diffstat */
31+		if (!nlogcommits && !r)
32+			continue;
33+
34 		if (!(ci = commitinfo_getbyoid(&id)))
35 			break;
36-		/* lookup stats: only required here */
37+		/* diffstat: for stagit HTML required for the log.html line */
38 		if (commitinfo_getstats(ci) == -1)
39 			goto err;
40 
41-		writelogline(fp, ci);
42+		if (nlogcommits < 0) {
43+			writelogline(fp, ci);
44+		} else if (nlogcommits > 0) {
45+			writelogline(fp, ci);
46+			nlogcommits--;
47+		}
48+
49 		if (cachefile)
50 			writelogline(wcachefp, ci);
51 
52-		relpath = "../";
53-
54-		r = snprintf(path, sizeof(path), "commit/%s.html", ci->oid);
55-		if (r == -1 || (size_t)r >= sizeof(path))
56-			errx(1, "path truncated: 'commit/%s.html'", ci->oid);
57-
58 		/* check if file exists if so skip it */
59-		if (access(path, F_OK)) {
60+		if (r) {
61+			relpath = "../";
62 			fpfile = efopen(path, "w");
63 			writeheader(fpfile, ci->summary);
64 			fputs("<pre>", fpfile);
65@@ -986,7 +1000,7 @@ err:
66 void
67 usage(char *argv0)
68 {
69-	fprintf(stderr, "%s [-c cachefile] repodir\n", argv0);
70+	fprintf(stderr, "%s [-c cachefile] [-l commits] repodir\n", argv0);
71 	exit(1);
72 }
73 
74@@ -1012,9 +1026,20 @@ main(int argc, char *argv[])
75 				usage(argv[0]);
76 			repodir = argv[i];
77 		} else if (argv[i][1] == 'c') {
78-			if (i + 1 >= argc)
79+			if (nlogcommits > 0 || i + 1 >= argc)
80 				usage(argv[0]);
81 			cachefile = argv[++i];
82+		} else if (argv[i][1] == 'l') {
83+			if (cachefile || i + 1 >= argc)
84+				usage(argv[0]);
85+			errno = 0;
86+			nlogcommits = strtoll(argv[++i], &p, 10);
87+			if (argv[i][0] == '\0' || *p != '\0' ||
88+			    nlogcommits <= 0)
89+				usage(argv[0]);
90+			if (errno == ERANGE && (nlogcommits == LLONG_MAX ||
91+			    nlogcommits == LLONG_MIN))
92+				usage(argv[0]);
93 		}
94 	}
95 	if (!cachefile && pledge("stdio rpath wpath cpath", NULL) == -1)