|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +""" |
| 4 | +Utility to show git graph diff. |
| 5 | +""" |
| 6 | + |
| 7 | +import sys |
| 8 | +import subprocess |
| 9 | +import os |
| 10 | +import pydot |
| 11 | +import git |
| 12 | +import graphdiff |
| 13 | + |
| 14 | + |
| 15 | +DIR_PATH = os.path.dirname(os.path.realpath(__file__)) |
| 16 | +COLOR_SCRIPT_PATH = os.path.join(DIR_PATH, "diff-graph-color") |
| 17 | + |
| 18 | + |
| 19 | +def get_diff_for_file(repo, path): |
| 20 | + """ |
| 21 | + Return the diff between the current status of the file and the head. |
| 22 | + """ |
| 23 | + hcommit = repo.head.commit |
| 24 | + for diff in hcommit.diff(None): |
| 25 | + if diff.a_path == path or diff.b_path == path: |
| 26 | + return diff |
| 27 | + return None |
| 28 | + |
| 29 | + |
| 30 | +def get_path_commit_diffs(repo, path): |
| 31 | + """ |
| 32 | + This function returns a generator which iterates through all commits of |
| 33 | + the repository located in the given path for the given branch. It yields |
| 34 | + file diff information to show a timeseries of file changes. |
| 35 | + """ |
| 36 | + for commit in repo.iter_commits(): |
| 37 | + if commit.parents: |
| 38 | + all_diffs = commit.diff(commit.parents[0]) |
| 39 | + else: |
| 40 | + all_diffs = commit.diff(None) |
| 41 | + |
| 42 | + for diff in all_diffs: |
| 43 | + if diff.a_path == path or diff.b_path == path: |
| 44 | + yield commit, diff |
| 45 | + |
| 46 | + |
| 47 | +def print_graph(graph): |
| 48 | + """ |
| 49 | + Print the given graph. |
| 50 | + """ |
| 51 | + pydot_graph = graphdiff.to_dot(graph) |
| 52 | + proc = subprocess.Popen([COLOR_SCRIPT_PATH], stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
| 53 | + proc.stdin.write(pydot_graph.to_string().encode("utf-8")) |
| 54 | + out, _ = proc.communicate() |
| 55 | + print(out.decode("utf-8")) |
| 56 | + |
| 57 | + |
| 58 | +def load_graph_from_blob(blob): |
| 59 | + """ |
| 60 | + Loads and returns the graph from the given diff blob. |
| 61 | + """ |
| 62 | + blob_bytes = blob.data_stream.read() |
| 63 | + # TODO: handle more than one graph |
| 64 | + blob_dot_graph = pydot.graph_from_dot_data(blob_bytes.decode("utf-8"))[0] |
| 65 | + return graphdiff.from_dot(blob_dot_graph) |
| 66 | + |
| 67 | + |
| 68 | +def main(mode, path): |
| 69 | + """ |
| 70 | + Main function of the module |
| 71 | + """ |
| 72 | + # Create the repository, raises an error if it isn't one. |
| 73 | + repo = git.Repo("./") |
| 74 | + |
| 75 | + if mode == "diff": |
| 76 | + diff = get_diff_for_file(repo, path) |
| 77 | + if diff: |
| 78 | + a_graph = graphdiff.Graph() |
| 79 | + if diff.a_blob: |
| 80 | + a_graph = load_graph_from_blob(diff.a_blob) |
| 81 | + b_graph = graphdiff.from_dot(pydot.graph_from_dot_file(path)[0]) |
| 82 | + print_graph(graphdiff.generate_diff_graph(a_graph, b_graph)) |
| 83 | + elif mode == "log": |
| 84 | + for commit, diff in get_path_commit_diffs(repo, path): |
| 85 | + a_graph = graphdiff.Graph() |
| 86 | + if diff.a_blob: |
| 87 | + a_graph = load_graph_from_blob(diff.a_blob) |
| 88 | + |
| 89 | + b_graph = graphdiff.Graph() |
| 90 | + if diff.b_blob: |
| 91 | + b_graph = load_graph_from_blob(diff.b_blob) |
| 92 | + print("commit {}".format(commit.hexsha)) |
| 93 | + print("Author: {} <{}>".format(commit.author.name, commit.author.email)) |
| 94 | + print("Date: {}".format(commit.authored_datetime.strftime("%a %b %d %H:%M:%S %Y"))) |
| 95 | + print() |
| 96 | + print_graph(graphdiff.generate_diff_graph(b_graph, a_graph)) |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == "__main__": |
| 100 | + main(sys.argv[1], sys.argv[2]) |
0 commit comments