Skip to content

Commit 99c4ff1

Browse files
committed
Merge branch 'dl/submodule-set-url'
"git submodule" learned a subcommand "set-url". * dl/submodule-set-url: submodule: teach set-url subcommand
2 parents 55d607d + 26b0610 commit 99c4ff1

File tree

4 files changed

+113
-2
lines changed

4 files changed

+113
-2
lines changed

Documentation/git-submodule.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ SYNOPSIS
1616
'git submodule' [--quiet] deinit [-f|--force] (--all|[--] <path>...)
1717
'git submodule' [--quiet] update [<options>] [--] [<path>...]
1818
'git submodule' [--quiet] set-branch [<options>] [--] <path>
19+
'git submodule' [--quiet] set-url [--] <path> <newurl>
1920
'git submodule' [--quiet] summary [<options>] [--] [<path>...]
2021
'git submodule' [--quiet] foreach [--recursive] <command>
2122
'git submodule' [--quiet] sync [--recursive] [--] [<path>...]
@@ -184,6 +185,11 @@ set-branch (-d|--default) [--] <path>::
184185
`--default` option removes the submodule.<name>.branch configuration
185186
key, which causes the tracking branch to default to 'master'.
186187

188+
set-url [--] <path> <newurl>::
189+
Sets the URL of the specified submodule to <newurl>. Then, it will
190+
automatically synchronize the submodule's new remote URL
191+
configuration.
192+
187193
summary [--cached|--files] [(-n|--summary-limit) <n>] [commit] [--] [<path>...]::
188194
Show commit summary between the given commit (defaults to HEAD) and
189195
working tree/index. For a submodule in question, a series of commits

contrib/completion/git-completion.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2783,7 +2783,7 @@ _git_submodule ()
27832783
{
27842784
__git_has_doubledash && return
27852785

2786-
local subcommands="add status init deinit update set-branch summary foreach sync absorbgitdirs"
2786+
local subcommands="add status init deinit update set-branch set-url summary foreach sync absorbgitdirs"
27872787
local subcommand="$(__git_find_on_cmdline "$subcommands")"
27882788
if [ -z "$subcommand" ]; then
27892789
case "$cur" in

git-submodule.sh

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ USAGE="[--quiet] [--cached]
1212
or: $dashless [--quiet] deinit [-f|--force] (--all| [--] <path>...)
1313
or: $dashless [--quiet] update [--init] [--remote] [-N|--no-fetch] [-f|--force] [--checkout|--merge|--rebase] [--[no-]recommend-shallow] [--reference <repository>] [--recursive] [--] [<path>...]
1414
or: $dashless [--quiet] set-branch (--default|--branch <branch>) [--] <path>
15+
or: $dashless [--quiet] set-url [--] <path> <newurl>
1516
or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
1617
or: $dashless [--quiet] foreach [--recursive] <command>
1718
or: $dashless [--quiet] sync [--recursive] [--] [<path>...]
@@ -766,6 +767,55 @@ cmd_set_branch() {
766767
fi
767768
}
768769

770+
#
771+
# Configures a submodule's remote url
772+
#
773+
# $@ = requested path, requested url
774+
#
775+
cmd_set_url() {
776+
while test $# -ne 0
777+
do
778+
case "$1" in
779+
-q|--quiet)
780+
GIT_QUIET=1
781+
;;
782+
--)
783+
shift
784+
break
785+
;;
786+
-*)
787+
usage
788+
;;
789+
*)
790+
break
791+
;;
792+
esac
793+
shift
794+
done
795+
796+
if test $# -ne 2
797+
then
798+
usage
799+
fi
800+
801+
# we can't use `git submodule--helper name` here because internally, it
802+
# hashes the path so a trailing slash could lead to an unintentional no match
803+
name="$(git submodule--helper list "$1" | cut -f2)"
804+
if test -z "$name"
805+
then
806+
exit 1
807+
fi
808+
809+
url="$2"
810+
if test -z "$url"
811+
then
812+
exit 1
813+
fi
814+
815+
git submodule--helper config submodule."$name".url "$url"
816+
git submodule--helper sync ${GIT_QUIET:+--quiet} "$name"
817+
}
818+
769819
#
770820
# Show commit summary for submodules in index or working tree
771821
#
@@ -1065,7 +1115,7 @@ cmd_absorbgitdirs()
10651115
while test $# != 0 && test -z "$command"
10661116
do
10671117
case "$1" in
1068-
add | foreach | init | deinit | update | set-branch | status | summary | sync | absorbgitdirs)
1118+
add | foreach | init | deinit | update | set-branch | set-url | status | summary | sync | absorbgitdirs)
10691119
command=$1
10701120
;;
10711121
-q|--quiet)

t/t7420-submodule-set-url.sh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/bin/sh
2+
#
3+
# Copyright (c) 2019 Denton Liu
4+
#
5+
6+
test_description='Test submodules set-url subcommand
7+
8+
This test verifies that the set-url subcommand of git-submodule is working
9+
as expected.
10+
'
11+
12+
TEST_NO_CREATE_REPO=1
13+
. ./test-lib.sh
14+
15+
test_expect_success 'submodule config cache setup' '
16+
mkdir submodule &&
17+
(
18+
cd submodule &&
19+
git init &&
20+
echo a >file &&
21+
git add file &&
22+
git commit -ma
23+
) &&
24+
mkdir super &&
25+
(
26+
cd super &&
27+
git init &&
28+
git submodule add ../submodule &&
29+
git commit -m "add submodule"
30+
)
31+
'
32+
33+
test_expect_success 'test submodule set-url' '
34+
# add a commit and move the submodule (change the url)
35+
(
36+
cd submodule &&
37+
echo b >>file &&
38+
git add file &&
39+
git commit -mb
40+
) &&
41+
mv submodule newsubmodule &&
42+
43+
git -C newsubmodule show >expect &&
44+
(
45+
cd super &&
46+
test_must_fail git submodule update --remote &&
47+
git submodule set-url submodule ../newsubmodule &&
48+
grep -F "url = ../newsubmodule" .gitmodules &&
49+
git submodule update --remote
50+
) &&
51+
git -C super/submodule show >actual &&
52+
test_cmp expect actual
53+
'
54+
55+
test_done

0 commit comments

Comments
 (0)