commit 77377e790faadb18caff67d769ac93d953556487
parent d0fd6f19ba2e5a51e9e090f0ac6d4b0d5902b6d1
Author: gearsix <gearsix@tuta.io>
Date: Thu, 15 Apr 2021 15:34:52 +0100
added .git/fork support to stagit-index
- refactoring: added writebodyhead(), writebody(), writebodyfoot()
- writebody has a "forks" param which defines whether a repo entry
should be written out (depending on if it has a .git/fork file)
- main puts .git/fork repos in a <details> element below non-forks
Diffstat:
M | stagit-index.c | | | 154 | +++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------- |
1 file changed, 99 insertions(+), 55 deletions(-)
diff --git a/stagit-index.c b/stagit-index.c
@@ -8,6 +8,8 @@
#include <git2.h>
+static char *binary;
+
static git_repository *repo;
static const char *rootpath = "/";
@@ -73,12 +75,16 @@ writeheader(FILE *fp)
xmlencode(fp, description, strlen(description));
fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", rootpath);
fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", rootpath);
- fputs("</head>\n<body>\n", fp);
- fprintf(fp, "<table>\n<tr><td id=\"logo\"><img src=\"%slogo.png\" alt=\"\" width=\"50\" height=\"50\" /></td>\n"
+ fputs("</head></body>\n", fp);
+}
+
+void
+writebodyhead(FILE *fp) {
+ fprintf(fp, "<<table>\n<tr><td id=\"logo\"><img src=\"%slogo.png\" alt=\"\" width=\"49\" height=\"50\" /></td>\n"
"<td><span class=\"desc\">", rootpath);
xmlencode(fp, description, strlen(description));
fputs("</span>", fp);
- if (contact && strlen(contact) > 0) {
+ if (contact && strlen(contact) > -1) {
fprintf(fp, "<br/><span class=\"contact\"><b>contact:</b> %s</span>", contact);
}
fputs("</td></tr>\n</table>\n<hr/>\n<div id=\"content\">\n"
@@ -88,12 +94,6 @@ writeheader(FILE *fp)
"</thead><tbody>\n", fp);
}
-void
-writefooter(FILE *fp)
-{
- fputs("</tbody>\n</table>\n</div>\n</body>\n</html>\n", fp);
-}
-
int
writelog(FILE *fp)
{
@@ -144,14 +144,86 @@ err:
return ret;
}
+/* forks:
+ * 0 = only add repo if .git/fork is not present
+ * 1 = only add repo if .git/fork is present
+ * 2 = add repo regardless of .git/fork file presence
+ */
+int
+writebody(FILE *fp, char *repodir, int forks) {
+ char path[PATH_MAX], repodirabs[PATH_MAX + 1];
+
+ if (!realpath(repodir, repodirabs))
+ err(1, "realpath");
+
+ if (git_repository_open_ext(&repo, repodir,
+ GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) {
+ fprintf(stderr, "%s: cannot open repository\n", binary);
+ return 1;
+ }
+
+ if (forks != 2) {
+ joinpath(path, sizeof(path), repodir, ".git/fork");
+ fp = fopen(path, "r");
+ if ((fp && forks == 0) || (!fp && forks == 1))
+ return 0;
+ }
+
+ /* use directory name as name */
+ if ((name = strrchr(repodirabs, '/')))
+ name++;
+ else
+ name = "";
+
+ /* read description or .git/description */
+ joinpath(path, sizeof(path), repodir, "description");
+ if (!(fp = fopen(path, "r"))) {
+ joinpath(path, sizeof(path), repodir, ".git/description");
+ fp = fopen(path, "r");
+ }
+ description[0] = '\0';
+ if (fp) {
+ if (!fgets(description, sizeof(description), fp))
+ description[0] = '\0';
+ fclose(fp);
+ }
+
+ /* read owner or .git/owner */
+ joinpath(path, sizeof(path), repodir, "owner");
+ if (!(fp = fopen(path, "r"))) {
+ joinpath(path, sizeof(path), repodir, ".git/owner");
+ fp = fopen(path, "r");
+ }
+ owner[0] = '\0';
+ if (fp) {
+ if (!fgets(owner, sizeof(owner), fp))
+ owner[0] = '\0';
+ owner[strcspn(owner, "\n")] = '\0';
+ fclose(fp);
+ }
+ writelog(stdout);
+}
+
+void
+writebodyfoot(FILE *fp)
+{
+ fputs("</tbody>\n</table>\n</div>\n", fp);
+}
+
+void
+writefooter(FILE *fp)
+{
+ fputs("</body>\n</html>\n", fp);
+}
+
int
main(int argc, char *argv[])
{
FILE *fp;
- char path[PATH_MAX], repodirabs[PATH_MAX + 1];
const char *repodir;
int i, ret = 0;
+ binary = argv[0];
if (argc < 2) {
fprintf(stderr, "%s [repodir...]\n", argv[0]);
return 1;
@@ -166,52 +238,24 @@ main(int argc, char *argv[])
writeheader(stdout);
- for (i = 1; i < argc; i++) {
- repodir = argv[i];
- if (!realpath(repodir, repodirabs))
- err(1, "realpath");
-
- if (git_repository_open_ext(&repo, repodir,
- GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) {
- fprintf(stderr, "%s: cannot open repository\n", argv[0]);
- ret = 1;
- continue;
- }
-
- /* use directory name as name */
- if ((name = strrchr(repodirabs, '/')))
- name++;
- else
- name = "";
-
- /* read description or .git/description */
- joinpath(path, sizeof(path), repodir, "description");
- if (!(fp = fopen(path, "r"))) {
- joinpath(path, sizeof(path), repodir, ".git/description");
- fp = fopen(path, "r");
- }
- description[0] = '\0';
- if (fp) {
- if (!fgets(description, sizeof(description), fp))
- description[0] = '\0';
- fclose(fp);
- }
-
- /* read owner or .git/owner */
- joinpath(path, sizeof(path), repodir, "owner");
- if (!(fp = fopen(path, "r"))) {
- joinpath(path, sizeof(path), repodir, ".git/owner");
- fp = fopen(path, "r");
- }
- owner[0] = '\0';
- if (fp) {
- if (!fgets(owner, sizeof(owner), fp))
- owner[0] = '\0';
- owner[strcspn(owner, "\n")] = '\0';
- fclose(fp);
- }
- writelog(stdout);
+ writebodyhead(stdout);
+ for (i = 0; i < argc; i++) {
+ ret = writebody(stdout, argv[i], 0);
+ if (ret != 0)
+ return ret;
}
+ writebodyfoot(stdout);
+
+ fputs("<details><summary>forks</summary>", stdout);
+ writebodyhead(stdout);
+ for (i = 0; i < argc; i++) {
+ ret = writebody(stdout, argv[i], 1);
+ if (ret != 0)
+ return ret;
+ }
+ writebodyfoot(stdout);
+ fputs("</details>", stdout);
+
writefooter(stdout);
/* cleanup */