Parse Windows' version correctly in post-install script #506
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.
The following snippet in
post-install.bat
attempts to parse Windows' major version number fromver
builtin command output:However, that code has numerous problems:
ver
listing varies between major Windows releases (though Git for Windows' support for these has most likely phased out already) and may not necessarily yield the same amount of tokens, thus it's better to perform a two-step tokenization - string, then versionver
hasn't been found to contain 'poisonous' Batch characters (e.g. <>|&), it's preferred to wrap it in double quotes to prevent the contents 'escaping' into another command and use~
during assignment should it contain double quotes itselfver
encounters an error, yielding an empty variable or inheriting a value from the environment the script happened to be running in, introducing potential security issues (PR note: especially when a buggy AutoRun command is executed in that subshell before)ver
contain an empty string, thus the variable needs to be pre-initialized with a numeric valueif
This can result in the following, rather cryptic error:
Echoed due to
%ver%
being empty because of the problems listed above.Fixes git-for-windows/git#1585 and git-for-windows/git#4019; maybe more.
Ran into this while looking for clues related to git-for-windows/git#4466...