[Replicant] [vendor_replicant-scripts] [PATCH v4 3/3] replicant_prepare_patch.py: Add -C option to run it in a given directory

Denis 'GNUtoo' Carikli GNUtoo at cyberdimension.org
Thu Oct 8 15:19:39 UTC 2020


Signed-off-by: Denis 'GNUtoo' Carikli <GNUtoo at cyberdimension.org>
---
 patches/replicant_prepare_patch.py | 39 +++++++++++++++++++++---------
 1 file changed, 28 insertions(+), 11 deletions(-)

diff --git a/patches/replicant_prepare_patch.py b/patches/replicant_prepare_patch.py
index 5ed3311..57f8d6c 100755
--- a/patches/replicant_prepare_patch.py
+++ b/patches/replicant_prepare_patch.py
@@ -36,8 +36,10 @@ git show {commit}
 """
 
 def usage(progname):
-    print('{} <git_revision> [nr_patches] <patches_serie_revision>'.format(
+    print('Usage:\n\t{} [-C <directory>] <git_revision> [nr_patches] <patches_serie_revision>'.format(
         progname))
+    print('Options:')
+    print('\t-C <directory>\t\tRun as if it was started in the given git directory')
     sys.exit(1)
 
 def get_config():
@@ -90,15 +92,20 @@ def get_config():
     return config
 
 class GitRepo(object):
-    def __init__(self, config):
+    def __init__(self, config, directory):
         self.config = config
+        self.directory = re.sub('/+$', '', directory)
+        if self.directory:
+            self.git = sh.git.bake("-C", self.directory)
+        else:
+            self.git = sh.git.bake()
 
     def get_repo_url(self):
-        output = sh.git('remote', 'get-url', self.config['local']['git_remote'])
+        output = self.git('remote', 'get-url', self.config['local']['git_remote'])
         return output
 
     def get_repo_name(self):
-        output = sh.git('remote', 'get-url', config['local']['git_remote'])
+        output = self.git('remote', 'get-url', config['local']['git_remote'])
         output = os.path.basename(str(output))
         output = re.sub(os.linesep, '', output)
         output = re.sub('\.git', '', output)
@@ -144,11 +151,16 @@ class GitRepo(object):
         if patches_revision != None:
             git_arguments.append('-v{}'.format(patches_revision))
 
-        patches = sh.git(*git_arguments).split(os.linesep)
-
+        patches = self.git(*git_arguments).split(os.linesep)
         patches.remove('')
 
-        return patches
+        if self.directory:
+            results = []
+            for patch in patches:
+                results.append(self.directory + os.sep + patch)
+            return results
+        else:
+            return patches
 
     def generate_cover_mail_text(self, commit, nr_patches, repo):
         cgit_url = 'https://git.replicant.us'
@@ -186,8 +198,8 @@ class GitRepo(object):
         return command
 
     def get_git_revision(self, git_revision):
-        output = sh.git('--no-pager', 'log', '--oneline', git_revision, '-1',
-                        '--format=%H')
+        output = self.git('--no-pager', 'log', '--oneline', git_revision, '-1',
+                          '--format=%H')
         revision = re.sub(os.linesep, '', str(output))
 
         return revision
@@ -195,10 +207,15 @@ class GitRepo(object):
 if __name__ == '__main__':
     nr_patches = 1
     patches_revision = None
+    directory = None
 
-    if len(sys.argv) not in [2, 3, 4]:
+    if len(sys.argv) not in [2, 3, 4, 5, 6]:
         usage(sys.argv[0])
 
+    if sys.argv[1] == '-C':
+        sys.argv.pop(1)
+        directory =  sys.argv.pop(1)
+
     if len (sys.argv) >= 3:
         nr_patches = int(sys.argv[2])
 
@@ -211,7 +228,7 @@ if __name__ == '__main__':
         print('Failed to find a configuration file')
         sys.exit(1)
 
-    repo = GitRepo(config)
+    repo = GitRepo(config, directory)
     git_revision = repo.get_git_revision(sys.argv[1])
 
     patches = repo.generate_patches(git_revision, nr_patches, patches_revision)
-- 
2.28.0



More information about the Replicant mailing list