From 7a10963fcdc94dbf72c7cba72a885cab360f914c Mon Sep 17 00:00:00 2001
From: Tomas Tomecek <ttomecek@redhat.com>
Date: Oct 19 2015 12:31:53 +0000
Subject: [PATCH 1/2] move repo checking to a method


---

diff --git a/src/pyrpkg/__init__.py b/src/pyrpkg/__init__.py
index 08b2a61..def8d9f 100644
--- a/src/pyrpkg/__init__.py
+++ b/src/pyrpkg/__init__.py
@@ -1670,6 +1670,17 @@ class Commands(object):
         return self.lookasidecache.remote_file_exists(pkg_name, filename,
                                                       checksum)
 
+    def check_repo(self, is_dirty=True, all_pushed=True):
+        if is_dirty:
+            if self.repo.is_dirty():
+                raise rpkgError('%s has uncommitted changes.  Use git status '
+                                'to see details' % self.path)
+        if all_pushed:
+            branch = self.repo.active_branch
+            full_branch = '%s/%s' % (self.branch_remote, self.branch_merge)
+            if self.repo.git.rev_list('%s...%s' % (full_branch, branch)):
+                raise rpkgError('There are unpushed changes in your repo')
+
     def build(self, skip_tag=False, scratch=False, background=False,
               url=None, chain=None, arches=None, sets=False, nvr_check=True):
         """Initiate a build of the module.  Available options are:
@@ -1702,17 +1713,9 @@ class Commands(object):
         # construct the url
         if not url:
             # We don't have a url, so build from the latest commit
-            # Check to see if the tree is dirty
-            if self.repo.is_dirty():
-                raise rpkgError('%s has uncommitted changes.  Use git status '
-                                'to see details' % self.path)
-            # Need to check here to see if the local commit you want to build
-            # is pushed or not
-            branch = self.repo.active_branch
-            full_branch = '%s/%s' % (self.branch_remote, self.branch_merge)
-            if self.repo.git.rev_list('%s...%s' % (full_branch, branch)):
-                raise rpkgError('There are unpushed changes in your repo')
-
+            # Check to see if the tree is dirty and if all local commits
+            # are pushed
+            self.check_repo()
             url = self.anongiturl % {'module': self.module_name} + \
                 '?#%s' % self.commithash
         # Check to see if the target is valid
diff --git a/test/commands/test_check_repo.py b/test/commands/test_check_repo.py
new file mode 100644
index 0000000..e5be5a6
--- /dev/null
+++ b/test/commands/test_check_repo.py
@@ -0,0 +1,70 @@
+import os
+import shutil
+import subprocess
+import tempfile
+
+from pyrpkg.errors import rpkgError
+
+from . import CommandTestCase
+
+
+class CheckRepoCase(CommandTestCase):
+
+    def setUp(self):
+        super(CheckRepoCase, self).setUp()
+        self.dist = "master"
+        self.make_new_git(self.module)
+        moduledir = os.path.join(self.gitroot, self.module)
+
+        self.altpath = tempfile.mkdtemp(prefix='rpkg-tests.')
+        self.clonedir = os.path.join(self.altpath, self.module)
+        subprocess.check_call(['git', 'clone', 'file://%s' % moduledir],
+                              cwd=self.altpath, stdout=subprocess.PIPE,
+                              stderr=subprocess.PIPE)
+        import pyrpkg
+        self.cmd = pyrpkg.Commands(
+            self.clonedir, self.lookaside, self.lookasidehash,
+            self.lookaside_cgi, self.gitbaseurl,
+            self.anongiturl, self.branchre, self.kojiconfig,
+            self.build_client, self.user, self.dist,
+            self.target, self.quiet
+        )
+
+    def tearDown(self):
+        super(CheckRepoCase, self).tearDown()
+        # Drop the clone
+        shutil.rmtree(self.altpath)
+
+    def test_repo_is_dirty(self):
+        with open(os.path.join(self.clonedir, 'sources'), 'w') as fd:
+            fd.write("a")
+
+        with self.assertRaises(rpkgError) as cm:
+            self.cmd.check_repo(is_dirty=True, all_pushed=False)
+        exception = cm.exception
+        print exception.message
+        self.assertTrue("has uncommitted changes" in exception.message)
+
+    def test_repo_has_unpushed_changes(self):
+        with open(os.path.join(self.clonedir, 'sources'), 'w') as fd:
+            fd.write("a")
+        subprocess.check_call(
+            ['git', 'add', 'sources'],
+            cwd=self.clonedir
+        )
+        subprocess.check_call(
+            ['git', 'commit', '-m', 'commit sources'],
+            cwd=self.clonedir,
+        )
+
+        with self.assertRaises(rpkgError) as cm:
+            self.cmd.check_repo(is_dirty=False, all_pushed=True)
+        exception = cm.exception
+        print exception.message
+        self.assertTrue("There are unpushed changes in your repo" in exception.message)
+
+    def test_repo_is_clean(self):
+        self.cmd.check_repo(is_dirty=True, all_pushed=False)
+
+    def test_repo_has_everything_pushed(self):
+        self.cmd.check_repo(is_dirty=False, all_pushed=True)

From c5e8efed21128a1bd607ba05e3060fc03d6443bd Mon Sep 17 00:00:00 2001
From: Tomas Tomecek <ttomecek@redhat.com>
Date: Oct 19 2015 12:31:53 +0000
Subject: [PATCH 2/2] container-build: check repo


---

diff --git a/src/pyrpkg/__init__.py b/src/pyrpkg/__init__.py
index def8d9f..1e2c719 100644
--- a/src/pyrpkg/__init__.py
+++ b/src/pyrpkg/__init__.py
@@ -2411,6 +2411,9 @@ class Commands(object):
             if "buildContainer" not in self.kojisession.system.listMethods():
                 raise RuntimeError("Kojihub instance does not support buildContainer")
 
+            # check if repo is dirty and all commits are pushed
+            self.check_repo()
+
             build_target = self.kojisession.getBuildTarget(docker_target)
             if not build_target:
                 msg = "Unknown build target: %s" % docker_target

