git/list[1] front-page[2] threads[3] people[4] search[5] about
 

How do I "git fetch" with a custom <refspec> but a default remote?

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Nov 12, 2020, 14:19 UTC
Message-ID
<877dqqhd3s.fsf@evledraar.gmail.com>
In-Reply-To
<20201111151754.31527-1-avarab@gmail.com>
On Wed, Nov 11 2020, Ævar Arnfjörð Bjarmason wrote:
Show 5 quoted lines
> In any case, this is one thing that came out of that
> investigation. The code we're keeping by moving it to git-submodule.sh
> can also be replaced by some C code we have, but I wanted to leave
> that to another submission (if I'll get to it), and make this simply a
> code removal.

I may have missed a way to do $subject, but I don't think it's possible. The reason I want it is because git-submodule.sh does this:

    git fetch $(get_default_remote) "$@" ;;

Where that shellscript function gets the name of the remote configured for the current branch. If you do just a:

    git fetch
Then it will do the right thing, per its documentation:
     When no remote is specified, by default the origin remote will be
     used, unless there’s an upstream branch configured for the current
     branch.
But git-submodule.sh wants to do:
    git fetch <default-remote> <some-sha1>
So the caller is forced to find out what that is.
I came up with this patch:
    
    diff --git a/builtin/fetch.c b/builtin/fetch.c
    index f9c3c49f14..f110ac8d08 100644
    --- a/builtin/fetch.c
    +++ b/builtin/fetch.c
    @@ -56,6 +56,7 @@ static int prune_tags = -1; /* unspecified */
     #define PRUNE_TAGS_BY_DEFAULT 0 /* do we prune tags by default? */
     
     static int all, append, dry_run, force, keep, multiple, update_head_ok;
    +static int default_remote;
     static int write_fetch_head = 1;
     static int verbosity, deepen_relative, set_upstream;
     static int progress = -1;
    @@ -140,6 +141,8 @@ static struct option builtin_fetch_options[] = {
            OPT__VERBOSITY(&verbosity),
            OPT_BOOL(0, "all", &all,
                     N_("fetch from all remotes")),
    +       OPT_BOOL(0, "default-remote", &default_remote,
    +                N_("fetch from default remote")),
            OPT_BOOL(0, "set-upstream", &set_upstream,
                     N_("set upstream for git pull/fetch")),
            OPT_BOOL('a', "append", &append,
    @@ -1852,7 +1855,7 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
                    else if (argc > 1)
                            die(_("fetch --all does not make sense with refspecs"));
                    (void) for_each_remote(get_one_remote_for_fetch, &list);
    -       } else if (argc == 0) {
    +       } else if (argc == 0 || default_remote) {
                    /* No arguments -- use default remote */
                    remote = remote_get(NULL);
            } else if (multiple) {
Which allows me to do:
    -               git fetch $(get_default_remote) "$@" ;;
    +               git fetch --default-remote "$@" ;;

So it works, but what do we think about this calling convention? Do we have any prior art for commands that take positional arguments like <remote> and <refspec> where you'd like to use a default for an earlier argument to provide a subsequent one?

To make it more general and consistent we'de probably like a --remote=* and --refspec arguments, so the invocation would be:

    git fetch ([--remote=]<name> | --default-remote) [([--refspec=]<refspec> | --default-refspec)]
But maybe I'm overthinking it...
Previous: Jeff KingNext: Jeff King
Message 13 of 62 in “Start retiring .git/remotes/ and .git/branches/ for good”
  1. 00/11 Start retiring .git/remotes/ and .git/branches/ for goodJohannes Schindelin, May 11, 2017
  2. 05/11 Revert "Revert "Don't create the $GIT_DIR/branches directory on init""Johannes Schindelin, May 11, 2017
  3. Stefan BellerMay 11, 2017
  4. 04/11 remote: warn loud and clear when .git/remotes/ is *still* usedJohannes Schindelin, May 11, 2017
  5. 01/11 git-parse-remote: fix highly misleading man pageJohannes Schindelin, May 11, 2017
  6. Stefan BellerMay 11, 2017
  7. Johannes SchindelinMay 11, 2017
  8. 0/5 Remove now-unused git-parse-remoteÆvar Arnfjörð Bjarmason, Nov 11, 2020
  9. Jeff KingNov 11, 2020
  10. Junio C HamanoNov 11, 2020
  11. Ævar Arnfjörð BjarmasonNov 12, 2020
  12. Jeff KingNov 12, 2020
  13. How do I "git fetch" with a custom <refspec> but a default remote?Ævar Arnfjörð Bjarmason, Nov 12, 2020
  14. Jeff KingNov 12, 2020
  15. Chris TorekNov 12, 2020
  16. Jeff KingNov 12, 2020
  17. Junio C HamanoNov 12, 2020
  18. Ævar Arnfjörð BjarmasonNov 14, 2020
  19. 1/5 parse-remote: remove unused GIT_DIR variableÆvar Arnfjörð Bjarmason, Nov 11, 2020
  20. 2/5 parse-remote: remove long-dead rebase codeÆvar Arnfjörð Bjarmason, Nov 11, 2020
  21. 4/5 parse-remote: move used code to git-submodule.shÆvar Arnfjörð Bjarmason, Nov 11, 2020
  22. 5/5 parse-remote: remove this now-unused libraryÆvar Arnfjörð Bjarmason, Nov 11, 2020
  23. Junio C HamanoNov 11, 2020
  24. 0/2 Retire git-parse-remoteJunio C Hamano, Nov 12, 2020
  25. 1/2 parse-remote: move used code to git-submodule.shJunio C Hamano, Nov 12, 2020
  26. 2/2 parse-remote: remove this now-unused libraryJunio C Hamano, Nov 12, 2020
  27. Jeff KingNov 12, 2020
  28. Junio C HamanoNov 12, 2020
  29. Ævar Arnfjörð BjarmasonNov 13, 2020
  30. 0/3 submodule sh->C & retire parse-remoteÆvar Arnfjörð Bjarmason, Nov 14, 2020
  31. 1/3 submodule: use "fetch" logic instead of custom remote discoveryÆvar Arnfjörð Bjarmason, Nov 14, 2020
  32. Junio C HamanoNov 16, 2020
  33. 3/3 parse-remote: remove this now-unused libraryÆvar Arnfjörð Bjarmason, Nov 14, 2020
  34. Junio C HamanoNov 16, 2020
  35. Ævar Arnfjörð BjarmasonNov 17, 2020
  36. 2/3 submodule: remove sh function in favor of helperÆvar Arnfjörð Bjarmason, Nov 14, 2020
  37. 3/5 parse-remote: remove long-dead git-pull.sh codeÆvar Arnfjörð Bjarmason, Nov 11, 2020
  38. 03/11 remote: warn loud and clear when .git/branches/ is *still* usedJohannes Schindelin, May 11, 2017
  39. 09/11 PREVIEW: t5515: remove .git/remotes/ and .git/branches/ testsJohannes Schindelin, May 11, 2017
  40. 02/11 Documentation: really deprecate .git/remotes/ and .git/branches/Johannes Schindelin, May 11, 2017
  41. 10/11 PREVIEW: t0060: stop testing support for .git/remotes/ and .git/branches/Johannes Schindelin, May 11, 2017
  42. 11/11 PREVIEW: remove support for .git/remotes/ and .git/branches/Johannes Schindelin, May 11, 2017
  43. Stefan BellerMay 11, 2017
  44. Johannes SchindelinMay 11, 2017
  45. 06/11 PREVIEW: t5510: convert .git/remotes/ test to use a regular remoteJohannes Schindelin, May 11, 2017
  46. 07/11 PREVIEW: t5516: stop testing .git/branches/ functionalityJohannes Schindelin, May 11, 2017
  47. 08/11 PREVIEW: remote: remove support for migrating ancient remotesJohannes Schindelin, May 11, 2017
  48. Junio C HamanoMay 12, 2017
  49. Johannes SchindelinMay 12, 2017
  50. Junio C HamanoMay 16, 2017
  51. Ævar Arnfjörð BjarmasonMay 16, 2017
  52. Junio C HamanoMay 16, 2017
  53. Ævar Arnfjörð BjarmasonMay 16, 2017
  54. Junio C HamanoMay 17, 2017
  55. Junio C HamanoMay 12, 2017
  56. Johannes SchindelinMay 12, 2017
  57. Jonathan NiederMay 12, 2017
  58. Junio C HamanoMay 13, 2017
  59. Junio C HamanoMay 12, 2017
  60. Johannes SchindelinMay 15, 2017
  61. Jeff KingMay 12, 2017
  62. Johannes SchindelinMay 12, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.