-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
describe-commit: New script to describe commits in terms of refs #12
Open
dbnicholson
wants to merge
2
commits into
ostreedev:master
Choose a base branch
from
dbnicholson:describe-commit
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#!/usr/bin/env python | ||
# | ||
# Describe a commit in terms of refs | ||
# | ||
# Copyright 2017 Dan Nicholson <[email protected]> | ||
# Licensed under the new-BSD license (http://www.opensource.org/licenses/bsd-license.php) | ||
|
||
from __future__ import print_function | ||
|
||
from argparse import ArgumentParser | ||
from collections import defaultdict | ||
from gi import require_version | ||
require_version('OSTree', '1.0') | ||
from gi.repository import GLib, Gio, OSTree | ||
import sys | ||
|
||
def commit_describe(repo, checksum, all_refs=False): | ||
refmap = defaultdict(list) | ||
|
||
_, ref_info = repo.list_refs() | ||
for ref, rev in ref_info.items(): | ||
# Walk the ref | ||
depth = 0 | ||
while True: | ||
_, commit = repo.load_variant_if_exists(OSTree.ObjectType.COMMIT, | ||
rev) | ||
if commit is None: | ||
if depth == 0: | ||
# The ref file is pointing to a non-existent commit | ||
print('Ref', ref, 'commit', rev, 'does not exist', | ||
file=sys.stderr) | ||
exit(1) | ||
else: | ||
# The commit's parent doesn't exist. Either it was | ||
# pruned out or never pulled in the first place. | ||
# Silently move on. | ||
break | ||
|
||
# Add this commit to the map | ||
refmap[rev].append((depth, ref + depth * '^')) | ||
|
||
# Get the parent | ||
rev = OSTree.commit_get_parent(commit) | ||
if rev is None: | ||
break | ||
depth += 1 | ||
|
||
# Sort by depth then ref name | ||
matches = sorted(refmap[checksum]) | ||
if not all_refs: | ||
matches = matches[:1] | ||
return [ref for depth, ref in matches] | ||
|
||
aparser = ArgumentParser( | ||
description='Describe a commit in terms of refs' | ||
) | ||
aparser.add_argument('--repo', help='repository path') | ||
aparser.add_argument('--all', action='store_true', | ||
help='show all descriptions') | ||
aparser.add_argument('commit', help='commit checksum or ref') | ||
args = aparser.parse_args() | ||
|
||
if args.repo is None: | ||
repo = OSTree.Repo.new_default() | ||
else: | ||
repo_file = Gio.File.new_for_path(args.repo) | ||
repo = OSTree.Repo.new(repo_file) | ||
repo.open() | ||
|
||
_, rev = repo.resolve_rev(args.commit, False) | ||
matches = commit_describe(repo, rev, all_refs=args.all) | ||
if len(matches) > 0: | ||
print(*matches, sep='\n') |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This also silently exits if the user typo'd a checksum. Is that intentional? We already break out below if there's no parent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I hadn't thought about that case. The reason this silently breaks is to catch the case that the parent commit is missing. The case below is when the commit doesn't actually have a parent. Perhaps this just needs to check if depth is 0 and raise an exception since that would represent the checksum that the user passed in.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually this part is not coupled to the checksum that was passed in by the user. This loop is entirely about building up names for referenced commits and their parents. I can add a warning or error if the commit pointed to by the ref itself (
depth == 0
) does not exist. What do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I turned this into an error if the ref is pointing to a non-existent commit. See below.