project: fallback to hardlinks with git hooks

Windows requires Administrator access to create symlinks.  We can
mitigate this a bit by falling back to hardlinks as those may be
created by any user on the system.  Do this with the git hooks as
these are supposed to be internal only and people shouldn't be
modifying them.  If they do, they'll have to delink first.  This
seems worth it to allow repo usage without extra privileges.

Change-Id: I996ea9c9238f7bd7d27d1d9b1f2786593bf75ef7
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/256312
Reviewed-by: David Pursehouse <dpursehouse@collab.net>
Tested-by: Mike Frysinger <vapier@google.com>
This commit is contained in:
Mike Frysinger 2020-02-21 00:04:15 -05:00
parent 8c268c0e7b
commit 7951e14385

View File

@ -2860,19 +2860,23 @@ class Project(object):
if platform_utils.islink(dst): if platform_utils.islink(dst):
continue continue
if os.path.exists(dst): if os.path.exists(dst):
if filecmp.cmp(stock_hook, dst, shallow=False): # If the files are the same, we'll leave it alone. We create symlinks
platform_utils.remove(dst) # below by default but fallback to hardlinks if the OS blocks them.
else: # So if we're here, it's probably because we made a hardlink below.
if not filecmp.cmp(stock_hook, dst, shallow=False):
if not quiet: if not quiet:
_warn("%s: Not replacing locally modified %s hook", _warn("%s: Not replacing locally modified %s hook",
self.relpath, name) self.relpath, name)
continue continue
try: try:
platform_utils.symlink( platform_utils.symlink(
os.path.relpath(stock_hook, os.path.dirname(dst)), dst) os.path.relpath(stock_hook, os.path.dirname(dst)), dst)
except OSError as e: except OSError as e:
if e.errno == errno.EPERM: if e.errno == errno.EPERM:
raise GitError(self._get_symlink_error_message()) try:
os.link(stock_hook, dst)
except OSError:
raise GitError(self._get_symlink_error_message())
else: else:
raise raise