2
2
import json
3
3
import logging
4
4
5
- from pygit2 import Commit
5
+ from pygit2 import Commit , Diff
6
6
from ..info import *
7
7
from pygit2 .repository import Repository
8
+ from pygit2 .enums import DeltaStatus , CheckoutStrategy
8
9
from pathlib import Path
9
10
from ..graph import Graph
10
11
from .git_graph import GitGraph
@@ -31,7 +32,7 @@ def is_ignored(file_path: str, ignore_list: List[str]) -> bool:
31
32
32
33
return any (file_path .startswith (ignore ) for ignore in ignore_list )
33
34
34
- def classify_changes (diff , ignore_list : List [str ]) -> tuple [list [Path ], list [Path ], list [Path ]]:
35
+ def classify_changes (diff : Diff , repo : Repository , ignore_list : List [str ]) -> tuple [list [Path ], list [Path ], list [Path ]]:
35
36
"""
36
37
Classifies changes into added, deleted, and modified files.
37
38
@@ -45,16 +46,16 @@ def classify_changes(diff, ignore_list: List[str]) -> tuple[list[Path], list[Pat
45
46
46
47
added , deleted , modified = [], [], []
47
48
48
- for change in diff :
49
- if change .new_file and not is_ignored (change .b_path , ignore_list ):
50
- logging .debug (f"new file: { change .b_path } " )
51
- added .append (Path (change .b_path ))
52
- if change .deleted_file and not is_ignored (change .a_path , ignore_list ):
53
- logging .debug (f"deleted file: { change .a_path } " )
54
- deleted .append (Path (change .a_path ))
55
- if change .change_type == 'M' and not is_ignored (change .a_path , ignore_list ):
56
- logging .debug (f"change file: { change .a_path } " )
57
- modified .append (Path (change .a_path ))
49
+ for change in diff . deltas :
50
+ if change .status == DeltaStatus . ADDED and not is_ignored (change .new_file . path , ignore_list ):
51
+ logging .debug (f"new file: { change .new_file } " )
52
+ added .append (Path (f" { repo . workdir } / { change .new_file . path } " ))
53
+ if change .status == DeltaStatus . DELETED and not is_ignored (change .old_file . path , ignore_list ):
54
+ logging .debug (f"deleted file: { change .old_file . path } " )
55
+ deleted .append (Path (f" { repo . workdir } / { change .old_file . path } " ))
56
+ if change .status == DeltaStatus . MODIFIED and not is_ignored (change .new_file . path , ignore_list ):
57
+ logging .debug (f"change file: { change .new_file . path } " )
58
+ modified .append (Path (f" { repo . workdir } / { change .new_file . path } " ))
58
59
59
60
return added , deleted , modified
60
61
@@ -89,7 +90,7 @@ def build_commit_graph(path: str, repo_name: str, ignore_list: Optional[List[str
89
90
# Save current git for later restoration
90
91
repo = Repository ('.' )
91
92
current_commit = repo .walk (repo .head .target ).__next__ ()
92
- current_commit_hexsha = current_commit .hex
93
+ current_commit_hexsha = current_commit .short_id
93
94
94
95
# Add commit to the git graph
95
96
git_graph .add_commit (current_commit )
@@ -108,7 +109,7 @@ def build_commit_graph(path: str, repo_name: str, ignore_list: Optional[List[str
108
109
git_graph .add_commit (parent_commit )
109
110
110
111
# connect child parent commits relation
111
- git_graph .connect_commits (child_commit .hex , parent_commit .hex )
112
+ git_graph .connect_commits (child_commit .short_id , parent_commit .short_id )
112
113
113
114
# Represents the changes going backward!
114
115
# e.g. which files need to be deleted when moving back one commit
@@ -120,15 +121,15 @@ def build_commit_graph(path: str, repo_name: str, ignore_list: Optional[List[str
120
121
121
122
# Process file changes in this commit
122
123
logging .info (f"""Computing diff between
123
- child { child_commit .hexsha } : { child_commit .message }
124
- and { parent_commit .hexsha } : { parent_commit .message } """ )
124
+ child { child_commit .short_id } : { child_commit .message }
125
+ and { parent_commit .short_id } : { parent_commit .message } """ )
125
126
126
- diff = child_commit .diff (parent_commit )
127
- added , deleted , modified = classify_changes (diff , ignore_list )
127
+ diff = repo .diff (child_commit , parent_commit )
128
+ added , deleted , modified = classify_changes (diff , repo , ignore_list )
128
129
129
130
# Checkout prev commit
130
- logging .info (f"Checking out commit: { parent_commit .hexsha } " )
131
- repo .checkout (parent_commit .hex )
131
+ logging .info (f"Checking out commit: { parent_commit .short_id } " )
132
+ repo .checkout_tree (parent_commit .tree , strategy = CheckoutStrategy . FORCE )
132
133
133
134
#-----------------------------------------------------------------------
134
135
# Apply changes going backwards
@@ -138,12 +139,10 @@ def build_commit_graph(path: str, repo_name: str, ignore_list: Optional[List[str
138
139
# TODO: a bit of a waste, compute in previous loop
139
140
deleted_files = []
140
141
for deleted_file_path in deleted :
141
- _ext = os . path . splitext ( deleted_file_path )[ 1 ]
142
+ _ext = deleted_file_path . suffix
142
143
if _ext in supported_types :
143
- _path = os .path .dirname (deleted_file_path )
144
- _name = os .path .basename (deleted_file_path )
145
144
deleted_files .append (
146
- {'path' : _path , 'name' : _name , 'ext' : _ext })
145
+ {'path' : str ( deleted_file_path ) , 'name' : deleted_file_path . name , 'ext' : _ext })
147
146
148
147
# remove deleted files from the graph
149
148
if len (deleted_files ) > 0 :
@@ -167,15 +166,15 @@ def build_commit_graph(path: str, repo_name: str, ignore_list: Optional[List[str
167
166
168
167
# Log transitions
169
168
logging .debug (f"""Save graph transition from
170
- commit: { child_commit .hex }
169
+ commit: { child_commit .short_id }
171
170
to
172
- commit: { parent_commit .hex }
171
+ commit: { parent_commit .short_id }
173
172
Queries: { queries }
174
173
Parameters: { params }
175
174
""" )
176
175
177
- git_graph .set_parent_transition (child_commit .hex ,
178
- parent_commit .hex , queries , params )
176
+ git_graph .set_parent_transition (child_commit .short_id ,
177
+ parent_commit .short_id , queries , params )
179
178
# advance to the next commit
180
179
child_commit = parent_commit
181
180
@@ -185,24 +184,24 @@ def build_commit_graph(path: str, repo_name: str, ignore_list: Optional[List[str
185
184
186
185
logging .info ("Computing transition queries moving forward" )
187
186
parent_commit = child_commit
188
- while parent_commit .hex != current_commit_hexsha :
189
- child_commit = git_graph .get_child_commit (parent_commit .hex )
187
+ while parent_commit .short_id != current_commit_hexsha :
188
+ child_commit = git_graph .get_child_commit (parent_commit .short_id )
190
189
child_commit = repo .walk (child_commit ['hash' ]).__next__ ()
191
190
192
191
# Represents the changes going forward
193
192
# e.g. which files need to be deleted when moving forward one commit
194
193
195
194
# Process file changes in this commit
196
195
logging .info (f"""Computing diff between
197
- child { parent_commit .hex } : { parent_commit .message }
198
- and { child_commit .hex } : { child_commit .message } """ )
196
+ child { parent_commit .short_id } : { parent_commit .message }
197
+ and { child_commit .short_id } : { child_commit .message } """ )
199
198
200
199
diff = repo .diff (parent_commit , child_commit )
201
- added , deleted , modified = classify_changes (diff , ignore_list )
200
+ added , deleted , modified = classify_changes (diff , repo , ignore_list )
202
201
203
202
# Checkout child commit
204
- logging .info (f"Checking out commit: { child_commit .hex } " )
205
- repo .checkout (child_commit .hex )
203
+ logging .info (f"Checking out commit: { child_commit .short_id } " )
204
+ repo .checkout_tree (child_commit .tree , strategy = CheckoutStrategy . FORCE )
206
205
207
206
#-----------------------------------------------------------------------
208
207
# Apply changes going forward
@@ -241,15 +240,15 @@ def build_commit_graph(path: str, repo_name: str, ignore_list: Optional[List[str
241
240
242
241
# Log transitions
243
242
logging .debug (f"""Save graph transition from
244
- commit: { parent_commit .hex }
243
+ commit: { parent_commit .short_id }
245
244
to
246
- commit: { child_commit .hex }
245
+ commit: { child_commit .short_id }
247
246
Queries: { queries }
248
247
Parameters: { params }
249
248
""" )
250
249
251
- git_graph .set_child_transition (child_commit .hex ,
252
- parent_commit .hex , queries , params )
250
+ git_graph .set_child_transition (child_commit .short_id ,
251
+ parent_commit .short_id , queries , params )
253
252
# advance to the child_commit
254
253
parent_commit = child_commit
255
254
0 commit comments