repostead

static page generator for VCS repositories

git clone https://www.shemshak.org/~amin/repostead


main/tree/repostead.c (-rw-r--r--, 32.0 KiB, raw)

   1 /*
   2  * repostead --- repository homesteading tool
   3  *
   4  * Copyright (c) 2026 Amin Bandali <amin@shemshak.org>
   5  *
   6  * repostead is free software: you can redistribute it and/or modify
   7  * it under the terms of the GNU Affero General Public License as
   8  * published by the Free Software Foundation, either version 3 of the
   9  * License, or (at your option) any later version.
  10  *
  11  * repostead is distributed in the hope that it will be useful,
  12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14  * GNU Affero General Public License for more details.
  15  *
  16  * You should have received a copy of the GNU Affero General Public License
  17  * along with repostead.  If not, see <https://www.gnu.org/licenses/>.
  18  */
  19 
  20 #define _XOPEN_SOURCE 700
  21 #include <assert.h>
  22 #include <errno.h>
  23 #include <inttypes.h>
  24 #include <limits.h>
  25 #include <stdarg.h>
  26 #include <stdbool.h>
  27 #include <stdint.h>
  28 #include <stdio.h>
  29 #include <stdlib.h>
  30 #include <string.h>
  31 #include <sys/stat.h>
  32 #include <time.h>
  33 #include <unistd.h>
  34 
  35 #include <git2.h>
  36 
  37 #ifdef HAVE_LOWDOWN
  38 #include <sys/queue.h>
  39 #include <lowdown.h>
  40 #endif
  41 
  42 #define RS_NAME "repostead"
  43 #define RS_VERSION "0.1.0"
  44 
  45 #define RS_PATH_MAX 4096
  46 /* UINT64_MAX (20 digits) + space + 3-char unit + NULL */
  47 #define RS_FILE_INFO_SIZE_STRLEN 25
  48 #define RS_FILE_INFO_MODE_STRLEN 11 /* 10 chars + NULL */
  49 
  50 #define RS_TREE_ROOT "tree"
  51 #define RS_RAW_ROOT "raw"
  52 #define RS_LOG_ROOT "log"
  53 
  54 #if defined(__has_c_attribute)
  55 #if __has_c_attribute(nodiscard) \
  56 	&& defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L
  57 #define NODISCARD [[nodiscard]]
  58 #define NORETURN [[noreturn]]
  59 #endif
  60 #endif
  61 #if !defined(NODISCARD)
  62 #if defined(__GNUC__)
  63 #define NODISCARD __attribute__((warn_unused_result))
  64 #else
  65 #define NODISCARD
  66 #endif
  67 #endif
  68 #if !defined(NORETURN)
  69 #if defined(__GNUC__)
  70 #define NORETURN __attribute__((noreturn))
  71 #else
  72 #define NORETURN
  73 #endif
  74 #endif
  75 
  76 struct repostead_config {
  77 	const char *clone_url;
  78 	const char *repo_path;
  79 	const char *out_path;
  80 	const char *repo_name;
  81 	const char *repo_git_dir;
  82 	const char *repo_desc;
  83 	git_repository *repo;
  84 };
  85 
  86 struct repostead_branch {
  87 	const char *name;
  88 	char dir[RS_PATH_MAX];
  89 	git_tree *tree;
  90 };
  91 
  92 struct repostead_file_info {
  93 	const char *name;
  94 	const char *dir;
  95 	char size[RS_FILE_INFO_SIZE_STRLEN];
  96 	char mode[RS_FILE_INFO_MODE_STRLEN];
  97 };
  98 
  99 static const char *const readme_files[] = {
 100 	"README", "README.md"
 101 };
 102 
 103 static const char *const html_ent[UCHAR_MAX + 1] = {
 104 	['<'] = "&lt;",
 105 	['>'] = "&gt;",
 106 	['&'] = "&amp;",
 107 	['"'] = "&quot;",
 108 	['\''] = "&#39;",
 109 	['\0'] = "&#9216;",
 110 	['\f'] = "&#9228;",
 111 	['\v'] = "&#9238;",
 112 };
 113 
 114 ␌
 115 static void
 116 vwarn(const char *fmt, va_list ap)
 117 {
 118 	fputs(RS_NAME ": ", stderr);
 119 	vfprintf(stderr, fmt, ap);
 120 	fputc('\n', stderr);
 121 }
 122 
 123 static void
 124 warn(const char *fmt, ...)
 125 {
 126 	va_list ap;
 127 	va_start(ap, fmt);
 128 	vwarn(fmt, ap);
 129 	va_end(ap);
 130 }
 131 
 132 NORETURN
 133 static void
 134 die(const char *fmt, ...)
 135 {
 136 	va_list ap;
 137 	va_start(ap, fmt);
 138 	vwarn(fmt, ap);
 139 	va_end(ap);
 140 	exit(EXIT_FAILURE);
 141 }
 142 
 143 NORETURN
 144 static void
 145 gitdie(const char *msg)
 146 {
 147 	const git_error *e = git_error_last();
 148 	die("%s: %s", msg, e && e->message ? e->message : "unknown error");
 149 }
 150 
 151 static void
 152 xmkdir(const char *path)
 153 {
 154 	struct stat st;
 155 	int err = 0;
 156 
 157 	if (mkdir(path, 0755) == 0) {
 158 		return;
 159 	}
 160 	err = errno;
 161 	if (err == EEXIST) {
 162 		if (stat(path, &st) == 0 && S_ISDIR(st.st_mode)) {
 163 			return;
 164 		}
 165 		die("failed to create %s: exists and is not a directory", path);
 166 	}
 167 	die("failed to create %s: %s", path, strerror(err));
 168 }
 169 
 170 static void
 171 html_escn(FILE *f, const char *s, size_t len)
 172 {
 173 	const char *run = s, *end = s + len;
 174 
 175 	/* write each run of safe characters in one go */
 176 	for (; s < end; s++) {
 177 		const char *ent = html_ent[(unsigned char)*s];
 178 		if (ent) {
 179 			fwrite(run, 1, (size_t)(s - run), f);
 180 			fputs(ent, f);
 181 			run = s + 1;
 182 		}
 183 	}
 184 	fwrite(run, 1, (size_t)(end - run), f);
 185 }
 186 
 187 static void
 188 html_esc(FILE *f, const char *s)
 189 {
 190 	html_escn(f, s, strlen(s));
 191 }
 192 
 193 static bool
 194 rfc3986_unreserved(int c)
 195 {
 196 	return c > 0 && c < 128 && strchr(
 197 		"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 198 		"abcdefghijklmnopqrstuvwxyz"
 199 		"0123456789"
 200 		"-._~", c) != NULL;
 201 }
 202 
 203 static void
 204 url_encn(FILE *f, const char *s, size_t len)
 205 {
 206 	for (; len > 0; s++, len--) {
 207 		unsigned char c = (unsigned char)*s;
 208 		if (rfc3986_unreserved(c) || c == '/') {
 209 			putc(c, f);
 210 		} else {
 211 			fprintf(f, "%%%02X", c);
 212 		}
 213 	}
 214 }
 215 
 216 static void
 217 url_enc(FILE *f, const char *s)
 218 {
 219 	url_encn(f, s, strlen(s));
 220 }
 221 
 222 static void
 223 write_header(FILE *f, const struct repostead_config *conf, const char *title,
 224 	bool is_root)
 225 {
 226 	fputs(
 227 		"<!DOCTYPE html><html lang=\"en\"><head>"
 228 		"<meta charset=\"utf-8\" />"
 229 		"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />"
 230 		"<meta name=\"color-scheme\" content=\"light dark\" />"
 231 		"<title>", f);
 232 	html_esc(f, title);
 233 	fputs(
 234 		"</title>"
 235 		"<style>"
 236 		"body{font-family:sans-serif}"
 237 		"h1{font-size:1.25em}h2{font-size:1.125em;margin-bottom:0.5em}"
 238 		"h2 small{font-weight:normal}"
 239 		".desc{margin-bottom:0.6em}.clone{margin-top:0.6em}"
 240 		"table{border-collapse:collapse}"
 241 		"th{padding:0.3em 0.6em}td{padding:0.2em 0.6em}"
 242 		"tr:hover,tr:has(a:focus){background-color:#f2f2f2}"
 243 		".tree-list{font-family:monospace}"
 244 		".tree-list a:hover{text-decoration:none}"
 245 		".tree-list .s{text-align:right}"
 246 		".dir .n, .sub .n{font-weight:bold}"
 247 		"pre .ln{display:table-cell;color:grey;text-decoration:none;"
 248 		"user-select:none;border-right:1px solid;padding:0 0.75em 0 0.25em}"
 249 		"pre .ln:hover{color:black}"
 250 		"pre code{display:table-row}"
 251 		"pre code:after{content:\"\";display:table-cell;width:100%}"
 252 		"pre code:target{background-color:#fffbdd}"
 253 		"@media(prefers-color-scheme:dark){"
 254 		"tr:hover,tr:has(a:focus){background-color:#25242b}"
 255 		"pre .ln:hover{color:white}"
 256 		"pre code:target{background-color:#302d17}"
 257 		"}"
 258 		"</style>"
 259 		"</head><body>"
 260 		"<h1>", f);
 261 	html_esc(f, conf->repo_name);
 262 	fputs(
 263 		"</h1>"
 264 		"<p class=\"desc\">", f);
 265 	html_esc(f, conf->repo_desc);
 266 	fputs("</p>", f);
 267 	if (strcmp(conf->clone_url, "") != 0) {
 268 		fprintf(f, "<p class=\"clone\"><code>git clone %s</code></p>",
 269 			conf->clone_url);
 270 	}
 271 	if (!is_root) {
 272 		fputs("<hr />", f);
 273 	}
 274 }
 275 
 276 static void
 277 write_footer(FILE *f)
 278 {
 279 	fputs("</body></html>", f);
 280 }
 281 
 282 NODISCARD
 283 static bool
 284 path_join(char *dest, size_t dsize, const char *a, const char *b)
 285 {
 286 	const char *sep = NULL;
 287 	int n = 0;
 288 
 289 	assert(a && b);
 290 	sep = (*a && a[strlen(a) - 1] != '/' && *b != '/') ? "/" : "";
 291 	n = snprintf(dest, dsize, "%s%s%s", a, sep, b);
 292 	return n >= 0 && (size_t)n < dsize;
 293 }
 294 
 295 static void
 296 xpath_join(char *dest, size_t dsize, const char *a, const char *b)
 297 {
 298 	if(!path_join(dest, dsize, a, b)) {
 299 		die("path too long: %s/%s", a, b);
 300 	}
 301 }
 302 
 303 NODISCARD
 304 static bool
 305 path_ext(char *dest, size_t dsize, const char *path, const char *ext)
 306 {
 307 	const char *dot = NULL;
 308 	int n = 0;
 309 
 310 	assert(path && ext && *ext);
 311 	dot = (*ext == '.') ? "" : ".";
 312 	n = snprintf(dest, dsize, "%s%s%s", path, dot, ext);
 313 	return n >= 0 && (size_t)n < dsize;
 314 }
 315 
 316 static size_t
 317 path_basename(const char *path, size_t *end)
 318 {
 319 	size_t beg_ = 0, end_ = *end;
 320 
 321 	while (end_ > 0 && path[end_ - 1] == '/') {
 322 		end_--;
 323 	}
 324 	beg_ = end_;
 325 	while (beg_ > 0 && path[beg_ - 1] != '/') {
 326 		beg_--;
 327 	}
 328 	*end = end_;
 329 	return beg_;
 330 }
 331 
 332 static size_t
 333 path_dirname(const char *path, size_t *end)
 334 {
 335 	size_t e = *end, end_ = path_basename(path, &e);
 336 
 337 	while (end_ > 0 && path[end_ - 1] == '/') {
 338 		end_--;
 339 	}
 340 	if (end_ == 0 && *end > 0 && path[0] == '/') {
 341 		end_ = 1;
 342 	}
 343 	*end = end_;
 344 	return 0;
 345 }
 346 
 347 static int
 348 mkdirs_(char *path, mode_t mode)
 349 {
 350 	struct stat st;
 351 	size_t len, end;
 352 	char saved;
 353 	int ret;
 354 
 355 	if (mkdir(path, mode) == 0) {
 356 		return 0;
 357 	}
 358 	if (errno == ENOENT) {
 359 		len = end = strlen(path);
 360 		(void)path_dirname(path, &end);
 361 		if (end == 0 || end >= len) {
 362 			return -1;
 363 		}
 364 		saved = path[end];
 365 		path[end] = 0;
 366 		ret = mkdirs_(path, mode | S_IWUSR | S_IXUSR);
 367 		path[end] = saved;
 368 		if (ret != 0) {
 369 			return -1;
 370 		}
 371 		if (mkdir(path, mode) == 0) {
 372 			return 0;
 373 		}
 374 	}
 375 	if (errno == EEXIST) {
 376 		if (stat(path, &st) == 0 && S_ISDIR(st.st_mode)) {
 377 			return 0;
 378 		}
 379 		errno = ENOTDIR;
 380 	}
 381 	return -1;
 382 }
 383 
 384 static int
 385 mkdirs(const char *path, mode_t mode)
 386 {
 387 	char p[RS_PATH_MAX] = "";
 388 	size_t len = strlen(path);
 389 
 390 	if (len == 0) {
 391 		errno = ENOENT;
 392 		return -1;
 393 	}
 394 	else if (len >= sizeof(p)) {
 395 		errno = ENAMETOOLONG;
 396 		return -1;
 397 	}
 398 	memcpy(p, path, len + 1);
 399 	return mkdirs_(p, mode);
 400 }
 401 
 402 static void
 403 xmkdirs(const char *path)
 404 {
 405 	if (mkdirs(path, 0755) < 0) {
 406 		die("failed to mkdirs %s: %s", path, strerror(errno));
 407 	}
 408 }
 409 
 410 static size_t
 411 path_depth(const char *path)
 412 {
 413 	size_t beg, end = strlen(path), n = 0;
 414 
 415 	for (;;) {
 416 		beg = path_basename(path, &end);
 417 		if (beg == end) {
 418 			break;
 419 		}
 420 		n++;
 421 		end = beg;
 422 	}
 423 	return n;
 424 }
 425 
 426 static void
 427 path_ups(FILE *f, size_t ups)
 428 {
 429 	size_t i;
 430 
 431 	if (ups == 0) {
 432 		fputc('.', f);
 433 		return;
 434 	}
 435 	fputs("..", f);
 436 	for (i = 1; i < ups; i++) {
 437 		fputs("/..", f);
 438 	}
 439 }
 440 
 441 static void
 442 path_crumb(FILE *f, const char *s, size_t len, bool link, size_t ups)
 443 {
 444 	if (link) {
 445 		fputs("<a href=\"", f);
 446 		path_ups(f, ups);
 447 		fputs("\">", f);
 448 	}
 449 	html_escn(f, s, len);
 450 	if (link) {
 451 		fputs("</a>", f);
 452 	}
 453 }
 454 
 455 static void
 456 path_breadcrumb(FILE *f, const char *branch, const char *root,
 457 	const char *path, bool is_file)
 458 {
 459 	size_t n = path_depth(path);
 460 	size_t ups = (is_file && n > 0) ? n - 1 : n;
 461 	size_t beg, end = 0, i;
 462 
 463 	path_crumb(f, branch, strlen(branch), true, ups + 1);
 464 	fputc('/', f);
 465 	path_crumb(f, root, strlen(root), n > 0, ups);
 466 
 467 	for (i = 0; i < n; i++) {
 468 		bool link = i + 1 < n;
 469 
 470 		beg = end + strspn(path + end, "/");
 471 		end = beg + strcspn(path + beg, "/");
 472 		fputc('/', f);
 473 		path_crumb(f, path + beg, end - beg, link,
 474 			link ? ups - (i + 1) : 0);
 475 	}
 476 }
 477 
 478 static void
 479 path_across_roots(FILE *f, const char *from, const char *to,
 480 	const char *dir, const char *name)
 481 {
 482 	if (strcmp(from, to) != 0) {
 483 		path_ups(f, path_depth(dir) + 1);
 484 		fputc('/', f);
 485 		url_enc(f, to);
 486 		fputc('/', f);
 487 		if (*dir) {
 488 			url_enc(f, dir);
 489 			fputc('/', f);
 490 		}
 491 	}
 492 	url_enc(f, name);
 493 }
 494 
 495 static void
 496 branch_path(char *dest, size_t dsize,
 497 	const struct repostead_config *conf,
 498 	const struct repostead_branch *br, const char *root, const char *path)
 499 {
 500 	assert(root && *root && path);
 501 	if (*path) {
 502 		char root_dir[RS_PATH_MAX] = "";
 503 		xpath_join(root_dir, sizeof(root_dir), br->dir, root);
 504 		xpath_join(dest, dsize, root_dir, path);
 505 	} else {
 506 		xpath_join(dest, dsize, br->dir, root);
 507 	}
 508 }
 509 
 510 /* from curl's curlx_strcopy */
 511 static void
 512 str_copy(char *dest, size_t dsize, const char *src, size_t slen)
 513 {
 514 	assert(slen < dsize);
 515 	if (slen < dsize) {
 516 		memcpy(dest, src, slen);
 517 		dest[slen] = 0;
 518 	} else if (dsize) {
 519 		dest[0] = 0;
 520 	}
 521 }
 522 
 523 static void
 524 repo_name_from_path(char *dest, size_t dsize, const char *path)
 525 {
 526 	size_t end = strlen(path);
 527 	size_t beg = path_basename(path, &end);
 528 
 529 	if (end - beg == 4 && memcmp(path + beg, ".git", 4) == 0) {
 530 		/* repo/.git -> repo */
 531 		end = beg;
 532 		beg = path_basename(path, &end);
 533 	} else if (end - beg > 4 && memcmp(path + end - 4, ".git", 4) == 0) {
 534 		/* repo.git -> repo */
 535 		end -= 4;
 536 	}
 537 
 538 	if (end == beg) {
 539 		die("could not make a repo name from \"%s\"", path);
 540 	}
 541 	if (end - beg >= dsize) {
 542 		die("repo name too long");
 543 	}
 544 	str_copy(dest, dsize, path + beg, end - beg);
 545 }
 546 
 547 NODISCARD
 548 static bool
 549 format_bytes(char *dest, size_t dsize, uint64_t bytes)
 550 {
 551 	static const char *const units[] = {
 552 		"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"
 553 	};
 554 	const int max_unit = ((int)(sizeof(units) / sizeof(units[0]))) - 1;
 555 	double size;
 556 	int i, n;
 557 
 558 	assert(dest);
 559 	size = (double)bytes;
 560 	i = 0;
 561 
 562 	while (size >= 1024.0 && i < max_unit) {
 563 		size /= 1024.0;
 564 		i++;
 565 	}
 566 
 567 	/* if we round up to 1024.0, move up to the next unit */
 568 	if (i > 0 && i < max_unit && size >= 1023.95) {
 569 		size /= 1024.0;
 570 		i++;
 571 	}
 572 
 573 	if (i == 0) {
 574 		n = snprintf(dest, dsize, "%" PRIu64 " %s", bytes, units[0]);
 575 	} else {
 576 		n = snprintf(dest, dsize, "%.1f %s", size, units[i]);
 577 	}
 578 
 579 	return n >= 0 && ((size_t)n) < dsize;
 580 }
 581 
 582 static char *
 583 git_filemode_str(char dest[RS_FILE_INFO_MODE_STRLEN], git_filemode_t mode)
 584 {
 585 	static const char rwx[] = "rwxrwxrwx";
 586 	size_t i;
 587 
 588 	memset(dest, '-', RS_FILE_INFO_MODE_STRLEN - 1);
 589 	dest[RS_FILE_INFO_MODE_STRLEN - 1] = 0;
 590 
 591 	switch (mode & 0170000) {
 592 	case 0100000: /* blob */
 593 		for (i = 0; i < sizeof(rwx) - 1; i++) {
 594 			if (mode & (0400 >> i)) {
 595 				dest[i + 1] = rwx[i];
 596 			}
 597 		}
 598 		break;
 599 	case GIT_FILEMODE_TREE:
 600 		dest[0] = 'd';
 601 		break;
 602 	case GIT_FILEMODE_LINK:
 603 		dest[0] = 'l';
 604 		break;
 605 	case GIT_FILEMODE_COMMIT:
 606 		dest[0] = 'm';
 607 		break;
 608 	default:
 609 		dest[0] = '?';
 610 		break;
 611 	}
 612 
 613 	return dest;
 614 }
 615 
 616 static bool
 617 tree_name_okn(const char *name, size_t len)
 618 {
 619 	return len > 0
 620 		&& !(len == 1 && name[0] == '.')
 621 		&& !(len == 2 && name[0] == '.' && name[1] == '.')
 622 		&& memchr(name, '/', len) == NULL;
 623 }
 624 
 625 static bool
 626 tree_name_ok(const char *name)
 627 {
 628 	return tree_name_okn(name, strlen(name));
 629 }
 630 
 631 static bool
 632 branch_name_ok(const char *name)
 633 {
 634 	for (;;) {
 635 		size_t len = strcspn(name, "/");
 636 
 637 		if (!tree_name_okn(name, len)) {
 638 			return false;
 639 		}
 640 		if (name[len] == 0) {
 641 			return true;
 642 		}
 643 		name += len + 1;
 644 	}
 645 }
 646 
 647 static FILE *
 648 page_begin(const struct repostead_config *conf,
 649 	const char *path, const char *title, bool is_root)
 650 {
 651 	FILE *f = fopen(path, "w");
 652 	if (!f) {
 653 		die("failed to open %s for writing: %s", path, strerror(errno));
 654 	}
 655 	write_header(f, conf, title, is_root);
 656 	return f;
 657 }
 658 
 659 static void
 660 page_end(FILE *f, const char *path)
 661 {
 662 	int err = 0;
 663 	write_footer(f);
 664 	err = ferror(f);
 665 	if (fclose(f) == EOF || err) {
 666 		die("error writing %s: %s", path, strerror(errno));
 667 	}
 668 }
 669 
 670 static size_t
 671 str_linecount(const char *s, size_t len)
 672 {
 673 	const char *end = s + len, *p = s;
 674 	size_t n = 0;
 675 
 676 	while (p < end && (p = memchr(p, '\n', (size_t)(end - p))) != NULL) {
 677 		n++;
 678 		p++;
 679 	}
 680 	if (len > 0 && s[len - 1] != '\n') {
 681 		n++;
 682 	}
 683 	return n;
 684 }
 685 
 686 static void
 687 print_listing(FILE *f, const char *s, size_t len)
 688 {
 689 	const char *end = s + len;
 690 	size_t total = str_linecount(s, len), l = 1, t = 0;
 691 	int width = 1;
 692 
 693 	for (t = total; t >= 10; t /= 10) {
 694 		width++;
 695 	}
 696 
 697 	fputs("<pre>", f);
 698 	while (s < end) {
 699 		const char *nl = memchr(s, '\n', (size_t)(end - s));
 700 		const char *eol = nl ? nl : end;
 701 
 702 		fprintf(f, "<code id=\"L%zu\">", l);
 703 		fprintf(f, "<a href=\"#L%zu\" class=\"ln\">%*zu</a>&nbsp;",
 704 			l, width, l);
 705 		html_escn(f, s, (size_t)(eol - s));
 706 		fputs("</code>", f);
 707 		if (nl) {
 708 			putc('\n', f);
 709 			s = nl + 1;
 710 		} else {
 711 			s = end;
 712 		}
 713 		l++;
 714 	}
 715 	fputs("</pre>", f);
 716 }
 717 
 718 ␌
 719 static void
 720 process_file(struct repostead_file_info *fi,
 721 	const struct repostead_config *conf,
 722 	const struct repostead_branch *br, const git_blob *blob)
 723 {
 724 	char html_dir[RS_PATH_MAX] = "";
 725 	char raw_dir[RS_PATH_MAX] = "";
 726 	char file_path[RS_PATH_MAX] = "";
 727 	char file_html_path[RS_PATH_MAX] = "";
 728 	char file_html_base[RS_PATH_MAX] = "";
 729 	char file_rel_path[RS_PATH_MAX] = "";
 730 	FILE *file = NULL;
 731 	FILE *file_html = NULL;
 732 	const void *data = NULL;
 733 	git_object_size_t size = 0;
 734 
 735 	branch_path(html_dir, sizeof(html_dir), conf,
 736 		br, RS_TREE_ROOT, fi->dir);
 737 	branch_path(raw_dir, sizeof(raw_dir), conf,
 738 		br, RS_RAW_ROOT, fi->dir);
 739 
 740 	xpath_join(file_path, sizeof(file_path), raw_dir, fi->name);
 741 	xpath_join(file_html_base, sizeof(file_html_base), html_dir, fi->name);
 742 	if (!path_ext(file_html_path, sizeof(file_html_path),
 743 			file_html_base, "html")) {
 744 		die("file path too long %s.html", file_html_base);
 745 	}
 746 	xpath_join(file_rel_path, sizeof(file_rel_path), fi->dir, fi->name);
 747 
 748 	data = git_blob_rawcontent(blob);
 749 	size = git_blob_rawsize(blob);
 750 
 751 	if (size > SIZE_MAX) {
 752 		die("blob too large");
 753 	}
 754 
 755 	file = fopen(file_path, "wb");
 756 	if (!file) {
 757 		die("failed to open file %s for writing: %s",
 758 			file_path, strerror(errno));
 759 	}
 760 
 761 	if (size > 0 && fwrite(data, 1, (size_t)size, file) != (size_t)size) {
 762 		die("error writing %s", file_path);
 763 	} else if (fclose(file) == EOF) {
 764 		die("error writing %s: %s", file_path, strerror(errno));
 765 	}
 766 
 767 	file_html = page_begin(conf, file_html_path, fi->name, false);
 768 
 769 	if (!format_bytes(fi->size, sizeof(fi->size), size)) {
 770 		die("file size string too long");
 771 	}
 772 
 773 	fputs("<h2>", file_html);
 774 	path_breadcrumb(file_html, br->name, RS_TREE_ROOT, file_rel_path, true);
 775 	fprintf(file_html, " <small>(<span class=\"m\">%s</span>, ", fi->mode);
 776 	fprintf(file_html, "<span class=\"s\">%s</span>, ", fi->size);
 777 	fputs("<a class=\"raw\" href=\"", file_html);
 778 	path_across_roots(file_html, RS_TREE_ROOT, RS_RAW_ROOT,
 779 		fi->dir, fi->name);
 780 	fputs("\">raw</a>)</small></h2>", file_html);
 781 
 782 	if (git_blob_is_binary(blob)) {
 783 		fputs("<p>binary file</p>", file_html);
 784 	} else {
 785 		print_listing(file_html, data, (size_t)size);
 786 	}
 787 
 788 	page_end(file_html, file_html_path);
 789 }
 790 
 791 static void
 792 tree_readme(FILE *f, const struct repostead_config *conf, const git_tree *tree)
 793 {
 794 	size_t i = 0;
 795 	const git_tree_entry *e = NULL;
 796 
 797 	for (i = 0; i < sizeof(readme_files) / sizeof(readme_files[0]); i++) {
 798 		e = git_tree_entry_byname(tree, readme_files[i]);
 799 		if (e && git_tree_entry_type(e) == GIT_OBJECT_BLOB) {
 800 			const git_oid *oid = git_tree_entry_id(e);
 801 			git_blob *blob = NULL;
 802 			const void *data = NULL;
 803 			git_object_size_t size = 0;
 804 #ifdef HAVE_LOWDOWN
 805 			size_t fnlen = strlen(readme_files[i]);
 806 			bool is_md = (fnlen >= 3
 807 				&& strcmp(readme_files[i] + fnlen - 3, ".md") == 0);
 808 			struct lowdown_buf *out;
 809 			struct lowdown_doc *doc;
 810 			struct lowdown_node *n;
 811 			struct lowdown_opts opts;
 812 			char *ovr[] = { "shiftheadinglevelby: 1\n" };
 813 			void *rndr;
 814 #endif
 815 
 816 			if (git_blob_lookup(&blob, conf->repo, oid) != 0) {
 817 				gitdie("git_blob_lookup");
 818 			}
 819 			data = git_blob_rawcontent(blob);
 820 			size = git_blob_rawsize(blob);
 821 			if (size > SIZE_MAX) {
 822 				die("blob too large");
 823 			}
 824 			fputs("<h2 id=\"readme\">", f);
 825 			html_esc(f, readme_files[i]);
 826 			fputs("</h2>", f);
 827 #ifdef HAVE_LOWDOWN
 828 			if (is_md) {
 829 				memset(&opts, 0, sizeof(opts));
 830 				opts.type = LOWDOWN_HTML;
 831 				opts.maxdepth = 128;
 832 				opts.metaovr = ovr;
 833 				opts.metaovrsz = 1;
 834 				if ((doc = lowdown_doc_new(&opts)) == NULL) {
 835 					die("lowdown_doc_new");
 836 				}
 837 				if ((n = lowdown_doc_parse(doc, NULL, data,
 838 						(size_t)size, NULL)) == NULL) {
 839 					die("lowdown_doc_parse");
 840 				}
 841 				if ((out = lowdown_buf_new(256)) == NULL) {
 842 					die("lowdown_buf_new");
 843 				}
 844 				if ((rndr = lowdown_html_new(NULL)) == NULL) {
 845 					die("lowdown_html_new");
 846 				}
 847 				if (!lowdown_html_rndr(out, rndr, n)) {
 848 					die("lowdown_html_rndr");
 849 				}
 850 				fwrite(out->data, 1, out->size, f);
 851 				lowdown_html_free(rndr);
 852 				lowdown_buf_free(out);
 853 				lowdown_node_free(n);
 854 				lowdown_doc_free(doc);
 855 			} else {
 856 #endif
 857 			print_listing(f, data, (size_t)size);
 858 #ifdef HAVE_LOWDOWN
 859 			}
 860 #endif
 861 			git_blob_free(blob);
 862 		}
 863 	}
 864 }
 865 
 866 static void
 867 process_tree(const struct repostead_config *conf,
 868 	const struct repostead_branch *br,
 869 	const git_tree *tree, const char *name, const char *path)
 870 {
 871 	char tree_path[RS_PATH_MAX] = "";
 872 	char raw_path[RS_PATH_MAX] = "";
 873 	const char *index_file_name = "index.html";
 874 	char index_file_path[RS_PATH_MAX] = "";
 875 	FILE *index_file = NULL;
 876 	size_t i = 0, count = git_tree_entrycount(tree);
 877 
 878 	branch_path(tree_path, sizeof(tree_path), conf, br, RS_TREE_ROOT, path);
 879 	xmkdir(tree_path);
 880 	branch_path(raw_path, sizeof(raw_path), conf, br, RS_RAW_ROOT, path);
 881 	xmkdir(raw_path);
 882 
 883 	xpath_join(index_file_path, sizeof(index_file_path),
 884 		tree_path, index_file_name);
 885 	index_file = page_begin(conf, index_file_path, name, false);
 886 
 887 	fputs("<h2>", index_file);
 888 	path_breadcrumb(index_file, br->name, RS_TREE_ROOT, path, false);
 889 	fputs("</h2>", index_file);
 890 
 891 	/* directories and submodules */
 892 	fputs("<table class=\"tree-list\">", index_file);
 893 	for (i = 0; i < count; i++) {
 894 		const git_tree_entry *e = git_tree_entry_byindex(tree, i);
 895 		if (git_tree_entry_type(e) == GIT_OBJECT_TREE) {
 896 			const git_oid *oid = git_tree_entry_id(e);
 897 			char mode[RS_FILE_INFO_MODE_STRLEN] = "";
 898 			git_filemode_str(mode, git_tree_entry_filemode_raw(e));
 899 			const char *child_name = git_tree_entry_name(e);
 900 			char child_path[RS_PATH_MAX];
 901 			git_tree *child = NULL;
 902 
 903 			if (!tree_name_ok(child_name)) {
 904 				die("child tree name unsafe");
 905 			}
 906 
 907 			xpath_join(child_path, sizeof(child_path),
 908 				path, child_name);
 909 			fprintf(index_file,
 910 				"<tr class=\"dir\">"
 911 				"<td class=\"m\">%s</td>",
 912 				mode);
 913 			fputs("<td class=\"n\"><a href=\"", index_file);
 914 			url_enc(index_file, child_name);
 915 			fputs("/\">", index_file);
 916 			html_esc(index_file, child_name);
 917 			fputs("/</a></td><td></td></tr>", index_file);
 918 			if (git_tree_lookup(&child, conf->repo, oid) != 0) {
 919 				gitdie("git_tree_lookup");
 920 			}
 921 			process_tree(conf, br, child, child_name, child_path);
 922 			git_tree_free(child);
 923 		}
 924 		else if (git_tree_entry_filemode(e) == GIT_FILEMODE_COMMIT) {
 925 			const git_oid *oid = git_tree_entry_id(e);
 926 			char mode[RS_FILE_INFO_MODE_STRLEN] = "";
 927 			git_filemode_str(mode, git_tree_entry_filemode_raw(e));
 928 			const char *oid_str = git_oid_tostr_s(oid);
 929 			fprintf(index_file,
 930 				"<tr class=\"sub\">"
 931 				"<td class=\"m\">%s</td>",
 932 				mode);
 933 			fputs("<td class=\"n\">", index_file);
 934 			html_esc(index_file, git_tree_entry_name(e));
 935 			fprintf(index_file,
 936 				" @ %s</td>"
 937 				"<td></td></tr>",
 938 				oid_str);
 939 		}
 940 	}
 941 	/* everything else */
 942 	for (i = 0; i < count; i++) {
 943 		const git_tree_entry *e = git_tree_entry_byindex(tree, i);
 944 		const git_oid *oid = git_tree_entry_id(e);
 945 		if (!(git_tree_entry_type(e) == GIT_OBJECT_TREE
 946 				|| git_tree_entry_filemode(e) == GIT_FILEMODE_COMMIT)) {
 947 			git_blob *blob = NULL;
 948 			struct repostead_file_info fi;
 949 
 950 			if (git_blob_lookup(&blob, conf->repo, oid) != 0) {
 951 				gitdie("git_blob_lookup");
 952 			}
 953 
 954 			memset(&fi, 0, sizeof(fi));
 955 			fi.name = git_tree_entry_name(e);
 956 			fi.dir = path;
 957 
 958 			if (!tree_name_ok(fi.name)) {
 959 				die("child tree name unsafe");
 960 			}
 961 
 962 			git_filemode_str(fi.mode, git_tree_entry_filemode_raw(e));
 963 			process_file(&fi, conf, br, blob);
 964 			fprintf(index_file,
 965 				"<tr class=\"file\">"
 966 				"<td class=\"m\">%s</td>",
 967 				fi.mode);
 968 			fputs("<td class=\"n\"><a href=\"", index_file);
 969 			url_enc(index_file, fi.name);
 970 			fputs(".html\">", index_file);
 971 			html_esc(index_file, fi.name);
 972 			fprintf(index_file,
 973 				"</a></td>"
 974 				"<td class=\"s\">%s</td></tr>",
 975 				fi.size);
 976 			git_blob_free(blob);
 977 		}
 978 	}
 979 	fputs("</table>", index_file);
 980 
 981 	tree_readme(index_file, conf, tree);
 982 
 983 	page_end(index_file, index_file_path);
 984 }
 985 
 986 static int
 987 print_diff_line(const git_diff_delta *delta,
 988 	const git_diff_hunk *hunk,
 989 	const git_diff_line *line,
 990 	void *f) /* FILE *f */
 991 {
 992 	if (line->origin == GIT_DIFF_LINE_CONTEXT
 993 			|| line->origin == GIT_DIFF_LINE_ADDITION
 994 			|| line->origin == GIT_DIFF_LINE_DELETION) {
 995 		fprintf((FILE *)f, "%c", line->origin);
 996 	}
 997 	html_escn((FILE *)f, line->content, line->content_len);
 998 	return 0;
 999 }
