git-for-windows / git

A fork of Git containing Windows-specific patches.
http://gitforwindows.org/
Other
8.31k stars 2.52k forks source link

`git config --global --add safe.directory` instructions are wrong (`'` should be `"`) on a Windows non-posix shell (`cmd`) #3856

Open idanmiara opened 2 years ago

idanmiara commented 2 years ago

On Windows, the instructions for git config --global --add safe.directory "%(prefix)///mynas/porjects/my project" path should be with the within " instead of '

Setup

$ git --version --build-options

git version 2.36.1.windows.1
cpu: x86_64
built from commit: e2ff68a2d1426758c78d023f863bfa1e03cbc768
sizeof-long: 4
sizeof-size_t: 8
shell-path: /bin/sh
feature: fsmonitor--daemon

Windows 11 64 bit

$ cmd.exe /c ver

Microsoft Windows [Version 10.0.22000.675]

Default

# One of the following:
> type "C:\Program Files\Git\etc\install-options.txt"
> type "C:\Program Files (x86)\Git\etc\install-options.txt"
> type "%USERPROFILE%\AppData\Local\Programs\Git\etc\install-options.txt"
> type "$env:USERPROFILE\AppData\Local\Programs\Git\etc\install-options.txt"
$ cat /etc/install-options.txt

Editor Option: VIM
Custom Editor Path:
Default Branch Option:
Path Option: Cmd
SSH Option: OpenSSH
Tortoise Option: false
CURL Option: OpenSSL
CRLF Option: CRLFAlways
Bash Terminal Option: MinTTY
Git Pull Behavior Option: Merge
Use Credential Manager: Enabled
Performance Tweaks FSCache: Enabled
Enable Symlinks: Disabled
Enable Pseudo Console Support: Disabled
Enable FSMonitor: Disabled

Related issues: https://github.com/git-for-windows/git/pull/3790 https://github.com/git-for-windows/git/pull/3791

Details

cmd

run git status on a shared network drive folder

x:\projects\my project>git status
fatal: unsafe repository ('//mynas/porjects/my project' is owned by someone else)
To add an exception for this directory, call:

        git config --global --add safe.directory '%(prefix)///mynas/porjects/my project'

.gitconfig should be updated

error: wrong number of arguments, should be 2

Using double quotes instead of single quotes solves the problem.

git config --global --add safe.directory "%(prefix)///mynas/porjects/my project"

No

rimrul commented 2 years ago

They're not completely wrong, they just assume a POSIX-ish shell (like Git Bash). Git tends to do that when giving out advice about command line syntax.

We don't have a way to detect the shell you're calling git from to customise the advice though.

rimrul commented 2 years ago

An approach that could be easier to implement than detecting the shell could be a config option that sets a preferred command line syntax.

dscho commented 2 years ago

We don't have a way to detect the shell you're calling git from to customise the advice though.

I guess we could use the presence of the environment variable PSModulePath as a tell-tale that we're running inside a PowerShell, PROMPT as a tell-tale that we're running inside a CMD, and SHELL as a tell-tale that we're running inside a Bash (but that somehow does not work for dash, so we might want to fall back to assuming a POSIX shell anyway).

rimrul commented 2 years ago

That approach could still get confused when calling one shell from another.

landstander668 commented 2 years ago

Would it make sense to instead provide multiple variations, and rely upon the user to choose the appropriate item? Something like:

fatal: unsafe repository ('//mynas/porjects/my project' is owned by someone else)
To add an exception for this directory, please choose the appropriate command below based upon the environment in use:

bash/posix shell:   git config --global --add safe.directory '%(prefix)///mynas/porjects/my project'
cmd/powershell:     git config --global --add safe.directory "%(prefix)///mynas/porjects/my project"

It's not perfect—naturally—since some users won't know which version to select (especially when using third-party software that invokes GfW), but may be useful as a starting point.

idanmiara commented 2 years ago

I actually got the initial error message from TortoiseGit, so it wasn't even from a shell. So I think it's a good idea to give this informative error.

On Tue, 17 May 2022, 19:58 Adric Norris, @.***> wrote:

Would it make sense to instead provide multiple variations, and rely upon the user to choose the appropriate item? Something like:

fatal: unsafe repository ('//mynas/porjects/my project' is owned by someone else)

To add an exception for this directory, please choose the appropriate command below based upon the environment in use:

bash/posix shell: git config --global --add safe.directory '%(prefix)///mynas/porjects/my project'

cmd/powershell: git config --global --add safe.directory "%(prefix)///mynas/porjects/my project"

It's not perfect—naturally—since some users won't know which version to select (especially when using third-party software that invokes GfW), but may be useful as a starting point.

— Reply to this email directly, view it on GitHub https://github.com/git-for-windows/git/issues/3856#issuecomment-1129104434, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGJBBLPN564LGJEZ3JXVDYTVKPF35ANCNFSM5WDVH6OA . You are receiving this because you authored the thread.Message ID: @.***>

dscho commented 2 years ago

That approach could still get confused when calling one shell from another.

All too true.

Would it make sense to instead provide multiple variations, and rely upon the user to choose the appropriate item? Something like:

fatal: unsafe repository ('//mynas/porjects/my project' is owned by someone else)
To add an exception for this directory, please choose the appropriate command below based upon the environment in use:

bash/posix shell:   git config --global --add safe.directory '%(prefix)///mynas/porjects/my project'
cmd/powershell:     git config --global --add safe.directory "%(prefix)///mynas/porjects/my project"

It's not perfect—naturally—since some users won't know which version to select (especially when using third-party software that invokes GfW), but may be useful as a starting point.

I'd rather just add a comment (e.g. "Use single quotes instead of double quotes to run this command in PowerShell/CMD").