From ca36dcc98c478e2cb0bef544e23a2a654dedde8a Mon Sep 17 00:00:00 2001 From: Sverre Rabbelier Date: Mon, 14 Jul 2008 18:44:01 +0200 Subject: [PATCH] Add an option to ignore parent in the 'belongs to' metric For repositories with long history it is faster to first call 'git rev-list' for the target commit and then ignore those commits. However, for smaller repositories the overhead of all these rev-lists makes it more slow. --- src/git_stats/branch.py | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/src/git_stats/branch.py b/src/git_stats/branch.py index 4b0b6a1..e3d9459 100644 --- a/src/git_stats/branch.py +++ b/src/git_stats/branch.py @@ -82,7 +82,7 @@ def _bestMetric(metrics): return min, champs -def _calculateDilution(target, start, parent_lists, global_memory): +def _calculateDilution(target, start, parent_lists, global_memory, ignore=[]): """Calculates the dilution for the specified commit. Args: @@ -90,6 +90,7 @@ def _calculateDilution(target, start, parent_lists, global_memory): start: The initial metric information. parent_list: A dictionary with commits and their parents. global_memory: A dictionary with all commits and their parents. + ignore: The parent commits of the target commit which should be ignored. """ metrics = [] @@ -103,25 +104,29 @@ def _calculateDilution(target, start, parent_lists, global_memory): while stack: current = stack.pop() + # We found what we are looking for + if target == current.commit: + metrics.append(current) + continue + + # Ignore commits that are past the target + if current.commit in ignore: + continue + # If we already checked this commit, don't add it again if memory.has_key(current.commit): dilution = memory[current.commit] if dilution <= current.dilution: - continue + continue if global_memory.has_key(current.commit): dilution = global_memory[current.commit] if dilution < current.dilution: - continue + continue memory[current.commit] = current.dilution global_memory[current.commit] = current.dilution - # We found what we are looking for - if target == current.commit: - metrics.append(current) - continue - parents = parent_lists[current.commit] # Root commit, we can't go any further @@ -130,9 +135,9 @@ def _calculateDilution(target, start, parent_lists, global_memory): for parent in parents: if parent == parents[0]: - newdilution = current.dilution + newdilution = current.dilution else: - newdilution = current.dilution + 1 + newdilution = current.dilution + 1 next = Metric() next.commit = parent @@ -202,13 +207,19 @@ def getParentList(commits): return parent_list -def belongsTo(commit, branch_map, parent_list, pretty_names={}, debug=False): +def belongsTo(commit, + branch_map, + parent_list, + pretty_names={}, + debug=False, + ignore_parents=False): """Returns which branches the specified commit belongs to Args: commit: The commit to examine. branch_map: A dictionary of all refs and their hashes. parent_list: A dictionary with tuples of refs and their rev-lists. + pretty_names: A dictionary with commits and their names debug: Whether to return debug information as well. Returns: A list of branches that the commit belongs to. @@ -223,6 +234,12 @@ def belongsTo(commit, branch_map, parent_list, pretty_names={}, debug=False): metrics = [] + if ignore_parents: + result = git.rev_list(commit) + ignore = set(result.split('\n')) + else: + ignore = [] + debugPrint(debug, "Checking branches now:") memory = {} @@ -236,7 +253,7 @@ def belongsTo(commit, branch_map, parent_list, pretty_names={}, debug=False): metric.dilution = 0 # Find the dilution for this branch - metric = _calculateDilution(commit, metric, parent_list, memory) + metric = _calculateDilution(commit, metric, parent_list, memory, ignore) metrics = metrics + metric debugPrint(debug, "Done.\n") -- 2.11.4.GIT