1000 
1001 static void
1002 process_commit(const struct repostead_config *conf,
1003 	const struct repostead_branch *br, git_commit *commit)
1004 {
1005 	char file_base_path[RS_PATH_MAX] = "";
1006 	char file_path[RS_PATH_MAX] = "";
1007 	FILE *file = NULL;
1008 	git_commit *parent = NULL;
1009 	git_tree *parent_tree = NULL;
1010 	git_tree *tree = NULL;
1011 	git_diff *diff = NULL;
1012 	const git_oid *oid = NULL;
1013 	const git_oid *parent_oid = NULL;
1014 	const char *oid_str;
1015 	const char *parent_oid_str;
1016 
1017 	if (git_commit_parentcount(commit) > 0) {
1018 		if (git_commit_parent(&parent, commit, 0) < 0) {
1019 			gitdie("git_commit_parent");
1020 		}
1021 		if (git_commit_tree(&parent_tree, parent) < 0) {
1022 			gitdie("git_commit_tree");
1023 		}
1024 		parent_oid = git_commit_id(parent);
1025 		if (!parent_oid) {
1026 			gitdie("git_commit_id");
1027 		}
1028 		parent_oid_str = git_oid_tostr_s(parent_oid);
1029 		if (!parent_oid_str) {
1030 			gitdie("git_oid_tostr_s");
1031 		}
1032 	}
1033 
1034 	oid = git_commit_id(commit);
1035 	if (!oid) {
1036 		gitdie("git_commit_id");
1037 	}
1038 
1039 	oid_str = git_oid_tostr_s(oid);
1040 	if (!oid_str) {
1041 		gitdie("git_oid_tostr_s");
1042 	}
1043 
1044 	if (git_commit_tree(&tree, commit) < 0) {
1045 		gitdie("git_commit_tree");
1046 	}
1047 
1048 	if (git_diff_tree_to_tree(&diff, conf->repo, parent_tree,
1049 			tree, NULL) < 0) {
1050 		gitdie("git_diff_tree_to_tree");
1051 	}
1052 
1053 	branch_path(file_base_path, sizeof(file_base_path), conf, br,
1054 		RS_LOG_ROOT, oid_str);
1055 	if (!path_ext(file_path, sizeof(file_path), file_base_path, "html")) {
1056 		die("file path too long %s.html", file_base_path);
1057 	}
1058 
1059 	file = page_begin(conf, file_path, oid_str, false);
1060 
1061 	fputs("<pre>", file);
1062 	if (git_diff_print(diff, GIT_DIFF_FORMAT_PATCH,
1063 			print_diff_line, file) < 0) {
1064 		gitdie("git_diff_print");
1065 	}
1066 	fputs("</pre>", file);
1067 
1068 	page_end(file, file_path);
1069 
1070 	git_diff_free(diff);
1071 	git_tree_free(tree);
1072 	git_tree_free(parent_tree);
1073 	git_commit_free(parent);
1074 }
1075 
1076 static void
1077 process_commits(const struct repostead_config *conf,
1078 	const struct repostead_branch *br, const git_oid *commit_oid)
1079 {
1080 	char log_path[RS_PATH_MAX] = "";
1081 	const char *index_file_name = "index.html";
1082 	char index_file_path[RS_PATH_MAX] = "";
1083 	FILE *index_file = NULL;
1084 	git_revwalk *walker = NULL;
1085 	git_oid oid;
1086 	int error = 0;
1087 
1088 	branch_path(log_path, sizeof(log_path), conf, br, RS_LOG_ROOT, "");
1089 	xmkdir(log_path);
1090 
1091 	xpath_join(index_file_path, sizeof(index_file_path),
1092 		log_path, index_file_name);
1093 	index_file = page_begin(conf, index_file_path, br->name, false);
1094 
1095 	if (git_revwalk_new(&walker, conf->repo) < 0) {
1096 		gitdie("git_revwalk_new");
1097 	}
1098 	if (git_revwalk_push(walker, commit_oid) < 0) {
1099 		gitdie("git_revwalk_push");
1100 	}
1101 
1102 	fputs("<table class=\"commit-list\">", index_file);
1103 	while ((error = git_revwalk_next(&oid, walker)) == 0) {
1104 		git_commit *c = NULL;
1105 		if (git_commit_lookup(&c, conf->repo, &oid) == 0) {
1106 			const char *summary = git_commit_summary(c);
1107 			const git_signature *author = git_commit_author(c);
1108 			const char *oid_str = git_oid_tostr_s(&oid);
1109 
1110 			if (!oid_str) {
1111 				gitdie("git_oid_tostr_s");
1112 			}
1113 			fputs("<tr class=\"commit\">", index_file);
1114 			if (author) {
1115 				struct tm tm;
1116 				char tbuf[11];
1117 				git_time_t sec = author->when.time;
1118 				int off = author->when.offset;
1119 				time_t t = (time_t)(sec + (git_time_t)off * 60);
1120 				gmtime_r(&t, &tm);
1121 				strftime(tbuf, sizeof(tbuf), "%Y-%m-%d", &tm);
1122 				fputs("<td class=\"date\">", index_file);
1123 				fputs(tbuf, index_file);
1124 				fputs("</td>", index_file);
1125 			}
1126 			fputs("<td class=\"summary\"><a href=\"", index_file);
1127 			url_enc(index_file, oid_str);
1128 			fputs(".html\">", index_file);
1129 			html_esc(index_file, summary ? summary : "(no message)");
1130 			fputs("</a></td>", index_file);
1131 			if (author) {
1132 				fputs("<td class=\"author\">", index_file);
1133 				html_esc(index_file, author->name ? author->name : "");
1134 				fputs("</td>", index_file);
1135 			}
1136 			fputs("</tr>", index_file);
1137 			process_commit(conf, br, c);
1138 			git_commit_free(c);
1139 		} else {
1140 			gitdie("git_commit_lookup");
1141 		}
1142 	}
1143 	fputs("</table>", index_file);
1144 
1145 	if (error != GIT_ITEROVER) {
1146 		gitdie("git_revwalk_next");
1147 	}
1148 
1149 	git_revwalk_free(walker);
1150 
1151 	page_end(index_file, index_file_path);
1152 }
1153 
1154 NODISCARD
1155 static git_tree *
1156 process_branch(const struct repostead_config *conf,
1157 	const char *name, const char *dir, const git_oid *tip_oid)
1158 {
1159 	git_commit *tip = NULL;
1160 	git_tree *tree = NULL;
1161 	struct repostead_branch br;
1162 
1163 	memset(&br, 0, sizeof(br));
1164 
1165 	if (git_commit_lookup(&tip, conf->repo, tip_oid) < 0) {
1166 		gitdie("git_commit_lookup");
1167 	}
1168 	if (git_commit_tree(&tree, tip) < 0) {
1169 		gitdie("git_commit_tree");
1170 	}
1171 
1172 	br.name = name;
1173 	br.tree = tree;
1174 	xpath_join(br.dir, sizeof(br.dir), conf->out_path, dir);
1175 
1176 	/* mkdirs in case branch name contains '/' */
1177 	xmkdirs(br.dir);
1178 
1179 	process_tree(conf, &br, br.tree, br.name, "");
1180 	process_commits(conf, &br, tip_oid);
1181 
1182 	git_commit_free(tip);
1183 	return tree;
1184 }
1185 
1186 static int
1187 read_desc_file(char *dest, size_t dsize, const char *git_dir)
1188 {
1189 	const char *desc_file_name = "description";
1190 	char desc_file_path[RS_PATH_MAX] = "";
1191 	FILE *fp = NULL;
1192 	size_t n = 0;
1193 	bool truncated = false;
1194 
1195 	if (dsize == 0) {
1196 		return -1;
1197 	}
1198 	dest[0] = 0;
1199 
1200 	if (path_join(desc_file_path, sizeof(desc_file_path),
1201 			git_dir, desc_file_name)) {
1202 		fp = fopen(desc_file_path, "rb");
1203 		if (fp) {
1204 			n = fread(dest, 1, dsize - 1, fp);
1205 			if (ferror(fp)) {
1206 				fclose(fp);
1207 				dest[0] = 0;
1208 				warn("error reading repo description file, ignoring");
1209 				return -1;
1210 			}
1211 			truncated = (n == dsize - 1) && (fgetc(fp) != EOF);
1212 			fclose(fp);
1213 			dest[n] = 0;
1214 			return truncated ? -2 : 0;
1215 		} else {
1216 			warn("failed to open repo description file for reading: %s",
1217 				strerror(errno));
1218 			return -1;
1219 		}
1220 	} else {
1221 		warn("repo description file path too long, ignoring");
1222 		return -1;
1223 	}
1224 }
1225 
1226 static void
1227 process_repo(struct repostead_config *conf)
1228 {
1229 	char repo_desc[RS_PATH_MAX] = "";
1230 	const char *index_file_name = "index.html";
1231 	char index_file_path[RS_PATH_MAX] = "";
1232 	FILE *index_file = NULL;
1233 	git_branch_iterator *brit = NULL;
1234 	git_branch_t brty;
1235 	git_reference *ref = NULL;
1236 	git_tree *head_tree = NULL;
1237 	int error = 0;
1238 
1239 	if (git_libgit2_init() < 0) {
1240 		gitdie("git_libgit2_init");
1241 	}
1242 	if (git_repository_open(&conf->repo, conf->repo_path) < 0) {
1243 		gitdie("git_repository_open");
1244 	}
1245 
1246 	conf->repo_git_dir = git_repository_commondir(conf->repo);
1247 	read_desc_file(repo_desc, sizeof(repo_desc), conf->repo_git_dir);
1248 	conf->repo_desc = repo_desc;
1249 
1250 	xpath_join(index_file_path, sizeof(index_file_path),
1251 			conf->out_path, index_file_name);
1252 	index_file = page_begin(conf, index_file_path, conf->repo_name, true);
1253 
1254 	if (git_branch_iterator_new(
1255 			&brit, conf->repo, GIT_BRANCH_LOCAL) < 0) {
1256 		gitdie("git_branch_iterator_new");
1257 	}
1258 
1259 	fputs("<h2>branches</h2><table class=\"branch-list\">", index_file);
1260 	while ((error = git_branch_next(&ref, &brty, brit)) == 0) {
1261 		const char *brnm = NULL;
1262 		const git_oid *tip = git_reference_target(ref);
1263 		bool is_head = (git_branch_is_head(ref) == 1);
1264 
1265 		if (git_branch_name(&brnm, ref) < 0) {
1266 			warn("failed to get branch name, skipping");
1267 		} else if (!branch_name_ok(brnm)) {
1268 			warn("unsafe branch name, skipping");
1269 		}
1270 		fputs("<tr>", index_file);
1271 		if (is_head) {
1272 			fputs("<td class=\"head\"><b>", index_file);
1273 			html_esc(index_file, brnm);
1274 			fputs("</b></td>", index_file);
1275 		} else {
1276 			fputs("<td>", index_file);
1277 			html_esc(index_file, brnm);
1278 			fputs("</td>", index_file);
1279 		}
1280 		if (tip) {
1281 			fputs("<td><a href=\"", index_file);
1282 			url_enc(index_file, brnm);
1283 			fputc('/', index_file);
1284 			url_enc(index_file, RS_TREE_ROOT);
1285 			fputs("/\">tree</a></td>", index_file);
1286 			fputs("<td><a href=\"", index_file);
1287 			url_enc(index_file, brnm);
1288 			fputc('/', index_file);
1289 			url_enc(index_file, RS_LOG_ROOT);
1290 			fputs("/\">log</a></td>", index_file);
1291 
1292 			head_tree = process_branch(conf, brnm, brnm, tip);
1293 			if (!is_head) {
1294 				git_tree_free(head_tree);
1295 				head_tree = NULL;
1296 			}
1297 		}
1298 		fputs("</tr>", index_file);
1299 		git_reference_free(ref);
1300 	}
1301 	fputs("</table>", index_file);
1302 
1303 	if (error != GIT_ITEROVER) {
1304 		gitdie("git_branch_next");
1305 	}
1306 
1307 	if (head_tree) {
1308 		tree_readme(index_file, conf, head_tree);
1309 	}
1310 
1311 	page_end(index_file, index_file_path);
1312 
1313 	git_tree_free(head_tree);
1314 	git_branch_iterator_free(brit);
1315 	git_repository_free(conf->repo);
1316 	conf->repo = NULL;
1317 	conf->repo_desc = NULL;
1318 	conf->repo_git_dir = NULL;
1319 	git_libgit2_shutdown();
1320 }
1321 
1322 ␌
1323 NORETURN
1324 static void
1325 usage(FILE *out, const char *prog, int status)
1326 {
1327 	fprintf(out,
1328 		"Usage: %s [OPTIONS] repo_path\n"
1329 		"Arguments:\n"
1330 		"  repo_path     path to repository\n"
1331 		"Options:\n"
1332 		"  -u clone_url  repository clone URL\n"
1333 		"  -h            show this help and exit\n"
1334 		"  -v            show version and exit\n",
1335 		prog);
1336 	exit(status);
1337 }
1338 
1339 int
1340 main(int argc, char **argv)
1341 {
1342 	const char *prog = argv[0] ? argv[0] : RS_NAME;
1343 	int c = 0;
1344 	char *clone_url = "";
1345 	char *repo_path = NULL;
1346 	char out_path[RS_PATH_MAX] = "";
1347 	char repo_name[RS_PATH_MAX] = "";
1348 	struct repostead_config conf; /* zeroed below */
1349 
1350 	while ((c = getopt(argc, argv, ":u:hv")) != -1) {
1351 		switch (c) {
1352 		case 'u':
1353 			clone_url = optarg;
1354 			break;
1355 		case 'h':
1356 			usage(stdout, prog, EXIT_SUCCESS);
1357 		case 'v':
1358 			printf("%s %s\n", prog, RS_VERSION);
1359 			return EXIT_SUCCESS;
1360 		case ':':
1361 			fprintf(stderr, "%s: option -%c requires an argument\n",
1362 				prog, optopt);
1363 			usage(stderr, prog, EXIT_FAILURE);
1364 		case '?':
1365 		default:
1366 			fprintf(stderr, "%s: unknown option -%c\n",
1367 				prog, optopt);
1368 			usage(stderr, prog, EXIT_FAILURE);
1369 		}
1370 	}
1371 	argc -= optind;
1372 	argv += optind;
1373 
1374 	if (argc != 1) {
1375 		fprintf(stderr, "%s: expected one argument, got %d\n",
1376 			prog, argc);
1377 		usage(stderr, prog, EXIT_FAILURE);
1378 	}
1379 
1380 	repo_path = realpath(argv[0], NULL);
1381 	if (!repo_path) {
1382 		die("failed to resolve path %s: %s", argv[0], strerror(errno));
1383 	}
1384 
1385 	if (!getcwd(out_path, sizeof(out_path))) {
1386 		die("failed to getcwd(): %s", strerror(errno));
1387 	}
1388 
1389 	memset(&conf, 0, sizeof(conf));
1390 	conf.clone_url = clone_url;
1391 	conf.repo_path = repo_path;
1392 	conf.out_path = out_path;
1393 
1394 	repo_name_from_path(repo_name, sizeof(repo_name), conf.repo_path);
1395 	conf.repo_name = repo_name;
1396 	conf.repo_git_dir = ""; /* will be set in process_repo() */
1397 	conf.repo_desc = ""; /* read from .git/description later */
1398 
1399 	process_repo(&conf);
1400 
1401 	free(repo_path);
1402 	return EXIT_SUCCESS;
1403 }