Skip to content

Commit ae2b596

Browse files
committed
Made sure dry-run is properly implemented in Submodule.remove()
The root-submodule test is still failing though, this time even earlier than before
1 parent c7b16ad commit ae2b596

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

git/objects/submodule/base.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ def move(self, module_path, configuration=True, module=True):
672672
return self
673673

674674
@unbare_repo
675-
def remove(self, module=True, force=False, configuration=True, dry_run=False):
675+
def remove(self, module=True, force=False, configuration=True, dry_run=False, _is_recursive=False):
676676
"""Remove this submodule from the repository. This will remove our entry
677677
from the .gitmodules file and the entry in the .git/config file.
678678
@@ -705,7 +705,7 @@ def remove(self, module=True, force=False, configuration=True, dry_run=False):
705705

706706
# Recursively remove children of this submodule
707707
for csm in self.children():
708-
csm.remove(module, force, configuration, dry_run)
708+
csm.remove(module, force, configuration, dry_run, _is_recursive=True)
709709
del(csm)
710710
# end
711711

@@ -772,7 +772,7 @@ def remove(self, module=True, force=False, configuration=True, dry_run=False):
772772
# END delete tree if possible
773773
# END handle force
774774

775-
if os.path.isdir(git_dir):
775+
if not dry_run and os.path.isdir(git_dir):
776776
rmtree(git_dir)
777777
# end handle separate bare repository
778778
# END handle module deletion
@@ -781,22 +781,28 @@ def remove(self, module=True, force=False, configuration=True, dry_run=False):
781781
######################
782782
if configuration and not dry_run:
783783
# first the index-entry
784-
index = self.repo.index
784+
parent_index = self.repo.index
785785
try:
786-
del(index.entries[index.entry_key(self.path, 0)])
786+
del(parent_index.entries[parent_index.entry_key(self.path, 0)])
787787
except KeyError:
788788
pass
789789
# END delete entry
790-
index.write()
790+
parent_index.write()
791791

792792
# now git config - need the config intact, otherwise we can't query
793-
# inforamtion anymore
793+
# information anymore
794794
writer = self.repo.config_writer()
795795
writer.remove_section(sm_section(self.name))
796796
writer.release()
797797
writer = self.config_writer()
798798
writer.remove_section()
799799
writer.release()
800+
801+
# Assure we don't leave the parent repository in a dirty state, and commit our changes
802+
# It's important for recursive, unforced, deletions to work as expected
803+
if _is_recursive:
804+
self.module().index.commit("Removed submodule '%s'" % self.name)
805+
# end
800806
# END delete configuration
801807

802808
# void our data not to delay invalid access

git/test/test_submodule.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -666,9 +666,12 @@ def test_git_submodule_compatibility(self, rwdir):
666666

667667
# remove
668668
sm_module_path = sm.module().git_dir
669-
sm.remove()
670-
assert not sm.exists()
671-
assert not sm.module_exists()
672-
assert not os.path.isdir(sm_module_path)
669+
670+
for dry_run in (True, False):
671+
sm.remove(dry_run=dry_run)
672+
assert sm.exists() == dry_run
673+
assert sm.module_exists() == dry_run
674+
assert os.path.isdir(sm_module_path) == dry_run
675+
# end for each dry-run mode
673676

674677

0 commit comments

Comments
 (0)