1#!/bin/sh
2#
3# Copyright (c) 2005 Junio C Hamano
4#
5# Fetch one or more remote refs and merge it/them into the current HEAD.
6
7. git-sh-setup || die "Not a git archive"
8
9usage () {
10 die "git pull [-n] [-s strategy]... <repo> <head>..."
11}
12
13strategy_args= no_summary=
14while case "$#,$1" in 0) break ;; *,-*) ;; *) break ;; esac
15do
16 case "$1" in
17 -n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\
18 --no-summa|--no-summar|--no-summary)
19 no_summary=-n ;;
20 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
21 --strateg=*|--strategy=*|\
22 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
23 case "$#,$1" in
24 *,*=*)
25 strategy=`expr "$1" : '-[^=]*=\(.*\)'` ;;
26 1,*)
27 usage ;;
28 *)
29 strategy="$2"
30 shift ;;
31 esac
32 strategy_args="${strategy_args}-s $strategy "
33 ;;
34 -*)
35 usage
36 ;;
37 esac
38 shift
39done
40
41orig_head=$(cat "$GIT_DIR/HEAD") || die "Pulling into a black hole?"
42git-fetch --update-head-ok "$@" || exit 1
43
44curr_head=$(cat "$GIT_DIR/HEAD")
45if test "$curr_head" != "$orig_head"
46then
47 # The fetch involved updating the current branch.
48
49 # The working tree and the index file is still based on the
50 # $orig_head commit, but we are merging into $curr_head.
51 # First update the working tree to match $curr_head.
52
53 echo >&2 "Warning: fetch updated the current branch head."
54 echo >&2 "Warning: fast forwarding your working tree."
55 git-read-tree -u -m "$orig_head" "$curr_head" ||
56 die "You need to first update your working tree."
57fi
58
59merge_head=$(sed -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | tr '\012' ' ')
60
61case "$merge_head" in
62?*' '?*)
63 merge_name="Octopus merge of "$(
64 perl -e '
65 my @src;
66 my %src;
67
68 sub andjoin {
69 my ($label, $labels, $stuff, $src) = @_;
70 my $l = scalar @$stuff;
71 my $m = "";
72 if ($l == 0) {
73 return "";
74 }
75 if ($l == 1) {
76 $m = "$label $stuff->[0]";
77 }
78 else {
79 $m = ("$labels " .
80 join (", ", @{$stuff}[0..$l-2]) .
81 " and $stuff->[-1]");
82 }
83 if ($src ne ".") {
84 $m .= " from $src";
85 }
86 return $m;
87 }
88
89 while (<>) {
90 my ($bname, $tname, $gname, $src);
91 s/^[0-9a-f]* //;
92 if (s/ of (.*)$//) {
93 $src = $1;
94 } else {
95 $src = ".";
96 }
97 if (! exists $src{$src}) {
98 push @src, $src;
99 $src{$src} = { BRANCH => [], TAG => [], GENERIC => [] };
100 }
101 if (/^branch (.*)$/) {
102 push @{$src{$src}{BRANCH}}, $1;
103 }
104 elsif (/^tag (.*)$/) {
105 push @{$src{$src}{TAG}}, $1;
106 }
107 else {
108 push @{$src{$src}{GENERIC}}, $1;
109 }
110 }
111 my @msg;
112 for my $src (@src) {
113 my $bag = $src{$src}{BRANCH};
114 if (@{$bag}) {
115 push @msg, andjoin("branch", "branches", $bag, $src);
116 }
117 $bag = $src{$src}{TAG};
118 if (@{$bag}) {
119 push @msg, andjoin("tag", "tags", $bag, $src);
120 }
121 $bag = $src{$src}{GENERIC};
122 if (@{$bag}) {
123 push @msg, andjoin("commit", "commits", $bag, $src);
124 }
125 }
126 print join("; ", @msg);
127 ' "$GIT_DIR"/FETCH_HEAD
128 )
129 ;;
130*)
131 merge_name="Merge "$(sed -e 's/^[0-9a-f]* //' \
132 "$GIT_DIR"/FETCH_HEAD)
133 ;;
134esac
135
136case "$merge_head" in
137'')
138 echo >&2 "No changes."
139 exit 0
140 ;;
141esac
142
143git-merge $no_summary $strategy_args "$merge_name" HEAD $merge_head