I have recently observed a rather large number of users who forget to specify the base revision when they start a new branch with git-checkout -b. Many of these users are shocked many hours and commits later when their prior branch is now also part of the new branch. Nasty words about Git usually follow the discovery. This introduces a new config option: checkout.requireSourceBranch, which the user can set to make git-checkout -b require them to supply not only the new branch name but also the initial version for that branch. This prevents the command from assuming the user meant HEAD when they omitted an argument by accident. To keep behavior backwards compatible with any existing scripts this option is currently disabled by default, but it would be more friendly to new users if the option was enabled by default. Signed-off-by: Shawn O. Pearce <spearce@xxxxxxxxxxx> --- Documentation/config.txt | 6 ++++++ git-checkout.sh | 12 +++++++++++- t/t3200-branch.sh | 14 ++++++++++++++ 3 files changed, 31 insertions(+), 1 deletions(-) diff --git a/Documentation/config.txt b/Documentation/config.txt index 9090762..9d754c8 100644 --- a/Documentation/config.txt +++ b/Documentation/config.txt @@ -130,6 +130,12 @@ branch.<name>.merge:: When in branch <name>, it tells `git fetch` the default remote branch to be merged. +checkout.requireSourceBranch:: + If true tells git-checkout -b to require the user to + supply two arguments, rather than assuming HEAD should + be the source version if only one argument is supplied. + Default is false, to stay compatible with prior behavior. + pager.color:: A boolean to enable/disable colored output when the pager is in use (default is true). diff --git a/git-checkout.sh b/git-checkout.sh index 737abd0..5f9fb6e 100755 --- a/git-checkout.sh +++ b/git-checkout.sh @@ -137,7 +137,17 @@ then cd "$cdup" fi -[ -z "$new" ] && new=$old && new_name="$old_name" +# If we have no new name default to 'HEAD', unless we are +# making a new branch and the user told us not to assume. +if [ -z "$new" ]; then + if [ "$newbranch" ] && + [ Xtrue = "X`git-repo-config --bool checkout.requireSourceBranch`" ] + then + die "A source branch is required when creating a branch." + fi + new=$old + new_name="$old_name" +fi # If we don't have an old branch that we're switching to, # and we don't have a new branch name for the target we diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh index acb54b6..7e0c48b 100755 --- a/t/t3200-branch.sh +++ b/t/t3200-branch.sh @@ -70,4 +70,18 @@ test_expect_success \ git-branch -d l/m && git-branch l' +test_expect_success \ + 'git checkout -b M works if checkout.requireSourceBranch not set' \ + 'git-checkout -b M' + +test_expect_failure \ + 'git checkout -b N fails if checkout.requireSourceBranch is set' \ + 'git-repo-config checkout.requireSourceBranch true + git-checkout -b N' + +test_expect_success \ + 'git checkout -b N works if checkout.requireSourceBranch is false' \ + 'git-repo-config checkout.requireSourceBranch false + git-checkout -b N' + test_done -- 1.4.4.2.gb772 - To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html