git-fmt-merge-msg.perlon commit [PATCH] Provide access to git_dir through get_git_dir(). (5da1606)
   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        if (s/ of (.*)$//) {
  35                $src = $1;
  36        } else {
  37                # Pulling HEAD
  38                $src = $_;
  39                $_ = 'HEAD';
  40        }
  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,
  50                };
  51        }
  52        if (/^branch (.*)$/) {
  53                push @{$src{$src}{BRANCH}}, $1;
  54                $src{$src}{HEAD_STATUS} |= 2;
  55        }
  56        elsif (/^tag (.*)$/) {
  57                push @{$src{$src}{TAG}}, $1;
  58                $src{$src}{HEAD_STATUS} |= 2;
  59        }
  60        elsif (/^HEAD$/) {
  61                $src{$src}{HEAD_STATUS} |= 1;
  62        }
  63        else {
  64                push @{$src{$src}{GENERIC}}, $_;
  65                $src{$src}{HEAD_STATUS} |= 2;
  66        }
  67}
  68
  69my @msg;
  70for my $src (@src) {
  71        if ($src{$src}{HEAD_STATUS} == 1) {
  72                # Only HEAD is fetched, nothing else.
  73                push @msg, $src;
  74                next;
  75        }
  76        my @this;
  77        if ($src{$src}{HEAD_STATUS} == 3) {
  78                # HEAD is fetched among others.
  79                push @this, andjoin('', '', ['HEAD']);
  80        }
  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";
  90        }
  91        push @msg, $this;
  92}
  93print "Merge ", join("; ", @msg), "\n";