Diff: --name-status output format.
[git/git-svn.git] / git-fmt-merge-msg.perl
blobf0f3100eb1c089efd990b71bb747f4dd0cd035f9
1 #!/usr/bin/perl -w
3 # Copyright (c) 2005 Junio C Hamano
5 # Read .git/FETCH_HEAD and make a human readable merge message
6 # by grouping branches and tags together to form a single line.
8 use strict;
10 my @src;
11 my %src;
12 sub andjoin {
13 my ($label, $labels, $stuff) = @_;
14 my $l = scalar @$stuff;
15 my $m = '';
16 if ($l == 0) {
17 return ();
19 if ($l == 1) {
20 $m = "$label$stuff->[0]";
22 else {
23 $m = ("$labels" .
24 join (', ', @{$stuff}[0..$l-2]) .
25 " and $stuff->[-1]");
27 return ($m);
30 while (<>) {
31 my ($bname, $tname, $gname, $src);
32 chomp;
33 s/^[0-9a-f]* //;
34 if (s/ of (.*)$//) {
35 $src = $1;
36 } else {
37 # Pulling HEAD
38 $src = $_;
39 $_ = 'HEAD';
41 if (! exists $src{$src}) {
42 push @src, $src;
43 $src{$src} = {
44 BRANCH => [],
45 TAG => [],
46 GENERIC => [],
47 # &1 == has HEAD.
48 # &2 == has others.
49 HEAD_STATUS => 0,
52 if (/^branch (.*)$/) {
53 push @{$src{$src}{BRANCH}}, $1;
54 $src{$src}{HEAD_STATUS} |= 2;
56 elsif (/^tag (.*)$/) {
57 push @{$src{$src}{TAG}}, $1;
58 $src{$src}{HEAD_STATUS} |= 2;
60 elsif (/^HEAD$/) {
61 $src{$src}{HEAD_STATUS} |= 1;
63 else {
64 push @{$src{$src}{GENERIC}}, $_;
65 $src{$src}{HEAD_STATUS} |= 2;
69 my @msg;
70 for my $src (@src) {
71 if ($src{$src}{HEAD_STATUS} == 1) {
72 # Only HEAD is fetched, nothing else.
73 push @msg, $src;
74 next;
76 my @this;
77 if ($src{$src}{HEAD_STATUS} == 3) {
78 # HEAD is fetched among others.
79 push @this, andjoin('', '', ['HEAD']);
81 push @this, andjoin("branch ", "branches ",
82 $src{$src}{BRANCH});
83 push @this, andjoin("tag ", "tags ",
84 $src{$src}{TAG});
85 push @this, andjoin("commit ", "commits ",
86 $src{$src}{GENERIC});
87 my $this = join(', ', @this);
88 if ($src ne '.') {
89 $this .= " from $src";
91 push @msg, $this;
93 print "Merge ", join("; ", @msg), "\n";