10
10
import requests
11
11
12
12
from ._compat import console_to_str
13
+ from .command import CommandExecutor
13
14
from .exception import DirtyException , GitAggregatorException
14
15
15
16
FETCH_DEFAULTS = ("depth" , "shallow-since" , "shallow-exclude" )
@@ -30,13 +31,13 @@ def ishex(s):
30
31
return True
31
32
32
33
33
- class Repo :
34
+ class Repo ( CommandExecutor ) :
34
35
35
36
_git_version = None
36
37
37
38
def __init__ (self , cwd , remotes , merges , target ,
38
39
shell_command_after = None , fetch_all = False , defaults = None ,
39
- force = False ):
40
+ force = False , patches = None ):
40
41
"""Initialize a git repository aggregator
41
42
42
43
:param cwd: path to the directory where to initialize the repository
@@ -56,7 +57,7 @@ def __init__(self, cwd, remotes, merges, target,
56
57
:param bool force:
57
58
When ``False``, it will stop if repo is dirty.
58
59
"""
59
- self . cwd = cwd
60
+ super (). __init__ ( cwd )
60
61
self .remotes = remotes
61
62
if fetch_all is True :
62
63
self .fetch_all = frozenset (r ["name" ] for r in remotes )
@@ -67,6 +68,7 @@ def __init__(self, cwd, remotes, merges, target,
67
68
self .shell_command_after = shell_command_after or []
68
69
self .defaults = defaults or dict ()
69
70
self .force = force
71
+ self .patches = patches
70
72
71
73
@property
72
74
def git_version (self ):
@@ -148,21 +150,6 @@ def query_remote_ref(self, remote, ref):
148
150
return 'HEAD' , sha
149
151
return None , ref
150
152
151
- def log_call (self , cmd , callwith = subprocess .check_call ,
152
- log_level = logging .DEBUG , ** kw ):
153
- """Wrap a subprocess call with logging
154
- :param meth: the calling method to use.
155
- """
156
- logger .log (log_level , "%s> call %r" , self .cwd , cmd )
157
- try :
158
- ret = callwith (cmd , ** kw )
159
- except Exception :
160
- logger .error ("%s> error calling %r" , self .cwd , cmd )
161
- raise
162
- if callwith == subprocess .check_output :
163
- ret = console_to_str (ret )
164
- return ret
165
-
166
153
def aggregate (self ):
167
154
""" Aggregate all merges into the target branch
168
155
If the target_dir doesn't exist, create an empty git repo otherwise
@@ -187,6 +174,7 @@ def aggregate(self):
187
174
self ._reset_to (origin ["remote" ], origin ["ref" ])
188
175
for merge in merges :
189
176
self ._merge (merge )
177
+ self .patches .apply ()
190
178
self ._execute_shell_command_after ()
191
179
logger .info ('End aggregation of %s' , self .cwd )
192
180
@@ -313,6 +301,24 @@ def _merge(self, merge):
313
301
cmd += self ._fetch_options (merge ) + (merge ["remote" ], merge ["ref" ])
314
302
self .log_call (cmd , cwd = self .cwd )
315
303
304
+ def _patch (self , patch_path ):
305
+ cmd = (
306
+ "patch" ,
307
+ "-p1" ,
308
+ "--no-backup-if-mismatch" ,
309
+ "-t" ,
310
+ "-i" ,
311
+ str (patch_path .resolve ()),
312
+ )
313
+ if logger .getEffectiveLevel () != logging .DEBUG :
314
+ cmd += ('--quiet' ,)
315
+ self .log_call (cmd , cwd = self .cwd )
316
+ self .log_call (("git" , "add" , "." ), cwd = self .cwd )
317
+ self .log_call (
318
+ ("git" , "commit" , "-am" , "Applied patch %s" % str (patch_path )),
319
+ cwd = self .cwd ,
320
+ )
321
+
316
322
def _get_remotes (self ):
317
323
lines = self .log_call (
318
324
['git' , 'remote' , '-v' ],
0 commit comments