Commit graph

30 commits

Author SHA1 Message Date
Stefan Haller 6b311ccb62 Fix quoting of shell commands on Windows
lazygit builds a shell command by interpolating Quote'd arguments into a
template and running the result via `cmd /c`. Several things were wrong
on Windows:

- Quote emitted bash-style `\"…\"` quoting, which cmd.exe doesn't
  understand. Making it usable at all previously required a fragile
  round-trip through str.ToArgv and re-escaping.
- The assembled command line was handed to `cmd /c` without `/s`, so
  cmd's default rules stripped the wrong quotes once the line contained
  more than two of them (e.g. a quoted editor path at a location with
  spaces, plus a quoted filename that also contains spaces).
- Shell metacharacters were escaped with `^` (`&` → `^&`, etc.), which
  neutralised command chaining, pipes, redirection and `%VAR%` expansion
  in custom commands.

Quote now emits the standard Windows convention directly, and NewShell
hands cmd.exe the fully-assembled line verbatim via SysProcAttr.CmdLine,
wrapped as `cmd /s /c "<command>"`. The /s flag strips exactly the outer
quote pair we add, leaving each argument's own quoting intact. With the
`^` escaping gone, metacharacters in a custom command reach cmd as the
author intended; this also removes the spurious `^` reported in #3092.

Fixes #5560
Fixes #2427
Fixes #4147

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 14:11:00 +02:00
Stefan Haller e0fcdf1c3f Demonstrate that shell metacharacters are mangled on Windows
On Windows, NewShell escapes shell metacharacters (`&`, `|`, `<`, `>`,
`%`) with `^` and splits the command into separate arguments. The
operators in a custom command therefore never reach cmd as operators,
so command chaining (`&&`), pipes, redirection and `%VAR%` expansion all
silently break (#2427, #4147, #5113; the stray `^` is also what #3092
reports).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 14:11:00 +02:00
Stefan Haller 41efc9a37a Demonstrate that Quote produces invalid Windows quoting
On Windows, Quote wraps arguments in bash-style `\"…\"` and rewrites
embedded double quotes as `"'"'"`. Neither convention is understood by
cmd.exe or CommandLineToArgvW, so commands built from quoted arguments
are mis-parsed once they contain quotes or spaces (#5560).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 14:11:00 +02:00
stk 7fbc2a8c14 Close temp file handles before cleanup on Windows
Windows cannot remove files while handles are still open. In
TestOSCommandFileType we created files and immediately called RemoveAll without
closing handles, which left untracked artifacts (e.g. "testFile" and "file with
spaces") in the working tree. Close the handles and assert RemoveAll succeeds so
cleanup failures are visible.
2026-05-02 17:59:27 +02:00
Nathan Baulch 508cdb40a8 Fix typos 2025-01-03 10:50:36 +11:00
Jesse Duffield 63dc07fded Construct arg vector manually rather than parse string
By constructing an arg vector manually, we no longer need to quote arguments

Mandate that args must be passed when building a command

Now you need to provide an args array when building a command.
There are a handful of places where we need to deal with a string,
such as with user-defined custom commands, and for those we now require
that at the callsite they use str.ToArgv to do that. I don't want
to provide a method out of the box for it because I want to discourage its
use.

For some reason we were invoking a command through a shell when amending a
commit, and I don't believe we needed to do that as there was nothing user-
supplied about the command. So I've switched to using a regular command out-
side the shell there
2023-05-23 19:49:19 +10:00
jiepeng b8900baf1a remove deprecated calls 2022-09-17 15:10:41 -07:00
Luka Markušić fdf6a9cc2b Test appending to empty file 2022-08-04 13:52:04 +02:00
Luka Markušić 6160d85d4f Use tmpdir for tests 2022-08-03 14:06:12 +02:00
Luka Markušić d238d8952b Add AppendLineToFile tests 2022-08-03 07:55:51 +02:00
Jesse Duffield 02c5559704 run integration tests in parallel and properly cache windows build 2022-06-11 13:06:29 +10:00
Ryooooooga 2fbb52fa2c chore: remove dead code 2022-04-02 08:48:38 +11:00
Jesse Duffield a34bdf1a04 update linters 2022-03-19 12:12:57 +11:00
Jesse Duffield 18f48a43d5 add some more linters 2022-01-09 14:09:53 +11:00
Jesse Duffield 25195eacee WIP 2022-01-04 09:07:15 +11:00
Jesse Duffield 9b2b0fc122 WIP 2022-01-04 09:07:15 +11:00
Jesse Duffield 96c2887fd0 WIP 2022-01-04 09:07:15 +11:00
Jesse Duffield 43a4fa970d WIP 2022-01-04 09:07:15 +11:00
Jesse Duffield b4c078d565 WIP 2022-01-04 09:07:15 +11:00
Ryooooooga 7564e506b5 Enable/disable os specific tests at compile time 2021-10-17 11:00:20 +11:00
Ryooooooga 1e50764b4d Fix tests 2021-10-17 11:00:20 +11:00
Ryooooooga 2dc8396deb Fix test 2021-10-16 22:40:50 +11:00
Francisco Miamoto f8f596d097 add tests for open file cmd on linux 2021-07-27 20:28:00 +10:00
Ryooooooga 64daf1310d Fix staging/unstaging files containing " in paths 2021-03-13 11:02:31 +11:00
Ryooooooga 50e4e9d58d fix command escaping 2021-03-13 10:49:40 +11:00
Jesse Duffield 09f32d4f84 add secureexec file for getting around windows checking for a binary first in the current dir 2021-02-08 14:40:30 -08:00
Jesse Duffield 999e170f1d standardise how we read from the config 2020-11-28 10:45:30 +11:00
Jesse Duffield 1e48afeb8f quote config file when editing 2020-10-12 08:47:12 +11:00
Jesse Duffield 4912205adb remove viper
WIP
2020-10-10 00:23:01 +11:00
Jesse Duffield 1759ddf247 move OS commands into their own package 2020-09-29 20:48:49 +10:00
Renamed from pkg/commands/os_test.go (Browse further)