Update Iot.js
[platform/upstream/iotjs.git] / tools / check_signed_off.sh
1 #!/bin/bash
2
3 # Copyright 2015-present Samsung Electronics Co., Ltd. and other contributors
4 # Copyright 2016 University of Szeged
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #     http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18 # Usage
19 function print_usage
20 {
21  echo "Usage: $0 [--help] [--tolerant] [--travis]"
22 }
23
24 function print_help
25 {
26  echo "$0: Check Signed-off-by message of the latest commit"
27  echo ""
28  print_usage
29  echo ""
30  echo "Optional arguments:"
31  echo "  --help            print this help message"
32  echo "  --tolerant        check the existence of the message only but don't"
33  echo "                    require the name and email address to match the author"
34  echo "                    of the commit"
35  echo "  --travis          perform check in tolerant mode if on Travis CI and not"
36  echo "                    checking a pull request, perform strict check otherwise"
37  echo ""
38  echo "The last line of every commit message must follow the form of:"
39  echo "'IoT.js-DCO-1.0-Signed-off-by: NAME EMAIL', where NAME and EMAIL must"
40  echo "match the name and email address of the author of the commit (unless in"
41  echo "tolerant mode)."
42 }
43
44 # Processing command line
45 TOLERANT="no"
46 while [ "$#" -gt 0 ]
47 do
48  if [ "$1" == "--help" ]
49  then
50   print_help
51   exit 0
52  elif [ "$1" == "--tolerant" ]
53  then
54   TOLERANT="yes"
55   shift
56  elif [ "$1" == "--travis" ]
57  then
58   if [ "$TRAVIS_PULL_REQUEST" == "" ]
59   then
60    echo -e "\e[1;33mWarning! Travis-tolerant mode requested but not running on Travis CI! \e[0m"
61   elif [ "$TRAVIS_PULL_REQUEST" == "false" ]
62   then
63    TOLERANT="yes"
64   else
65    TOLERANT="no"
66   fi
67   shift
68  else
69   print_usage
70   exit 1
71  fi
72 done
73
74 # Determining latest commit
75 parent_hashes=(`git show -s --format=%p HEAD | head -1`)
76
77 if [ "${#parent_hashes[@]}" -eq 1 ]
78 then
79  commit_hash=`git show -s --format=%h HEAD | head -1`
80 elif [ "${#parent_hashes[@]}" -eq 2 ]
81 then
82  commit_hash=${parent_hashes[1]}
83 else
84  echo "$0: cannot handle commit with ${#parent_hashes[@]} parents ${parent_hashes[@]}"
85  exit 1
86 fi
87
88 # Checking the last line
89 actual_signed_off_by_line=`git show -s --format=%B $commit_hash | sed '/^$/d' | tr -d '\015' | tail -n 1`
90
91 if [ "$TOLERANT" == "no" ]
92 then
93  author_name=`git show -s --format=%an $commit_hash`
94  author_email=`git show -s --format=%ae $commit_hash`
95  required_signed_off_by_line="IoT.js-DCO-1.0-Signed-off-by: $author_name $author_email"
96
97  if [ "$actual_signed_off_by_line" != "$required_signed_off_by_line" ]
98  then
99   echo -e "\e[1;33mSigned-off-by message is incorrect. The following line should be at the end of the $commit_hash commit's message: '$required_signed_off_by_line'. \e[0m"
100   exit 1
101  fi
102 else
103   echo -e "\e[1;33mWarning! The name and email address of the author of the $commit_hash commit is not checked in tolerant mode! \e[0m"
104   if echo "$actual_signed_off_by_line" | grep -q -v '^IoT.js-DCO-1.0-Signed-off-by:'
105   then
106    echo -e "\e[1;33mSigned-off-by message is incorrect. The following line should be at the end of the $commit_hash commit's message: '$required_signed_off_by_line'. \e[0m"
107    exit 1
108   fi
109 fi
110
111 exit 0