Skip to content

Commit

Permalink
Add multivar support to hook installer/uninstaller (#7689)
Browse files Browse the repository at this point in the history
# About the pull request

This PR is sort of a follow up to #7595 where it was revealed several
people have weird configs preventing the hook install from working due
to having multiple entries for the same merge driver. Now all `driver`
values for `dmm` and `dmi` are nuked before install and the uninstaller
also specifies if no change was made.

# Explain why it's good for the game

Fixes situations like 

![image](https://github.com/user-attachments/assets/ee9eb0a2-e8bd-4e4f-8faa-c2fa83ca3e24)

# Changelog
:cl: Drathek
code: Merge hook installer now can handle duplicate/old hook installs
/:cl:
  • Loading branch information
Drulikar authored Nov 28, 2024
1 parent a842ed1 commit f1c0304
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions tools/hooks/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def _find_stuff(target=None):

def uninstall(target=None, keep=()):
repo, hooks_dir = _find_stuff(target)
tools_hooks = os.path.split(__file__)[0]

# Remove hooks
for fname in glob.glob(os.path.join(hooks_dir, '*')):
Expand All @@ -48,12 +49,19 @@ def uninstall(target=None, keep=()):
os.unlink(fname)

# Remove merge driver configuration
for entry in repo.config:
match = re.match(r'^merge\.([^.]+)\.driver$', entry.name)
if match and f"{match.group(1)}.merge" not in keep:
print('Removing merge driver:', match.group(1))
del repo.config[entry.name]

for full_path in glob.glob(os.path.join(tools_hooks, '*.merge')):
# Merge drivers are documented here: https://git-scm.com/docs/gitattributes
_, fname = os.path.split(full_path)
name, _ = os.path.splitext(fname)
driver_name = f"merge.{name}.driver"
if fname in keep:
continue
try:
repo.config.delete_multivar(driver_name, r".*")
print('Removed merge driver:', name)
except:
print(f'No {name} merge driver to remove.')
pass

def install(target=None):
repo, hooks_dir = _find_stuff(target)
Expand All @@ -73,6 +81,7 @@ def install(target=None):
# Merge drivers are documented here: https://git-scm.com/docs/gitattributes
_, fname = os.path.split(full_path)
name, _ = os.path.splitext(fname)
driver_name = f"merge.{name}.driver"
print('Installing merge driver:', name)
keep.add(fname)
# %P: "real" path of the file, should not usually be read or modified
Expand All @@ -81,7 +90,11 @@ def install(target=None):
# %B: other branches' version
# %L: conflict marker size
relative_path = shlex.quote(os.path.relpath(full_path, repo.workdir).replace('\\', '/'))
repo.config[f"merge.{name}.driver"] = f'{relative_path} %P %O %A %B %L'
try:
repo.config.delete_multivar(driver_name, r".*")
except:
pass
repo.config[driver_name] = f'{relative_path} %P %O %A %B %L'

uninstall(target, keep=keep)

Expand Down

0 comments on commit f1c0304

Please sign in to comment.