Skip to content

Commit 2de2baa

Browse files
authored
Merge pull request #5 from yeger00/git-graph-diff-tool
Git graph diff tool
2 parents d0c3409 + 6d96530 commit 2de2baa

File tree

6 files changed

+97
-111
lines changed

6 files changed

+97
-111
lines changed

README.md

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# graph-diff
22
Utilities to view diff between graphs
33

4-
# Getting starget
4+
# Getting started
55

66
## Install prerequisites
7-
1. graph-easy
8-
7+
Debian / Ubuntu:
8+
```
9+
sudo apt-get install libgraph-easy-perl
10+
```
911
## Install
1012
```
1113
git clone https://github.com/yeger00/graph-diff
@@ -16,9 +18,24 @@ pip install -e .
1618
```
1719
cat samples/before.dot | graph-easy --as boxart
1820
cat samples/after.dot | graph-easy --as boxart
19-
python -m graphdiff samples/before.dot samples/after.dot ./diff.dot
21+
python -m graphdiff samples/before.dot samples/after.dot > ./diff.dot
2022
cat ./diff.dot | ./diff-graph-color
2123
```
2224

23-
# git-diff
24-
TODO
25+
# git-graph-diff-tool
26+
It is possible to use graph-diff with git, with `git-graph-diff-tool` provided in this library. An usage example:
27+
![](images/git-log-example.gif?raw=true "git-graph-diff-tool example")
28+
29+
## Install
30+
For every repository you would like to install you need to add to .gitattributes file a rules to know how to handle .dot files. For example:
31+
```
32+
echo "*.dot diff=graph_diff" >> .gitattributes
33+
```
34+
Then, configure the difftool to be the `git-graph-diff-tool`. For example:
35+
```
36+
git config diff.graph_diff.command /path/to/git-graph-diff-tool
37+
```
38+
Then, you can use git as usual, while adding `--ext-diff` flag to enable external difftools.
39+
```
40+
git log -p --ext-diff
41+
```

git-graph

Lines changed: 0 additions & 100 deletions
This file was deleted.

git-graph-diff-tool

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env sh
2+
3+
# Git graph-diff tool.
4+
# GIT_EXTERNAL_DIFF command line arguments are available <here>
5+
6+
set -eu
7+
8+
readonly SCRIPT_DIR=$(dirname $(readlink -f $0))
9+
readonly SCRIPT=$(basename $(readlink -f $0))
10+
11+
perror()
12+
{
13+
echo "${SCRIPT}: ERROR: ${@}" >&2
14+
exit 1
15+
}
16+
17+
help()
18+
{
19+
(
20+
cat <<EOF
21+
Git graph-diff tool.
22+
See GIT_EXTERNAL_DIFF documentation for explanation of the arguments.
23+
24+
./${SCRIPT} [-h] [-x] PATH OLD-FILE OLD-HEX OLD-MODE NEW-FILE NEW-HEX NEW-MODE
25+
| * -h - print this help.
26+
| * -x - set bash's -x flag.
27+
EOF
28+
) | sed -r 's/^\s*\|?//'
29+
}
30+
31+
while getopts ":hx" option
32+
do
33+
case "${option}" in
34+
h) help
35+
exit 0
36+
;;
37+
38+
x) set -x
39+
;;
40+
41+
\?) perror "unknown option -${OPTARG}"
42+
;;
43+
esac
44+
done
45+
shift $((OPTIND-1))
46+
47+
# validate number of arguments left
48+
if [ "${#}" -ne 7 ]
49+
then
50+
perror "Invalid number of arguments (use -h for help)."
51+
fi
52+
53+
path="${1}"
54+
old_file="${2}"
55+
old_hex="${3}"
56+
old_mode="${4}"
57+
new_file="${5}"
58+
new_hex="${6}"
59+
new_mode="${7}"
60+
61+
python -m graphdiff "${old_file}" "${new_file}" | ${SCRIPT_DIR}/diff-graph-color
62+

graphdiff/__main__.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@ def load_graph(path):
88
'''
99
Loads a graph from .dot file into a Graph() object.
1010
'''
11-
pydot_graph = pydot.graph_from_dot_file(path)[0]
12-
return graphdiff.from_dot(pydot_graph)
11+
with open(path) as f:
12+
data = f.read()
13+
graph = graphdiff.Graph()
14+
if data:
15+
# TODO: handle multiple graphs
16+
pydot_graph = pydot.graph_from_dot_data(data)[0]
17+
graph = graphdiff.from_dot(pydot_graph)
18+
return graph
1319

1420

1521
def save_graph(graph, path):
@@ -26,8 +32,8 @@ def main():
2632
before_graph = load_graph(sys.argv[1])
2733
after_graph = load_graph(sys.argv[2])
2834
diff = graphdiff.generate_diff_graph(before_graph, after_graph)
29-
save_graph(diff, sys.argv[3])
30-
# print_graph(diff)
35+
# save_graph(diff, sys.argv[3])
36+
print_graph(diff)
3137

3238

3339
if __name__ == "__main__":

graphdiff/graphdiff.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ def remove_quotes(pydot_graph):
112112
node.set_label(node.get_label().replace('"', ''))
113113

114114
for edge in pydot_graph.get_edges():
115-
edge.set_label(edge.get_label().replace('"', ''))
115+
if edge.get_label():
116+
edge.set_label(edge.get_label().replace('"', ''))
116117

117118
def from_dot(pydot_graph):
118119
"""Generated a graph from pydot graph."""

images/git-log-example.gif

2.35 MB
Loading

0 commit comments

Comments
 (0)