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

getgit: Fix user path reg query result potentially getting truncated #558

Merged
merged 4 commits into from
Jun 12, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion git-extra/getgit
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ echo "be removed from the context menu is available as $rm_ctxmenu "
echo ""
# Setting ENV
echo -n "Setting env ..."
userpath=$(reg query "HKEY_CURRENT_USER\Environment" //v Path 2>/dev/null | awk 'NR==3 {print $NF}')
userpath=$(reg query "HKEY_CURRENT_USER\Environment" //v Path 2>/dev/null | awk 'NR==3 {print}' | cut -d ' ' -f 13-)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would the following work, too?

reg query "HKEY_CURRENT_USER\Environment" //v Path | sed -n 's/^ *Path *REG_EXPAND_SZ *//p'

I would have a preference for that if it worked, because:

  1. It avoids spawning two processes, and
  2. It spells out what is expected instead of relying on the magic row number 3 and the magic column number 13.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like that doesn't work all the time because the path variable may be of type REG_SZ or REG_EXPAND_SZ, depending on whether or not variables like %USERPROFILE% are used (see https://learn.microsoft.com/en-us/windows/win32/sysinfo/registry-value-types for registry value types). I assume those are the only valid types for that key that the system accepts, although REG_MULTI_SZ also sounds possible to me. If you prefer the sed approach, reg query "HKEY_CURRENT_USER\Environment" //v Path | sed -n 's/^ *Path *REG\(_EXPAND\)\?_SZ *//p' has worked for both REG_SZ and REG_EXPAND_SZ in my testing.

It's a bit unfortunate that the reg query output is inherently badly formatted for automated reading, but I think the assumptions that have worked so far are safe (value always on line 3, separators are exactly 4 spaces...).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit unfortunate that the reg query output is inherently badly formatted for automated reading, but I think the assumptions that have worked so far are safe (value always on line 3, separators are exactly 4 spaces...).

It might be better to turn this into a PowerShell scriptlet, anyway. Something like this:

userpath="$(powershell -command '(Get-ItemProperty -Path 'HKCU:\Environment' -Name Path).Path')"

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that would be ideal. Given this only runs on windows, powershell should be guaranteed to be available.

if [[ "$userpath" == "" ]]; then
setx PATH "$(cygpath -m /cmd)" > /dev/null 2>&1
elif [[ "$userpath" != *"$(cygpath -m /cmd)"* ]]; then
Expand Down
Loading