From d5d8a4f0b5609328027df9379282dddfbd2bbe58 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Karl=20Hasselstr=C3=B6m?= Date: Mon, 28 Jan 2008 18:45:42 +0100 Subject: [PATCH] Teach new infrastructure to apply patches MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Two new methods: one index method that applies a patch to that index or fails without side-effects (without touching a worktree in either case); and one repository method that uses a temp index to apply a patch to a tree and returning the new tree (or None if the application failed), entirely side-effect free. Nothing uses this yet, but "stg edit" will soon. Signed-off-by: Karl Hasselström --- stgit/lib/git.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/stgit/lib/git.py b/stgit/lib/git.py index 2ca4495..45879df 100644 --- a/stgit/lib/git.py +++ b/stgit/lib/git.py @@ -339,6 +339,23 @@ class Repository(RunWithEnv): return None finally: index.delete() + def apply(self, tree, patch_text): + """Given a tree and a patch, will either return the new tree that + results when the patch is applied, or None if the patch + couldn't be applied.""" + assert isinstance(tree, Tree) + if not patch_text: + return tree + index = self.temp_index() + try: + index.read_tree(tree) + try: + index.apply(patch_text) + return index.write_tree() + except MergeException: + return None + finally: + index.delete() class MergeException(exception.StgException): pass @@ -375,6 +392,13 @@ class Index(RunWithEnv): """In-index merge, no worktree involved.""" self.run(['git', 'read-tree', '-m', '-i', '--aggressive', base.sha1, ours.sha1, theirs.sha1]).no_output() + def apply(self, patch_text): + """In-index patch application, no worktree involved.""" + try: + self.run(['git', 'apply', '--cached'] + ).raw_input(patch_text).no_output() + except run.RunException: + raise MergeException('Patch does not apply cleanly') def delete(self): if os.path.isfile(self.__filename): os.remove(self.__filename) -- 2.11.0