1
1
import os
2
2
import time
3
+ import json
3
4
import redis
4
5
import threading
5
6
import subprocess
6
7
from git import Repo
7
8
from ..graph import Graph
9
+ from .git_graph import GitGraph
10
+ from typing import List , Optional
8
11
9
12
monitor_thread = None
10
13
replica_process = None
@@ -98,23 +101,39 @@ def stop_monitor_effects():
98
101
print ("monitor thread exited" )
99
102
100
103
# build a graph capturing the git commit history
101
- def build_commit_graph (path : str ):
104
+ def build_commit_graph (path : str , ignore_list : Optional [List [str ]] = []) -> GitGraph :
105
+ print (f"Processing git history at: { path } " )
106
+ print (f"ignoring the following paths: { ignore_list } " )
107
+
102
108
repo = Repo (path )
103
109
104
110
repo_name = os .path .split (os .path .normpath (path ))[- 1 ]
105
- g = Graph (repo_name )
111
+ g = Graph (repo_name )
112
+ git_graph = GitGraph ('{' + repo_name + '}' + '_git' )
106
113
107
114
#setup_replication()
108
115
109
116
# start monitoring graph effects
110
117
# these capture the changes a graph goes through when moving from one
111
118
# git commit to another
112
- start_monitor_effects (g .g .name )
119
+ # start_monitor_effects(g.g.name)
113
120
114
121
head_commit = repo .commit ("HEAD" )
122
+
123
+ # add commit to the git graph
124
+ git_graph .add_commit (head_commit .hexsha , head_commit .author .name ,
125
+ head_commit .message , head_commit .committed_date )
126
+
115
127
while len (head_commit .parents ) > 0 :
116
128
prev_commit = head_commit .parents [0 ]
117
129
130
+ # add commit to the git graph
131
+ git_graph .add_commit (prev_commit .hexsha , prev_commit .author .name ,
132
+ prev_commit .message , prev_commit .committed_date )
133
+
134
+ # connect child parent commits relation
135
+ git_graph .connect_commits (head_commit .hexsha , prev_commit .hexsha )
136
+
118
137
# represents the changes going backward!
119
138
# e.g. which files need to be deleted when moving back one commit
120
139
#
@@ -134,16 +153,17 @@ def build_commit_graph(path: str):
134
153
135
154
for change in diff :
136
155
if change .new_file :
137
- #print(f"new_file: {change.b_path}")
138
- added .append (change .b_path )
156
+ if all (not change .b_path .startswith (ignore ) for ignore in ignore_list ):
157
+ #print(f"new_file: {change.b_path}")
158
+ added .append (change .b_path )
139
159
elif change .deleted_file :
140
- print (f"deleted_file: { change .a_path } " )
141
- deleted .append (change .a_path )
160
+ if all (not change .a_path .startswith (ignore ) for ignore in ignore_list ):
161
+ print (f"deleted_file: { change .a_path } " )
162
+ deleted .append (change .a_path )
142
163
elif change .change_type == 'M' :
143
- #print(f"modified_file: {change.a_path}")
144
- modified .append (change .a_path )
145
-
146
- head_commit = prev_commit
164
+ if all (not change .a_path .startswith (ignore ) for ignore in ignore_list ):
165
+ #print(f"modified_file: {change.a_path}")
166
+ modified .append (change .a_path )
147
167
148
168
#-----------------------------------------------------------------------
149
169
# apply changes
@@ -159,16 +179,62 @@ def build_commit_graph(path: str):
159
179
'ext' : os .path .splitext (path )[1 ]} for path in deleted ]
160
180
161
181
# remove deleted files from the graph
162
- q , params = g .delete_files (deleted_files , True )
163
- if (q is not None ):
182
+ transition = g .delete_files (deleted_files , True )
183
+ if (transition is not None ):
184
+ queries , params = transition
164
185
# log transition action
186
+ git_graph .set_parent_transition (head_commit .hexsha ,
187
+ prev_commit .hexsha , queries ,
188
+ json .dumps (params ))
165
189
166
- input ("Press Enter to continue..." )
190
+ # advance to the next commit
191
+ head_commit = prev_commit
167
192
168
193
# clean up
169
194
#stop_monitor_effects()
170
195
#teardown_replica()
171
196
197
+ return git_graph
198
+
199
+ def switch_commit (repo : str , to : str ):
200
+ """switch graph state from its current commit to given commit"""
201
+
202
+ g = Graph (repo )
203
+ git_graph = GitGraph ('{' + repo + '}' + 'git' )
204
+
205
+ # Get the graph's current commit
206
+ current_hash = g .get_graph_commit ()
207
+
208
+ # Find path from current commit to desired commit
209
+ commits = git_graph .get_commits ([current_hash , to ])
210
+
211
+ if len (commits ) != 2 :
212
+ print ("missing commits" )
213
+ return
214
+
215
+ # determine relation between commits
216
+ current_commit = commits [0 ] if commits [0 ]['hash' ] == current_hash else commits [1 ]
217
+ new_commit = commits [0 ] if commits [0 ]['hash' ] == to else commits [1 ]
218
+
219
+ if current_commit ['date' ] > new_commit ['date' ]:
220
+ print ("moving backwared" )
221
+ queries , params = git_graph .get_parent_transition (current_commit ['hash' ], new_commit ['hash' ])
222
+ else :
223
+ print ("moving forwards" )
224
+ queries , params = git_graph .get_child_transition (current_commit ['hash' ], new_commit ['hash' ])
225
+
226
+ # Apply transitions
227
+ for i in range (0 , len (queries )):
228
+ q = queries [i ]
229
+ p = json .loads (params [i ])
230
+ print (f"query: { q } " )
231
+ print (f"params: { p } " )
232
+
233
+ g .rerun_query (q , p )
234
+
235
+ # update graph's commit
236
+ g .set_graph_commit (to )
237
+
172
238
if __name__ == "__main__" :
173
239
build_commit_graph ("/Users/roilipman/Dev/FalkorDB" )
174
240
0 commit comments