23
23
safe_encode ,
24
24
safe_decode ,
25
25
)
26
- from git .exc import (
27
- UnmergedEntriesError ,
28
- HookExecutionError
29
- )
26
+ from git .exc import UnmergedEntriesError , HookExecutionError
30
27
from git .objects .fun import (
31
28
tree_to_stream ,
32
29
traverse_tree_recursive ,
33
- traverse_trees_recursive
30
+ traverse_trees_recursive ,
34
31
)
35
32
from git .util import IndexFileSHA1Writer , finalize_process
36
33
from gitdb .base import IStream
37
34
from gitdb .typ import str_tree_type
38
35
39
36
import os .path as osp
40
37
41
- from .typ import (
42
- BaseIndexEntry ,
43
- IndexEntry ,
44
- CE_NAMEMASK ,
45
- CE_STAGESHIFT
46
- )
47
- from .util import (
48
- pack ,
49
- unpack
50
- )
38
+ from .typ import BaseIndexEntry , IndexEntry , CE_NAMEMASK , CE_STAGESHIFT
39
+ from .util import pack , unpack
51
40
52
41
53
- S_IFGITLINK = S_IFLNK | S_IFDIR # a submodule
42
+ S_IFGITLINK = S_IFLNK | S_IFDIR # a submodule
54
43
CE_NAMEMASK_INV = ~ CE_NAMEMASK
55
44
56
- __all__ = ('write_cache' , 'read_cache' , 'write_tree_from_cache' , 'entry_key' ,
57
- 'stat_mode_to_index_mode' , 'S_IFGITLINK' , 'run_commit_hook' , 'hook_path' )
45
+ __all__ = (
46
+ "write_cache" ,
47
+ "read_cache" ,
48
+ "write_tree_from_cache" ,
49
+ "entry_key" ,
50
+ "stat_mode_to_index_mode" ,
51
+ "S_IFGITLINK" ,
52
+ "run_commit_hook" ,
53
+ "hook_path" ,
54
+ )
58
55
59
56
60
57
def hook_path (name , git_dir ):
61
58
""":return: path to the given named hook in the given git repository directory"""
62
- return osp .join (git_dir , ' hooks' , name )
59
+ return osp .join (git_dir , " hooks" , name )
63
60
64
61
65
62
def run_commit_hook (name , index , * args ):
66
63
"""Run the commit hook of the given name. Silently ignores hooks that do not exist.
67
64
:param name: name of hook, like 'pre-commit'
68
65
:param index: IndexFile instance
69
66
:param args: arguments passed to hook file
70
- :raises HookExecutionError: """
67
+ :raises HookExecutionError:"""
71
68
hp = hook_path (name , index .repo .git_dir )
72
69
if not os .access (hp , os .X_OK ):
73
70
return
74
71
75
72
env = os .environ .copy ()
76
- env [' GIT_INDEX_FILE' ] = safe_decode (index .path ) if PY3 else safe_encode (index .path )
77
- env [' GIT_EDITOR' ] = ':'
73
+ env [" GIT_INDEX_FILE" ] = safe_decode (index .path ) if PY3 else safe_encode (index .path )
74
+ env [" GIT_EDITOR" ] = ":"
78
75
try :
79
- cmd = subprocess .Popen ([hp ] + list (args ),
80
- env = env ,
81
- stdout = subprocess .PIPE ,
82
- stderr = subprocess .PIPE ,
83
- cwd = index .repo .working_dir ,
84
- close_fds = is_posix ,
85
- creationflags = PROC_CREATIONFLAGS ,)
76
+ cmd = subprocess .Popen (
77
+ [hp ] + list (args ),
78
+ env = env ,
79
+ stdout = subprocess .PIPE ,
80
+ stderr = subprocess .PIPE ,
81
+ cwd = index .repo .working_dir ,
82
+ close_fds = is_posix ,
83
+ creationflags = PROC_CREATIONFLAGS ,
84
+ )
86
85
except Exception as ex :
87
86
raise HookExecutionError (hp , ex )
88
87
else :
89
88
stdout = []
90
89
stderr = []
91
90
handle_process_output (cmd , stdout .append , stderr .append , finalize_process )
92
- stdout = '' .join (stdout )
93
- stderr = '' .join (stderr )
91
+ stdout = "" .join (stdout )
92
+ stderr = "" .join (stderr )
94
93
if cmd .returncode != 0 :
95
94
stdout = force_text (stdout , defenc )
96
95
stderr = force_text (stderr , defenc )
@@ -101,11 +100,11 @@ def run_commit_hook(name, index, *args):
101
100
def stat_mode_to_index_mode (mode ):
102
101
"""Convert the given mode from a stat call to the corresponding index mode
103
102
and return it"""
104
- if S_ISLNK (mode ): # symlinks
103
+ if S_ISLNK (mode ): # symlinks
105
104
return S_IFLNK
106
- if S_ISDIR (mode ) or S_IFMT (mode ) == S_IFGITLINK : # submodules
105
+ if S_ISDIR (mode ) or S_IFMT (mode ) == S_IFGITLINK : # submodules
107
106
return S_IFGITLINK
108
- return S_IFREG | 0o644 | (mode & 0o111 ) # blobs with or without executable bit
107
+ return S_IFREG | 0o644 | (mode & 0o111 ) # blobs with or without executable bit
109
108
110
109
111
110
def write_cache (entries , stream , extension_data = None , ShaStreamCls = IndexFileSHA1Writer ):
@@ -134,17 +133,28 @@ def write_cache(entries, stream, extension_data=None, ShaStreamCls=IndexFileSHA1
134
133
# body
135
134
for entry in entries :
136
135
beginoffset = tell ()
137
- write (entry [4 ]) # ctime
138
- write (entry [5 ]) # mtime
136
+ write (entry [4 ]) # ctime
137
+ write (entry [5 ]) # mtime
139
138
path = entry [3 ]
140
139
path = force_bytes (path , encoding = defenc )
141
- plen = len (path ) & CE_NAMEMASK # path length
140
+ plen = len (path ) & CE_NAMEMASK # path length
142
141
assert plen == len (path ), "Path %s too long to fit into index" % entry [3 ]
143
- flags = plen | (entry [2 ] & CE_NAMEMASK_INV ) # clear possible previous values
144
- write (pack (">LLLLLL20sH" , entry [6 ], entry [7 ], entry [0 ],
145
- entry [8 ], entry [9 ], entry [10 ], entry [1 ], flags ))
142
+ flags = plen | (entry [2 ] & CE_NAMEMASK_INV ) # clear possible previous values
143
+ write (
144
+ pack (
145
+ ">LLLLLL20sH" ,
146
+ entry [6 ],
147
+ entry [7 ],
148
+ entry [0 ],
149
+ entry [8 ],
150
+ entry [9 ],
151
+ entry [10 ],
152
+ entry [1 ],
153
+ flags ,
154
+ )
155
+ )
146
156
write (path )
147
- real_size = (( tell () - beginoffset + 8 ) & ~ 7 )
157
+ real_size = (tell () - beginoffset + 8 ) & ~ 7
148
158
write (b"\0 " * ((beginoffset + real_size ) - tell ()))
149
159
# END for each entry
150
160
@@ -195,14 +205,17 @@ def read_cache(stream):
195
205
beginoffset = tell ()
196
206
ctime = unpack (">8s" , read (8 ))[0 ]
197
207
mtime = unpack (">8s" , read (8 ))[0 ]
198
- (dev , ino , mode , uid , gid , size , sha , flags ) = \
199
- unpack (">LLLLLL20sH" , read (20 + 4 * 6 + 2 ))
208
+ (dev , ino , mode , uid , gid , size , sha , flags ) = unpack (
209
+ ">LLLLLL20sH" , read (20 + 4 * 6 + 2 )
210
+ )
200
211
path_size = flags & CE_NAMEMASK
201
212
path = read (path_size ).decode (defenc )
202
213
203
- real_size = (( tell () - beginoffset + 8 ) & ~ 7 )
214
+ real_size = (tell () - beginoffset + 8 ) & ~ 7
204
215
read ((beginoffset + real_size ) - tell ())
205
- entry = IndexEntry ((mode , sha , flags , path , ctime , mtime , dev , ino , uid , gid , size ))
216
+ entry = IndexEntry (
217
+ (mode , sha , flags , path , ctime , mtime , dev , ino , uid , gid , size )
218
+ )
206
219
# entry_key would be the method to use, but we safe the effort
207
220
entries [(path , entry .stage )] = entry
208
221
count += 1
@@ -215,8 +228,10 @@ def read_cache(stream):
215
228
# 4 bytes length of chunk
216
229
# repeated 0 - N times
217
230
extension_data = stream .read (~ 0 )
218
- assert len (extension_data ) > 19 , "Index Footer was not at least a sha on content as it was only %i bytes in size" \
219
- % len (extension_data )
231
+ assert len (extension_data ) > 19 , (
232
+ "Index Footer was not at least a sha on content as it was only %i bytes in size"
233
+ % len (extension_data )
234
+ )
220
235
221
236
content_sha = extension_data [- 20 :]
222
237
@@ -246,7 +261,7 @@ def write_tree_from_cache(entries, odb, sl, si=0):
246
261
raise UnmergedEntriesError (entry )
247
262
# END abort on unmerged
248
263
ci += 1
249
- rbound = entry .path .find ('/' , si )
264
+ rbound = entry .path .find ("/" , si )
250
265
if rbound == - 1 :
251
266
# its not a tree
252
267
tree_items_append ((entry .binsha , entry .mode , entry .path [si :]))
@@ -256,7 +271,7 @@ def write_tree_from_cache(entries, odb, sl, si=0):
256
271
xi = ci
257
272
while xi < end :
258
273
oentry = entries [xi ]
259
- orbound = oentry .path .find ('/' , si )
274
+ orbound = oentry .path .find ("/" , si )
260
275
if orbound == - 1 or oentry .path [si :orbound ] != base :
261
276
break
262
277
# END abort on base mismatch
@@ -265,7 +280,9 @@ def write_tree_from_cache(entries, odb, sl, si=0):
265
280
266
281
# enter recursion
267
282
# ci - 1 as we want to count our current item as well
268
- sha , tree_entry_list = write_tree_from_cache (entries , odb , slice (ci - 1 , xi ), rbound + 1 ) # @UnusedVariable
283
+ sha , tree_entry_list = write_tree_from_cache (
284
+ entries , odb , slice (ci - 1 , xi ), rbound + 1
285
+ ) # @UnusedVariable
269
286
tree_items_append ((sha , S_IFDIR , base ))
270
287
271
288
# skip ahead
@@ -283,7 +300,9 @@ def write_tree_from_cache(entries, odb, sl, si=0):
283
300
284
301
285
302
def _tree_entry_to_baseindexentry (tree_entry , stage ):
286
- return BaseIndexEntry ((tree_entry [1 ], tree_entry [0 ], stage << CE_STAGESHIFT , tree_entry [2 ]))
303
+ return BaseIndexEntry (
304
+ (tree_entry [1 ], tree_entry [0 ], stage << CE_STAGESHIFT , tree_entry [2 ])
305
+ )
287
306
288
307
289
308
def aggressive_tree_merge (odb , tree_shas ):
@@ -301,7 +320,7 @@ def aggressive_tree_merge(odb, tree_shas):
301
320
# one and two way is the same for us, as we don't have to handle an existing
302
321
# index, instrea
303
322
if len (tree_shas ) in (1 , 2 ):
304
- for entry in traverse_tree_recursive (odb , tree_shas [- 1 ], '' ):
323
+ for entry in traverse_tree_recursive (odb , tree_shas [- 1 ], "" ):
305
324
out_append (_tree_entry_to_baseindexentry (entry , 0 ))
306
325
# END for each entry
307
326
return out
@@ -311,7 +330,7 @@ def aggressive_tree_merge(odb, tree_shas):
311
330
raise ValueError ("Cannot handle %i trees at once" % len (tree_shas ))
312
331
313
332
# three trees
314
- for base , ours , theirs in traverse_trees_recursive (odb , tree_shas , '' ):
333
+ for base , ours , theirs in traverse_trees_recursive (odb , tree_shas , "" ):
315
334
if base is not None :
316
335
# base version exists
317
336
if ours is not None :
@@ -320,8 +339,15 @@ def aggressive_tree_merge(odb, tree_shas):
320
339
# it exists in all branches, if it was changed in both
321
340
# its a conflict, otherwise we take the changed version
322
341
# This should be the most common branch, so it comes first
323
- if (base [0 ] != ours [0 ] and base [0 ] != theirs [0 ] and ours [0 ] != theirs [0 ]) or \
324
- (base [1 ] != ours [1 ] and base [1 ] != theirs [1 ] and ours [1 ] != theirs [1 ]):
342
+ if (
343
+ base [0 ] != ours [0 ]
344
+ and base [0 ] != theirs [0 ]
345
+ and ours [0 ] != theirs [0 ]
346
+ ) or (
347
+ base [1 ] != ours [1 ]
348
+ and base [1 ] != theirs [1 ]
349
+ and ours [1 ] != theirs [1 ]
350
+ ):
325
351
# changed by both
326
352
out_append (_tree_entry_to_baseindexentry (base , 1 ))
327
353
out_append (_tree_entry_to_baseindexentry (ours , 2 ))
0 commit comments