On 10/8/21 13:12, Jakub Jelinek wrote:
I think changing the default is ok, but dropping --full is not,
it should stay and behave the way it did before (i.e. print
r12-4245-gdb3d7270b42fe27fb05664c4fdf524ab7ad13a75
same thing as the new default except for full hash instead of
first 14 chars from it).

All right, makes sense, but updating the git alias doesn't work for me.
Thus I'm suggesting a Python replacement of it, which does:

$ ./describe.py basepoints/gcc-11

r11-0-g50ee04838efc16


$ ./describe.py

r12-4285-g07dd3bcda17f97


$ ./describe.py --short

r12-4285


$ ./describe.py --full

r12-4285-g07dd3bcda17f97cf5476c3d6f2f2501c1e0712e6


Motivation behind usage of Python:
- the script can be put to contrib and nobody would have to re-run 
contrib/gcc-git-customization.sh locally
- it's not a single line script and one doesn't have to do bash escaping in the 
contrib/gcc-git-customization.sh script
- positional argument support

Thoughts?

Note I can port the same way the counterpart script.
One another note:

$ git gcc-descr basepoints/gcc-11

r11-0


seems to me like bogus.

Cheers,
Martin
#!/usr/bin/env python3

import argparse
import subprocess
import sys

DEFAULT_REV = 'master'
PREFIX = 'basepoints/gcc-'
hash_length = 14


def run_git(cmd):
    return subprocess.run(cmd, shell=True, encoding='utf8',
                          stdout=subprocess.PIPE, stderr=subprocess.PIPE)


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 = run_git(f'git describe --all --match {PREFIX}[0-9]* '
            f'{args.revision} --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 PREFIX in descr
descr = 'r' + descr[descr.find(PREFIX) + len(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
r = run_git('git config --get gcc-config.upstream')
upstream = r.stdout.strip() if r.returncode else 'origin'
gcc_branch = parts[0][1:]

r = run_git(f'git rev-parse --quiet --verify origin/releases/gcc-{gcc_branch}')
branch = f'releases/gcc-{gcc_branch}' if r.returncode == 0 else 'master'

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)

Reply via email to