From 842ab9f14f071de8ddc48c74167def51dce06413 Mon Sep 17 00:00:00 2001
From: Chenxiong Qi <cqi@redhat.com>
Date: Sep 02 2016 07:33:16 +0000
Subject: Push to remote same name explicitly


Signed-off-by: Chenxiong Qi <cqi@redhat.com>

---

diff --git a/src/pyrpkg/__init__.py b/src/pyrpkg/__init__.py
index 3385442..114023a 100644
--- a/src/pyrpkg/__init__.py
+++ b/src/pyrpkg/__init__.py
@@ -176,6 +176,8 @@ class Commands(object):
         self.clone_config = None
         # Git namespacing for more than just rpm build artifacts
         self.distgit_namespaced = distgit_namespaced
+        # Current local branch
+        self._local_branch = None
 
     # Define properties here
     # Properties allow us to "lazy load" various attributes, which also means
@@ -335,6 +337,19 @@ class Commands(object):
                 raise rpkgError('Could not login to %s' % defaults['server'])
 
     @property
+    def local_branch(self):
+        """Return current active branch name"""
+        if not self._local_branch:
+            self.load_local_branch()
+        return self._local_branch
+
+    def load_local_branch(self):
+        try:
+            self._local_branch = self.repo.active_branch.name
+        except TypeError as e:
+            raise rpkgError('Repo in inconsistent state: %s' % e)
+
+    @property
     def branch_merge(self):
         """This property ensures the branch attribute"""
 
@@ -346,22 +361,15 @@ class Commands(object):
         """Find the remote tracking branch from the branch we're on.
 
         The goal of this function is to catch if we are on a branch we
-
         can make some assumptions about.  If there is no merge point
-
         then we raise and ask the user to specify.
         """
-
         if self.dist:
             self._branch_merge = self.dist
         else:
+            localbranch = self.local_branch
             try:
-                localbranch = self.repo.active_branch.name
-            except TypeError as e:
-                raise rpkgError('Repo in inconsistent state: %s' % e)
-            try:
-                merge = self.repo.git.config('--get',
-                                             'branch.%s.merge' % localbranch)
+                merge = self.repo.git.config('--get', 'branch.%s.merge' % localbranch)
             except git.GitCommandError as e:
                 raise rpkgError('Unable to find remote branch.  Use --dist')
             # Trim off the refs/heads so that we're just working with
@@ -1701,8 +1709,16 @@ class Commands(object):
         return patches_not_tracked
 
     def push(self, force=False):
-        """Push changes to the remote repository"""
+        """Push changes to the remote repository
+
+        Push changes from current active branch to the same name in remote
+        repository. Using following format of git-push::
 
+          git push origin branch_name
+
+        This avoids the need of setting push.default explicitly by packagers
+        manually.
+        """
         # see if our branch is tracking anything
         try:
             self.load_branch_merge()
@@ -1716,9 +1732,10 @@ class Commands(object):
                 ', '.join(untracked_patches),
                 'is' if len(untracked_patches) == 1 else 'are')
 
-        cmd = ['git', 'push']
+        ref = ':'.join((self.local_branch, self.branch_merge))
+        cmd = ['git', 'push', self.branch_remote, ref]
         if self.quiet:
-            cmd.append('-q')
+            cmd.insert(2, '-q')
         self._run_command(cmd, cwd=self.path)
 
     def sources(self, outdir=None):
diff --git a/test/commands/test_push.py b/test/commands/test_push.py
index 0c681f7..6b1d0e2 100644
--- a/test/commands/test_push.py
+++ b/test/commands/test_push.py
@@ -50,8 +50,9 @@ class CommandPushTestCase(CommandTestCase):
                               self.lookasidehash,
                               self.lookaside_cgi, self.gitbaseurl,
                               self.anongiturl, self.branchre, self.kojiconfig,
-                              self.build_client, self.user, self.dist,
-                              self.target, self.quiet)
+                              self.build_client,
+                              user=self.user, dist=None,
+                              target=self.target, quiet=self.quiet)
         cmd.clone_config = CLONE_CONFIG
         cmd.clone(self.module, anon=True)
         cmd.path = os.path.join(self.path, self.module)
@@ -82,8 +83,9 @@ class TestPushWithPatches(CommandTestCase):
                                    self.lookaside_cgi, self.gitbaseurl,
                                    self.anongiturl, self.branchre,
                                    self.kojiconfig,
-                                   self.build_client, self.user, self.dist,
-                                   self.target, self.quiet)
+                                   self.build_client,
+                                   user=self.user, dist=None,
+                                   target=self.target, quiet=self.quiet)
         self.cmd.clone_config = CLONE_CONFIG
         self.cmd.clone(self.module, anon=True)
         self.cmd.path = os.path.join(self.path, self.module)

