Skip to content

Commit 1454a31

Browse files
authored
Merge pull request #264 from jdmansour/fix-reset-regression
Fix regression: can't reset some files anymore
2 parents ccf4899 + 16dcbdb commit 1454a31

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

nbgitpuller/pull.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,11 @@ def reset_deleted_files(self):
170170
'git', 'ls-files', '--deleted', '-z'
171171
], cwd=self.repo_dir).decode().strip().split('\0')
172172

173+
upstream_deleted = self.find_upstream_changed('D')
173174
for filename in deleted_files:
174-
if filename: # Filter out empty lines
175-
yield from execute_cmd(['git', 'checkout', '--', filename], cwd=self.repo_dir)
175+
# Filter out empty lines, and files that were deleted in the remote
176+
if filename and filename not in upstream_deleted:
177+
yield from execute_cmd(['git', 'checkout', 'origin/{}'.format(self.branch_name), '--', filename], cwd=self.repo_dir)
176178

177179
def repo_is_dirty(self):
178180
"""
@@ -196,13 +198,13 @@ def find_upstream_changed(self, kind):
196198
Return list of files that have been changed upstream belonging to a particular kind of change
197199
"""
198200
output = subprocess.check_output([
199-
'git', 'log', '..origin/{}'.format(self.branch_name),
200-
'--oneline', '--name-status'
201+
'git', 'diff', '..origin/{}'.format(self.branch_name),
202+
'--name-status'
201203
], cwd=self.repo_dir).decode()
202204
files = []
203205
for line in output.split('\n'):
204206
if line.startswith(kind):
205-
files.append(os.path.join(self.repo_dir, line.split('\t', 1)[1]))
207+
files.append(line.split('\t', 1)[1])
206208

207209
return files
208210

@@ -237,6 +239,7 @@ def rename_local_untracked(self):
237239
# Find what files have been added!
238240
new_upstream_files = self.find_upstream_changed('A')
239241
for f in new_upstream_files:
242+
f = os.path.join(self.repo_dir, f)
240243
if os.path.exists(f):
241244
# If there's a file extension, put the timestamp before that
242245
ts = datetime.datetime.now().strftime('__%Y%m%d%H%M%S')

tests/test_gitpuller.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,34 @@ def test_reset_file():
351351
assert puller.read_file('README.md') == pusher.read_file('README.md') == '1'
352352
assert puller.read_file('unicode🙂.txt') == pusher.read_file('unicode🙂.txt') == '2'
353353

354+
355+
def test_reset_file_after_changes():
356+
"""
357+
Test that we get the latest version of a file if we:
358+
- change the file locally
359+
- sync, so the change is preserved
360+
- delete the file, in order to reset it
361+
- sync again
362+
"""
363+
with Remote() as remote, Pusher(remote) as pusher:
364+
pusher.push_file('README.md', 'original')
365+
366+
with Puller(remote) as puller:
367+
puller.write_file('README.md', 'local change')
368+
pusher.push_file('README.md', 'remote change')
369+
puller.pull_all()
370+
371+
# It should keep the local change
372+
assert puller.read_file('README.md') == 'local change'
373+
374+
# Delete the local file manually and pull
375+
os.remove(os.path.join(puller.path, 'README.md'))
376+
puller.pull_all()
377+
378+
# It should restore the remote change
379+
assert puller.read_file('README.md') == 'remote change'
380+
381+
354382
def test_delete_conflicted_file():
355383
"""
356384
Test that after deleting a file that had a conflict, we can still pull
@@ -374,6 +402,25 @@ def test_delete_conflicted_file():
374402
puller.pull_all()
375403

376404

405+
def test_delete_remotely_modify_locally():
406+
"""
407+
Test that we can delete a file upstream, and edit it at the same time locally
408+
"""
409+
with Remote() as remote, Pusher(remote) as pusher:
410+
pusher.push_file('README.md', 'new')
411+
412+
with Puller(remote) as puller:
413+
# Delete the file remotely
414+
pusher.git('rm', 'README.md')
415+
pusher.git('commit', '-m', 'Deleted file')
416+
417+
# Edit locally
418+
pusher.push_file('README.md', 'HELLO')
419+
puller.pull_all()
420+
421+
assert puller.read_file('README.md') == 'HELLO'
422+
423+
377424
def test_delete_locally_and_remotely():
378425
"""
379426
Test that sync works after deleting a file locally and remotely

0 commit comments

Comments
 (0)