GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Use bash to open extensionless hooks on windows by idbrii · Pull Request #1399 · gitpython-developers/GitPython

Conversation

@idbrii

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@idbrii
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 Byron added this to the v3.1.27 - Bugfixes milestone Jan 13, 2022
@Byron

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 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@idbrii

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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

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 reacted with rocket emoji
All reactions
  • 🚀 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@idbrii
idbrii deleted the fix-hook branch January 14, 2022 05:00
@EliahKagan EliahKagan mentioned this pull request Sep 26, 2023
Make clear every test's status in every CI run #1679
Merged
@EliahKagan EliahKagan mentioned this pull request Nov 28, 2023
Test native Windows on CI #1745
Merged
@EliahKagan 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

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Milestone

v3.1.27 - Bugfixes

Development

Successfully merging this pull request may close these issues.

OSError: [WinError 193] %1 is not a valid Win32 application when commiting LFS-enabled repository on Windows Hooks are not executed on windows correctly

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@idbrii @Byron
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.