pre-upload: fix running with upstream repo checkout

The pre-upload script has assumed that, when imported, the repo program
is running out of $CHROMEOS_CHECKOUT/.repo/repo/.  This is normally the
case when people use the repo launcher script from depot_tools.

This fails if repo is being run directly from somewhere else.  I've got
an upstream version of repo living in /usr/local/src/repo/ (to test the
latest version before we update), so the argv[0] trick no longer works.

Lets switch the logic to use a different fact: when the upload hook is
run, the cwd is always the top level of the repo checkout.  This is in
the upstream hook documentation.

TEST=`repo upload` works using local .repo/ and upstream versions

Change-Id: I5d805bb5d1f66b86c9776274205372e80e937df0
Commit-Ready: Mike Frysinger <>
Tested-by: Mike Frysinger <>
Reviewed-by: Luis Hector Chavez <>
diff --git a/ b/
index ead2edb..375f695 100755
--- a/
+++ b/
@@ -26,10 +26,17 @@
 from errors import (VerifyException, HookFailure, PrintErrorForProject,
-# If repo imports us, the __name__ will be __builtin__, and the wrapper will
-# be in $CHROMEOS_CHECKOUT/.repo/repo/, so we need to go two directories
-# up. The same logic also happens to work if we're executed directly.
-if __name__ in ('__builtin__', '__main__'):
+# If repo imports us, the __name__ will be __builtin__, and the cwd will be in
+# the top level of the checkout (i.e. $CHROMEOS_CHECKOUT).  chromite will be in
+# that directory, so add it to our path.  This works whether we're running the
+# repo in $CHROMEOS_CHECKOUT/.repo/repo/ or a custom version in a completely
+# different tree.
+if __name__ == '__builtin__':
+  sys.path.insert(0, os.getcwd())
+# If we're run directly, we'll find chromite relative to the repohooks dir in
+# $CHROMEOS_CHECKOUT/src/repohooks, so go up two dirs.
+if __name__ == '__main__':
   sys.path.insert(0, os.path.join(os.path.dirname(sys.argv[0]), '..', '..'))
 from chromite.lib import commandline