|
diff --git a/cgit.c b/cgit.c index e1d2216..96900bb 100644 --- a/ cgit.c+++ b/ cgit.c |
|
@@ -517,193 +517,193 @@ char *get_first_line(char *txt) |
517 | } |
517 | } |
518 | |
518 | |
519 | void print_repo(FILE *f, struct cgit_repo *repo) |
519 | void print_repo(FILE *f, struct cgit_repo *repo) |
520 | { |
520 | { |
521 | fprintf(f, "repo.url=%s\n", repo->url); |
521 | fprintf(f, "repo.url=%s\n", repo->url); |
522 | fprintf(f, "repo.name=%s\n", repo->name); |
522 | fprintf(f, "repo.name=%s\n", repo->name); |
523 | fprintf(f, "repo.path=%s\n", repo->path); |
523 | fprintf(f, "repo.path=%s\n", repo->path); |
524 | if (repo->owner) |
524 | if (repo->owner) |
525 | fprintf(f, "repo.owner=%s\n", repo->owner); |
525 | fprintf(f, "repo.owner=%s\n", repo->owner); |
526 | if (repo->desc) { |
526 | if (repo->desc) { |
527 | char *tmp = get_first_line(repo->desc); |
527 | char *tmp = get_first_line(repo->desc); |
528 | fprintf(f, "repo.desc=%s\n", tmp); |
528 | fprintf(f, "repo.desc=%s\n", tmp); |
529 | free(tmp); |
529 | free(tmp); |
530 | } |
530 | } |
531 | if (repo->readme) |
531 | if (repo->readme) |
532 | fprintf(f, "repo.readme=%s\n", repo->readme); |
532 | fprintf(f, "repo.readme=%s\n", repo->readme); |
533 | if (repo->defbranch) |
533 | if (repo->defbranch) |
534 | fprintf(f, "repo.defbranch=%s\n", repo->defbranch); |
534 | fprintf(f, "repo.defbranch=%s\n", repo->defbranch); |
535 | if (repo->module_link) |
535 | if (repo->module_link) |
536 | fprintf(f, "repo.module-link=%s\n", repo->module_link); |
536 | fprintf(f, "repo.module-link=%s\n", repo->module_link); |
537 | if (repo->section) |
537 | if (repo->section) |
538 | fprintf(f, "repo.section=%s\n", repo->section); |
538 | fprintf(f, "repo.section=%s\n", repo->section); |
539 | if (repo->clone_url) |
539 | if (repo->clone_url) |
540 | fprintf(f, "repo.clone-url=%s\n", repo->clone_url); |
540 | fprintf(f, "repo.clone-url=%s\n", repo->clone_url); |
541 | fprintf(f, "repo.enable-log-filecount=%d\n", |
541 | fprintf(f, "repo.enable-log-filecount=%d\n", |
542 | repo->enable_log_filecount); |
542 | repo->enable_log_filecount); |
543 | fprintf(f, "repo.enable-log-linecount=%d\n", |
543 | fprintf(f, "repo.enable-log-linecount=%d\n", |
544 | repo->enable_log_linecount); |
544 | repo->enable_log_linecount); |
545 | if (repo->about_filter && repo->about_filter != ctx.cfg.about_filter) |
545 | if (repo->about_filter && repo->about_filter != ctx.cfg.about_filter) |
546 | fprintf(f, "repo.about-filter=%s\n", repo->about_filter->cmd); |
546 | fprintf(f, "repo.about-filter=%s\n", repo->about_filter->cmd); |
547 | if (repo->commit_filter && repo->commit_filter != ctx.cfg.commit_filter) |
547 | if (repo->commit_filter && repo->commit_filter != ctx.cfg.commit_filter) |
548 | fprintf(f, "repo.commit-filter=%s\n", repo->commit_filter->cmd); |
548 | fprintf(f, "repo.commit-filter=%s\n", repo->commit_filter->cmd); |
549 | if (repo->source_filter && repo->source_filter != ctx.cfg.source_filter) |
549 | if (repo->source_filter && repo->source_filter != ctx.cfg.source_filter) |
550 | fprintf(f, "repo.source-filter=%s\n", repo->source_filter->cmd); |
550 | fprintf(f, "repo.source-filter=%s\n", repo->source_filter->cmd); |
551 | if (repo->snapshots != ctx.cfg.snapshots) { |
551 | if (repo->snapshots != ctx.cfg.snapshots) { |
552 | char *tmp = build_snapshot_setting(repo->snapshots); |
552 | char *tmp = build_snapshot_setting(repo->snapshots); |
553 | fprintf(f, "repo.snapshots=%s\n", tmp); |
553 | fprintf(f, "repo.snapshots=%s\n", tmp); |
554 | free(tmp); |
554 | free(tmp); |
555 | } |
555 | } |
556 | if (repo->max_stats != ctx.cfg.max_stats) |
556 | if (repo->max_stats != ctx.cfg.max_stats) |
557 | fprintf(f, "repo.max-stats=%s\n", |
557 | fprintf(f, "repo.max-stats=%s\n", |
558 | cgit_find_stats_periodname(repo->max_stats)); |
558 | cgit_find_stats_periodname(repo->max_stats)); |
559 | fprintf(f, "\n"); |
559 | fprintf(f, "\n"); |
560 | } |
560 | } |
561 | |
561 | |
562 | void print_repolist(FILE *f, struct cgit_repolist *list, int start) |
562 | void print_repolist(FILE *f, struct cgit_repolist *list, int start) |
563 | { |
563 | { |
564 | int i; |
564 | int i; |
565 | |
565 | |
566 | for(i = start; i < list->count; i++) |
566 | for(i = start; i < list->count; i++) |
567 | print_repo(f, &list->repos[i]); |
567 | print_repo(f, &list->repos[i]); |
568 | } |
568 | } |
569 | |
569 | |
570 | /* Scan 'path' for git repositories, save the resulting repolist in 'cached_rc' |
570 | /* Scan 'path' for git repositories, save the resulting repolist in 'cached_rc' |
571 | * and return 0 on success. |
571 | * and return 0 on success. |
572 | */ |
572 | */ |
573 | static int generate_cached_repolist(const char *path, const char *cached_rc) |
573 | static int generate_cached_repolist(const char *path, const char *cached_rc) |
574 | { |
574 | { |
575 | char *locked_rc; |
575 | char *locked_rc; |
576 | int idx; |
576 | int idx; |
577 | FILE *f; |
577 | FILE *f; |
578 | |
578 | |
579 | locked_rc = xstrdup(fmt("%s.lock", cached_rc)); |
579 | locked_rc = xstrdup(fmt("%s.lock", cached_rc)); |
580 | f = fopen(locked_rc, "wx"); |
580 | f = fopen(locked_rc, "wx"); |
581 | if (!f) { |
581 | if (!f) { |
582 | /* Inform about the error unless the lockfile already existed, |
582 | /* Inform about the error unless the lockfile already existed, |
583 | * since that only means we've got concurrent requests. |
583 | * since that only means we've got concurrent requests. |
584 | */ |
584 | */ |
585 | if (errno != EEXIST) |
585 | if (errno != EEXIST) |
586 | fprintf(stderr, "[cgit] Error opening %s: %s (%d)\n", |
586 | fprintf(stderr, "[cgit] Error opening %s: %s (%d)\n", |
587 | locked_rc, strerror(errno), errno); |
587 | locked_rc, strerror(errno), errno); |
588 | return errno; |
588 | return errno; |
589 | } |
589 | } |
590 | idx = cgit_repolist.count; |
590 | idx = cgit_repolist.count; |
591 | if (ctx.cfg.project_list) |
591 | if (ctx.cfg.project_list) |
592 | scan_projects(path, ctx.cfg.project_list, repo_config); |
592 | scan_projects(path, ctx.cfg.project_list, repo_config); |
593 | else |
593 | else |
594 | scan_tree(path, repo_config); |
594 | scan_tree(path, repo_config); |
595 | print_repolist(f, &cgit_repolist, idx); |
595 | print_repolist(f, &cgit_repolist, idx); |
596 | if (rename(locked_rc, cached_rc)) |
596 | if (rename(locked_rc, cached_rc)) |
597 | fprintf(stderr, "[cgit] Error renaming %s to %s: %s (%d)\n", |
597 | fprintf(stderr, "[cgit] Error renaming %s to %s: %s (%d)\n", |
598 | locked_rc, cached_rc, strerror(errno), errno); |
598 | locked_rc, cached_rc, strerror(errno), errno); |
599 | fclose(f); |
599 | fclose(f); |
600 | return 0; |
600 | return 0; |
601 | } |
601 | } |
602 | |
602 | |
603 | static void process_cached_repolist(const char *path) |
603 | static void process_cached_repolist(const char *path) |
604 | { |
604 | { |
605 | struct stat st; |
605 | struct stat st; |
606 | char *cached_rc; |
606 | char *cached_rc; |
607 | time_t age; |
607 | time_t age; |
608 | unsigned long hash; |
608 | unsigned long hash; |
609 | |
609 | |
610 | hash = hash_str(path); |
610 | hash = hash_str(path); |
611 | if (ctx.cfg.project_list) |
611 | if (ctx.cfg.project_list) |
612 | hash += hash_str(ctx.cfg.project_list); |
612 | hash += hash_str(ctx.cfg.project_list); |
613 | cached_rc = xstrdup(fmt("%s/rc-%8x", ctx.cfg.cache_root, hash)); |
613 | cached_rc = xstrdup(fmt("%s/rc-%8lx", ctx.cfg.cache_root, hash)); |
614 | |
614 | |
615 | if (stat(cached_rc, &st)) { |
615 | if (stat(cached_rc, &st)) { |
616 | /* Nothing is cached, we need to scan without forking. And |
616 | /* Nothing is cached, we need to scan without forking. And |
617 | * if we fail to generate a cached repolist, we need to |
617 | * if we fail to generate a cached repolist, we need to |
618 | * invoke scan_tree manually. |
618 | * invoke scan_tree manually. |
619 | */ |
619 | */ |
620 | if (generate_cached_repolist(path, cached_rc)) { |
620 | if (generate_cached_repolist(path, cached_rc)) { |
621 | if (ctx.cfg.project_list) |
621 | if (ctx.cfg.project_list) |
622 | scan_projects(path, ctx.cfg.project_list, |
622 | scan_projects(path, ctx.cfg.project_list, |
623 | repo_config); |
623 | repo_config); |
624 | else |
624 | else |
625 | scan_tree(path, repo_config); |
625 | scan_tree(path, repo_config); |
626 | } |
626 | } |
627 | return; |
627 | return; |
628 | } |
628 | } |
629 | |
629 | |
630 | parse_configfile(cached_rc, config_cb); |
630 | parse_configfile(cached_rc, config_cb); |
631 | |
631 | |
632 | /* If the cached configfile hasn't expired, lets exit now */ |
632 | /* If the cached configfile hasn't expired, lets exit now */ |
633 | age = time(NULL) - st.st_mtime; |
633 | age = time(NULL) - st.st_mtime; |
634 | if (age <= (ctx.cfg.cache_scanrc_ttl * 60)) |
634 | if (age <= (ctx.cfg.cache_scanrc_ttl * 60)) |
635 | return; |
635 | return; |
636 | |
636 | |
637 | /* The cached repolist has been parsed, but it was old. So lets |
637 | /* The cached repolist has been parsed, but it was old. So lets |
638 | * rescan the specified path and generate a new cached repolist |
638 | * rescan the specified path and generate a new cached repolist |
639 | * in a child-process to avoid latency for the current request. |
639 | * in a child-process to avoid latency for the current request. |
640 | */ |
640 | */ |
641 | if (fork()) |
641 | if (fork()) |
642 | return; |
642 | return; |
643 | |
643 | |
644 | exit(generate_cached_repolist(path, cached_rc)); |
644 | exit(generate_cached_repolist(path, cached_rc)); |
645 | } |
645 | } |
646 | |
646 | |
647 | static void cgit_parse_args(int argc, const char **argv) |
647 | static void cgit_parse_args(int argc, const char **argv) |
648 | { |
648 | { |
649 | int i; |
649 | int i; |
650 | int scan = 0; |
650 | int scan = 0; |
651 | |
651 | |
652 | for (i = 1; i < argc; i++) { |
652 | for (i = 1; i < argc; i++) { |
653 | if (!strncmp(argv[i], "--cache=", 8)) { |
653 | if (!strncmp(argv[i], "--cache=", 8)) { |
654 | ctx.cfg.cache_root = xstrdup(argv[i]+8); |
654 | ctx.cfg.cache_root = xstrdup(argv[i]+8); |
655 | } |
655 | } |
656 | if (!strcmp(argv[i], "--nocache")) { |
656 | if (!strcmp(argv[i], "--nocache")) { |
657 | ctx.cfg.nocache = 1; |
657 | ctx.cfg.nocache = 1; |
658 | } |
658 | } |
659 | if (!strcmp(argv[i], "--nohttp")) { |
659 | if (!strcmp(argv[i], "--nohttp")) { |
660 | ctx.env.no_http = "1"; |
660 | ctx.env.no_http = "1"; |
661 | } |
661 | } |
662 | if (!strncmp(argv[i], "--query=", 8)) { |
662 | if (!strncmp(argv[i], "--query=", 8)) { |
663 | ctx.qry.raw = xstrdup(argv[i]+8); |
663 | ctx.qry.raw = xstrdup(argv[i]+8); |
664 | } |
664 | } |
665 | if (!strncmp(argv[i], "--repo=", 7)) { |
665 | if (!strncmp(argv[i], "--repo=", 7)) { |
666 | ctx.qry.repo = xstrdup(argv[i]+7); |
666 | ctx.qry.repo = xstrdup(argv[i]+7); |
667 | } |
667 | } |
668 | if (!strncmp(argv[i], "--page=", 7)) { |
668 | if (!strncmp(argv[i], "--page=", 7)) { |
669 | ctx.qry.page = xstrdup(argv[i]+7); |
669 | ctx.qry.page = xstrdup(argv[i]+7); |
670 | } |
670 | } |
671 | if (!strncmp(argv[i], "--head=", 7)) { |
671 | if (!strncmp(argv[i], "--head=", 7)) { |
672 | ctx.qry.head = xstrdup(argv[i]+7); |
672 | ctx.qry.head = xstrdup(argv[i]+7); |
673 | ctx.qry.has_symref = 1; |
673 | ctx.qry.has_symref = 1; |
674 | } |
674 | } |
675 | if (!strncmp(argv[i], "--sha1=", 7)) { |
675 | if (!strncmp(argv[i], "--sha1=", 7)) { |
676 | ctx.qry.sha1 = xstrdup(argv[i]+7); |
676 | ctx.qry.sha1 = xstrdup(argv[i]+7); |
677 | ctx.qry.has_sha1 = 1; |
677 | ctx.qry.has_sha1 = 1; |
678 | } |
678 | } |
679 | if (!strncmp(argv[i], "--ofs=", 6)) { |
679 | if (!strncmp(argv[i], "--ofs=", 6)) { |
680 | ctx.qry.ofs = atoi(argv[i]+6); |
680 | ctx.qry.ofs = atoi(argv[i]+6); |
681 | } |
681 | } |
682 | if (!strncmp(argv[i], "--scan-tree=", 12) || |
682 | if (!strncmp(argv[i], "--scan-tree=", 12) || |
683 | !strncmp(argv[i], "--scan-path=", 12)) { |
683 | !strncmp(argv[i], "--scan-path=", 12)) { |
684 | /* HACK: the global snapshot bitmask defines the |
684 | /* HACK: the global snapshot bitmask defines the |
685 | * set of allowed snapshot formats, but the config |
685 | * set of allowed snapshot formats, but the config |
686 | * file hasn't been parsed yet so the mask is |
686 | * file hasn't been parsed yet so the mask is |
687 | * currently 0. By setting all bits high before |
687 | * currently 0. By setting all bits high before |
688 | * scanning we make sure that any in-repo cgitrc |
688 | * scanning we make sure that any in-repo cgitrc |
689 | * snapshot setting is respected by scan_tree(). |
689 | * snapshot setting is respected by scan_tree(). |
690 | * BTW: we assume that there'll never be more than |
690 | * BTW: we assume that there'll never be more than |
691 | * 255 different snapshot formats supported by cgit... |
691 | * 255 different snapshot formats supported by cgit... |
692 | */ |
692 | */ |
693 | ctx.cfg.snapshots = 0xFF; |
693 | ctx.cfg.snapshots = 0xFF; |
694 | scan++; |
694 | scan++; |
695 | scan_tree(argv[i] + 12, repo_config); |
695 | scan_tree(argv[i] + 12, repo_config); |
696 | } |
696 | } |
697 | } |
697 | } |
698 | if (scan) { |
698 | if (scan) { |
699 | qsort(cgit_repolist.repos, cgit_repolist.count, |
699 | qsort(cgit_repolist.repos, cgit_repolist.count, |
700 | sizeof(struct cgit_repo), cmp_repos); |
700 | sizeof(struct cgit_repo), cmp_repos); |
701 | print_repolist(stdout, &cgit_repolist, 0); |
701 | print_repolist(stdout, &cgit_repolist, 0); |
702 | exit(0); |
702 | exit(0); |
703 | } |
703 | } |
704 | } |
704 | } |
705 | |
705 | |
706 | static int calc_ttl() |
706 | static int calc_ttl() |
707 | { |
707 | { |
708 | if (!ctx.repo) |
708 | if (!ctx.repo) |
709 | return ctx.cfg.cache_root_ttl; |
709 | return ctx.cfg.cache_root_ttl; |
|