1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-05-13 14:36:08 +02:00
git/t/t3207-branch-submodule.sh
Glen Choo 961b130d20 branch: add --recurse-submodules option for branch creation
To improve the submodules UX, we would like to teach Git to handle
branches in submodules. Start this process by teaching "git branch" the
--recurse-submodules option so that "git branch --recurse-submodules
topic" will create the `topic` branch in the superproject and its
submodules.

Although this commit does not introduce breaking changes, it does not
work well with existing --recurse-submodules commands because "git
branch --recurse-submodules" writes to the submodule ref store, but most
commands only consider the superproject gitlink and ignore the submodule
ref store. For example, "git checkout --recurse-submodules" will check
out the commits in the superproject gitlinks (and put the submodules in
detached HEAD) instead of checking out the submodule branches.

Because of this, this commit introduces a new configuration value,
`submodule.propagateBranches`. The plan is for Git commands to
prioritize submodule ref store information over superproject gitlinks if
this value is true. Because "git branch --recurse-submodules" writes to
submodule ref stores, for the sake of clarity, it will not function
unless this configuration value is set.

This commit also includes changes that support working with submodules
from a superproject commit because "branch --recurse-submodules" (and
future commands) need to read .gitmodules and gitlinks from the
superproject commit, but submodules are typically read from the
filesystem's .gitmodules and the index's gitlinks. These changes are:

* add a submodules_of_tree() helper that gives the relevant
  information of an in-tree submodule (e.g. path and oid) and
  initializes the repository
* add is_tree_submodule_active() by adding a treeish_name parameter to
  is_submodule_active()
* add the "submoduleNotUpdated" advice to advise users to update the
  submodules in their trees

Incidentally, fix an incorrect usage string that combined the 'list'
usage of git branch (-l) with the 'create' usage; this string has been
incorrect since its inception, a8dfd5eac4 (Make builtin-branch.c use
parse_options., 2007-10-07).

Helped-by: Jonathan Tan <jonathantanmy@google.com>
Signed-off-by: Glen Choo <chooglen@google.com>
Reviewed-by: Jonathan Tan <jonathantanmy@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2022-02-04 08:16:39 -08:00

293 lines
9.2 KiB
Bash
Executable File

