[PATCH] Allow naming the Git remote.

Mathieu Bridon bochecha at fedoraproject.org
Thu Feb 20 10:25:56 UTC 2014


By default, Git remotes are named 'origin', but some users of rpkg might
be interacting with several remotes for the same package module.

This change allows sites to define how remotes should be called by
setting the appropriate variable in the configuration file.

If nothing is specified, the default is 'origin', to not break existing
clones.

This has been tested with both regular clones and clones with folders
for each branch.

The rpkg bash completion resource had to be modified accoringly. Since
the fedpkg one already handles this properly, a simple copy-paste of
that specific line was enough.

Nothing else is assuming the remote to be called 'origin', so everything
should go on working as expected.
---
This is important for us.

We have this rpkg-based tool called nbpkg, which we use to interact with our
packaging infrastructure, similar to fedpkg:

    https://github.com/network-box/nbpkg

But as I'm also a Fedora packager, I like to be able to interact with both
our internal infrastructure, and the Fedora one, in the same Git clone, as
it allows me to easily flow changes in both directions.

That requires having different names for each remote though, they can't just
be all called 'origin'.

This patch makes this much easier.

I can imagine a similar use case for RHEL packagers, which could probably
want to interact with both the Fedora and RHEL packaging infrastructures in
the same Git clone, flowing changes both ways.

 src/pyrpkg/__init__.py | 22 ++++++++++++++--------
 src/pyrpkg/cli.py      |  1 +
 src/rpkg.bash          |  3 +--
 src/rpkg.conf          |  5 +++++
 4 files changed, 21 insertions(+), 10 deletions(-)

diff --git a/src/pyrpkg/__init__.py b/src/pyrpkg/__init__.py
index c5b4ae4..b2dd7d9 100644
--- a/src/pyrpkg/__init__.py
+++ b/src/pyrpkg/__init__.py
@@ -61,7 +61,7 @@ class Commands(object):
     """
 
     def __init__(self, path, lookaside, lookasidehash, lookaside_cgi,
-                 gitbaseurl, anongiturl, branchre, kojiconfig,
+                 gitbaseurl, anongiturl, branchre, remote, kojiconfig,
                  build_client, user=None, dist=None, target=None,
                  quiet=False):
         """Init the object and some configuration details."""
@@ -80,6 +80,8 @@ class Commands(object):
         self.anongiturl = anongiturl
         # The regex of branches we care about
         self.branchre = branchre
+        # The local name of the remote (usually 'origin')
+        self.remote = remote
         # The location of the buildsys config file
         self.kojiconfig = kojiconfig
         # The buildsys client to use
@@ -961,6 +963,11 @@ class Commands(object):
         else:
             self.log.debug('Cloning %s' % giturl)
             cmd.extend([giturl])
+
+        if not bare_dir:
+            # --bare and --origin are incompatible
+            cmd.extend(['--origin', self.remote])
+
         self._run_command(cmd, cwd=path)
 
         return
@@ -1009,14 +1016,14 @@ class Commands(object):
         for branch in branches:
             try:
                 # Make a local clone for our branch
-                top_git.clone("--branch", branch, repo_path, branch)
+                top_git.clone("--branch", branch, '--origin', self.remote,
+                        repo_path, branch)
 
-                # Set the origin correctly
+                # Set the remote correctly
                 branch_path = os.path.join(top_path, branch)
                 branch_git = git.Git(branch_path)
-                branch_git.config("--replace-all", "remote.origin.url", giturl)
-                # Bad use of "origin" here, need to fix this when more than one
-                # remote is used.
+                branch_git.config(
+                        "--replace-all", "remote.%s.url" % self.remote, giturl)
             except (git.GitCommandError, OSError), e:
                 raise rpkgError('Could not locally clone %s from %s: %s' %
                         (branch, repo_path, e))
@@ -1452,8 +1459,7 @@ class Commands(object):
             self.log.debug('No local branch found, creating a new one')
             totrack = None
             for remote in remotes:
-                # bad use of "origin" here, will have to be fixed
-                if remote.replace('origin/', '') == branch:
+                if remote.endswith(branch):
                     totrack = remote
                     break
             else:
diff --git a/src/pyrpkg/cli.py b/src/pyrpkg/cli.py
index 909e3c0..8d6850d 100755
--- a/src/pyrpkg/cli.py
+++ b/src/pyrpkg/cli.py
@@ -80,6 +80,7 @@ class cliClient(object):
                                        items['gitbaseurl'],
                                        items['anongiturl'],
                                        items['branchre'],
+                                       items.get('remote', 'origin'),
                                        items['kojiconfig'],
                                        items['build_client'],
                                        user=self.args.user,
diff --git a/src/rpkg.bash b/src/rpkg.bash
index 047c98d..a2e45b6 100644
--- a/src/rpkg.bash
+++ b/src/rpkg.bash
@@ -283,8 +283,7 @@ _rpkg_branch()
     local git_options= format="--format %(refname:short)"
     [[ -n $1 ]] && git_options="--git-dir=$1/.git"
 
-    git $git_options for-each-ref $format 'refs/remotes/origin/*' \
-        | sed 's,origin/,,'
+    git $git_options for-each-ref $format 'refs/remotes' | sed 's,.*/,,'
     git $git_options for-each-ref $format 'refs/heads'
 }
 
diff --git a/src/rpkg.conf b/src/rpkg.conf
index a0f3d34..0cf61aa 100644
--- a/src/rpkg.conf
+++ b/src/rpkg.conf
@@ -5,5 +5,10 @@ lookaside_cgi = https://localhost/repo/pkgs/upload.cgi
 gitbaseurl = ssh://%(user)s at localhost/%(module)s
 anongiturl = git://localhost/%(module)s
 branchre = f\d$|f\d\d$|el\d$|olpc\d$|master$
+
+# Set the following if you want your remote to be named something else
+# than 'origin'
+#remote = origin
+
 kojiconfig = /etc/koji.conf
 build_client = koji
-- 
1.8.5.3



More information about the rel-eng mailing list