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