summaryrefslogtreecommitdiffstats
path: root/best-branch.pl
blob: 6cecd51ed56dff53e263387639cfb3ee8f8d71bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/perl
($rev, $branches) = @ARGV;

sub count_commits($$) {
    my($f, $t) = @_;
    my($rl, $l);
    my $count = 0;

    open($rl, "-|", "git rev-list \Q${f}\E..\Q${t}\E") or return undef;
    while (defined($l = <$rl>)) {
	$count++;
    }
    close($rl);
    return $count;
}

%branches = ();
open(BRANCHES, '<', $branches) or die;
while (defined($r = <BRANCHES>)) {
    chomp $r;
    $branches{$r}++;
}
close(BRANCHES);

$best = 'branch?';
undef $best_metric;
open(PRESENT, '-|', "git branch -a --contains \"\Q${rev}\E\"") or die;
while (defined($l = <PRESENT>)) {
    chomp $l;
    if ($l =~ m:^[ \*] (remotes/|)origin/(.*)$:) {
	$l = $2;
	if ($branches{$l}) {
	    $metric = count_commits("refs/remotes/origin/$l", $rev);
	    if (defined($metric) && (!defined($best_metric) || $best < $best_metric)) {
		$best = $l;
		$best_metric = $metric;
	    }
	}
    }
}
close(PRESENT);

print $best, "\n";