From 4e35067325022e4d8790d45557156fd1166484df Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Thu, 2 Nov 2023 13:35:54 -0400 Subject: [PATCH] windows hook scripts newlines without CR Windows: When git-annex init is installing hook scripts, it will avoid ending lines with CR for portability. Existing hook scripts that do have CR line endings will not be changed. While it would be possible to have git-annex init upgrade them, users would need to know to use that command to do that, and it would add complexity that does not seem warranted for the portability benefit alone. Sponsored-by: Luke T. Shumaker on Patreon --- CHANGELOG | 10 ++++++---- Git/Hook.hs | 12 ++++++++++- ..._e711bdfcee4ec9a2860662e8c65a9c0f._comment | 20 +++++++++++++++++++ 3 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 doc/forum/Windows_eol_issues/comment_5_e711bdfcee4ec9a2860662e8c65a9c0f._comment diff --git a/CHANGELOG b/CHANGELOG index 551339a701..4dec40cb78 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -5,11 +5,13 @@ git-annex (10.20230927) UNRELEASED; urgency=medium * importfeed: Use caching database to avoid needing to list urls on every run, and avoid using too much memory. * Improve memory use of --all when using annex.private. - * Windows: Consistently avoid ending output lines with CR. This matches - the behavior of git on Windows, which does not end lines with CR - either. - * Windows: Fix CRLF handling in some log files. * lookupkey: Sped up --batch. + * Windows: Consistently avoid ending standard output lines with CR. + This matches the behavior of git on Windows. + * Windows: Fix CRLF handling in some log files. + * Windows: When git-annex init is installing hook scripts, it will + avoid ending lines with CR for portability. Existing hook scripts + that do have CR line endings will not be changed. -- Joey Hess Tue, 10 Oct 2023 13:17:31 -0400 diff --git a/Git/Hook.hs b/Git/Hook.hs index ba9031d800..1301ec2180 100644 --- a/Git/Hook.hs +++ b/Git/Hook.hs @@ -19,6 +19,7 @@ import qualified Utility.RawFilePath as R import System.PosixCompat.Files (fileMode) #endif +import qualified Data.ByteString as B data Hook = Hook { hookName :: FilePath @@ -57,7 +58,12 @@ hookWrite h r = ifM (doesFileExist f) where f = hookFile h r go = do - viaTmp writeFile f (hookScript h) + -- On Windows, using B.writeFile here avoids + -- the newline translation done by writeFile. + -- Hook scripts on Windows could use CRLF endings, but + -- they typically use unix newlines, which does work there + -- and makes the repository more portable. + viaTmp B.writeFile f (encodeBS (hookScript h)) void $ tryIO $ modifyFileMode (toRawFilePath f) (addModes executeModes) @@ -81,6 +87,10 @@ data ExpectedContent = UnexpectedContent | ExpectedContent | OldExpectedContent expectedContent :: Hook -> Repo -> IO ExpectedContent expectedContent h r = do + -- Note that on windows, this readFile does newline translation, + -- and so a hook file that has CRLF will be treated the same as one + -- that has LF. That is intentional, since users may have a reason + -- to prefer one or the other. content <- readFile $ hookFile h r return $ if content == hookScript h then ExpectedContent diff --git a/doc/forum/Windows_eol_issues/comment_5_e711bdfcee4ec9a2860662e8c65a9c0f._comment b/doc/forum/Windows_eol_issues/comment_5_e711bdfcee4ec9a2860662e8c65a9c0f._comment new file mode 100644 index 0000000000..2faa2e4ce8 --- /dev/null +++ b/doc/forum/Windows_eol_issues/comment_5_e711bdfcee4ec9a2860662e8c65a9c0f._comment @@ -0,0 +1,20 @@ +[[!comment format=mdwn + username="joey" + subject="""comment 5""" + date="2023-11-02T17:28:49Z" + content=""" +Took a look at hook script line endings on windows, and found that they +work both with CRLF endings and without the CR. + +It seems better to not use CR in them, because git for windows does not +in the examples it writes, and because it will make the hooks portable if +the repository gets moved to a unix system. + +So, I made `git-annex init` omit the CR when writing new hook scripts. +Existing hook scripts that use CRLF will be preserved by git-annex. + +I have not been able to reproduce this problem with or without CR in hook +scripts: + + fatal: cannot run hooks/post-receive: No such file or directory +"""]]