@matheusbsbs / matheus-bagliano / commits / 981b625346
Fork 0

Add git-topic script.

Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Dec 6, 2006 at 19:20 UTC 981b62534612f0f1a97bb419ecb4afcd4b40e415
1 file changed +95
git-topic.perl new
+95
@@ -0,0 +1,95 @@
1 +#!/usr/bin/perl -w
2 +#
3 +# Copyright (c) 2006 Junio C Hamano
4 +#
5 +
6 +use strict;
7 +use Getopt::Long;
8 +
9 +my $topic_pattern = '??/*';
10 +my $base = 'next';
11 +my @stage = qw(next pu);
12 +my @mark = ('.', '?', '-', '+');
13 +my $all = 0;
14 +
15 +my @custom_stage;
16 +my @custom_mark;
17 +GetOptions("topic=s" => \$topic_pattern,
18 + "base=s" => \$base,
19 + "stage=s" => \@custom_stage,
20 + "mark=s" => \@custom_mark,
21 + "all!" => \$all)
22 + or die;
23 +
24 +if (@custom_stage) { @stage = @custom_stage; }
25 +if (@custom_mark) { @mark = @custom_mark; }
26 +
27 +sub read_revs_short {
28 + my ($bottom, $top) = @_;
29 + my @revs;
30 + open(REVS, '-|', qw(git rev-list --no-merges), "$bottom..$top")
31 + or die;
32 + while (<REVS>) {
33 + chomp;
34 + push @revs, $_;
35 + }
36 + close(REVS);
37 + return @revs;
38 +}
39 +
40 +sub read_revs {
41 + my ($bottom, $top, $mask) = @_;
42 + my @revs;
43 + open(REVS, '-|', qw(git rev-list --pretty=oneline --no-merges),
44 + "$bottom..$top")
45 + or die;
46 + while (<REVS>) {
47 + chomp;
48 + my ($sha1, $topic) = /^([0-9a-f]{40}) (.*)$/;
49 + push @revs, [$sha1, $topic, $mask];
50 + }
51 + close(REVS);
52 + return @revs;
53 +}
54 +
55 +sub wrap_print {
56 + my ($prefix, $string) = @_;
57 + format STDOUT =
58 + @ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
59 + $prefix, $string
60 + ~~^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
61 + $string
62 +.
63 + write;
64 +}
65 +
66 +open(TOPIC, '-|', qw(git for-each-ref),
67 + '--sort=-authordate',
68 + '--format=%(objectname) %(authordate) %(refname)',
69 + "refs/heads/$topic_pattern")
70 + or die;
71 +
72 +while (<TOPIC>) {
73 + chomp;
74 + my ($sha1, $date, $topic) = m|^([0-9a-f]{40})\s(.*?)\srefs/heads/(.+)$|
75 + or next;
76 + my @revs = read_revs($base, $sha1, (1<<@stage)-1);
77 + next unless (@revs || $all);
78 +
79 + my %revs = map { $_->[0] => $_ } @revs; # fast index
80 + for (my $i = 0; $i < @stage; $i++) {
81 + for my $item (read_revs_short($stage[$i], $sha1)) {
82 + if (exists $revs{$item}) {
83 + $revs{$item}[2] &= ~(1 << $i);
84 + }
85 + }
86 + }
87 + print "* $topic ($date)\n";
88 + for my $item (@revs) {
89 + my $mark = $item->[2];
90 + if ($mark < @mark) {
91 + $mark = $mark[$mark];
92 + }
93 + wrap_print($mark, $item->[1]);
94 + }
95 +}