hghooks: add hook to prevent try_task_config.json from landing on non-try trees (bug 1380357), r?gps draft
authorAndrew Halberstadt <ahalberstadt@mozilla.com>
Mon, 17 Jul 2017 15:36:54 -0400
changeset 11399 15d37aea53904007134cbea66dfb3c0e1acf6230
parent 11341 72b7800ab16fe777cfbb6e4d5ce47f8d74f4fe80
child 11400 1c69e0e7307981c15eb4e1c2d2e97d63a5957dcb
push id1742
push userahalberstadt@mozilla.com
push dateFri, 28 Jul 2017 17:46:39 +0000
reviewersgps
bugs1380357
hghooks: add hook to prevent try_task_config.json from landing on non-try trees (bug 1380357), r?gps MozReview-Commit-ID: 67RzJtZrfYy
hghooks/mozhghooks/prevent_try_config.py
hghooks/tests/test-prevent-try-config.t
new file mode 100644
--- /dev/null
+++ b/hghooks/mozhghooks/prevent_try_config.py
@@ -0,0 +1,30 @@
+#!/usr/bin/env python
+
+TRY_CONFIG_FOUND = """
+You are trying to commit the temporary 'try_task_config.json' file
+on a non-try branch. Either make sure you are pushing to try or
+remove the file and push again.
+"""
+
+
+def print_banner(ui, level, message):
+    width = max(len(l) for l in message.splitlines())
+    banner = [
+        ' {} '.format(level.upper()).center(width, '*'),
+        message.strip(),
+        '*' * width,
+    ]
+    ui.write('\n' + '\n'.join(banner) + '\n\n')
+
+
+def hook(ui, repo, node, source=None, **kwargs):
+    if source in ('pull', 'strip'):
+        return 0
+
+    for rev in repo.changelog.revs(repo[node].rev()):
+        ctx = repo[rev]
+        if 'try_task_config.json' in ctx.files():
+            print_banner(ui, 'error', TRY_CONFIG_FOUND)
+            return 1
+
+    return 0
new file mode 100644
--- /dev/null
+++ b/hghooks/tests/test-prevent-try-config.t
@@ -0,0 +1,71 @@
+  $ hg init server
+  $ cat > server/.hg/hgrc << EOF
+  > [hooks]
+  > pretxnchangegroup.prevent_try_config = python:mozhghooks.prevent_try_config.hook
+  > EOF
+
+  $ hg -q clone server client
+  $ cd client
+
+Regular file changes work
+
+  $ touch file0
+  $ hg -q commit -A -m "initial"
+
+  $ hg push
+  pushing to $TESTTMP/server
+  searching for changes
+  adding changesets
+  adding manifests
+  adding file changes
+  added 1 changesets with 1 changes to 1 files
+
+Can't push try_task_config.json
+
+  $ echo "config" > try_task_config.json
+  $ hg -q commit -A -m 'add nsprpub/file'
+
+  $ hg push
+  pushing to $TESTTMP/server
+  searching for changes
+  adding changesets
+  adding manifests
+  adding file changes
+  added 1 changesets with 1 changes to 1 files
+  
+  ***************************** ERROR ******************************
+  You are trying to commit the temporary 'try_task_config.json' file
+  on a non-try branch. Either make sure you are pushing to try or
+  remove the file and push again.
+  ******************************************************************
+  
+  transaction abort!
+  rollback completed
+  abort: pretxnchangegroup.prevent_try_config hook failed
+  [255]
+
+Multiple changesets handled properly
+
+  $ touch file1
+  $ hg -q commit -A -m 'add file1'
+  $ touch file2
+  $ hg -q commit -A -m 'add file2'
+
+  $ hg push
+  pushing to $TESTTMP/server
+  searching for changes
+  adding changesets
+  adding manifests
+  adding file changes
+  added 3 changesets with 3 changes to 3 files
+  
+  ***************************** ERROR ******************************
+  You are trying to commit the temporary 'try_task_config.json' file
+  on a non-try branch. Either make sure you are pushing to try or
+  remove the file and push again.
+  ******************************************************************
+  
+  transaction abort!
+  rollback completed
+  abort: pretxnchangegroup.prevent_try_config hook failed
+  [255]