blob: 712921c95c5be800e9ac44cff5e329611a50fb3a [file] [log] [blame]
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -07001#!/bin/sh
2# From Gerrit Code Review 2.1.2-rc2-33-g7e30c72
3#
4# Part of Gerrit Code Review (http://code.google.com/p/gerrit/)
5#
6# Copyright (C) 2009 The Android Open Source Project
7#
8# Licensed under the Apache License, Version 2.0 (the "License");
9# you may not use this file except in compliance with the License.
10# You may obtain a copy of the License at
11#
12# http://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing, software
15# distributed under the License is distributed on an "AS IS" BASIS,
16# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17# See the License for the specific language governing permissions and
18# limitations under the License.
19#
20
21CHANGE_ID_AFTER="Bug|Issue"
22MSG="$1"
23
24# Check for, and add if missing, a unique Change-Id
25#
26add_ChangeId() {
27 clean_message=$(sed -e '
28 /^diff --git a\/.*/{
29 s///
30 q
31 }
32 /^Signed-off-by:/d
33 /^#/d
34 ' "$MSG" | git stripspace)
35 if test -z "$clean_message"
36 then
37 return
38 fi
39
40 if grep -i '^Change-Id:' "$MSG" >/dev/null
41 then
42 return
43 fi
44
45 id=$(_gen_ChangeId)
46 perl -e '
47 $MSG = shift;
48 $id = shift;
49 $CHANGE_ID_AFTER = shift;
50
51 undef $/;
52 open(I, $MSG); $_ = <I>; close I;
53 s|^diff --git a/.*||ms;
54 s|^#.*$||mg;
55 exit unless $_;
56
57 @message = split /\n/;
58 $haveFooter = 0;
59 $startFooter = @message;
60 for($line = @message - 1; $line >= 0; $line--) {
61 $_ = $message[$line];
62
63 ($haveFooter++, next) if /^[a-zA-Z0-9-]+:/;
64 next if /^[ []/;
65 $startFooter = $line if ($haveFooter && /^\r?$/);
66 last;
67 }
68
69 @footer = @message[$startFooter+1..@message];
70 @message = @message[0..$startFooter];
71 push(@footer, "") unless @footer;
72
73 for ($line = 0; $line < @footer; $line++) {
74 $_ = $footer[$line];
75 next if /^($CHANGE_ID_AFTER):/i;
76 last;
77 }
78 splice(@footer, $line, 0, "Change-Id: I$id");
79
80 $_ = join("\n", @message, @footer);
81 open(O, ">$MSG"); print O; close O;
82 ' "$MSG" "$id" "$CHANGE_ID_AFTER"
83}
84_gen_ChangeIdInput() {
85 echo "tree $(git write-tree)"
86 if parent=$(git rev-parse HEAD^0 2>/dev/null)
87 then
88 echo "parent $parent"
89 fi
90 echo "author $(git var GIT_AUTHOR_IDENT)"
91 echo "committer $(git var GIT_COMMITTER_IDENT)"
92 echo
93 printf '%s' "$clean_message"
94}
95_gen_ChangeId() {
96 _gen_ChangeIdInput |
97 git hash-object -t commit --stdin
98}
99
100
101add_ChangeId