Add a presubmit hook for platform2

src/platform2 contains a lot of projects which creates a lot of noise in git
log. Namespacing the git commits will make it easier to match a git commit to a
given project.

The repo hook will:
* Do nothing for commits spanning multiple top level directories.
* For changes contained in a single top level directory, enforce a commit
* message starting with "project_name: " where project name is:
  - the content of .project_alias if it exists
  or
  - the top level directory name.

BUG=chromium:391131
TEST=Create a commit that modify only libchromeos. The commit is accepted if and
only if the commit message start with libchromeos:.
TEST=Create a commit that modify only libchromeos. Create a file .project_alias
containing lchromeos. The commit is accepted if and only if the commit message
startswith lchromeos:.

Change-Id: I77a9302bdc689fd32df0a872a9f8f7e3d08e01b5
Reviewed-on: https://chromium-review.googlesource.com/206879
Reviewed-by: Alex Vakulenko <avakulenko@chromium.org>
Commit-Queue: Bertrand Simonnet <bsimonnet@chromium.org>
Tested-by: Bertrand Simonnet <bsimonnet@chromium.org>
diff --git a/pre-upload.py b/pre-upload.py
index d87ddf1..cb8c3b7 100755
--- a/pre-upload.py
+++ b/pre-upload.py
@@ -862,6 +862,32 @@
                      'made there.' % project)
 
 
+def _check_project_prefix(_project, commit):
+  """Fails if the change is project specific and the commit message is not
+  prefixed by the project_name.
+  """
+
+  files = _get_affected_files(commit, relative=True)
+  prefix = os.path.commonprefix(files)
+  prefix = os.path.dirname(prefix)
+
+  # If there is no common prefix, the CL span multiple projects.
+  if prefix == '':
+    return
+
+  project_name = prefix.split('/')[0]
+  alias_file = os.path.join(prefix, '.project_alias')
+  # If an alias exists, use it.
+  if os.path.isfile(alias_file):
+    with open(alias_file, 'r') as f:
+      project_name = f.read().strip()
+
+  if not _get_commit_desc(commit).startswith(project_name + ': '):
+    return HookFailure('The commit title for changes affecting only %s'
+                       ' should start with \"%s: \"'
+                       % (project_name, project_name))
+
+
 # Base
 
 
@@ -897,6 +923,7 @@
     "chromiumos/overlays/board-overlays": [_check_manifests],
     "chromiumos/overlays/chromiumos-overlay": [_check_manifests],
     "chromiumos/overlays/portage-stable": [_check_manifests],
+    "chromiumos/platform2": [_check_project_prefix],
     "chromiumos/platform/depthcharge": [_run_checkpatch_depthcharge],
     "chromiumos/platform/ec": [_run_checkpatch_ec,
                                _check_change_has_branch_field],