Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use bash to open extensionless hooks on windows #1399

Merged
merged 1 commit into from
Jan 14, 2022

Conversation

idbrii
Copy link
Contributor

@idbrii idbrii commented Jan 13, 2022

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).

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 Byron added this to the v3.1.27 - Bugfixes milestone Jan 13, 2022
@Byron
Copy link
Member

Byron commented Jan 13, 2022

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
Copy link
Contributor Author

idbrii commented Jan 13, 2022

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 Byron merged commit b719e18 into gitpython-developers:main Jan 14, 2022
@Byron
Copy link
Member

Byron commented Jan 14, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants