blob: fd76c0744ba8082c7718836e9afca662470c5441 [file] [log] [blame]
Shawn O. Pearcea949fa52009-08-22 18:17:46 -07001#!/bin/sh
Shawn O. Pearcec0249122009-08-25 11:38:11 -07002# From Gerrit Code Review v2.0.19.1-4-g21d307b
Shawn O. Pearce15f65792009-08-22 19:23:55 -07003#
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#
Shawn O. Pearcea949fa52009-08-22 18:17:46 -070020
21MSG="$1"
22
23# Check for, and add if missing, a unique Change-Id
24#
25add_ChangeId() {
Shawn O. Pearcec0249122009-08-25 11:38:11 -070026 clean_message=$(sed -e '
27 /^diff --git a\/.*/{
28 s///
29 q
30 }
31 /^Signed-off-by:/d
32 /^#/d
33 ' "$MSG" | git stripspace)
34 if test -z "$clean_message"
35 then
36 return
37 fi
38
39 if grep -i '^Change-Id:' "$MSG" >/dev/null
Shawn O. Pearcea949fa52009-08-22 18:17:46 -070040 then
41 return
42 fi
43
44 id=$(_gen_ChangeId)
Shawn O. Pearcec0249122009-08-25 11:38:11 -070045 out="$MSG.OUT"
46 ftt="$MSG.FTT"
Shawn O. Pearce15f65792009-08-22 19:23:55 -070047 sed -e '2,${
48 /^[A-Za-z][A-Za-z0-9-]*: /,$d
49 }' <"$MSG" >"$out"
50 sed -ne '2,${
51 /^[A-Za-z][A-Za-z0-9-]*: /,$p
52 }' <"$MSG" >"$ftt"
Shawn O. Pearcec0249122009-08-25 11:38:11 -070053 if ! test -s "$ftt"
Shawn O. Pearcea949fa52009-08-22 18:17:46 -070054 then
55 echo >>"$out"
56 fi
57 echo "Change-Id: I$id" >>"$out"
58 cat "$ftt" >>"$out"
59 mv -f "$out" "$MSG"
60 rm -f "$out" "$ftt"
61}
62_gen_ChangeIdInput() {
63 echo "tree $(git write-tree)"
64 if parent=$(git rev-parse HEAD^0 2>/dev/null)
65 then
66 echo "parent $parent"
67 fi
68 echo "author $(git var GIT_AUTHOR_IDENT)"
69 echo "committer $(git var GIT_COMMITTER_IDENT)"
70 echo
Shawn O. Pearcec0249122009-08-25 11:38:11 -070071 printf '%s' "$clean_message"
Shawn O. Pearcea949fa52009-08-22 18:17:46 -070072}
73_gen_ChangeId() {
74 _gen_ChangeIdInput |
75 git hash-object -t commit --stdin
76}
77
78
79add_ChangeId