repo: add trace support to the launcher

Now that we have a central run_command point, we can easily add
tracing support to the launcher script.

Change-Id: I9e0335c196cafd6263ff501925abfe835f036c5e
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/254755
Tested-by: Mike Frysinger <vapier@google.com>
Reviewed-by: David Pursehouse <dpursehouse@collab.net>
diff --git a/repo b/repo
index bbcf0d5..ea3df1d 100755
--- a/repo
+++ b/repo
@@ -17,8 +17,29 @@
 import sys
 
 
+# Keep basic logic in sync with repo_trace.py.
+class Trace(object):
+  """Trace helper logic."""
+
+  REPO_TRACE = 'REPO_TRACE'
+
+  def __init__(self):
+    self.set(os.environ.get(self.REPO_TRACE) == '1')
+
+  def set(self, value):
+    self.enabled = bool(value)
+
+  def print(self, *args, **kwargs):
+    if self.enabled:
+      print(*args, **kwargs)
+
+
+trace = Trace()
+
+
 def exec_command(cmd):
   """Execute |cmd| or return None on failure."""
+  trace.print(':', ' '.join(cmd))
   try:
     if platform.system() == 'Windows':
       ret = subprocess.call(cmd)
@@ -309,6 +330,7 @@
     stdout = stdout.decode('utf-8')
   if stderr is not None:
     stderr = stderr.decode('utf-8')
+  trace.print(':', ' '.join(cmd))
   ret = RunResult(proc.returncode, stdout, stderr)
 
   # If things failed, print useful debugging output.
@@ -810,6 +832,8 @@
       opt.help = True
     elif a == '--version':
       opt.version = True
+    elif a == '--trace':
+      trace.set(True)
     elif not a.startswith('-'):
       cmd = a
       arg = args[i + 1:]