#!/bin/sh
test_description='git branch submodule tests'
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
. ./test-lib.sh
. "$TEST_DIRECTORY"/lib-rebase.sh
pwd=$(pwd)
# Creates a clean test environment in "pwd" by copying the repo setup
# from test_dirs.
reset_test () {
rm -fr super &&
rm -fr sub-sub-upstream &&
rm -fr sub-upstream &&
cp -r test_dirs/* .
}
# Tests that the expected branch does not exist
test_no_branch () {
DIR=$1 &&
BRANCH_NAME=$2 &&
test_must_fail git -C "$DIR" rev-parse "$BRANCH_NAME" 2>err &&
grep "ambiguous argument .$BRANCH_NAME." err
}
test_expect_success 'setup superproject and submodule' '
mkdir test_dirs &&
(
cd test_dirs &&
git init super &&
test_commit -C super foo &&
git init sub-sub-upstream &&
test_commit -C sub-sub-upstream foo &&
git init sub-upstream &&
# Submodule in a submodule
git -C sub-upstream submodule add "${pwd}/test_dirs/sub-sub-upstream" sub-sub &&
git -C sub-upstream commit -m "add submodule" &&
# Regular submodule
git -C super submodule add "${pwd}/test_dirs/sub-upstream" sub &&
# Submodule in a subdirectory
git -C super submodule add "${pwd}/test_dirs/sub-sub-upstream" second/sub &&
git -C super commit -m "add submodule" &&
git -C super config submodule.propagateBranches true &&
git -C super/sub submodule update --init
) &&
reset_test
'
# Test the argument parsing
test_expect_success '--recurse-submodules should create branches' '
test_when_finished "reset_test" &&
(
cd super &&
git branch --recurse-submodules branch-a &&
git rev-parse branch-a &&
git -C sub rev-parse branch-a &&
git -C sub/sub-sub rev-parse branch-a &&
git -C second/sub rev-parse branch-a
)
'
test_expect_success '--recurse-submodules should die if submodule.propagateBranches is false' '
test_when_finished "reset_test" &&
(
cd super &&
echo "fatal: branch with --recurse-submodules can only be used if submodule.propagateBranches is enabled" >expected &&
test_must_fail git -c submodule.propagateBranches=false branch --recurse-submodules branch-a 2>actual &&
test_cmp expected actual
)
'
test_expect_success '--recurse-submodules should fail when not creating branches' '
test_when_finished "reset_test" &&
(
cd super &&
git branch --recurse-submodules branch-a &&
echo "fatal: --recurse-submodules can only be used to create branches" >expected &&
test_must_fail git branch --recurse-submodules -D branch-a 2>actual &&
test_cmp expected actual &&
# Assert that the branches were not deleted
git rev-parse branch-a &&
git -C sub rev-parse branch-a
)
'
test_expect_success 'should respect submodule.recurse when creating branches' '
test_when_finished "reset_test" &&
(
cd super &&
git -c submodule.recurse=true branch branch-a &&
git rev-parse branch-a &&
git -C sub rev-parse branch-a
)
'
test_expect_success 'should ignore submodule.recurse when not creating branches' '
test_when_finished "reset_test" &&
(
cd super &&
git branch --recurse-submodules branch-a &&
git -c submodule.recurse=true branch -D branch-a &&
test_no_branch . branch-a &&
git -C sub rev-parse branch-a
)
'
# Test branch creation behavior
test_expect_success 'should create branches based off commit id in superproject' '
test_when_finished "reset_test" &&
(
cd super &&
git branch --recurse-submodules branch-a &&
git checkout --recurse-submodules branch-a &&
git -C sub rev-parse HEAD >expected &&
# Move the tip of sub:branch-a so that it no longer matches the commit in super:branch-a
git -C sub checkout branch-a &&
test_commit -C sub bar &&
# Create a new branch-b branch with start-point=branch-a
git branch --recurse-submodules branch-b branch-a &&
git rev-parse branch-b &&
git -C sub rev-parse branch-b >actual &&
# Assert that the commit id of sub:second-branch matches super:branch-a and not sub:branch-a
test_cmp expected actual
)
'
test_expect_success 'should not create any branches if branch is not valid for all repos' '
test_when_finished "reset_test" &&
(
cd super &&
git -C sub branch branch-a &&
test_must_fail git branch --recurse-submodules branch-a 2>actual &&
test_no_branch . branch-a &&
grep "submodule .sub.: fatal: A branch named .branch-a. already exists" actual
)
'
test_expect_success 'should create branches if branch exists and --force is given' '
test_when_finished "reset_test" &&
(
cd super &&
git -C sub rev-parse HEAD >expected &&
test_commit -C sub baz &&
# branch-a in sub now points to a newer commit.
git -C sub branch branch-a HEAD &&
git -C sub rev-parse branch-a >actual-old-branch-a &&
git branch --recurse-submodules --force branch-a &&
git rev-parse branch-a &&
git -C sub rev-parse branch-a >actual-new-branch-a &&
test_cmp expected actual-new-branch-a &&
# assert that branch --force actually moved the sub
# branch
! test_cmp expected actual-old-branch-a
)
'
test_expect_success 'should create branch when submodule is not in HEAD:.gitmodules' '
test_when_finished "reset_test" &&
(
cd super &&
git branch branch-a &&
git checkout -b branch-b &&
git submodule add ../sub-upstream sub2 &&
git -C sub2 submodule update --init &&
# branch-b now has a committed submodule not in branch-a
git commit -m "add second submodule" &&
git checkout branch-a &&
git branch --recurse-submodules branch-c branch-b &&
git checkout --recurse-submodules branch-c &&
git -C sub2 rev-parse branch-c &&
git -C sub2/sub-sub rev-parse branch-c
)
'
test_expect_success 'should not create branches in inactive submodules' '
test_when_finished "reset_test" &&
test_config -C super submodule.sub.active false &&
(
cd super &&
git branch --recurse-submodules branch-a &&
git rev-parse branch-a &&
test_no_branch sub branch-a
)
'
test_expect_success 'should set up tracking of local branches with track=always' '
test_when_finished "reset_test" &&
(
cd super &&
git -c branch.autoSetupMerge=always branch --recurse-submodules branch-a main &&
git -C sub rev-parse main &&
test_cmp_config -C sub . branch.branch-a.remote &&
test_cmp_config -C sub refs/heads/main branch.branch-a.merge
)
'
test_expect_success 'should set up tracking of local branches with explicit track' '
test_when_finished "reset_test" &&
(
cd super &&
git branch --track --recurse-submodules branch-a main &&
git -C sub rev-parse main &&
test_cmp_config -C sub . branch.branch-a.remote &&
test_cmp_config -C sub refs/heads/main branch.branch-a.merge
)
'
test_expect_success 'should not set up unnecessary tracking of local branches' '
test_when_finished "reset_test" &&
(
cd super &&
git branch --recurse-submodules branch-a main &&
git -C sub rev-parse main &&
test_cmp_config -C sub "" --default "" branch.branch-a.remote &&
test_cmp_config -C sub "" --default "" branch.branch-a.merge
)
'
reset_remote_test () {
rm -fr super-clone &&
reset_test
}
test_expect_success 'setup tests with remotes' '
(
cd test_dirs &&
(
cd super &&
git branch branch-a &&
git checkout -b branch-b &&
git submodule add ../sub-upstream sub2 &&
# branch-b now has a committed submodule not in branch-a
git commit -m "add second submodule"
) &&
git clone --branch main --recurse-submodules super super-clone &&
git -C super-clone config submodule.propagateBranches true
) &&
reset_remote_test
'
test_expect_success 'should get fatal error upon branch creation when submodule is not in .git/modules' '
test_when_finished "reset_remote_test" &&
(
cd super-clone &&
# This should succeed because super-clone has sub in .git/modules
git branch --recurse-submodules branch-a origin/branch-a &&
# This should fail because super-clone does not have sub2 .git/modules
test_must_fail git branch --recurse-submodules branch-b origin/branch-b 2>actual &&
grep "fatal: submodule .sub2.: unable to find submodule" actual &&
test_no_branch . branch-b &&
test_no_branch sub branch-b &&
# User can fix themselves by initializing the submodule
git checkout origin/branch-b &&
git submodule update --init --recursive &&
git branch --recurse-submodules branch-b origin/branch-b
)
'
test_expect_success 'should set up tracking of remote-tracking branches' '
test_when_finished "reset_remote_test" &&
(
cd super-clone &&
git branch --recurse-submodules branch-a origin/branch-a &&
test_cmp_config origin branch.branch-a.remote &&
test_cmp_config refs/heads/branch-a branch.branch-a.merge &&
# "origin/branch-a" does not exist for "sub", but it matches the refspec
# so tracking should be set up
test_cmp_config -C sub origin branch.branch-a.remote &&
test_cmp_config -C sub refs/heads/branch-a branch.branch-a.merge &&
test_cmp_config -C sub/sub-sub origin branch.branch-a.remote &&
test_cmp_config -C sub/sub-sub refs/heads/branch-a branch.branch-a.merge
)
'
test_expect_success 'should not fail when unable to set up tracking in submodule' '
test_when_finished "reset_remote_test" &&
(
cd super-clone &&
git remote rename origin ex-origin &&
git branch --recurse-submodules branch-a ex-origin/branch-a &&
test_cmp_config ex-origin branch.branch-a.remote &&
test_cmp_config refs/heads/branch-a branch.branch-a.merge &&
test_cmp_config -C sub "" --default "" branch.branch-a.remote &&
test_cmp_config -C sub "" --default "" branch.branch-a.merge
)
'
test_done