← Winnitude! Search  ·  Reader Mode (947 ms)
Links: Stay in Reader Retro Site Converter Retro Mode: OFF Original Page ↗
GITHUB · 40 LINKS

Use bash to open extensionless hooks on windows by idbrii · Pull Request #1399 · gitpython-developers/GitPython

Repository navigation

Use bash to open extensionless hooks on windows - #1399


Merged
Byron merged 1 commit into gitpython-developers:main from idbrii:fix-hook Jan 14, 2022
Merged

Use bash to open extensionless hooks on windows #1399 Byron merged 1 commit into gitpython-developers:main from idbrii:fix-hook

Conversation

idbrii commented Jan 13, 2022



Copy link Copy Markdown
Contributor

Fix #971 . Partly resolve #703 .

If the hook doesn't have a file extension, then Windows won't know how
to run it and you'll get "[WinError 193] %1 is not a valid Win32
application". It's very likely that it's a shell script of some kind, so
use bash.exe (commonly installed via Windows Subsystem for Linux). We
don't want to run all hooks with bash because they could be .bat files.

test_index.py passes (two are skipped).

Use bash to open extensionless hooks on windows 7fcef2a
Fix gitpython-developers#971. Partly resolve gitpython-developers#703.

If the hook doesn't have a file extension, then Windows won't know how
to run it and you'll get "[WinError 193] %1 is not a valid Win32
application". It's very likely that it's a shell script of some kind, so
use bash.exe (commonly installed via Windows Subsystem for Linux). We
don't want to run all hooks with bash because they could be .bat files.

Update tests to get several hook ones working. More work necessary to
get commit-msg hook working. The hook writes to the wrong file because
it's not using forward slashes in the path:
C:\Users\idbrii\AppData\Local\Temp\bare_test_commit_msg_hook_successy5fo00du\CUsersidbriiAppDataLocalTempbare_test_commit_msg_hook_successy5fo00duCOMMIT_EDITMSG
Byron added this to the v3.1.27 - Bugfixes milestone Jan 13, 2022

Byron commented Jan 13, 2022



Copy link Copy Markdown
Member

Thanks a lot for the contribution, much appreciated.

Doing this makes a lot of sense and will generally make life easier on windows. If there is any demand, one could imagine making the default interpreter/runner binary configurable, and using bash.exe for now seems like a decent default.

idbrii commented Jan 13, 2022



Copy link Copy Markdown
Contributor

Author

I wrote a test for a python hook to prove to myself it would fail:

diff --git a/test/test_index.py b/test/test_index.py
index 233a4c64..d6373250 100644
--- a/test/test_index.py
+++ b/test/test_index.py
@@ -56,14 +56,14 @@ HOOKS_SHEBANG = "#!/usr/bin/env sh\n"
 is_win_without_bash = is_win and not shutil.which('bash.exe')
 
 
-def _make_hook(git_dir, name, content, make_exec=True):
+def _make_hook(git_dir, name, content, make_exec=True, shebang=HOOKS_SHEBANG):
     """A helper to create a hook"""
     hp = hook_path(name, git_dir)
     hpd = osp.dirname(hp)
     if not osp.isdir(hpd):
         os.mkdir(hpd)
     with open(hp, "wt") as fp:
-        fp.write(HOOKS_SHEBANG + content)
+        fp.write(shebang + content)
     if make_exec:
         os.chmod(hp, 0o744)
     return hp
@@ -912,6 +912,33 @@ class TestIndex(TestBase):
         new_commit = index.commit(commit_message)
         self.assertEqual(new_commit.message, "{} {}".format(commit_message, from_hook_message))
 
+    @with_rw_repo('HEAD', bare=True)
+    def test_commit_msg_hook_success_in_py(self, rw_repo):
+        index = rw_repo.index
+        hp = _make_hook(
+            index.repo.git_dir,
+            'commit-msg',
+            "import sys; print('stdout'); print('stderr', file=sys.stderr); sys.exit(1)",
+            shebang = "#!/usr/bin/env python3\n"
+        )
+        try:
+            index.commit("This should fail")
+        except HookExecutionError as err:
+            if is_win_without_bash:
+                self.assertIsInstance(err.status, OSError)
+                self.assertEqual(err.command, [hp])
+                self.assertEqual(err.stdout, '')
+                self.assertEqual(err.stderr, '')
+                assert str(err)
+            else:
+                self.assertEqual(err.status, 1)
+                self.assertEqual(err.command, [hp])
+                self.assertEqual(err.stdout, "\n  stdout: 'stdout\n'")
+                self.assertEqual(err.stderr, "\n  stderr: 'stderr\n'")
+                assert str(err)
+        else:
+            raise AssertionError("Should have cought a HookExecutionError")
+
     @with_rw_repo('HEAD', bare=True)
     def test_commit_msg_hook_fail(self, rw_repo):
         index = rw_repo.index

And I was thinking instead of calling bash.exe with the hook, we could call it with a call_from_unix script like this so bash will figure out the right program to use:

#! /bin/sh
$1

And use:

        cmd = ["bash.exe", "call_from_unix.sh", relative_hp]

I'm currently not sure if this is a good idea or terrible idea.

Byron merged commit b719e18 into gitpython-developers : main Jan 14, 2022

Byron commented Jan 14, 2022



Copy link Copy Markdown
Member

Sorry, actually I forgot to merge yesterday and didn't mean to instill doubt.

I'd say this PR is already an improvement due to the added chance for success.

If there are any issues that the new suggestions with the 'proxy script' (for lack of a better name) would solve that can be implemented, too, but I'd wait. The current solution won't add another program invocation which might be unnecessary, but always costly.

idbrii deleted the fix-hook branch January 14, 2022 05:00 EliahKagan mentioned this pull request Sep 26, 2023 Make clear every test's status in every CI run #1679 Merged EliahKagan mentioned this pull request Nov 28, 2023 Test native Windows on CI #1745 Merged EliahKagan mentioned this pull request Jan 19, 2024 Override bash executable, defaulting to Git for Windows git bash over WSL bash #1791 Closed Sign up for free to join this conversation on GitHub . Already have an account? Sign in to comment

Labels

None yet

2 participants