@matheusbsbs / matheus-bagliano / commits / e30496dfcb
Fork 0

gitweb: Support for 'forks'

On repo.or.cz, I want to support project 'forks', which are meant for repositories which are spinoffs of a given project and share its objects database through the alternates mechanism. But another (and perhaps even greater) incentive for that is that those 'forked projects' do not clutter the main project index but are completely grouped inside of the project view. A forked project is just like a normal project, but given project $projectroot/$projname.git, the forked project resides in directory $projectroot/$projname/$forkname.git. This is a somewhat arbitrary naming rule, but I think that for now it's fine; if someone will need something wildly different, let them submit a patch. The 'forked' mode is by default off and can be turned on in runtime gitweb configuration just like other features. A project having forks is marked by a '+' (pointing to the list of forks) in the project list (this could become some cutesy AJAXy DHTML in the future), there is a forks section in the project summary similar to the heads and tags sections, and of course a forks view which looks the same as the root project list. Forks can be recursive. Signed-off-by: Petr Baudis <pasky@suse.cz> Signed-off-by: Junio C Hamano <junkio@cox.net>

Petr Baudis committed Oct 24, 2006 at 05:33 UTC e30496dfcb98a305a57b835c248cbc3aa2376bfc
1 file changed +174 -82
gitweb/gitweb.perl
+174 -82
@@ -164,6 +164,21 @@ our %feature = (
164 'pathinfo' => {
165 'override' => 0,
166 'default' => [0]},
167 +
168 + # Make gitweb consider projects in project root subdirectories
169 + # to be forks of existing projects. Given project $projname.git,
170 + # projects matching $projname/*.git will not be shown in the main
171 + # projects list, instead a '+' mark will be added to $projname
172 + # there and a 'forks' view will be enabled for the project, listing
173 + # all the forks. This feature is supported only if project list
174 + # is taken from a directory, not file.
175 +
176 + # To enable system wide have in $GITWEB_CONFIG
177 + # $feature{'forks'}{'default'} = [1];
178 + # Project specific override is not supported.
179 + 'forks' => {
180 + 'override' => 0,
181 + 'default' => [0]},
182 );
183
184 sub gitweb_check_feature {
@@ -409,6 +424,7 @@ my %actions = (
424 "commitdiff" => \&git_commitdiff,
425 "commitdiff_plain" => \&git_commitdiff_plain,
426 "commit" => \&git_commit,
427 + "forks" => \&git_forks,
428 "heads" => \&git_heads,
429 "history" => \&git_history,
430 "log" => \&git_log,
@@ -896,13 +912,19 @@ sub git_get_project_url_list {
912 }
913
914 sub git_get_projects_list {
915 + my ($filter) = @_;
916 my @list;
917
918 + $filter ||= '';
919 + $filter =~ s/\.git$//;
920 +
921 if (-d $projects_list) {
922 # search in directory
903 - my $dir = $projects_list;
923 + my $dir = $projects_list . ($filter ? "/$filter" : '');
924 my $pfxlen = length("$dir");
925
926 + my $check_forks = gitweb_check_feature('forks');
927 +
928 File::Find::find({
929 follow_fast => 1, # follow symbolic links
930 dangling_symlinks => 0, # ignore dangling symlinks, silently
@@ -914,8 +936,10 @@ sub git_get_projects_list {
936
937 my $subdir = substr($File::Find::name, $pfxlen + 1);
938 # we check related file in $projectroot
917 - if (check_export_ok("$projectroot/$subdir")) {
918 - push @list, { path => $subdir };
939 + if ($check_forks and $subdir =~ m#/.#) {
940 + $File::Find::prune = 1;
941 + } elsif (check_export_ok("$projectroot/$filter/$subdir")) {
942 + push @list, { path => ($filter ? "$filter/" : '') . $subdir };
943 $File::Find::prune = 1;
944 }
945 },
@@ -2146,6 +2170,124 @@ sub git_patchset_body {
2170
2171 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2172
2173 +sub git_project_list_body {
2174 + my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
2175 +
2176 + my $check_forks = gitweb_check_feature('forks');
2177 +
2178 + my @projects;
2179 + foreach my $pr (@$projlist) {
2180 + my (@aa) = git_get_last_activity($pr->{'path'});
2181 + unless (@aa) {
2182 + next;
2183 + }
2184 + ($pr->{'age'}, $pr->{'age_string'}) = @aa;
2185 + if (!defined $pr->{'descr'}) {
2186 + my $descr = git_get_project_description($pr->{'path'}) || "";
2187 + $pr->{'descr'} = chop_str($descr, 25, 5);
2188 + }
2189 + if (!defined $pr->{'owner'}) {
2190 + $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2191 + }
2192 + if ($check_forks) {
2193 + my $pname = $pr->{'path'};
2194 + $pname =~ s/\.git$//;
2195 + $pr->{'forks'} = -d "$projectroot/$pname";
2196 + }
2197 + push @projects, $pr;
2198 + }
2199 +
2200 + $order ||= "project";
2201 + $from = 0 unless defined $from;
2202 + $to = $#projects if (!defined $to || $#projects < $to);
2203 +
2204 + print "<table class=\"project_list\">\n";
2205 + unless ($no_header) {
2206 + print "<tr>\n";
2207 + if ($check_forks) {
2208 + print "<th></th>\n";
2209 + }
2210 + if ($order eq "project") {
2211 + @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2212 + print "<th>Project</th>\n";
2213 + } else {
2214 + print "<th>" .
2215 + $cgi->a({-href => href(project=>undef, order=>'project'),
2216 + -class => "header"}, "Project") .
2217 + "</th>\n";
2218 + }
2219 + if ($order eq "descr") {
2220 + @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2221 + print "<th>Description</th>\n";
2222 + } else {
2223 + print "<th>" .
2224 + $cgi->a({-href => href(project=>undef, order=>'descr'),
2225 + -class => "header"}, "Description") .
2226 + "</th>\n";
2227 + }
2228 + if ($order eq "owner") {
2229 + @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2230 + print "<th>Owner</th>\n";
2231 + } else {
2232 + print "<th>" .
2233 + $cgi->a({-href => href(project=>undef, order=>'owner'),
2234 + -class => "header"}, "Owner") .
2235 + "</th>\n";
2236 + }
2237 + if ($order eq "age") {
2238 + @projects = sort {$a->{'age'} <=> $b->{'age'}} @projects;
2239 + print "<th>Last Change</th>\n";
2240 + } else {
2241 + print "<th>" .
2242 + $cgi->a({-href => href(project=>undef, order=>'age'),
2243 + -class => "header"}, "Last Change") .
2244 + "</th>\n";
2245 + }
2246 + print "<th></th>\n" .
2247 + "</tr>\n";
2248 + }
2249 + my $alternate = 1;
2250 + for (my $i = $from; $i <= $to; $i++) {
2251 + my $pr = $projects[$i];
2252 + if ($alternate) {
2253 + print "<tr class=\"dark\">\n";
2254 + } else {
2255 + print "<tr class=\"light\">\n";
2256 + }
2257 + $alternate ^= 1;
2258 + if ($check_forks) {
2259 + print "<td>";
2260 + if ($pr->{'forks'}) {
2261 + print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
2262 + }
2263 + print "</td>\n";
2264 + }
2265 + print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2266 + -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
2267 + "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
2268 + "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
2269 + print "<td class=\"". age_class($pr->{'age'}) . "\">" .
2270 + $pr->{'age_string'} . "</td>\n" .
2271 + "<td class=\"link\">" .
2272 + $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
2273 + $cgi->a({-href => '/git-browser/by-commit.html?r='.$pr->{'path'}}, "graphiclog") . " | " .
2274 + $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
2275 + $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
2276 + ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
2277 + "</td>\n" .
2278 + "</tr>\n";
2279 + }
2280 + if (defined $extra) {
2281 + print "<tr>\n";
2282 + if ($check_forks) {
2283 + print "<td></td>\n";
2284 + }
2285 + print "<td colspan=\"5\">$extra</td>\n" .
2286 + "</tr>\n";
2287 + }
2288 + print "</table>\n";
2289 +}
2290 +
2291 sub git_shortlog_body {
2292 # uses global variable $project
2293 my ($revlist, $from, $to, $refs, $extra) = @_;
@@ -2364,25 +2506,9 @@ sub git_project_list {
2506 }
2507
2508 my @list = git_get_projects_list();
2367 - my @projects;
2509 if (!@list) {
2510 die_error(undef, "No projects found");
2511 }
2371 - foreach my $pr (@list) {
2372 - my (@aa) = git_get_last_activity($pr->{'path'});
2373 - unless (@aa) {
2374 - next;
2375 - }
2376 - ($pr->{'age'}, $pr->{'age_string'}) = @aa;
2377 - if (!defined $pr->{'descr'}) {
2378 - my $descr = git_get_project_description($pr->{'path'}) || "";
2379 - $pr->{'descr'} = chop_str($descr, 25, 5);
2380 - }
2381 - if (!defined $pr->{'owner'}) {
2382 - $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2383 - }
2384 - push @projects, $pr;
2385 - }
2512
2513 git_header_html();
2514 if (-f $home_text) {
@@ -2392,75 +2518,30 @@ sub git_project_list {
2518 close $fd;
2519 print "</div>\n";
2520 }
2395 - print "<table class=\"project_list\">\n" .
2396 - "<tr>\n";
2397 - $order ||= "project";
2398 - if ($order eq "project") {
2399 - @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2400 - print "<th>Project</th>\n";
2401 - } else {
2402 - print "<th>" .
2403 - $cgi->a({-href => href(project=>undef, order=>'project'),
2404 - -class => "header"}, "Project") .
2405 - "</th>\n";
2406 - }
2407 - if ($order eq "descr") {
2408 - @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2409 - print "<th>Description</th>\n";
2410 - } else {
2411 - print "<th>" .
2412 - $cgi->a({-href => href(project=>undef, order=>'descr'),
2413 - -class => "header"}, "Description") .
2414 - "</th>\n";
2415 - }
2416 - if ($order eq "owner") {
2417 - @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2418 - print "<th>Owner</th>\n";
2419 - } else {
2420 - print "<th>" .
2421 - $cgi->a({-href => href(project=>undef, order=>'owner'),
2422 - -class => "header"}, "Owner") .
2423 - "</th>\n";
2424 - }
2425 - if ($order eq "age") {
2426 - @projects = sort {$a->{'age'} <=> $b->{'age'}} @projects;
2427 - print "<th>Last Change</th>\n";
2428 - } else {
2429 - print "<th>" .
2430 - $cgi->a({-href => href(project=>undef, order=>'age'),
2431 - -class => "header"}, "Last Change") .
2432 - "</th>\n";
2521 + git_project_list_body(\@list, $order);
2522 + git_footer_html();
2523 +}
2524 +
2525 +sub git_forks {
2526 + my $order = $cgi->param('o');
2527 + if (defined $order && $order !~ m/project|descr|owner|age/) {
2528 + die_error(undef, "Unknown order parameter");
2529 }
2434 - print "<th></th>\n" .
2435 - "</tr>\n";
2436 - my $alternate = 1;
2437 - foreach my $pr (@projects) {
2438 - if ($alternate) {
2439 - print "<tr class=\"dark\">\n";
2440 - } else {
2441 - print "<tr class=\"light\">\n";
2442 - }
2443 - $alternate ^= 1;
2444 - print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2445 - -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
2446 - "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
2447 - "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
2448 - print "<td class=\"". age_class($pr->{'age'}) . "\">" .
2449 - $pr->{'age_string'} . "</td>\n" .
2450 - "<td class=\"link\">" .
2451 - $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
2452 - $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
2453 - $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
2454 - $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
2455 - "</td>\n" .
2456 - "</tr>\n";
2530 +
2531 + my @list = git_get_projects_list($project);
2532 + if (!@list) {
2533 + die_error(undef, "No forks found");
2534 }
2458 - print "</table>\n";
2535 +
2536 + git_header_html();
2537 + git_print_page_nav('','');
2538 + git_print_header_div('summary', "$project forks");
2539 + git_project_list_body(\@list, $order);
2540 git_footer_html();
2541 }
2542
2543 sub git_project_index {
2463 - my @projects = git_get_projects_list();
2544 + my @projects = git_get_projects_list($project);
2545
2546 print $cgi->header(
2547 -type => 'text/plain',
@@ -2503,6 +2584,10 @@ sub git_summary {
2584 push @taglist, $ref;
2585 }
2586 }
2587 + my @forklist;
2588 + if (gitweb_check_feature('forks')) {
2589 + @forklist = git_get_projects_list($project);
2590 + }
2591
2592 git_header_html();
2593 git_print_page_nav('summary','', $head);
@@ -2553,6 +2638,13 @@ sub git_summary {
2638 $cgi->a({-href => href(action=>"heads")}, "..."));
2639 }
2640
2641 + if (@forklist) {
2642 + git_print_header_div('forks');
2643 + git_project_list_body(\@forklist, undef, 0, 15,
2644 + $cgi->a({-href => href(action=>"forks")}, "..."),
2645 + 'noheader');
2646 + }
2647 +
2648 git_footer_html();
2649 }
2650