Skip to content

Commit

Permalink
allow passing a revision range to CLI or web UI
Browse files Browse the repository at this point in the history
Now revision ranges like A..B or A...B are also accepted, and then git
deps will analyse the dependencies for all revisions in that range.
In the CLI invocation, if a dependency is encountered more than once
from separate commits, it will only be output once.
  • Loading branch information
aspiers committed Jan 2, 2017
1 parent e4fb3d9 commit d601e35
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 28 deletions.
82 changes: 61 additions & 21 deletions git-deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ class CLIDependencyListener(DependencyListener):
too long for useful output if recursion is enabled.
"""

def __init__(self, options):
super(CLIDependencyListener, self).__init__(options)

# Count each mention of each revision, so we can avoid duplicating
# commits in the output.
self._revs = {}

def new_commit(self, commit):
rev = commit.hex
if rev not in self._revs:
self._revs[rev] = 0
self._revs[rev] += 1

def new_dependency(self, dependent, dependency, path, line_num):
dependent_sha1 = dependent.hex
dependency_sha1 = dependency.hex
Expand All @@ -117,10 +130,10 @@ def new_dependency(self, dependent, dependency, path, line_num):
else:
print("%s %s" % (dependent_sha1, dependency_sha1))
else:
if not self.options.log:
if not self.options.log and self._revs[dependency_sha1] <= 1:
print(dependency_sha1)

if self.options.log:
if self.options.log and self._revs[dependency_sha1] <= 1:
cmd = [
'git',
'--no-pager',
Expand Down Expand Up @@ -281,6 +294,12 @@ def refs_to(cls, sha1, repo):

return matching

@classmethod
def rev_list(cls, rev_range):
cmd = ['git', 'rev-list', rev_range]
return subprocess.check_output(cmd).strip().split('\n')


class InvalidCommitish(StandardError):
def __init__(self, commitish):
self.commitish = commitish
Expand Down Expand Up @@ -367,6 +386,9 @@ def default_logger(self):
logger.addHandler(handler)
return logger

def seen_commit(self, rev):
return rev in self.commits

def get_commit(self, rev):
if rev in self.commits:
return self.commits[rev]
Expand Down Expand Up @@ -670,11 +692,12 @@ def cli(options, args):

detector.add_listener(listener)

for dependent_rev in args:
try:
detector.find_dependencies(dependent_rev)
except KeyboardInterrupt:
pass
for revspec in args:
for rev in GitUtils.rev_list(revspec):
try:
detector.find_dependencies(rev)
except KeyboardInterrupt:
pass

if options.json:
print(json.dumps(listener.json(), sort_keys=True, indent=4))
Expand Down Expand Up @@ -743,26 +766,43 @@ def send_options():
client_options['repo_path'] = os.getcwd()
return jsonify(client_options)

@webserver.route('/deps.json/<commitish>')
def deps(commitish):
@webserver.route('/deps.json/<revspec>')
def deps(revspec):
detector = DependencyDetector(options)
listener = JSONDependencyListener(options)
detector.add_listener(listener)

try:
root_commit = detector.get_commit(commitish)
except InvalidCommitish as e:
return json_error(
422, 'Invalid commitish',
"Could not resolve commitish '%s'" % commitish,
commitish=commitish)
if '..' in revspec:
try:
revisions = GitUtils.rev_list(revspec)
except subprocess.CalledProcessError as e:
return json_err(
422, 'Invalid revision range',
"Could not resolve revision range '%s'" % revspec,
revspec=revspec)
else:
revisions = [revspec]

for rev in revisions:
try:
commit = detector.get_commit(rev)
except InvalidCommitish as e:
return json_error(
422, 'Invalid revision',
"Could not resolve revision '%s'" % rev,
rev=rev)

detector.find_dependencies(rev)

tip_commit = detector.get_commit(revisions[0])
tip_sha1 = tip_commit.hex

detector.find_dependencies(commitish)
json = listener.json()
json['root'] = {
'commitish': commitish,
'sha1': root_commit.hex,
'abbrev': GitUtils.abbreviate_sha1(root_commit.hex),
json['query'] = {
'revspec': revspec,
'revisions': revisions,
'tip_sha1': tip_sha1,
'tip_abbrev': GitUtils.abbreviate_sha1(tip_sha1),
}
return jsonify(json)

Expand Down
2 changes: 1 addition & 1 deletion html/git-deps.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ <h1>git commit dependency graph</h1>
</p>

<form class="commitish" action="#">
Detect dependencies for commit:
Detect dependencies for:
<input type="text" name="commitish" size="20"
value="master" autofocus />
<button>Submit</button>
Expand Down
2 changes: 1 addition & 1 deletion html/js/git-deps-data.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ add_data = (data) ->
return [
new_nodes
new_deps
data.root
data.query
]

return false
Expand Down
13 changes: 8 additions & 5 deletions html/js/git-deps-graph.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -356,13 +356,16 @@ launch_viewer = (d) ->
new_data_notification = (new_data) ->
new_nodes = new_data[0]
new_deps = new_data[1]
root = new_data[2]
query = new_data[2]
notification =
if root.commitish == root.sha1
"Analysed dependencies of #{root.abbrev}"
if query.revspec == query.tip_sha1
"Analysed dependencies of #{query.revspec}"
else if query.revisions.length == 1
"<span class=\"commit-ref\">#{query.revspec}</span>
resolved as #{query.tip_abbrev}"
else
"<span class=\"commit-ref\">#{root.commitish}</span>
resolved as #{root.sha1}"
"<span class=\"commit-ref\">#{query.revspec}</span>
expanded; tip is #{query.tip_abbrev}"
notification += "<p>#{new_nodes} new commit"
notification += "s" unless new_nodes == 1
notification += "; #{new_deps} new " +
Expand Down

0 comments on commit d601e35

Please sign in to comment.