Skip to content

Commit

Permalink
Merge branch 'dl/submodule-set-url'
Browse files Browse the repository at this point in the history
"git submodule" learned a subcommand "set-url".

* dl/submodule-set-url:
  submodule: teach set-url subcommand
  • Loading branch information
gitster committed Dec 10, 2019
2 parents 55d607d + 26b0610 commit 99c4ff1
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 2 deletions.
6 changes: 6 additions & 0 deletions Documentation/git-submodule.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ SYNOPSIS
'git submodule' [--quiet] deinit [-f|--force] (--all|[--] <path>...)
'git submodule' [--quiet] update [<options>] [--] [<path>...]
'git submodule' [--quiet] set-branch [<options>] [--] <path>
'git submodule' [--quiet] set-url [--] <path> <newurl>
'git submodule' [--quiet] summary [<options>] [--] [<path>...]
'git submodule' [--quiet] foreach [--recursive] <command>
'git submodule' [--quiet] sync [--recursive] [--] [<path>...]
Expand Down Expand Up @@ -184,6 +185,11 @@ set-branch (-d|--default) [--] <path>::
`--default` option removes the submodule.<name>.branch configuration
key, which causes the tracking branch to default to 'master'.

set-url [--] <path> <newurl>::
Sets the URL of the specified submodule to <newurl>. Then, it will
automatically synchronize the submodule's new remote URL
configuration.

summary [--cached|--files] [(-n|--summary-limit) <n>] [commit] [--] [<path>...]::
Show commit summary between the given commit (defaults to HEAD) and
working tree/index. For a submodule in question, a series of commits
Expand Down
2 changes: 1 addition & 1 deletion contrib/completion/git-completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -2783,7 +2783,7 @@ _git_submodule ()
{
__git_has_doubledash && return

local subcommands="add status init deinit update set-branch summary foreach sync absorbgitdirs"
local subcommands="add status init deinit update set-branch set-url summary foreach sync absorbgitdirs"
local subcommand="$(__git_find_on_cmdline "$subcommands")"
if [ -z "$subcommand" ]; then
case "$cur" in
Expand Down
52 changes: 51 additions & 1 deletion git-submodule.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ USAGE="[--quiet] [--cached]
or: $dashless [--quiet] deinit [-f|--force] (--all| [--] <path>...)
or: $dashless [--quiet] update [--init] [--remote] [-N|--no-fetch] [-f|--force] [--checkout|--merge|--rebase] [--[no-]recommend-shallow] [--reference <repository>] [--recursive] [--] [<path>...]
or: $dashless [--quiet] set-branch (--default|--branch <branch>) [--] <path>
or: $dashless [--quiet] set-url [--] <path> <newurl>
or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
or: $dashless [--quiet] foreach [--recursive] <command>
or: $dashless [--quiet] sync [--recursive] [--] [<path>...]
Expand Down Expand Up @@ -766,6 +767,55 @@ cmd_set_branch() {
fi
}

#
# Configures a submodule's remote url
#
# $@ = requested path, requested url
#
cmd_set_url() {
while test $# -ne 0
do
case "$1" in
-q|--quiet)
GIT_QUIET=1
;;
--)
shift
break
;;
-*)
usage
;;
*)
break
;;
esac
shift
done

if test $# -ne 2
then
usage
fi

# we can't use `git submodule--helper name` here because internally, it
# hashes the path so a trailing slash could lead to an unintentional no match
name="$(git submodule--helper list "$1" | cut -f2)"
if test -z "$name"
then
exit 1
fi

url="$2"
if test -z "$url"
then
exit 1
fi

git submodule--helper config submodule."$name".url "$url"
git submodule--helper sync ${GIT_QUIET:+--quiet} "$name"
}

#
# Show commit summary for submodules in index or working tree
#
Expand Down Expand Up @@ -1065,7 +1115,7 @@ cmd_absorbgitdirs()
while test $# != 0 && test -z "$command"
do
case "$1" in
add | foreach | init | deinit | update | set-branch | status | summary | sync | absorbgitdirs)
add | foreach | init | deinit | update | set-branch | set-url | status | summary | sync | absorbgitdirs)
command=$1
;;
-q|--quiet)
Expand Down
55 changes: 55 additions & 0 deletions t/t7420-submodule-set-url.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/bin/sh
#
# Copyright (c) 2019 Denton Liu
#

test_description='Test submodules set-url subcommand
This test verifies that the set-url subcommand of git-submodule is working
as expected.
'

TEST_NO_CREATE_REPO=1
. ./test-lib.sh

test_expect_success 'submodule config cache setup' '
mkdir submodule &&
(
cd submodule &&
git init &&
echo a >file &&
git add file &&
git commit -ma
) &&
mkdir super &&
(
cd super &&
git init &&
git submodule add ../submodule &&
git commit -m "add submodule"
)
'

test_expect_success 'test submodule set-url' '
# add a commit and move the submodule (change the url)
(
cd submodule &&
echo b >>file &&
git add file &&
git commit -mb
) &&
mv submodule newsubmodule &&
git -C newsubmodule show >expect &&
(
cd super &&
test_must_fail git submodule update --remote &&
git submodule set-url submodule ../newsubmodule &&
grep -F "url = ../newsubmodule" .gitmodules &&
git submodule update --remote
) &&
git -C super/submodule show >actual &&
test_cmp expect actual
'

test_done

0 comments on commit 99c4ff1

Please sign in to comment.