no_upstream_branch_msg)
from gbp.errors import GbpError
import gbp.log
+from gbp.scripts.pq_rpm import safe_patches, rm_patch_files, get_packager
+from gbp.scripts.common.pq import apply_and_commit_patch
from gbp.pkg import parse_archive_filename
no_packaging_branch_msg = """
You need to reate it or use --packaging-branch to specify it.
"""
+PATCH_AUTODELETE_COMMIT_MSG = """
+Autoremove imported patches from packaging
+
+Removed all imported patches from %s
+and patch files from the packaging dir.
+"""
+
class SkipImport(Exception):
"""Nothing imported"""
pass
+class PatchImportError(Exception):
+ """Patch import failed"""
+ pass
+
def download_file(target_dir, url):
"""Download a remote file"""
if options.pristine_tar:
gbp.log.info("Bare repository: setting %s option '--no-pristine-tar'")
options.pristine_tar = False
+ if options.patch_import:
+ gbp.log.info("Bare repository: setting %s option '--no-patch-import')")
+ options.patch_import = False
+
+
+def import_spec_patches(repo, spec):
+ """
+ Import patches from a spec file to the current branch
+ """
+ queue = spec.patchseries()
+ if len(queue) == 0:
+ return
+
+ gbp.log.info("Importing patches to '%s' branch" % repo.get_branch())
+ orig_head = repo.rev_parse("HEAD")
+ packager = get_packager(spec)
+
+ # Put patches in a safe place
+ queue = safe_patches(queue)
+ for patch in queue:
+ gbp.log.debug("Applying %s" % patch.path)
+ try:
+ apply_and_commit_patch(repo, patch, packager)
+ except (GbpError, GitRepositoryError):
+ repo.force_head(orig_head, hard=True)
+ raise PatchImportError("Patch(es) didn't apply, you need apply "
+ "and commit manually")
+
+ # Remove patches from spec and packaging directory
+ gbp.log.info("Removing imported patch files from spec and packaging dir")
+ rm_patch_files(spec)
+ try:
+ spec.update_patches([], {})
+ spec.write_spec_file()
+ except GbpError:
+ repo.force_head('HEAD', hard=True)
+ raise PatchImportError("Unable to update spec file, you need to edit"
+ "and commit it manually")
+ repo.commit_all(msg=PATCH_AUTODELETE_COMMIT_MSG % spec.specfile)
def force_to_branch_head(repo, branch):
dest="author_is_committer")
import_group.add_config_file_option(option_name="packaging-dir",
dest="packaging_dir")
+ import_group.add_boolean_config_file_option(option_name="patch-import",
+ dest="patch_import")
return parser
def parse_args(argv):
# Import patches on top of the source tree
# (only for non-native packages with non-orphan packaging)
force_to_branch_head(repo, options.packaging_branch)
+ if options.patch_import:
+ spec = SpecFile(os.path.join(repo.path,
+ options.packaging_dir, spec.specfile))
+ import_spec_patches(repo, spec)
+ commit = options.packaging_branch
# Create packaging tag
if not options.skip_packaging_tag:
except NoSpecError as err:
gbp.log.err("Failed determine spec file: %s" % err)
ret = 1
+ except PatchImportError as err:
+ gbp.log.err(err)
+ ret = 2
except SkipImport:
skipped = True
finally:
# Check repository state
repo = GitRepository('gbp-test')
files = {'Makefile', 'README', 'bar.tar.gz', 'dummy.sh', 'foo.txt',
- 'gbp-test.spec', 'my.patch', 'my2.patch', 'my3.patch'}
+ 'gbp-test.spec', 'my.patch', 'mydir/myfile.txt'}
self._check_repo_state(repo, 'master', ['master', 'upstream'], files)
- # Two commits: upstream and packaging files
- eq_(len(repo.get_commits()), 2)
+ # Four commits: upstream, packaging files, one patch and the removal
+ # of imported patches
+ eq_(len(repo.get_commits()), 4)
def test_basic_import2(self):
"""Import package with multiple spec files and full url patch"""
repo = GitRepository('gbp-test2')
files = {'Makefile', 'README', 'bar.tar.gz', 'dummy.sh', 'foo.txt',
'gbp-test2.spec', 'gbp-test2-alt.spec', 'my.patch',
- 'my2.patch', 'my3.patch'}
+ 'mydir/myfile.txt'}
self._check_repo_state(repo, 'master', ['master', 'upstream'], files)
- # Two commits: upstream and packaging files
- eq_(len(repo.get_commits()), 2)
+ # Four commits: upstream, packaging files, one patch and the removal
+ # of imported patches
+ eq_(len(repo.get_commits()), 4)
def test_basic_import_orphan(self):
"""
# Only one commit: packaging files
eq_(len(repo.get_commits()), 1)
+ def test_import_compressed_patches(self):
+ """Test importing of non-native src.rpm with compressed patches"""
+ srpm = os.path.join(DATA_DIR, 'gbp-test-1.1-2.src.rpm')
+ eq_(import_srpm(['arg0', srpm]), 0)
+ # Check repository state
+ repo = GitRepository('gbp-test')
+ files = set(['Makefile', 'README', 'AUTHORS', 'NEWS', 'bar.tar.gz',
+ 'dummy.sh', 'foo.txt', 'gbp-test.spec', 'my.patch',
+ 'mydir/myfile.txt'])
+ self._check_repo_state(repo, 'master', ['master', 'upstream'], files)
+ # Four commits: upstream, packaging files, three patches and the removal
+ # of imported patches
+ eq_(len(repo.get_commits()), 6)
+
def test_multiple_versions(self):
"""Test importing of multiple versions"""
srpms = [ os.path.join(DATA_DIR, 'gbp-test-1.0-1.src.rpm'),
eq_(mock_import(['--no-pristine-tar', srpms[0]]), 0)
repo = GitRepository('gbp-test')
self._check_repo_state(repo, 'master', ['master', 'upstream'])
- eq_(len(repo.get_commits()), 2)
+ eq_(len(repo.get_commits()), 4)
# Try to import same version again
eq_(mock_import([srpms[1]]), 0)
- eq_(len(repo.get_commits()), 2)
+ eq_(len(repo.get_commits()), 4)
eq_(len(repo.get_commits(until='upstream')), 1)
- eq_(mock_import(['--no-pristine-tar', '--allow-same-version', srpms[1]]), 0)
- # Added new version of packaging
- eq_(len(repo.get_commits()), 3)
+ eq_(mock_import(['--no-pristine-tar', '--allow-same-version',
+ srpms[1]]), 0)
+ # Added new versio packaging plus one patch
+ eq_(len(repo.get_commits()), 7)
eq_(len(repo.get_commits(until='upstream')), 1)
# Import new version
eq_(mock_import(['--no-pristine-tar', srpms[2]]), 0)
files = {'Makefile', 'README', 'bar.tar.gz', 'dummy.sh', 'foo.txt',
- 'gbp-test.spec', 'my.patch', 'my2.patch', 'my3.patch'}
+ 'gbp-test.spec', 'my.patch', 'mydir/myfile.txt'}
self._check_repo_state(repo, 'master', ['master', 'upstream'], files)
- eq_(len(repo.get_commits()), 5)
+ eq_(len(repo.get_commits()), 11)
eq_(len(repo.get_commits(until='upstream')), 2)
# Check number of tags
eq_(len(repo.get_tags('upstream/*')), 2)
self._check_log(-1, 'Also check the --create-missing-branches')
eq_(mock_import(['--no-pristine-tar', '--create-missing', srpm]), 0)
self._check_repo_state(repo, 'master', ['master', 'upstream'])
- # Four commits: our initial, upstream and packaging files
- eq_(len(repo.get_commits()), 3)
+ # Four commits: our initial, upstream, packaging files, one patch,
+ # and the removal of imported patches
+ eq_(len(repo.get_commits()), 5)
# The import should fail because missing packaging-branch
srpm = os.path.join(DATA_DIR, 'gbp-test-1.1-1.src.rpm')
# Check repository state
repo = GitRepository('gbp-test')
files = set(['Makefile', 'dummy.sh', 'bar.tar.gz', 'foo.txt',
- 'gbp-test.spec', 'my.patch', 'my2.patch', 'my3.patch'])
+ 'gbp-test.spec', 'my.patch', 'mydir/myfile.txt'])
self._check_repo_state(repo, 'master', ['master', 'upstream'], files)
def test_tagging(self):
srpm = os.path.join(DATA_DIR, 'gbp-test2-2.0-0.src.rpm')
eq_(mock_import(['--no-pristine-tar',
+ '--no-patch-import',
'--packaging-branch=pack',
'--upstream-branch=orig',
'--packaging-dir=packaging',
repo = GitRepository('gbp-test')
self._check_repo_state(repo, 'master', ['master', 'upstream',
'pristine-tar'])
- # Two commits: upstream and packaging files
- eq_(len(repo.get_commits()), 2)
+ # Four commits: upstream, packaging files, one patch and the removal
+ # of imported patches
+ eq_(len(repo.get_commits()), 4)
def test_unsupported_archive(self):
"""Test importing of src.rpm with a zip source archive"""