threads / discuss / 62630

New checkout --track behaviour?

Subject: New checkout --track behaviour?

## tl;dr

4 messages between Dec 12, 2024 and Dec 13, 2024.

replies: 3people: 3as markdown or json

Francesco Occhipinti· Dec 12, 2024, 10:29 UTC · lore
Hello,

Did the interface change in recent git versions? I am pretty sure i used to git checkout -tb new_branch, now getting error: option `--track' expects "direct" or "inherit".

User `geirha` on libera#git helped me troubleshooting. They noticed that:
- git help checkout differs in its description of --track between
  2.34.1 (ubuntu 22.04) and 2.43.0 (ubuntu 24.04), so looks like it
  did change recently
- according to the commit message at
  https://github.com/git/git/commit/6327f0efed36c64d98a140110171362b7cb75a52,
  -t is supposed to be equivalent to --track=direct, so likely a bug
  rather than intentional change

I did read all options in `man git config` looking for something related. Found only branch.autoSetupMerge, but neither setting it to `always` nor `inherit` helped

Cheers, fran

brian m. carlson· Dec 13, 2024, 03:48 UTC · re: Francesco Occhipinti · lore

Re: New checkout --track behaviour?

On 2024-12-12 at 10:29:59, Francesco Occhipinti wrote:
Show 16 quoted lines
> Hello,
> 
> Did the interface change in recent git versions? I am pretty sure i
> used to git checkout -tb new_branch, now getting error: option
> `--track' expects "direct" or "inherit".
> 
> User `geirha` on libera#git helped me troubleshooting. They noticed that:
> 
> - git help checkout differs in its description of --track between
>   2.34.1 (ubuntu 22.04) and 2.43.0 (ubuntu 24.04), so looks like it
>   did change recently
> 
> - according to the commit message at
>   https://github.com/git/git/commit/6327f0efed36c64d98a140110171362b7cb75a52,
>   -t is supposed to be equivalent to --track=direct, so likely a bug
>   rather than intentional change

`-t`, or `--track`, now takes an optional argument. Before, it didn't, so `-tb` was interpreted as `--track -b`. Now, it's interpreted as `--track=b`, since the `b` is interpreted as the option value to `--track`. That's not a valid value, so you get the error message.

This is indeed a subtle incompatibility in command-line option parsing, but it's not really avoidable and it does occur in pretty much any case where a short option learns to take an optional argument (for any software, not just Git). So what you probably want to do is `git checkout -t -b new_branch`, and it will work as before. That will also work on older versions as well, so if you have scripts or aliases that need to work across versions, that should be a safe syntax to use.

-- 
brian m. carlson (they/them or he/him)
Toronto, Ontario, CA
Junio C Hamano· Dec 13, 2024, 10:39 UTC · re: brian m. carlson · lore

Re: New checkout --track behaviour?

"brian m. carlson" <sandals@crustytoothpaste.net> writes:
> This is indeed a subtle incompatibility in command-line option parsing,
> but it's not really avoidable and it does occur in pretty much any case
> where a short option learns to take an optional argument (for any
> software, not just Git).

Perhaps we'd want something along this line? While it is not really avoidable if we have to add an optional value to an existing option, "not really avoidable" is not a very satisfactory explanation to please those whose established use cases they have, or their scripts, have got broken.

--- >8 ------ >8 ------ >8 ---
Subject: gitcli: explain why short options are better spelled separately

When the "--track" option of "git checkout" command learned to take an optional value a few years ago at d3115660 (branch: add flags and config to inherit tracking, 2021-12-20), "git checkout -tb foo" got broken, as it is no longer two options "--track" and "--branch=foo" given together, but "--track=b" and a regular argument "foo". The command parser dies upon seeing "-tb" saying "b" is not a valid optional value to "-t".

We already discourage users to spell short options in stuck form, and if they spelled "-t -b foo", they would not have noticed this breakage. Add a bit more words to "git help cli" to explain why we discourage the stuck form.

We might also want to add a note to ourselves (perhaps next to the Documentation/CodingGuidelines, we'd want design guidelines) to think very carefully before considering to introduce an option that takes an optional value, and refrain from adding an optional value to an existing option. It would save the end users from confusion.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 Documentation/gitcli.txt | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
diff --git c/Documentation/gitcli.txt w/Documentation/gitcli.txt
index bd62cbd043..dcc0aa5bf7 100644
--- c/Documentation/gitcli.txt
+++ w/Documentation/gitcli.txt
@@ -82,7 +82,9 @@ Here are the rules regarding the "flags" that you should follow when you are
 scripting Git:
 
  * Splitting short options to separate words (prefer `git foo -a -b`
-   to `git foo -ab`, the latter may not even work).
+   to `git foo -ab`, the latter may not even work, and even if it
+   worked, it will change the meaning once the '-a' option starts
+   taking an optional value).
 
  * When a command-line option takes an argument, use the 'stuck' form.  In
    other words, write `git foo -oArg` instead of `git foo -o Arg` for short
Francesco Occhipinti· Dec 13, 2024, 10:50 UTC · re: brian m. carlson · lore

Re: New checkout --track behaviour?

Yes, that works! Thanks Brian
________________________________________
From: brian m. carlson <sandals@crustytoothpaste.net>
Sent: Friday, December 13, 2024 4:48 AM
To: Francesco Occhipinti <Francesco.Occhipinti@tracsis.com>
Cc: git@vger.kernel.org <git@vger.kernel.org>
Subject: Re: New checkout --track behaviour?
 
[You don't often get email from sandals@crustytoothpaste.net. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]
CAUTION: This email originated from outside of the organisation. Do not click links or open attachments unless you recognise the sender and know the content is safe.

← back to recent threads