From ca2cedba70e9356a1a20b0e39acd07ab92fee80e Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Fri, 24 Apr 2009 09:06:38 +1000 Subject: git-submodule: add support for --rebase. 'git submodule update --rebase' rebases your local branch on top of what would have been checked out to a detached HEAD otherwise. In some cases, detaching the HEAD when updating a submodule complicates the workflow to commit to this submodule (checkout master, rebase, then commit). For submodules that require frequent updates but infrequent (if any) commits, a rebase can be executed directly by the git-submodule command, ensuring that the submodules stay on their respective branches. git-config key: submodule.$name.rebase (bool) Signed-off-by: Peter Hutterer Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano diff --git a/Documentation/git-submodule.txt b/Documentation/git-submodule.txt index 3b8df44..0286409 100644 --- a/Documentation/git-submodule.txt +++ b/Documentation/git-submodule.txt @@ -12,7 +12,7 @@ SYNOPSIS 'git submodule' [--quiet] add [-b branch] [--] 'git submodule' [--quiet] status [--cached] [--] [...] 'git submodule' [--quiet] init [--] [...] -'git submodule' [--quiet] update [--init] [-N|--no-fetch] [--] [...] +'git submodule' [--quiet] update [--init] [-N|--no-fetch] [--rebase] [--] [...] 'git submodule' [--quiet] summary [--summary-limit ] [commit] [--] [...] 'git submodule' [--quiet] foreach 'git submodule' [--quiet] sync [--] [...] @@ -113,7 +113,8 @@ init:: update:: Update the registered submodules, i.e. clone missing submodules and checkout the commit specified in the index of the containing repository. - This will make the submodules HEAD be detached. + This will make the submodules HEAD be detached unless '--rebase' is + specified or the key `submodule.$name.rebase` is set to `true`. + If the submodule is not yet initialized, and you just want to use the setting as stored in .gitmodules, you can automatically initialize the @@ -177,6 +178,15 @@ OPTIONS This option is only valid for the update command. Don't fetch new objects from the remote site. +--rebase:: + This option is only valid for the update command. + Rebase the current branch onto the commit recorded in the + superproject. If this option is given, the submodule's HEAD will not + be detached. If a a merge failure prevents this process, you will have + to resolve these failures with linkgit:git-rebase[1]. + If the key `submodule.$name.rebase` is set to `true`, this option is + implicit. + ...:: Paths to submodule(s). When specified this will restrict the command to only operate on the submodules found at the specified paths. diff --git a/Documentation/gitmodules.txt b/Documentation/gitmodules.txt index d1a17e2..7c22c40 100644 --- a/Documentation/gitmodules.txt +++ b/Documentation/gitmodules.txt @@ -30,6 +30,9 @@ submodule..path:: submodule..url:: Defines an url from where the submodule repository can be cloned. +submodule..rebase:: + Defines that the submodule should be rebased by default. + EXAMPLES -------- diff --git a/git-submodule.sh b/git-submodule.sh index 8e234a4..3176226 100755 --- a/git-submodule.sh +++ b/git-submodule.sh @@ -17,6 +17,7 @@ branch= quiet= cached= nofetch= +rebase= # # print stuff on stdout unless -q was specified @@ -294,6 +295,11 @@ cmd_init() git config submodule."$name".url "$url" || die "Failed to register url for submodule path '$path'" + test true != "$(git config -f .gitmodules --bool \ + submodule."$name".rebase)" || + git config submodule."$name".rebase true || + die "Failed to register submodule path '$path' as rebasing" + say "Submodule '$name' ($url) registered for path '$path'" done } @@ -321,6 +327,10 @@ cmd_update() shift nofetch=1 ;; + -r|--rebase) + shift + rebase=true + ;; --) shift break @@ -339,6 +349,7 @@ cmd_update() do name=$(module_name "$path") || exit url=$(git config submodule."$name".url) + rebase_module=$(git config --bool submodule."$name".rebase) if test -z "$url" then # Only mention uninitialized submodules when its @@ -359,6 +370,11 @@ cmd_update() die "Unable to find current revision in submodule path '$path'" fi + if test true = "$rebase" + then + rebase_module=true + fi + if test "$subsha1" != "$sha1" then force= @@ -374,11 +390,20 @@ cmd_update() die "Unable to fetch in submodule path '$path'" fi - (unset GIT_DIR; cd "$path" && - git-checkout $force -q "$sha1") || - die "Unable to checkout '$sha1' in submodule path '$path'" + if test true = "$rebase_module" + then + command="git-rebase" + action="rebase" + msg="rebased onto" + else + command="git-checkout $force -q" + action="checkout" + msg="checked out" + fi - say "Submodule path '$path': checked out '$sha1'" + (unset GIT_DIR; cd "$path" && $command "$sha1") || + die "Unable to $action '$sha1' in submodule path '$path'" + say "Submodule path '$path': $msg '$sha1'" fi done } diff --git a/t/t7406-submodule-update.sh b/t/t7406-submodule-update.sh new file mode 100755 index 0000000..3442c05 --- /dev/null +++ b/t/t7406-submodule-update.sh @@ -0,0 +1,140 @@ +#!/bin/sh +# +# Copyright (c) 2009 Red Hat, Inc. +# + +test_description='Test updating submodules + +This test verifies that "git submodule update" detaches the HEAD of the +submodule and "git submodule update --rebase" does not detach the HEAD. +' + +. ./test-lib.sh + + +compare_head() +{ + sha_master=`git-rev-list --max-count=1 master` + sha_head=`git-rev-list --max-count=1 HEAD` + + test "$sha_master" = "$sha_head" +} + + +test_expect_success 'setup a submodule tree' ' + echo file > file && + git add file && + test_tick && + git commit -m upstream + git clone . super && + git clone super submodule && + (cd super && + git submodule add ../submodule submodule && + test_tick && + git commit -m "submodule" && + git submodule init submodule + ) && + (cd submodule && + echo "line2" > file && + git add file && + git commit -m "Commit 2" + ) && + (cd super && + (cd submodule && + git pull --rebase origin + ) && + git add submodule && + git commit -m "submodule update" + ) +' + +test_expect_success 'submodule update detaching the HEAD ' ' + (cd super/submodule && + git reset --hard HEAD~1 + ) && + (cd super && + (cd submodule && + compare_head + ) && + git submodule update submodule && + cd submodule && + ! compare_head + ) +' + +test_expect_success 'submodule update --rebase staying on master' ' + (cd super/submodule && + git checkout master + ) && + (cd super && + (cd submodule && + compare_head + ) && + git submodule update --rebase submodule && + cd submodule && + compare_head + ) +' + +test_expect_success 'submodule update - rebase true in .git/config' ' + (cd super && + git config submodule.submodule.rebase true + ) && + (cd super/submodule && + git reset --hard HEAD~1 + ) && + (cd super && + (cd submodule && + compare_head + ) && + git submodule update submodule && + cd submodule && + compare_head + ) +' + +test_expect_success 'submodule update - rebase false in .git/config but --rebase given' ' + (cd super && + git config submodule.submodule.rebase false + ) && + (cd super/submodule && + git reset --hard HEAD~1 + ) && + (cd super && + (cd submodule && + compare_head + ) && + git submodule update --rebase submodule && + cd submodule && + compare_head + ) +' + +test_expect_success 'submodule update - rebase false in .git/config' ' + (cd super && + git config submodule.submodule.rebase false + ) && + (cd super/submodule && + git reset --hard HEAD^ + ) && + (cd super && + (cd submodule && + compare_head + ) && + git submodule update submodule && + cd submodule && + ! compare_head + ) +' + +test_expect_success 'submodule init picks up rebase' ' + (cd super && + git config submodule.rebasing.url git://non-existing/git && + git config submodule.rebasing.path does-not-matter && + git config submodule.rebasing.rebase true && + git submodule init rebasing && + test true = $(git config --bool submodule.rebasing.rebase) + ) +' + +test_done -- cgit v0.10.2-6-g49f6 From 329484256e0fe42676e93669122e7a5a007ef4ed Mon Sep 17 00:00:00 2001 From: Johan Herland Date: Wed, 3 Jun 2009 08:27:06 +0200 Subject: Rename submodule..rebase to submodule..update The addition of "submodule..rebase" demonstrates the usefulness of alternatives to the default behaviour of "git submodule update". However, by naming the config variable "submodule..rebase", and making it a boolean choice, we are artificially constraining future git versions that may want to add _more_ alternatives than just "rebase". Therefore, while "submodule..rebase" is not yet in a stable git release, future-proof it, by changing it from submodule..rebase = true/false to submodule..update = rebase/checkout where "checkout" specifies the default behaviour of "git submodule update" (checking out the new commit to a detached HEAD), and "rebase" specifies the --rebase behaviour (where the current local branch in the submodule is rebase onto the new commit). Thus .update == checkout is equivalent to .rebase == false, and .update == rebase is equivalent to .rebase == true. Finally, leaving .update unset is equivalent to leaving .rebase unset. In future git versions, other alternatives to "git submodule update" behaviour can be included by adding them to the list of allowable values for the submodule..update variable. Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano diff --git a/Documentation/git-submodule.txt b/Documentation/git-submodule.txt index 0286409..f993469 100644 --- a/Documentation/git-submodule.txt +++ b/Documentation/git-submodule.txt @@ -114,7 +114,7 @@ update:: Update the registered submodules, i.e. clone missing submodules and checkout the commit specified in the index of the containing repository. This will make the submodules HEAD be detached unless '--rebase' is - specified or the key `submodule.$name.rebase` is set to `true`. + specified or the key `submodule.$name.update` is set to `rebase`. + If the submodule is not yet initialized, and you just want to use the setting as stored in .gitmodules, you can automatically initialize the @@ -184,7 +184,7 @@ OPTIONS superproject. If this option is given, the submodule's HEAD will not be detached. If a a merge failure prevents this process, you will have to resolve these failures with linkgit:git-rebase[1]. - If the key `submodule.$name.rebase` is set to `true`, this option is + If the key `submodule.$name.update` is set to `rebase`, this option is implicit. ...:: diff --git a/Documentation/gitmodules.txt b/Documentation/gitmodules.txt index 7c22c40..1b67f0a 100644 --- a/Documentation/gitmodules.txt +++ b/Documentation/gitmodules.txt @@ -30,8 +30,14 @@ submodule..path:: submodule..url:: Defines an url from where the submodule repository can be cloned. -submodule..rebase:: - Defines that the submodule should be rebased by default. +submodule..update:: + Defines what to do when the submodule is updated by the superproject. + If 'checkout' (the default), the new commit specified in the + superproject will be checked out in the submodule on a detached HEAD. + If 'rebase', the current branch of the submodule will be rebased onto + the commit specified in the superproject. + This config option is overridden if 'git submodule update' is given + the '--rebase' option. EXAMPLES diff --git a/git-submodule.sh b/git-submodule.sh index 3176226..f384c6b 100755 --- a/git-submodule.sh +++ b/git-submodule.sh @@ -17,7 +17,7 @@ branch= quiet= cached= nofetch= -rebase= +update= # # print stuff on stdout unless -q was specified @@ -295,10 +295,10 @@ cmd_init() git config submodule."$name".url "$url" || die "Failed to register url for submodule path '$path'" - test true != "$(git config -f .gitmodules --bool \ - submodule."$name".rebase)" || - git config submodule."$name".rebase true || - die "Failed to register submodule path '$path' as rebasing" + upd="$(git config -f .gitmodules submodule."$name".update)" + test -z "$upd" || + git config submodule."$name".update "$upd" || + die "Failed to register update mode for submodule path '$path'" say "Submodule '$name' ($url) registered for path '$path'" done @@ -329,7 +329,7 @@ cmd_update() ;; -r|--rebase) shift - rebase=true + update="rebase" ;; --) shift @@ -349,7 +349,7 @@ cmd_update() do name=$(module_name "$path") || exit url=$(git config submodule."$name".url) - rebase_module=$(git config --bool submodule."$name".rebase) + update_module=$(git config submodule."$name".update) if test -z "$url" then # Only mention uninitialized submodules when its @@ -370,9 +370,9 @@ cmd_update() die "Unable to find current revision in submodule path '$path'" fi - if test true = "$rebase" + if ! test -z "$update" then - rebase_module=true + update_module=$update fi if test "$subsha1" != "$sha1" @@ -390,16 +390,18 @@ cmd_update() die "Unable to fetch in submodule path '$path'" fi - if test true = "$rebase_module" - then - command="git-rebase" + case "$update_module" in + rebase) + command="git rebase" action="rebase" msg="rebased onto" - else - command="git-checkout $force -q" + ;; + *) + command="git checkout $force -q" action="checkout" msg="checked out" - fi + ;; + esac (unset GIT_DIR; cd "$path" && $command "$sha1") || die "Unable to $action '$sha1' in submodule path '$path'" diff --git a/t/t7406-submodule-update.sh b/t/t7406-submodule-update.sh index 3442c05..0773fe4 100755 --- a/t/t7406-submodule-update.sh +++ b/t/t7406-submodule-update.sh @@ -76,9 +76,9 @@ test_expect_success 'submodule update --rebase staying on master' ' ) ' -test_expect_success 'submodule update - rebase true in .git/config' ' +test_expect_success 'submodule update - rebase in .git/config' ' (cd super && - git config submodule.submodule.rebase true + git config submodule.submodule.update rebase ) && (cd super/submodule && git reset --hard HEAD~1 @@ -93,9 +93,9 @@ test_expect_success 'submodule update - rebase true in .git/config' ' ) ' -test_expect_success 'submodule update - rebase false in .git/config but --rebase given' ' +test_expect_success 'submodule update - checkout in .git/config but --rebase given' ' (cd super && - git config submodule.submodule.rebase false + git config submodule.submodule.update checkout ) && (cd super/submodule && git reset --hard HEAD~1 @@ -110,9 +110,9 @@ test_expect_success 'submodule update - rebase false in .git/config but --rebase ) ' -test_expect_success 'submodule update - rebase false in .git/config' ' +test_expect_success 'submodule update - checkout in .git/config' ' (cd super && - git config submodule.submodule.rebase false + git config submodule.submodule.update checkout ) && (cd super/submodule && git reset --hard HEAD^ @@ -131,9 +131,9 @@ test_expect_success 'submodule init picks up rebase' ' (cd super && git config submodule.rebasing.url git://non-existing/git && git config submodule.rebasing.path does-not-matter && - git config submodule.rebasing.rebase true && + git config submodule.rebasing.update rebase && git submodule init rebasing && - test true = $(git config --bool submodule.rebasing.rebase) + test "rebase" = $(git config submodule.rebasing.update) ) ' -- cgit v0.10.2-6-g49f6