Update INSTALL: hslogger is mandatory
[ganeti-local] / devel / review
index febf80d..d3f6594 100755 (executable)
 # 02110-1301, USA.
 
 # To set user mappings, use this command:
-#   git config gnt-review.johndoe 'John Doe <johndoe@domain.tld>'
+#   git config gnt-review.johndoe 'John Doe <johndoe@example.com>'
+
+# To disable strict mode (enabled by default):
+#   git config gnt-review.strict false
+
+# To enable strict mode:
+#   git config gnt-review.strict true
 
 set -e
 
@@ -32,13 +38,15 @@ add_reviewed_by() {
   grep -q '^Reviewed-by: ' "$msgfile" && return
 
   perl -i -e '
+  my $reviewer = $ENV{"REVIEWER"};
+  defined($reviewer) or $reviewer = "";
   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";
+      print "Reviewed-by: $reviewer\n";
       $sob = -1;
     }
 
@@ -46,7 +54,7 @@ add_reviewed_by() {
   }
 
   if ($sob == 1) {
-    print "Reviewed-by: \n";
+    print "Reviewed-by: $reviewer\n";
   }
   ' "$msgfile"
 }
@@ -54,7 +62,13 @@ add_reviewed_by() {
 replace_users() {
   local msgfile="$1"
 
-  perl -i -e '
+  if perl -i -e '
+  use strict;
+  use warnings;
+
+  my $error = 0;
+  my $strict;
+
   sub map_username {
     my ($name) = @_;
 
@@ -72,6 +86,20 @@ replace_users() {
       return $output;
     }
 
+    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;
+    }
+
     return $name;
   }
 
@@ -91,6 +119,11 @@ replace_users() {
         $_;
       } split(m/,/, $1);
 
+      # Get unique names
+      my %saw;
+      @names = grep(!$saw{$_}++, @names);
+      undef %saw;
+
       foreach (sort @names) {
         print "Reviewed-by: $_\n";
       }
@@ -98,7 +131,15 @@ replace_users() {
       print;
     }
   }
+
+  exit($error? 33 : 0);
   ' "$msgfile"
+  then
+    :
+  else
+    [[ "$?" == 33 ]] && return 1
+    exit 1
+  fi
 
   if ! grep -q '^Reviewed-by: ' "$msgfile"
   then
@@ -157,14 +198,31 @@ copy_commit() {
   GIT_EDITOR="$me --commit-editor \"\$@\"" git commit -c "$rev" -s
 }
 
-main() {
-  local range="$1" target_branch="$2"
+usage() {
+  echo "Usage: $me_plain [from..to] <target-branch>" >&2
+  echo "  If not passed from..to defaults to target-branch..HEAD" >&2
+  exit 1
+}
 
-  if [[ -z "$target_branch" || "$range" != *..* ]]
-  then
-    echo "Usage: $me_plain <from..to> <target-branch>" >&2
-    exit 1
-  fi
+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
 
   git checkout "$target_branch"
   local old_head=$(git rev-parse HEAD)