Hello.

There's a complete patch that implements both git gcc-descr and gcc-undesrc
and sets corresponding git aliases to use them.

Ready to be installed?
Thanks,
Martin
From bf46024d03d00edf09d804449acbc5ff17690127 Mon Sep 17 00:00:00 2001
From: Martin Liska <mli...@suse.cz>
Date: Mon, 11 Oct 2021 14:36:19 +0200
Subject: [PATCH] Port git gcc-{,un}descr to Python.

contrib/ChangeLog:

	* gcc-git-customization.sh: Use the new python implementation.
	* describe_common.py: New file.
	* git-describe.py: New file.
	* git-undescribe.py: New file.
---
 contrib/describe_common.py       | 27 +++++++++++++++
 contrib/gcc-git-customization.sh |  5 ++-
 contrib/git-describe.py          | 56 ++++++++++++++++++++++++++++++++
 contrib/git-undescribe.py        | 39 ++++++++++++++++++++++
 4 files changed, 124 insertions(+), 3 deletions(-)
 create mode 100644 contrib/describe_common.py
 create mode 100755 contrib/git-describe.py
 create mode 100755 contrib/git-undescribe.py

diff --git a/contrib/describe_common.py b/contrib/describe_common.py
new file mode 100644
index 00000000000..ff48bccc71c
--- /dev/null
+++ b/contrib/describe_common.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python3
+
+import subprocess
+
+BASE_PREFIX = 'basepoints/gcc-'
+
+
+def run_git(cmd):
+    return subprocess.run(cmd, shell=True, encoding='utf8',
+                          stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+
+
+def get_upstream():
+    r = run_git('git config --get gcc-config.upstream')
+    upstream = r.stdout.strip() if r.returncode else 'origin'
+    return upstream
+
+
+def get_branch_for_version(version):
+    r = run_git('git rev-parse --quiet --verify '
+                f'origin/releases/gcc-{version}')
+    return f'releases/gcc-{version}' if r.returncode == 0 else 'master'
+
+
+def get_description(revision, options=''):
+    return run_git(f'git describe --all --match {BASE_PREFIX}[0-9]* '
+                   f'{revision} ' + options)
diff --git a/contrib/gcc-git-customization.sh b/contrib/gcc-git-customization.sh
index aca61b781ff..1cc0a5abc34 100755
--- a/contrib/gcc-git-customization.sh
+++ b/contrib/gcc-git-customization.sh
@@ -22,9 +22,8 @@ git config alias.svn-rev '!f() { rev=$1; shift; git log --all --grep="^From-SVN:
 
 # Add git commands to convert git commit to monotonically increasing revision number
 # and vice versa
-git config alias.gcc-descr \!"f() { if test \${1:-no} = --full; then c=\${2:-master}; r=\$(git describe --all --abbrev=40 --match 'basepoints/gcc-[0-9]*' \$c | sed -n 's,^\\(tags/\\)\\?basepoints/gcc-,r,p'); expr match \${r:-no} '^r[0-9]\\+\$' >/dev/null && r=\${r}-0-g\$(git rev-parse \${2:-master}); else c=\${1:-master}; r=\$(git describe --all --match 'basepoints/gcc-[0-9]*' \$c | sed -n 's,^\\(tags/\\)\\?basepoints/gcc-\\([0-9]\\+\\)-\\([0-9]\\+\\)-g[0-9a-f]*\$,r\\2-\\3,p;s,^\\(tags/\\)\\?basepoints/gcc-\\([0-9]\\+\\)\$,r\\2-0,p'); fi; if test -n \$r; then o=\$(git config --get gcc-config.upstream); rr=\$(echo \$r | sed -n 's,^r\\([0-9]\\+\\)-[0-9]\\+\\(-g[0-9a-f]\\+\\)\\?\$,\\1,p'); if git rev-parse --verify --quiet \${o:-origin}/releases/gcc-\$rr >/dev/null; then m=releases/gcc-\$rr; else m=master; fi; git merge-base --is-ancestor \$c \${o:-origin}/\$m && \echo \${r}; fi; }; f"
-git config alias.gcc-undescr \!"f() { o=\$(git config --get gcc-config.upstream); r=\$(echo \$1 | sed -n 's,^r\\([0-9]\\+\\)-[0-9]\\+\$,\\1,p'); n=\$(echo \$1 | sed -n 's,^r[0-9]\\+-\\([0-9]\\+\\)\$,\\1,p'); test -z \$r && echo Invalid id \$1 && exit 1; h=\$(git rev-parse --verify --quiet \${o:-origin}/releases/gcc-\$r); test -z \$h && h=\$(git rev-parse --verify --quiet \${o:-origin}/master); p=\$(git describe --all --match 'basepoints/gcc-'\$r \$h | sed -n 's,^\\(tags/\\)\\?basepoints/gcc-[0-9]\\+-\\([0-9]\\+\\)-g[0-9a-f]*\$,\\2,p;s,^\\(tags/\\)\\?basepoints/gcc-[0-9]\\+\$,0,p'); git rev-parse --verify \$h~\$(expr \$p - \$n); }; f"
-
+git config alias.gcc-descr '!f() { "`git rev-parse --show-toplevel`/contrib/git-describe.py" $@; } ; f'
+git config alias.gcc-undescr '!f() { "`git rev-parse --show-toplevel`/contrib/git-undescribe.py" $@; } ; f'
 git config alias.gcc-verify '!f() { "`git rev-parse --show-toplevel`/contrib/gcc-changelog/git_check_commit.py" $@; } ; f'
 git config alias.gcc-backport '!f() { "`git rev-parse --show-toplevel`/contrib/git-backport.py" $@; } ; f'
 git config alias.gcc-mklog '!f() { "`git rev-parse --show-toplevel`/contrib/mklog.py" $@; } ; f'
diff --git a/contrib/git-describe.py b/contrib/git-describe.py
new file mode 100755
index 00000000000..db2d229a31d
--- /dev/null
+++ b/contrib/git-describe.py
@@ -0,0 +1,56 @@
+#!/usr/bin/env python3
+
+import argparse
+import sys
+
+from describe_common import BASE_PREFIX, get_branch_for_version
+from describe_common import get_description, get_upstream, run_git
+
+DEFAULT_REV = 'master'
+hash_length = 14
+
+parser = argparse.ArgumentParser(description='Describe a GCC git commit.')
+parser.add_argument('revision', nargs='?', default=DEFAULT_REV,
+                    help=f'Described revision ("{DEFAULT_REV}" by default)')
+parser.add_argument('--full', '-f', action='store_true',
+                    help='Print complete git hash')
+parser.add_argument('--short', '-s', action='store_true',
+                    help='Shorten described revision')
+args = parser.parse_args()
+
+if args.full:
+    hash_length = 40
+
+r = get_description(args.revision, f'--abbrev={hash_length}')
+if r.returncode != 0:
+    print(r.stderr, end='')
+    sys.exit(1)
+
+# produces e.g. r12-4285-g07dd3bcda17f97
+descr = r.stdout.strip()
+assert BASE_PREFIX in descr
+descr = 'r' + descr[descr.find(BASE_PREFIX) + len(BASE_PREFIX):]
+
+# handle basepoints
+if '-' not in descr:
+    r = run_git(f'git rev-parse {args.revision}')
+    descr += '-0-g' + r.stdout.strip()[:hash_length]
+
+parts = descr.split('-')
+assert len(parts) == 3
+
+if args.short:
+    descr = '-'.join(parts[:-1])
+
+# verify common ancestor
+upstream = get_upstream()
+version = parts[0][1:]
+branch = get_branch_for_version(version)
+
+r = run_git(f'git merge-base --is-ancestor {args.revision} '
+            f'{upstream}/{branch}')
+if r.returncode != 0:
+    print(r.stderr)
+    sys.exit(2)
+
+print(descr)
diff --git a/contrib/git-undescribe.py b/contrib/git-undescribe.py
new file mode 100755
index 00000000000..430e30a90d6
--- /dev/null
+++ b/contrib/git-undescribe.py
@@ -0,0 +1,39 @@
+#!/usr/bin/env python3
+
+import argparse
+import re
+import sys
+
+from describe_common import get_branch_for_version
+from describe_common import get_description, get_upstream, run_git
+
+
+FORMAT = re.compile(r'r(?P<version>[0-9]+)-(?P<revs>[0-9]+)')
+
+parser = argparse.ArgumentParser(description='Undescribe a GCC git commit.')
+parser.add_argument('revision', help='A revision to undescribe')
+args = parser.parse_args()
+
+match = FORMAT.match(args.revision)
+if not match:
+    print(f'Invalid id {args.revision}')
+    sys.exit(1)
+
+version = int(match.group('version'))
+revs = int(match.group('revs'))
+
+branch = get_branch_for_version(version)
+upstream = get_upstream()
+
+r = run_git(f'git rev-parse --verify --quiet {upstream}/{branch}')
+assert r.returncode == 0
+tip = r.stdout.strip()
+
+r = get_description(tip)
+assert r.returncode == 0
+
+# Example parsing: get 4346 from tags/basepoints/gcc-12-4346-geb92cd57a1e
+tip_steps = int(r.stdout.strip().split('-')[-2])
+
+r = run_git(f'git rev-parse {tip}~{tip_steps - revs}')
+print(r.stdout.strip())
-- 
2.33.0

Reply via email to