+# XXX needs testing for cgit compatibility
+# cf. cgit/scan-tree.c::add_repo
+sub cgit_repo_merge ($$$) {
+ my ($self, $base, $repo) = @_;
+ my $path = $repo->{dir};
+ if (defined(my $se = $self->{-cgit_strict_export})) {
+ return unless -e "$path/$se";
+ }
+ return if -e "$path/noweb";
+ # $repo = { url => 'foo.git', dir => '/path/to/foo.git' }
+ my $rel = $repo->{url};
+ unless (defined $rel) {
+ my $off = index($path, $base, 0);
+ if ($off != 0) {
+ $rel = $path;
+ } else {
+ $rel = substr($path, length($base) + 1);
+ }
+
+ $rel =~ s!/\.git\z!! or
+ $rel =~ s!/+\z!!;
+
+ $self->{-cgit_remove_suffix} and
+ $rel =~ s!/?\.git\z!!;
+ }
+ $self->{"coderepo.$rel.dir"} //= $path;
+ $self->{"coderepo.$rel.cgiturl"} //= $rel;
+}
+
+sub is_git_dir ($) {
+ my ($git_dir) = @_;
+ -d "$git_dir/objects" && -f "$git_dir/HEAD";
+}
+
+# XXX needs testing for cgit compatibility
+sub scan_path_coderepo {
+ my ($self, $base, $path) = @_;
+ opendir(my $dh, $path) or do {
+ warn "error opening directory: $path\n";
+ return
+ };
+ my $git_dir = $path;
+ if (is_git_dir($git_dir) || is_git_dir($git_dir .= '/.git')) {
+ my $repo = { dir => $git_dir };
+ cgit_repo_merge($self, $base, $repo);
+ return;
+ }
+ while (defined(my $dn = readdir $dh)) {
+ next if $dn eq '.' || $dn eq '..';
+ if (index($dn, '.') == 0 && !$self->{-cgit_scan_hidden_path}) {
+ next;
+ }
+ my $dir = "$path/$dn";
+ scan_path_coderepo($self, $base, $dir) if -d $dir;
+ }
+}
+
+sub scan_tree_coderepo ($$) {
+ my ($self, $path) = @_;
+ scan_path_coderepo($self, $path, $path);
+}
+
+sub scan_projects_coderepo ($$$) {
+ my ($self, $list, $path) = @_;
+ open my $fh, '<', $list or do {
+ warn "failed to open cgit projectlist=$list: $!\n";
+ return;
+ };
+ foreach (<$fh>) {
+ chomp;
+ scan_path_coderepo($self, $path, "$path/$_");
+ }