From 1c1518071c7fa79de13b8c599d8dbb371950b033 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=86var=20Arnfj=C3=B6r=C3=B0=20Bjarmason?= Date: Sat, 14 Nov 2020 13:21:30 +0100 Subject: submodule: use "fetch" logic instead of custom remote discovery MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace a use of the get_default_remote() function with an invocation of "git fetch" The "fetch" command already has logic to discover the remote for the current branch. However, before it learned to accept a custom refspec *and* use its idea of the default remote, it wasn't possible to get rid of some equivalent of the "get_default_remote" invocation here. As it turns out the recently added "--stdin" option to fetch[1] gives us a way to do that. Let's use it instead. While I'm at it simplify the "fetch_in_submodule" function. It wasn't necessary to pass "$@" to "fetch" since we'd only ever provide one SHA-1 as an argument in the previous "*" codepath (in addition to "--depth=N"). Rewrite the function to more narrowly reflect its use-case. 1. https://lore.kernel.org/git/87eekwf87n.fsf@evledraar.gmail.com/ Signed-off-by: Ævar Arnfjörð Bjarmason Signed-off-by: Junio C Hamano --- git-submodule.sh | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/git-submodule.sh b/git-submodule.sh index 7ce52872b7..d39fd226d8 100755 --- a/git-submodule.sh +++ b/git-submodule.sh @@ -416,13 +416,15 @@ is_tip_reachable () ( fetch_in_submodule () ( sanitize_submodule_env && cd "$1" && - case "$2" in - '') - git fetch ;; - *) - shift - git fetch $(get_default_remote) "$@" ;; - esac + if test $# -eq 3 + then + echo "$3" | git fetch --stdin "$2" + elif test "$2" -ne "" + then + git fetch "$2" + else + git fetch + fi ) # -- cgit v1.2.3 From e63f7b0acba326a47282036afffb83e2b3eb023d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=86var=20Arnfj=C3=B6r=C3=B0=20Bjarmason?= Date: Sat, 14 Nov 2020 13:21:31 +0100 Subject: submodule: remove sh function in favor of helper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Remove the now-redundant "get_default_remote" function by converting its last user to the "print-default-remote" helper. As can be seen in 13424764db ("submodule: port submodule subcommand 'sync' from shell to C", 2018-01-15) this helper is already used internally by the C code for submodule remote name discovery. The "get_default_remote" function in "git-parse-remote.sh" will be removed in a follow-up change. Signed-off-by: Ævar Arnfjörð Bjarmason Signed-off-by: Junio C Hamano --- git-submodule.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git-submodule.sh b/git-submodule.sh index d39fd226d8..d39a28215c 100755 --- a/git-submodule.sh +++ b/git-submodule.sh @@ -578,7 +578,7 @@ cmd_update() fetch_in_submodule "$sm_path" $depth || die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")" fi - remote_name=$(sanitize_submodule_env; cd "$sm_path" && get_default_remote) + remote_name=$(sanitize_submodule_env; cd "$sm_path" && git submodule--helper print-default-remote) sha1=$(sanitize_submodule_env; cd "$sm_path" && git rev-parse --verify "${remote_name}/${branch}") || die "$(eval_gettext "Unable to find current \${remote_name}/\${branch} revision in submodule path '\$sm_path'")" -- cgit v1.2.3 From a89a2fbfccd88bc8a3c8cce8d7bc03de3830ea25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=86var=20Arnfj=C3=B6r=C3=B0=20Bjarmason?= Date: Sat, 14 Nov 2020 13:21:32 +0100 Subject: parse-remote: remove this now-unused library MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The previous two commits removed the last use of a function in this library, but most of it had been dead code for a while[1][2]. Only the "get_default_remote" function was still being used. Even though we had a manual page for this library it was never intended (or I expect, actually) used outside of git.git. Let's just remove it, if anyone still cares about a function here they can pull them into their own project[3]. 1. Last use of error_on_missing_default_upstream(): d03ebd411c ("rebase: remove the rebase.useBuiltin setting", 2019-03-18) 2. Last use of get_remote_merge_branch(): 49eb8d39c7 ("Remove contrib/examples/*", 2018-03-25) 3. https://lore.kernel.org/git/87a6vmhdka.fsf@evledraar.gmail.com/ Signed-off-by: Ævar Arnfjörð Bjarmason Signed-off-by: Junio C Hamano --- .gitignore | 1 - Documentation/git-parse-remote.txt | 23 --------- Makefile | 2 - command-list.txt | 1 - git-parse-remote.sh | 101 ------------------------------------- git-submodule.sh | 1 - 6 files changed, 129 deletions(-) delete mode 100644 Documentation/git-parse-remote.txt delete mode 100644 git-parse-remote.sh diff --git a/.gitignore b/.gitignore index 6232d33924..9da275e4e8 100644 --- a/.gitignore +++ b/.gitignore @@ -114,7 +114,6 @@ /git-pack-redundant /git-pack-objects /git-pack-refs -/git-parse-remote /git-patch-id /git-prune /git-prune-packed diff --git a/Documentation/git-parse-remote.txt b/Documentation/git-parse-remote.txt deleted file mode 100644 index a45ea1ece8..0000000000 --- a/Documentation/git-parse-remote.txt +++ /dev/null @@ -1,23 +0,0 @@ -git-parse-remote(1) -=================== - -NAME ----- -git-parse-remote - Routines to help parsing remote repository access parameters - - -SYNOPSIS --------- -[verse] -'. "$(git --exec-path)/git-parse-remote"' - -DESCRIPTION ------------ -This script is included in various scripts to supply -routines to parse files under $GIT_DIR/remotes/ and -$GIT_DIR/branches/ and configuration variables that are related -to fetching, pulling and pushing. - -GIT ---- -Part of the linkgit:git[1] suite diff --git a/Makefile b/Makefile index 1fb0ec1705..9c154a2666 100644 --- a/Makefile +++ b/Makefile @@ -613,7 +613,6 @@ SCRIPT_SH += git-submodule.sh SCRIPT_SH += git-web--browse.sh SCRIPT_LIB += git-mergetool--lib -SCRIPT_LIB += git-parse-remote SCRIPT_LIB += git-rebase--preserve-merges SCRIPT_LIB += git-sh-i18n SCRIPT_LIB += git-sh-setup @@ -2577,7 +2576,6 @@ XGETTEXT_FLAGS_PERL = $(XGETTEXT_FLAGS) --language=Perl \ --keyword=__ --keyword=N__ --keyword="__n:1,2" LOCALIZED_C = $(C_OBJ:o=c) $(LIB_H) $(GENERATED_H) LOCALIZED_SH = $(SCRIPT_SH) -LOCALIZED_SH += git-parse-remote.sh LOCALIZED_SH += git-rebase--preserve-merges.sh LOCALIZED_SH += git-sh-setup.sh LOCALIZED_PERL = $(SCRIPT_PERL) diff --git a/command-list.txt b/command-list.txt index 0e3204e7d1..c19c8a94fe 100644 --- a/command-list.txt +++ b/command-list.txt @@ -135,7 +135,6 @@ git-p4 foreignscminterface git-pack-objects plumbingmanipulators git-pack-redundant plumbinginterrogators git-pack-refs ancillarymanipulators -git-parse-remote synchelpers git-patch-id purehelpers git-prune ancillarymanipulators complete git-prune-packed plumbingmanipulators diff --git a/git-parse-remote.sh b/git-parse-remote.sh deleted file mode 100644 index d3c39980f3..0000000000 --- a/git-parse-remote.sh +++ /dev/null @@ -1,101 +0,0 @@ -# This is a shell library to calculate the remote repository and -# upstream branch that should be pulled by "git pull" from the current -# branch. - -# git-ls-remote could be called from outside a git managed repository; -# this would fail in that case and would issue an error message. -GIT_DIR=$(git rev-parse -q --git-dir) || :; - -get_default_remote () { - curr_branch=$(git symbolic-ref -q HEAD) - curr_branch="${curr_branch#refs/heads/}" - origin=$(git config --get "branch.$curr_branch.remote") - echo ${origin:-origin} -} - -get_remote_merge_branch () { - case "$#" in - 0|1) - origin="$1" - default=$(get_default_remote) - test -z "$origin" && origin=$default - curr_branch=$(git symbolic-ref -q HEAD) && - [ "$origin" = "$default" ] && - echo $(git for-each-ref --format='%(upstream)' $curr_branch) - ;; - *) - repo=$1 - shift - ref=$1 - # FIXME: It should return the tracking branch - # Currently only works with the default mapping - case "$ref" in - +*) - ref=$(expr "z$ref" : 'z+\(.*\)') - ;; - esac - expr "z$ref" : 'z.*:' >/dev/null || ref="${ref}:" - remote=$(expr "z$ref" : 'z\([^:]*\):') - case "$remote" in - '' | HEAD ) remote=HEAD ;; - heads/*) remote=${remote#heads/} ;; - refs/heads/*) remote=${remote#refs/heads/} ;; - refs/* | tags/* | remotes/* ) remote= - esac - [ -n "$remote" ] && case "$repo" in - .) - echo "refs/heads/$remote" - ;; - *) - echo "refs/remotes/$repo/$remote" - ;; - esac - esac -} - -error_on_missing_default_upstream () { - cmd="$1" - op_type="$2" - op_prep="$3" # FIXME: op_prep is no longer used - example="$4" - branch_name=$(git symbolic-ref -q HEAD) - display_branch_name="${branch_name#refs/heads/}" - # If there's only one remote, use that in the suggestion - remote="$(gettext "")" - branch="$(gettext "")" - if test $(git remote | wc -l) = 1 - then - remote=$(git remote) - fi - - if test -z "$branch_name" - then - gettextln "You are not currently on a branch." - else - gettextln "There is no tracking information for the current branch." - fi - case "$op_type" in - rebase) - gettextln "Please specify which branch you want to rebase against." - ;; - merge) - gettextln "Please specify which branch you want to merge with." - ;; - *) - echo >&2 "BUG: unknown operation type: $op_type" - exit 1 - ;; - esac - eval_gettextln "See git-\${cmd}(1) for details." - echo - echo " $example" - echo - if test -n "$branch_name" - then - gettextln "If you wish to set tracking information for this branch you can do so with:" - echo - echo " git branch --set-upstream-to=$remote/$branch $display_branch_name" - echo - fi - exit 1 -} diff --git a/git-submodule.sh b/git-submodule.sh index d39a28215c..86ad60c05c 100755 --- a/git-submodule.sh +++ b/git-submodule.sh @@ -20,7 +20,6 @@ USAGE="[--quiet] [--cached] OPTIONS_SPEC= SUBDIRECTORY_OK=Yes . git-sh-setup -. git-parse-remote require_work_tree wt_prefix=$(git rev-parse --show-prefix) cd_to_toplevel -- cgit v1.2.3 From 66d36b94af6351d1e9b68a871d5faeb8a27d8a33 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Tue, 24 Nov 2020 04:06:05 -0500 Subject: submodule: fix fetch_in_submodule logic Commit 1c1518071c (submodule: use "fetch" logic instead of custom remote discovery, 2020-11-14) rewrote the logic in fetch_in_submodule to do: elif test "$2" -ne "" But this is nonsense in shell: -ne is for numeric comparisons. This should be "=" or more idiomatically: elif test -n "$2" But once we fix that, many tests start failing. Because that commit introduced another problem. The caller that passes 3 arguments looks like this: fetch_in_submodule "$sm_path" $depth "$sha1" Note the unquoted $depth parameter. When it isn't set, the function will see only 2 arguments, and the function has no idea if what it sees in $2 is an option to go on the command line, or a refspec to pass on stdin. In the old code before that commit: fetch_in_submodule () ( sanitize_submodule_env && cd "$1" && - case "$2" in - '') - git fetch ;; - *) - shift - git fetch $(get_default_remote) "$@" ;; - esac we treated those the same, so it didn't matter. But in the new logic (with my fix above): + if test $# -eq 3 + then + echo "$3" | git fetch --stdin "$2" + elif test -n "$n" + then + git fetch "$2" + else + git fetch + fi we use the number of parameters to distinguish the two. Let's insist that the caller pass an empty string for positional parameter two if they want to have a third parameter after it. But that still leaves one problem. In the --stdin block, we unconditionally pass "$2" to git-fetch, even if it's the empty string. Rather than add another conditional, we can use :+ parameter expansion to include it only if it's non-empty. In fact, we can do the same for the elif, too, simplifying it further. Technically this is overkill, since we know the --depth parameter will not have whitespace (and indeed, most callers do not bother quoting it), but it doesn't hurt for the function to be careful. It's somewhat amazing that no tests were failing. I think what happened is that: - the 3-arg form rarely triggered; any call with a non-empty $depth and a $sha1 would work, but one with an empty $depth would only have 2 arguments - because of the wrong arguments to "test", the shell would complain and exit non-zero. So we never ran the middle conditional at all - that left every call running "git fetch" with no arguments. A well-written test could have detected the distinction here, but in practice omitting --depth just means fetching more commits, and fetching everything (rather than a single sha1) works as long as the commit in question is reachable Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- git-submodule.sh | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/git-submodule.sh b/git-submodule.sh index 86ad60c05c..eb90f18229 100755 --- a/git-submodule.sh +++ b/git-submodule.sh @@ -412,17 +412,17 @@ is_tip_reachable () ( test -z "$rev" ) +# usage: fetch_in_submodule [] [] +# Because arguments are positional, use an empty string to omit +# but include . fetch_in_submodule () ( sanitize_submodule_env && cd "$1" && if test $# -eq 3 then - echo "$3" | git fetch --stdin "$2" - elif test "$2" -ne "" - then - git fetch "$2" + echo "$3" | git fetch --stdin ${2:+"$2"} else - git fetch + git fetch ${2:+"$2"} fi ) @@ -603,7 +603,7 @@ cmd_update() # Now we tried the usual fetch, but $sha1 may # not be reachable from any of the refs is_tip_reachable "$sm_path" "$sha1" || - fetch_in_submodule "$sm_path" $depth "$sha1" || + fetch_in_submodule "$sm_path" "$depth" "$sha1" || die "$(eval_gettext "Fetched in submodule path '\$displaypath', but it did not contain \$sha1. Direct fetching of that commit failed.")" fi -- cgit v1.2.3