public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Martin Liška" <mliska@suse.cz>
To: Jakub Jelinek <jakub@redhat.com>
Cc: gcc-patches@gcc.gnu.org, Jonathan Wakely <jwakely.gcc@gmail.com>,
	Martin Jambor <mjambor@suse.cz>
Subject: Re: [RFC] Port git gcc-descr to Python
Date: Tue, 12 Oct 2021 10:59:43 +0200	[thread overview]
Message-ID: <12d44ac0-8849-466c-9749-b4e1e7168ac7@suse.cz> (raw)
In-Reply-To: <11400b93-b2bb-6133-88f7-20219433f2a7@suse.cz>

[-- Attachment #1: Type: text/plain, Size: 170 bytes --]

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

[-- Attachment #2: 0001-Port-git-gcc-un-descr-to-Python.patch --]
[-- Type: text/x-patch, Size: 7304 bytes --]

From bf46024d03d00edf09d804449acbc5ff17690127 Mon Sep 17 00:00:00 2001
From: Martin Liska <mliska@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	other threads:[~2021-10-12  8:59 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-08 11:01 [PATCH] contrib: git gcc-descr defaulting to print hash Martin Liška
2021-10-08 11:12 ` Jakub Jelinek
2021-10-11 12:32   ` [RFC] Port git gcc-descr to Python Martin Liška
2021-10-12  8:59     ` Martin Liška [this message]
2021-10-14  9:06       ` Tobias Burnus
2021-10-14  9:19         ` Jakub Jelinek
2021-10-18  9:01       ` Martin Liška
2022-01-19 11:01         ` Martin Liška
2022-01-19 12:49           ` Martin Jambor
2022-01-19 13:17             ` Martin Liška
2022-01-19 16:54               ` Eric Gallager
2022-01-27 15:06             ` [PATCH] contrib: Put gcc-descr and gcc-undescr to file Martin Liška
2022-01-27 15:35               ` Jakub Jelinek
2022-01-27 17:44                 ` Martin Liška
2022-01-27 15:51               ` Jonathan Wakely

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=12d44ac0-8849-466c-9749-b4e1e7168ac7@suse.cz \
    --to=mliska@suse.cz \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jakub@redhat.com \
    --cc=jwakely.gcc@gmail.com \
    --cc=mjambor@suse.cz \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).