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
This commit is contained in:
Joey Hess 2023-11-02 13:35:54 -04:00
parent c41ca6c832
commit 4e35067325
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
3 changed files with 37 additions and 5 deletions

View file

@ -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 <id@joeyh.name> Tue, 10 Oct 2023 13:17:31 -0400

View file

@ -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

View file

@ -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
"""]]