Skip to content

Commit d543bb5

Browse files
authored
Merge pull request #14 from mhagger/describe-options
Add options `--describe` and `--describe-contains`
2 parents c1c8bdd + 1f15570 commit d543bb5

File tree

2 files changed

+55
-9
lines changed

2 files changed

+55
-9
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ optional arguments:
8585
characters (or more if needed to avoid ambiguity). See
8686
also whenmerged.abbrev below under CONFIGURATION.
8787
--no-abbrev Do not abbreviate commit SHA-1s.
88+
--describe Describe the merge commit in terms of the most recent
89+
tag reachable from the commit (see git-describe(1))
90+
--describe-contains Describe the merge commit in terms of a nearby tag
91+
that contains it (see git-describe(1))
8892
--log, -l Show the log for the merge commit. When used with
8993
"--show-branch/-b", show the log for all of the
9094
commits that were merged at the same time as the

bin/git-when-merged

+51-9
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,21 @@ def rev_parse(arg, abbrev=None):
207207
raise Failure('%r is not a valid commit!' % (arg,))
208208

209209

210+
def describe(arg, contains=False):
211+
cmd = ['git', 'describe']
212+
if contains:
213+
cmd += ['--contains']
214+
cmd += [arg]
215+
216+
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
217+
out, err = process.communicate()
218+
retcode = process.poll()
219+
if retcode:
220+
return None
221+
else:
222+
return _decode_output(out).strip()
223+
224+
210225
def rev_list(*args):
211226
"""Iterate over (commit, [parent,...]) for the selected commits.
212227
@@ -365,15 +380,26 @@ def get_full_name(branch):
365380
return branch
366381

367382

368-
FIRST_FORMAT = '%(refname)-38s %(sha1)s'
369-
OTHER_FORMAT = FIRST_FORMAT % dict(refname='', sha1='via %(sha1)s')
383+
FIRST_FORMAT = '%(refname)-38s %(name)s'
384+
OTHER_FORMAT = FIRST_FORMAT % dict(refname='', name='via %(name)s')
370385

371-
COMMIT_FORMAT = '%(sha1)s'
372-
BRANCH_FORMAT = '%(sha1)s^1..%(sha1)s'
386+
COMMIT_FORMAT = '%(name)s'
387+
BRANCH_FORMAT = '%(name)s^1..%(name)s'
373388

374389
WARN_FORMAT = '%(refname)-38s %(msg)s'
375390

376391

392+
def name_commit(sha1, options):
393+
if options.describe:
394+
return describe(sha1) or sha1
395+
elif options.describe_contains:
396+
return describe(sha1, contains=True) or sha1
397+
elif options.abbrev is not None:
398+
return rev_parse(sha1, abbrev=options.abbrev)
399+
else:
400+
return sha1
401+
402+
377403
def main(args):
378404
parser = argparse.ArgumentParser(
379405
prog='git when-merged',
@@ -439,7 +465,8 @@ def main(args):
439465
'--visualize.'
440466
),
441467
)
442-
parser.add_argument(
468+
group = parser.add_mutually_exclusive_group()
469+
group.add_argument(
443470
'--abbrev', metavar='N',
444471
action='store', type=int, default=default_abbrev,
445472
help=(
@@ -448,10 +475,24 @@ def main(args):
448475
'See also whenmerged.abbrev below under CONFIGURATION.'
449476
),
450477
)
451-
parser.add_argument(
478+
group.add_argument(
452479
'--no-abbrev', dest='abbrev', action='store_const', const=None,
453480
help='Do not abbreviate commit SHA-1s.',
454481
)
482+
group.add_argument(
483+
'--describe', action='store_true',
484+
help=(
485+
'Describe the merge commit in terms of the most recent tag '
486+
'reachable from the commit (see git-describe(1))'
487+
),
488+
)
489+
group.add_argument(
490+
'--describe-contains', action='store_true',
491+
help=(
492+
'Describe the merge commit in terms of a nearby tag '
493+
'that contains it (see git-describe(1))'
494+
),
495+
)
455496
parser.add_argument(
456497
'--log', '-l', action='store_true', default=False,
457498
help=(
@@ -549,15 +590,16 @@ def main(args):
549590
first = True
550591
try:
551592
for sha1 in find_merge(commit, branch):
552-
if options.abbrev is not None:
553-
sha1 = rev_parse(sha1, abbrev=options.abbrev)
593+
name = name_commit(sha1)
554594

555595
if first:
556596
format = first_format
557597
else:
558598
format = other_format
559599

560-
sys.stdout.write(format % dict(refname=branch, sha1=sha1) + '\n')
600+
sys.stdout.write(
601+
format % dict(refname=branch, sha1=sha1, name=name) + '\n',
602+
)
561603

562604
if options.log:
563605
cmd = ['git', '--no-pager', 'log']

0 commit comments

Comments
 (0)