review 4.49 KB
Newer Older
Michael Hanselmann's avatar
Michael Hanselmann committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/bin/bash

# Copyright (C) 2009 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

# To set user mappings, use this command:
21
#   git config gnt-review.johndoe 'John Doe <johndoe@example.com>'
Michael Hanselmann's avatar
Michael Hanselmann committed
22

23
24
25
26
27
28
# To disable strict mode (enabled by default):
#   git config gnt-review.strict false

# To enable strict mode:
#   git config gnt-review.strict true

Michael Hanselmann's avatar
Michael Hanselmann committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
set -e

# Get absolute path to myself
me_plain="$0"
me=$(readlink -f "$me_plain")

add_reviewed_by() {
  local msgfile="$1"

  grep -q '^Reviewed-by: ' "$msgfile" && return

  perl -i -e '
  my $sob = 0;
  while (<>) {
    if ($sob == 0 and m/^Signed-off-by:/) {
      $sob = 1;

    } elsif ($sob == 1 and not m/^Signed-off-by:/) {
      print "Reviewed-by: \n";
      $sob = -1;
    }

    print;
  }

  if ($sob == 1) {
    print "Reviewed-by: \n";
  }
  ' "$msgfile"
}

replace_users() {
  local msgfile="$1"

63
64
65
66
67
68
69
  if perl -i -e '
  use strict;
  use warnings;

  my $error = 0;
  my $strict;

Michael Hanselmann's avatar
Michael Hanselmann committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
  sub map_username {
    my ($name) = @_;

    return $name unless $name;

    my @cmd = ("git", "config", "--get", "gnt-review.$name");

    open(my $fh, "-|", @cmd) or die "Command \"@cmd\" failed: $!";
    my $output = do { local $/ = undef; <$fh> };
    close($fh);

    if ($? == 0) {
      chomp $output;
      $output =~ s/\s+/ /;
      return $output;
    }

87
88
89
90
91
92
93
94
95
96
97
98
99
100
    unless (defined $strict) {
      @cmd = ("git", "config", "--get", "--bool", "gnt-review.strict");

      open($fh, "-|", @cmd) or die "Command \"@cmd\" failed: $!";
      $output = do { local $/ = undef; <$fh> };
      close($fh);

      $strict = ($? != 0 or not $output or $output !~ m/^false$/);
    }

    if ($strict and $name !~ m/^.+<.+\@.+>$/) {
      $error = 1;
    }

Michael Hanselmann's avatar
Michael Hanselmann committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
    return $name;
  }

  while (<>) {
    if (m/^Reviewed-by:(.*)$/) {
      my @names = grep {
        # Ignore empty entries
        !/^$/
      } map {
        # Normalize whitespace
        $_ =~ s/(^\s+|\s+$)//g;
        $_ =~ s/\s+/ /g;

        # Map names
        $_ = map_username($_);

        $_;
      } split(m/,/, $1);

120
121
122
123
124
      # Get unique names
      my %saw;
      @names = grep(!$saw{$_}++, @names);
      undef %saw;

Michael Hanselmann's avatar
Michael Hanselmann committed
125
126
127
128
129
130
131
      foreach (sort @names) {
        print "Reviewed-by: $_\n";
      }
    } else {
      print;
    }
  }
132
133

  exit($error? 33 : 0);
Michael Hanselmann's avatar
Michael Hanselmann committed
134
  ' "$msgfile"
135
136
137
138
139
140
  then
    :
  else
    [[ "$?" == 33 ]] && return 1
    exit 1
  fi
Michael Hanselmann's avatar
Michael Hanselmann committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198

  if ! grep -q '^Reviewed-by: ' "$msgfile"
  then
    echo 'Missing Reviewed-by: line' >&2
    sleep 1
    return 1
  fi

  return 0
}

run_editor() {
  local filename="$1"
  local editor=${EDITOR:-vi}
  local args

  case "$(basename "$editor")" in
    vi* | *vim)
      # Start edit mode at Reviewed-by: line
      args='+/^Reviewed-by: +nohlsearch +startinsert!'
    ;;
    *)
      args=
    ;;
  esac

  $editor $args "$filename"
}

commit_editor() {
  local msgfile="$1"

  local tmpf=$(mktemp)
  trap "rm -f $tmpf" EXIT

  cp "$msgfile" "$tmpf"

  while :
  do
    add_reviewed_by "$tmpf"

    run_editor "$tmpf"

    replace_users "$tmpf" && break
  done

  cp "$tmpf" "$msgfile"
}

copy_commit() {
  local rev="$1" target_branch="$2"

  echo "Copying commit $rev ..."

  git cherry-pick -n "$rev"
  GIT_EDITOR="$me --commit-editor \"\$@\"" git commit -c "$rev" -s
}

199
200
201
202
203
usage() {
  echo "Usage: $me_plain [from..to] <target-branch>" >&2
  echo "  If not passed from..to defaults to target-branch..HEAD" >&2
  exit 1
}
Michael Hanselmann's avatar
Michael Hanselmann committed
204

205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
main() {
  local range target_branch

  case "$#" in
  1)
    target_branch="$1"
    range="$target_branch..$(git rev-parse HEAD)"
  ;;
  2)
    range="$1"
    target_branch="$2"
    if [[ "$range" != *..* ]]; then
      usage
    fi
  ;;
  *)
    usage
  ;;
  esac
Michael Hanselmann's avatar
Michael Hanselmann committed
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242

  git checkout "$target_branch"
  local old_head=$(git rev-parse HEAD)

  for rev in $(git rev-list --reverse "$range")
  do
    copy_commit "$rev"
  done

  git log "$old_head..$target_branch"
}

if [[ "$1" == --commit-editor ]]
then
  shift
  commit_editor "$@"
else
  main "$@"
fi