Skip to content

Commit 13fb22a

Browse files
bogdanpetreavtemian
authored andcommitted
Fixed writers number never reaching 0 in some cases.
1 parent 76e6906 commit 13fb22a

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

gitfs/views/current.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ def write(self, path, buf, offset, fh):
107107
result = super(CurrentView, self).write(path, buf, offset, fh)
108108
self.dirty[fh] = {
109109
'message': 'Update {}'.format(path),
110+
'stage': True
110111
}
111112

112113
log.debug("CurrentView: Wrote %s to %s", len(buf), path)
@@ -129,6 +130,7 @@ def mkdir(self, path, mode):
129130

130131
self.dirty[fh] = {
131132
'message': "Create the {} directory".format(path),
133+
'stage': True
132134
}
133135

134136
self.release(keep_path, fh)
@@ -143,6 +145,7 @@ def create(self, path, mode, fi=None):
143145

144146
self.dirty[fh] = {
145147
'message': "Created {}".format(path),
148+
'stage': True
146149
}
147150

148151
log.debug("CurrentView: Created %s", path)
@@ -191,6 +194,10 @@ def open_for_write(self, path, flags):
191194
global writers
192195
fh = self.open_for_read(path, flags)
193196
writers += 1
197+
self.dirty[fh] = {
198+
'message': "Opened {} for write".format(path),
199+
'stage': False
200+
}
194201

195202
log.debug("CurrentView: Open %s for write", path)
196203
return fh
@@ -215,11 +222,14 @@ def release(self, path, fh):
215222

216223
if fh in self.dirty:
217224
message = self.dirty[fh]['message']
225+
should_stage = self.dirty[fh].get('stage', False)
218226
del self.dirty[fh]
227+
219228
global writers
220229
writers -= 1
221-
log.debug("CurrentView: Staged %s for commit", path)
222-
self._stage(add=path, message=message)
230+
if should_stage:
231+
log.debug("CurrentView: Staged %s for commit", path)
232+
self._stage(add=path, message=message)
223233

224234
log.debug("CurrentView: Release %s", path)
225235
return os.close(fh)

tests/views/test_current.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ def test_write(self):
177177
assert current.dirty == {
178178
1: {
179179
'message': 'Update /path',
180+
'stage': True
180181
}
181182
}
182183
current_view.PassthroughView.write = old_write
@@ -224,7 +225,8 @@ def test_mkdir(self):
224225
)
225226
assert current.dirty == {
226227
10: {
227-
'message': "Create the /path directory"
228+
'message': "Create the /path directory",
229+
'stage': True
228230
}
229231
}
230232
mocked_release.assert_called_once_with(keep_path, 10)
@@ -419,7 +421,7 @@ def test_open(self):
419421
assert current.open("path/", os.O_WRONLY) == 1
420422
mocked_os.open.assert_called_once_with("full_path", os.O_WRONLY)
421423

422-
def test_release(self):
424+
def test_release_with_stage(self):
423425
message = "I need to stage this"
424426
mocked_os = MagicMock()
425427
mocked_stage = MagicMock()
@@ -433,11 +435,36 @@ def test_release(self):
433435
current._stage = mocked_stage
434436
current.dirty = {
435437
4: {
436-
'message': message
438+
'message': message,
439+
'stage': True
437440
}
438441
}
439442

440443
assert current.release("/path", 4) == 0
441444

442445
mocked_os.close.assert_called_once_with(4)
443446
mocked_stage.assert_called_once_with(add="/path", message=message)
447+
448+
def test_release_without_stage(self):
449+
message = "No need to stage this"
450+
mocked_os = MagicMock()
451+
mocked_stage = MagicMock()
452+
453+
mocked_os.close.return_value = 0
454+
455+
with patch.multiple('gitfs.views.current', os=mocked_os):
456+
current = CurrentView(repo="repo",
457+
repo_path="repo_path",
458+
ignore=CachedIgnore())
459+
current._stage = mocked_stage
460+
current.dirty = {
461+
4: {
462+
'message': message,
463+
'stage': False
464+
}
465+
}
466+
467+
assert current.release("/path", 4) == 0
468+
469+
mocked_os.close.assert_called_once_with(4)
470+
assert mocked_stage.call_count == 0

0 commit comments

Comments
 (0)