diff options
author | Jelmer Vernooij <jelmer@samba.org> | 2010-09-12 00:34:22 +0200 |
---|---|---|
committer | Jelmer Vernooij <jelmer@samba.org> | 2010-09-12 00:34:22 +0200 |
commit | 56203a5ee7cfae77b74af1e5e51f4cb37e2a0dc5 (patch) | |
tree | 33a6865bcee2bf20a1d1483aa3d05e82f515db95 | |
parent | cd6fd7746de85f146226b4cf98920f2a4a5529c3 (diff) | |
download | python-fastimport-56203a5ee7cfae77b74af1e5e51f4cb37e2a0dc5.tar.gz |
Use modes directly rather than bzr file kinds and executable flags.
-rw-r--r-- | fastimport/commands.py | 36 | ||||
-rw-r--r-- | fastimport/parser.py | 18 | ||||
-rw-r--r-- | fastimport/processors/filter_processor.py | 3 | ||||
-rw-r--r-- | fastimport/tests/test_commands.py | 16 | ||||
-rw-r--r-- | fastimport/tests/test_parser.py | 9 |
5 files changed, 41 insertions, 41 deletions
diff --git a/fastimport/commands.py b/fastimport/commands.py index ad72de5..5c68a76 100644 --- a/fastimport/commands.py +++ b/fastimport/commands.py @@ -16,6 +16,7 @@ """Import command classes.""" +import stat # There is a bug in git 1.5.4.3 and older by which unquoting a string consumes # one extra character. Set this variable to True to work-around it. It only @@ -290,12 +291,11 @@ class FileCommand(ImportCommand): class FileModifyCommand(FileCommand): - def __init__(self, path, kind, is_executable, dataref, data): + def __init__(self, path, mode, dataref, data): # Either dataref or data should be null FileCommand.__init__(self, 'filemodify') self.path = check_path(path) - self.kind = kind - self.is_executable = is_executable + self.mode = mode self.dataref = dataref self.data = data self._binary = ['data'] @@ -306,21 +306,23 @@ class FileModifyCommand(FileCommand): def __str__(self): return self.to_string(include_file_contents=False) - def to_string(self, include_file_contents=False): - if self.is_executable: - mode = "755" - elif self.kind == 'file': - mode = "644" - elif self.kind == 'directory': - mode = "040000" - elif self.kind == 'symlink': - mode = "120000" - elif self.kind == 'tree-reference': - mode = "160000" + def _format_mode(self, mode): + if mode in (0755, 0100755): + return "755" + elif mode in (0644, 0100644): + return "644" + elif mode == 040000: + return "040000" + elif mode == 0120000: + return "120000" + elif mode == 0160000: + return "160000" else: - raise AssertionError("unknown kind %s" % (self.kind,)) + raise AssertionError("Unknown mode %o" % mode) + + def to_string(self, include_file_contents=False): datastr = "" - if self.kind == 'directory': + if stat.S_ISDIR(self.mode): dataref = '-' elif self.dataref is None: dataref = "inline" @@ -329,7 +331,7 @@ class FileModifyCommand(FileCommand): else: dataref = "%s" % (self.dataref,) path = format_path(self.path) - return "M %s %s %s%s" % (mode, dataref, path, datastr) + return "M %s %s %s%s" % (self._format_mode(self.mode), dataref, path, datastr) class FileDeleteCommand(FileCommand): diff --git a/fastimport/parser.py b/fastimport/parser.py index a54d42a..9d36dcb 100644 --- a/fastimport/parser.py +++ b/fastimport/parser.py @@ -404,14 +404,14 @@ class ImportParser(LineBasedParser): """ params = info.split(' ', 2) path = self._path(params[2]) - is_executable, kind = self._mode(params[0]) + mode = self._mode(params[0]) if params[1] == 'inline': dataref = None data = self._get_data('filemodify') else: dataref = params[1] data = None - return commands.FileModifyCommand(path, kind, is_executable, dataref, + return commands.FileModifyCommand(path, mode, dataref, data) def _parse_reset(self, ref): @@ -603,21 +603,21 @@ class ImportParser(LineBasedParser): return map(_unquote_c_string, parts) def _mode(self, s): - """Parse a file mode into executable and kind. + """Check file mode format and parse into an int. - :return (is_executable, kind) + :return: mode as integer """ # Note: Output from git-fast-export slightly different to spec if s in ['644', '100644', '0100644']: - return False, 'file' + return 0100644 elif s in ['755', '100755', '0100755']: - return True, 'file' + return 0100755 elif s in ['040000', '0040000']: - return False, 'directory' + return 040000 elif s in ['120000', '0120000']: - return False, 'symlink' + return 0120000 elif s in ['160000', '0160000']: - return False, 'tree-reference' + return 0160000 else: self.abort(errors.BadFormat, 'filemodify', 'mode', s) diff --git a/fastimport/processors/filter_processor.py b/fastimport/processors/filter_processor.py index 0c8506e..c8336f9 100644 --- a/fastimport/processors/filter_processor.py +++ b/fastimport/processors/filter_processor.py @@ -26,6 +26,7 @@ from fastimport import ( helpers, processor, ) +import stat class FilterProcessor(processor.ImportProcessor): @@ -109,7 +110,7 @@ class FilterProcessor(processor.ImportProcessor): for fc in interesting_filecmds: if isinstance(fc, commands.FileModifyCommand): if (fc.dataref is not None and - fc.kind != 'directory'): + not stat.S_ISDIR(fc.mode)): self.referenced_blobs.append(fc.dataref) # Update from and merges to refer to commits in the output diff --git a/fastimport/tests/test_commands.py b/fastimport/tests/test_commands.py index ab44856..c887235 100644 --- a/fastimport/tests/test_commands.py +++ b/fastimport/tests/test_commands.py @@ -134,7 +134,7 @@ class TestCommitDisplay(TestCase): def test_commit_with_filecommands(self): file_cmds = iter([ commands.FileDeleteCommand('readme.txt'), - commands.FileModifyCommand('NEWS', 'file', False, None, + commands.FileModifyCommand('NEWS', 0100644, None, 'blah blah blah'), ]) # user tuple is (name, email, secs-since-epoch, secs-offset-from-utc) @@ -256,24 +256,24 @@ class TestTagDisplay(TestCase): class TestFileModifyDisplay(TestCase): def test_filemodify_file(self): - c = commands.FileModifyCommand("foo/bar", "file", False, ":23", None) + c = commands.FileModifyCommand("foo/bar", 0100644, ":23", None) self.assertEqual("M 644 :23 foo/bar", repr(c)) def test_filemodify_file_executable(self): - c = commands.FileModifyCommand("foo/bar", "file", True, ":23", None) + c = commands.FileModifyCommand("foo/bar", 0100755, ":23", None) self.assertEqual("M 755 :23 foo/bar", repr(c)) def test_filemodify_file_internal(self): - c = commands.FileModifyCommand("foo/bar", "file", False, None, + c = commands.FileModifyCommand("foo/bar", 0100644, None, "hello world") self.assertEqual("M 644 inline foo/bar\ndata 11\nhello world", repr(c)) def test_filemodify_symlink(self): - c = commands.FileModifyCommand("foo/bar", "symlink", False, None, "baz") + c = commands.FileModifyCommand("foo/bar", 0120000, None, "baz") self.assertEqual("M 120000 inline foo/bar\ndata 3\nbaz", repr(c)) def test_filemodify_treeref(self): - c = commands.FileModifyCommand("tree-info", "tree-reference", False, + c = commands.FileModifyCommand("tree-info", 0160000, "revision-id-info", None) self.assertEqual("M 160000 revision-id-info tree-info", repr(c)) @@ -320,9 +320,9 @@ class TestPathChecking(TestCase): def test_filemodify_path_checking(self): self.assertRaises(ValueError, commands.FileModifyCommand, "", - "file", False, None, "text") + 0100644, None, "text") self.assertRaises(ValueError, commands.FileModifyCommand, None, - "file", False, None, "text") + 0100644, None, "text") def test_filedelete_path_checking(self): self.assertRaises(ValueError, commands.FileDeleteCommand, "") diff --git a/fastimport/tests/test_parser.py b/fastimport/tests/test_parser.py index 267ec13..6c0e009 100644 --- a/fastimport/tests/test_parser.py +++ b/fastimport/tests/test_parser.py @@ -187,8 +187,7 @@ class TestImportParser(testtools.TestCase): file_cmd1 = result.pop(0) self.assertEqual('filemodify', file_cmd1.name) self.assertEqual('README', file_cmd1.path) - self.assertEqual('file', file_cmd1.kind) - self.assertEqual(False, file_cmd1.is_executable) + self.assertEqual(0100644, file_cmd1.mode) self.assertEqual('Welcome from bugs\n', file_cmd1.data) cmd5 = result.pop(0) self.assertEqual('commit', cmd5.name) @@ -206,8 +205,7 @@ class TestImportParser(testtools.TestCase): file_cmd2 = result.pop(0) self.assertEqual('filemodify', file_cmd2.name) self.assertEqual('README', file_cmd2.path) - self.assertEqual('file', file_cmd2.kind) - self.assertEqual(False, file_cmd2.is_executable) + self.assertEqual(0100644, file_cmd2.mode) self.assertEqual('Welcome from bugs, etc.', file_cmd2.data) cmd6 = result.pop(0) self.assertEqual(cmd6.name, 'checkpoint') @@ -229,8 +227,7 @@ class TestImportParser(testtools.TestCase): file_cmd1 = result.pop(0) self.assertEqual('filemodify', file_cmd1.name) self.assertEqual('tree-id', file_cmd1.path) - self.assertEqual('tree-reference', file_cmd1.kind) - self.assertEqual(False, file_cmd1.is_executable) + self.assertEqual(0160000, file_cmd1.mode) self.assertEqual("rev-id", file_cmd1.dataref) cmd = result.pop(0) self.assertEqual('feature', cmd.name) |