@matheusbsbs / matheus-bagliano / commits / 9db31bdf5c
Fork 0

submodule: Add --force option for git submodule update

By default git submodule update runs a simple checkout on submodules that are not up-to-date. If the submodules contains modified or untracked files, the command may exit sanely with an error: $ git submodule update error: Your local changes to the following files would be overwritten by checkout: file Please, commit your changes or stash them before you can switch branches. Aborting Unable to checkout '1b69c6e55606b48d3284a3a9efe4b58bfb7e8c9e' in submodule path 'test1' In order to reset a whole git submodule tree, a user has to run first 'git submodule foreach --recursive git checkout -f' and then run 'git submodule update'. This patch adds a --force option for the update command (only used for submodules without --rebase or --merge options). It passes the --force option to git checkout which will throw away the local changes. Signed-off-by: Nicolas Morey-Chaisemartin <nmorey@kalray.eu> Acked-by: Jens Lehmann <Jens.Lehmann@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nicolas Morey-Chaisemartin committed Apr 1, 2011 at 11:42 UTC 9db31bdf5c834689c826f809d6d15bf7ab8d3598
3 files changed +36 -7
Documentation/git-submodule.txt
+4 -2
@@ -185,8 +185,10 @@ OPTIONS
185
186 -f::
187 --force::
188 - This option is only valid for the add command.
189 - Allow adding an otherwise ignored submodule path.
188 + This option is only valid for add and update commands.
189 + When running add, allow adding an otherwise ignored submodule path.
190 + When running update, throw away local changes in submodules when
191 + switching to a different commit.
192
193 --cached::
194 This option is only valid for status and summary commands. These
git-submodule.sh
+9 -5
@@ -8,7 +8,7 @@ dashless=$(basename "$0" | sed -e 's/-/ /')
8 USAGE="[--quiet] add [-b branch] [-f|--force] [--reference <repository>] [--] <repository> [<path>]
9 or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]
10 or: $dashless [--quiet] init [--] [<path>...]
11 - or: $dashless [--quiet] update [--init] [-N|--no-fetch] [--rebase] [--reference <repository>] [--merge] [--recursive] [--] [<path>...]
11 + or: $dashless [--quiet] update [--init] [-N|--no-fetch] [-f|--force] [--rebase] [--reference <repository>] [--merge] [--recursive] [--] [<path>...]
12 or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
13 or: $dashless [--quiet] foreach [--recursive] <command>
14 or: $dashless [--quiet] sync [--] [<path>...]"
@@ -385,6 +385,9 @@ cmd_update()
385 -N|--no-fetch)
386 nofetch=1
387 ;;
388 + -f|--force)
389 + force=$1
390 + ;;
391 -r|--rebase)
392 update="rebase"
393 ;;
@@ -458,10 +461,11 @@ cmd_update()
461
462 if test "$subsha1" != "$sha1"
463 then
461 - force=
462 - if test -z "$subsha1"
464 + subforce=$force
465 + # If we don't already have a -f flag and the submodule has never been checked out
466 + if test -z "$subsha1" -a -z "$force"
467 then
464 - force="-f"
468 + subforce="-f"
469 fi
470
471 if test -z "$nofetch"
@@ -490,7 +494,7 @@ cmd_update()
494 msg="merged in"
495 ;;
496 *)
493 - command="git checkout $force -q"
497 + command="git checkout $subforce -q"
498 action="checkout"
499 msg="checked out"
500 ;;
t/t7406-submodule-update.sh
+23
@@ -74,6 +74,29 @@ test_expect_success 'submodule update detaching the HEAD ' '
74 )
75 '
76
77 +test_expect_success 'submodule update should fail due to local changes' '
78 + (cd super/submodule &&
79 + git reset --hard HEAD~1 &&
80 + echo "local change" > file
81 + ) &&
82 + (cd super &&
83 + (cd submodule &&
84 + compare_head
85 + ) &&
86 + test_must_fail git submodule update submodule
87 + )
88 +'
89 +test_expect_success 'submodule update should throw away changes with --force ' '
90 + (cd super &&
91 + (cd submodule &&
92 + compare_head
93 + ) &&
94 + git submodule update --force submodule &&
95 + cd submodule &&
96 + ! compare_head
97 + )
98 +'
99 +
100 test_expect_success 'submodule update --rebase staying on master' '
101 (cd super/submodule &&
102 git checkout master