Skip to content

adding log support to git graph #3

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

Merged
merged 1 commit into from
Jun 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@ Utilities to view diff between graphs

# Getting starget

## Intall prerequists
## Install prerequisites
1. graph-easy
1. `pip install -r requirements.txt`

## Install
```
git clone https://github.com/yeger00/graph-diff
pip install -e .
```

## Generate and view diff
```
cat samples/before.dot | graph-easy --as boxart
cat samples/after.dot | graph-easy --as boxart
python generate-diff-graph.py samples/before.dot samples/after.dot ./diff.dot
cat diff.dot | ./diff-graph-color
python -m graphdiff samples/before.dot samples/after.dot ./diff.dot
cat ./diff.dot | ./diff-graph-color
```

# git-diff
TODO
100 changes: 100 additions & 0 deletions git-graph
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/usr/bin/env python

"""
Utility to show git graph diff.
"""

import sys
import subprocess
import os
import pydot
import git
import graphdiff


DIR_PATH = os.path.dirname(os.path.realpath(__file__))
COLOR_SCRIPT_PATH = os.path.join(DIR_PATH, "diff-graph-color")


def get_diff_for_file(repo, path):
"""
Return the diff between the current status of the file and the head.
"""
hcommit = repo.head.commit
for diff in hcommit.diff(None):
if diff.a_path == path or diff.b_path == path:
return diff
return None


def get_path_commit_diffs(repo, path):
"""
This function returns a generator which iterates through all commits of
the repository located in the given path for the given branch. It yields
file diff information to show a timeseries of file changes.
"""
for commit in repo.iter_commits():
if commit.parents:
all_diffs = commit.diff(commit.parents[0])
else:
all_diffs = commit.diff(None)

for diff in all_diffs:
if diff.a_path == path or diff.b_path == path:
yield commit, diff


def print_graph(graph):
"""
Print the given graph.
"""
pydot_graph = graphdiff.to_dot(graph)
proc = subprocess.Popen([COLOR_SCRIPT_PATH], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
proc.stdin.write(pydot_graph.to_string().encode("utf-8"))
out, _ = proc.communicate()
print(out.decode("utf-8"))


def load_graph_from_blob(blob):
"""
Loads and returns the graph from the given diff blob.
"""
blob_bytes = blob.data_stream.read()
# TODO: handle more than one graph
blob_dot_graph = pydot.graph_from_dot_data(blob_bytes.decode("utf-8"))[0]
return graphdiff.from_dot(blob_dot_graph)


def main(mode, path):
"""
Main function of the module
"""
# Create the repository, raises an error if it isn't one.
repo = git.Repo("./")

if mode == "diff":
diff = get_diff_for_file(repo, path)
if diff:
a_graph = graphdiff.Graph()
if diff.a_blob:
a_graph = load_graph_from_blob(diff.a_blob)
b_graph = graphdiff.from_dot(pydot.graph_from_dot_file(path)[0])
print_graph(graphdiff.generate_diff_graph(a_graph, b_graph))
elif mode == "log":
for commit, diff in get_path_commit_diffs(repo, path):
a_graph = graphdiff.Graph()
if diff.a_blob:
a_graph = load_graph_from_blob(diff.a_blob)

b_graph = graphdiff.Graph()
if diff.b_blob:
b_graph = load_graph_from_blob(diff.b_blob)
print("commit {}".format(commit.hexsha))
print("Author: {} <{}>".format(commit.author.name, commit.author.email))
print("Date: {}".format(commit.authored_datetime.strftime("%a %b %d %H:%M:%S %Y")))
print()
print_graph(graphdiff.generate_diff_graph(b_graph, a_graph))


if __name__ == "__main__":
main(sys.argv[1], sys.argv[2])
43 changes: 0 additions & 43 deletions git-graph-diff

This file was deleted.

2 changes: 2 additions & 0 deletions graphdiff/graphdiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ def from_dot(pydot_graph):

for edge in pydot_graph.get_edges():
graph.edges.add(Edge(edge.get_source(), edge.get_destination()))
graph.nodes.add(edge.get_source())
graph.nodes.add(edge.get_destination())
return graph


Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ def run_tests(self):

setup(
name="graphdiff",
version="0.0.0",
version="0.0.1",
author="Avi Yeger",
author_email="yeger00@gmail.com",
description="",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/yeger00/",
packages=find_packages(),
install_requires=["pydot"],
tests_require=["pytest", "pytest_mock"],
cmdclass={"test": PyTest},
)