1010import requests
1111
1212from ._compat import console_to_str
13+ from .command import CommandExecutor
1314from .exception import DirtyException , GitAggregatorException
1415
1516FETCH_DEFAULTS = ("depth" , "shallow-since" , "shallow-exclude" )
@@ -30,13 +31,13 @@ def ishex(s):
3031 return True
3132
3233
33- class Repo :
34+ class Repo ( CommandExecutor ) :
3435
3536 _git_version = None
3637
3738 def __init__ (self , cwd , remotes , merges , target ,
3839 shell_command_after = None , fetch_all = False , defaults = None ,
39- force = False ):
40+ force = False , patches = None ):
4041 """Initialize a git repository aggregator
4142
4243 :param cwd: path to the directory where to initialize the repository
@@ -56,7 +57,7 @@ def __init__(self, cwd, remotes, merges, target,
5657 :param bool force:
5758 When ``False``, it will stop if repo is dirty.
5859 """
59- self . cwd = cwd
60+ super (). __init__ ( cwd )
6061 self .remotes = remotes
6162 if fetch_all is True :
6263 self .fetch_all = frozenset (r ["name" ] for r in remotes )
@@ -67,6 +68,7 @@ def __init__(self, cwd, remotes, merges, target,
6768 self .shell_command_after = shell_command_after or []
6869 self .defaults = defaults or dict ()
6970 self .force = force
71+ self .patches = patches
7072
7173 @property
7274 def git_version (self ):
@@ -148,21 +150,6 @@ def query_remote_ref(self, remote, ref):
148150 return 'HEAD' , sha
149151 return None , ref
150152
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-
166153 def aggregate (self ):
167154 """ Aggregate all merges into the target branch
168155 If the target_dir doesn't exist, create an empty git repo otherwise
@@ -187,6 +174,7 @@ def aggregate(self):
187174 self ._reset_to (origin ["remote" ], origin ["ref" ])
188175 for merge in merges :
189176 self ._merge (merge )
177+ self .patches .apply ()
190178 self ._execute_shell_command_after ()
191179 logger .info ('End aggregation of %s' , self .cwd )
192180
@@ -313,6 +301,24 @@ def _merge(self, merge):
313301 cmd += self ._fetch_options (merge ) + (merge ["remote" ], merge ["ref" ])
314302 self .log_call (cmd , cwd = self .cwd )
315303
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+
316322 def _get_remotes (self ):
317323 lines = self .log_call (
318324 ['git' , 'remote' , '-v' ],
0 commit comments