From db2ae8d63f46468c8d540e285458c960d38f6b85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Mon, 16 May 2022 16:25:54 +0200 Subject: [PATCH] Change: Update unstashing with applied changes in stash_unstaged_changes Don't apply diff if applied changes of a plugin are the same as the working tree changes. Improve the doc for what's going on here with all the git magic. --- autohooks/api/git.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/autohooks/api/git.py b/autohooks/api/git.py index 74f9e2f6..24646086 100644 --- a/autohooks/api/git.py +++ b/autohooks/api/git.py @@ -333,6 +333,7 @@ def stash_changes(self) -> None: # add changes from files to index stage_files(self.partially_staged) # save index as working tree + # unstaged changes are stored in the working tree now self.working_tree = _write_tree() # add ref to be able to restore working tree manually _set_ref(WORKING_REF, self.working_tree) @@ -367,18 +368,23 @@ def __exit__( self.restore_working_tree() _read_tree(self.index) else: - # save formatting changes - formatted_tree = _write_tree() + # save possible changes made to the index + changed_tree = _write_tree() self.restore_working_tree() # restore index - # formatted_tree will be the same as index if no changes are applied - _read_tree(formatted_tree) - - if formatted_tree != self.index: - # create diff between index and formatted_tree - patch = _get_tree_diff(self.index, formatted_tree) + _read_tree(changed_tree) + + # create and apply diff between index before running the plugin and + # changes made by the plugin if some changes have been applied and + # staged. + # changed_tree will be the same as index if no changes are applied. + # changed_tree may be the same as the working tree. in that case no + # further action is needed. + if changed_tree != self.index and changed_tree != self.working_tree: + # create diff between working tree and changed tree + patch = _get_tree_diff(self.index, changed_tree) try: # apply diff to working tree _apply_diff(patch)