From a848025fd5044cae451b296981c7a553e3a383d1 Mon Sep 17 00:00:00 2001 From: Tomas Kopecek Date: Nov 08 2022 11:26:17 +0000 Subject: PR#3509: Enable fetching any ref from git repo Merges #3509 https://pagure.io/koji/pull-request/3509 Fixes: #3508 https://pagure.io/koji/issue/3508 Enable fetching any ref from git repo --- diff --git a/koji/daemon.py b/koji/daemon.py index d487803..41ac10c 100644 --- a/koji/daemon.py +++ b/koji/daemon.py @@ -520,7 +520,7 @@ class SCM(object): # TODO: sanity check arguments sourcedir = '%s/%s' % (scmdir, self.module) - update_checkout_cmd = None + update_checkout_cmds = None update_checkout_dir = None env = None @@ -575,10 +575,18 @@ class SCM(object): checkout_path = os.path.basename(self.repository[:-4]) commonrepo = os.path.dirname(gitrepo[:-4]) + '/common.git' + # git-reset happily accepted origin/x spec, fetch has it split + if self.revision.startswith('origin/'): + rev = self.revision[7:] + else: + rev = self.revision sourcedir = '%s/%s' % (scmdir, checkout_path) module_checkout_cmd = ['git', 'clone', '-n', gitrepo, sourcedir] common_checkout_cmd = ['git', 'clone', commonrepo, 'common'] - update_checkout_cmd = ['git', 'reset', '--hard', self.revision] + update_checkout_cmds = [ + ['git', 'fetch', 'origin', '%s:KOJI_FETCH_HEAD' % rev], + ['git', 'reset', '--hard', 'KOJI_FETCH_HEAD'] + ] update_checkout_dir = sourcedir # self.module may be empty, in which case the specfile should be in the top-level @@ -605,10 +613,18 @@ class SCM(object): checkout_path = os.path.basename(self.repository[:-4]) commonrepo = os.path.dirname(gitrepo[:-4]) + '/common.git' + # git-reset happily accepted origin/x spec, fetch has it split + if self.revision.startswith('origin/'): + rev = self.revision[7:] + else: + rev = self.revision sourcedir = '%s/%s' % (scmdir, checkout_path) module_checkout_cmd = ['git', 'clone', '-n', gitrepo, sourcedir] common_checkout_cmd = ['git', 'clone', commonrepo, 'common'] - update_checkout_cmd = ['git', 'reset', '--hard', self.revision] + update_checkout_cmds = [ + ['git', 'fetch', 'origin', '%s:KOJI_FETCH_HEAD' % rev], + ['git', 'reset', '--hard', 'KOJI_FETCH_HEAD'] + ] update_checkout_dir = sourcedir # self.module may be empty, in which case the specfile should be in the top-level @@ -643,7 +659,7 @@ class SCM(object): # perform checkouts _run(module_checkout_cmd, chdir=scmdir, fatal=True) - if update_checkout_cmd: + if update_checkout_cmds: # Currently only required for GIT checkouts # Run the command in the directory the source was checked out into if self.scmtype.startswith('GIT') and globals().get('KOJIKAMID'): @@ -651,7 +667,8 @@ class SCM(object): chdir=update_checkout_dir, fatal=True) _run(['git', 'config', 'core.safecrlf', 'true'], chdir=update_checkout_dir, fatal=True) - _run(update_checkout_cmd, chdir=update_checkout_dir, fatal=True) + for cmd in update_checkout_cmds: + _run(cmd, chdir=update_checkout_dir, fatal=True) if self.use_common and not globals().get('KOJIKAMID'): _run(common_checkout_cmd, chdir=scmdir, fatal=True) diff --git a/tests/test_scm.py b/tests/test_scm.py index a75f259..3e95ace 100644 --- a/tests/test_scm.py +++ b/tests/test_scm.py @@ -466,10 +466,13 @@ class TestSCMCheckouts(unittest.TestCase): cmd = ['git', 'clone', '-n', 'git://nocommon/koji.git', self.tempdir + '/koji'] call1 = mock.call(self.session, cmd[0], cmd, self.logfile, self.uploadpath, cwd=self.tempdir, logerror=1, append=False, env=None) - cmd = ['git', 'reset', '--hard', 'asdasd'] + cmd = ['git', 'fetch', 'origin', 'asdasd:KOJI_FETCH_HEAD'] call2 = mock.call(self.session, cmd[0], cmd, self.logfile, self.uploadpath, cwd=self.tempdir + '/koji', logerror=1, append=True, env=None) - self.log_output.assert_has_calls([call1, call2]) + cmd = ['git', 'reset', '--hard', 'KOJI_FETCH_HEAD'] + call3 = mock.call(self.session, cmd[0], cmd, self.logfile, self.uploadpath, + cwd=self.tempdir + '/koji', logerror=1, append=True, env=None) + self.log_output.assert_has_calls([call1, call2, call3]) def test_checkout_gitssh_nocommon(self): @@ -484,10 +487,13 @@ class TestSCMCheckouts(unittest.TestCase): cmd = ['git', 'clone', '-n', 'git+ssh://user@nocommon/koji.git', self.tempdir + '/koji'] call1 = mock.call(self.session, cmd[0], cmd, self.logfile, self.uploadpath, cwd=self.tempdir, logerror=1, append=False, env=None) - cmd = ['git', 'reset', '--hard', 'asdasd'] + cmd = ['git', 'fetch', 'origin', 'asdasd:KOJI_FETCH_HEAD'] call2 = mock.call(self.session, cmd[0], cmd, self.logfile, self.uploadpath, cwd=self.tempdir + '/koji', logerror=1, append=True, env=None) - self.log_output.assert_has_calls([call1, call2]) + cmd = ['git', 'reset', '--hard', 'KOJI_FETCH_HEAD'] + call3 = mock.call(self.session, cmd[0], cmd, self.logfile, self.uploadpath, + cwd=self.tempdir + '/koji', logerror=1, append=True, env=None) + self.log_output.assert_has_calls([call1, call2, call3]) def test_checkout_git_common(self): @@ -502,13 +508,16 @@ class TestSCMCheckouts(unittest.TestCase): cmd = ['git', 'clone', '-n', 'git://default/koji.git', self.tempdir + '/koji'] call1 = mock.call(self.session, cmd[0], cmd, self.logfile, self.uploadpath, cwd=self.tempdir, logerror=1, append=False, env=None) - cmd = ['git', 'reset', '--hard', 'asdasd'] + cmd = ['git', 'fetch', 'origin', 'asdasd:KOJI_FETCH_HEAD'] call2 = mock.call(self.session, cmd[0], cmd, self.logfile, self.uploadpath, cwd=self.tempdir + '/koji', logerror=1, append=True, env=None) - cmd = ['git', 'clone', 'git://default/common.git', 'common'] + cmd = ['git', 'reset', '--hard', 'KOJI_FETCH_HEAD'] call3 = mock.call(self.session, cmd[0], cmd, self.logfile, self.uploadpath, + cwd=self.tempdir + '/koji', logerror=1, append=True, env=None) + cmd = ['git', 'clone', 'git://default/common.git', 'common'] + call4 = mock.call(self.session, cmd[0], cmd, self.logfile, self.uploadpath, cwd=self.tempdir, logerror=1, append=True, env=None) - self.log_output.assert_has_calls([call1, call2, call3]) + self.log_output.assert_has_calls([call1, call2, call3, call4]) def test_checkout_error_in_command(self):