◐ Shell
reader mode source ↗
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
File filter
Conversations
Jump to
Diff view
Apply and reload
Show whitespace
Diff view
Apply and reload
2 changes: 2 additions & 0 deletions git/test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# the BSD License: http://www.opensource.org/licenses/bsd-license.php

import tempfile

from git.test.lib import (
TestBase,
Expand Down Expand Up @@ -80,6 +81,7 @@ def test_lock_file(self):

# auto-release on destruction
del(other_lock_file)
lock_file._obtain_lock_or_raise()
lock_file._release_lock()

77 changes: 68 additions & 9 deletions git/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,56 @@

#{ Utility Methods


def unbare_repo(func):
"""Methods with this decorator raise InvalidGitRepositoryError if they
Expand Down Expand Up @@ -555,9 +605,10 @@ class LockFile(object):
As we are a utility class to be derived from, we only use protected methods.

Locks will automatically be released on destruction"""
__slots__ = ("_file_path", "_owns_lock")

def __init__(self, file_path):
self._file_path = file_path
self._owns_lock = False

Expand All @@ -579,20 +630,21 @@ def _obtain_lock_or_raise(self):
:raise IOError: if a lock was already present or a lock file could not be written"""
if self._has_lock():
return
lock_file = self._lock_file_path()
if os.path.isfile(lock_file):
raise IOError("Lock for file %r did already exist, delete %r in case the lock is illegal" %
(self._file_path, lock_file))

try:
flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL
if is_win:
flags |= os.O_SHORT_LIVED
fd = os.open(lock_file, flags, 0)
os.close(fd)
except OSError as e:
raise IOError(str(e))

self._owns_lock = True

def _obtain_lock(self):
Expand All @@ -605,14 +657,21 @@ def _release_lock(self):
if not self._has_lock():
return

# if someone removed our file beforhand, lets just flag this issue
# instead of failing, to make it more usable.
lfp = self._lock_file_path()
try:
rmfile(lfp)
except OSError:
pass
self._owns_lock = False


class BlockingLockFile(LockFile):
Expand Down Expand Up @@ -647,7 +706,7 @@ def _obtain_lock(self):
try:
super(BlockingLockFile, self)._obtain_lock()
except IOError:
# synity check: if the directory leading to the lockfile is not
# readable anymore, raise an execption
curtime = time.time()
if not os.path.isdir(os.path.dirname(self._lock_file_path())):
Expand Down
Toggle all file notes Toggle all file annotations