gst-uninstalled: Display current git branch name in the shell prompt
authorPhilippe Normand <philn@igalia.com>
Fri, 17 Aug 2018 14:33:09 +0000 (15:33 +0100)
committerPhilippe Normand <philn@igalia.com>
Thu, 30 Aug 2018 16:03:24 +0000 (17:03 +0100)
This is really useful when using multiple git worktrees.

https://bugzilla.gnome.org/show_bug.cgi?id=796989

common.py
gst-uninstalled.py

index 5478622..7f16391 100644 (file)
--- a/common.py
+++ b/common.py
@@ -44,7 +44,7 @@ class Colors:
 
 def git(*args, repository_path='.'):
     return subprocess.check_output(["git"] + list(args), cwd=repository_path,
-                                   stderr=subprocess.STDOUT).decode()
+                                   ).decode()
 
 def accept_command(commands):
     """Search @commands and returns the first found absolute path."""
index 0ddeea1..8e2edf2 100755 (executable)
@@ -15,6 +15,7 @@ import tempfile
 from distutils.sysconfig import get_python_lib
 
 from common import get_meson
+from common import git
 
 SCRIPTDIR = os.path.dirname(os.path.realpath(__file__))
 PREFIX_DIR = os.path.join(SCRIPTDIR, 'prefix')
@@ -29,7 +30,7 @@ def prepend_env_var(env, var, value):
     env[var] = env[var].replace(os.pathsep + os.pathsep, os.pathsep).strip(os.pathsep)
 
 
-def get_subprocess_env(options):
+def get_subprocess_env(options, gst_version):
     env = os.environ.copy()
 
     env["CURRENT_GST"] = os.path.normpath(SCRIPTDIR)
@@ -42,8 +43,8 @@ def get_subprocess_env(options):
     prepend_env_var(env, "PATH", os.path.normpath(
         "%s/subprojects/gst-devtools/validate/tools" % options.builddir))
     prepend_env_var(env, "PATH", os.path.join(SCRIPTDIR, 'meson'))
-    env["GST_VERSION"] = options.gst_version
-    env["GST_ENV"] = 'gst-' + options.gst_version
+    env["GST_VERSION"] = gst_version
+    env["GST_ENV"] = 'gst-' + gst_version
     env["GST_PLUGIN_SYSTEM_PATH"] = ""
     env["GST_PLUGIN_SCANNER"] = os.path.normpath(
         "%s/subprojects/gstreamer/libs/gst/helpers/gst-plugin-scanner" % options.builddir)
@@ -197,8 +198,6 @@ if __name__ == "__main__":
     parser.add_argument("--srcdir",
                         default=SCRIPTDIR,
                         help="The top level source directory")
-    parser.add_argument("--gst-version", default="master",
-                        help="The GStreamer major version")
     options, args = parser.parse_known_args()
 
     if not os.path.exists(options.builddir):
@@ -212,6 +211,9 @@ if __name__ == "__main__":
               options.srcdir)
         exit(1)
 
+    # The following incantation will retrieve the current branch name.
+    gst_version = git("rev-parse", "--symbolic-full-name", "--abbrev-ref", "HEAD", repository_path=options.srcdir)
+
     if not args:
         if os.name is 'nt':
             args = [os.environ.get("COMSPEC", r"C:\WINDOWS\system32\cmd.exe")]
@@ -223,13 +225,13 @@ if __name__ == "__main__":
                 tmprc = tempfile.NamedTemporaryFile(mode='w')
                 with open(bashrc, 'r') as src:
                     shutil.copyfileobj(src, tmprc)
-                tmprc.write('\nexport PS1="[gst-%s] $PS1"' % options.gst_version)
+                tmprc.write('\nexport PS1="[gst-%s] $PS1"' % gst_version)
                 tmprc.flush()
                 # Let the GC remove the tmp file
                 args.append("--rcfile")
                 args.append(tmprc.name)
     try:
         exit(subprocess.call(args, cwd=options.srcdir, close_fds=False,
-                             env=get_subprocess_env(options)))
+                             env=get_subprocess_env(options, gst_version)))
     except subprocess.CalledProcessError as e:
         exit(e.returncode)