git-submodule init: Record submodule.<name>.branch in repository config.
[git.git] / git-submodule.sh
index 3e2045e52daf43cc351af23af0fd770fb1a5044f..c51b6aeb18ab701408b14c13acd7c7d9995c2fa0 100755 (executable)
@@ -5,7 +5,7 @@
 # Copyright (c) 2007 Lars Hjemli
 
 dashless=$(basename "$0" | sed -e 's/-/ /')
-USAGE="[--quiet] add [-b branch] [-f|--force] [--reference <repository>] [--] <repository> [<path>]
+USAGE="[--quiet] add [-b branch] [--local-branch[=<branch>]] [-f|--force] [--reference <repository>] [--] <repository> [<path>]
    or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]
    or: $dashless [--quiet] init [--] [<path>...]
    or: $dashless [--quiet] update [--init] [-N|--no-fetch] [-f|--force] [--rebase] [--reference <repository>] [--merge] [--recursive] [--] [<path>...]
@@ -20,6 +20,8 @@ require_work_tree
 
 command=
 branch=
+local_branch=
+local_branch_empty=
 force=
 reference=
 cached=
@@ -257,6 +259,12 @@ cmd_add()
                        branch=$2
                        shift
                        ;;
+               --local-branch)
+                       local_branch_empty=true
+                       ;;
+               --local-branch=*)
+                       local_branch="${1#*=}"
+                       ;;
                -f | --force)
                        force=$1
                        ;;
@@ -328,6 +336,11 @@ cmd_add()
        git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 &&
        die "$(eval_gettext "'\$sm_path' already exists in the index")"
 
+       if test -z "$local_branch" && test "$local_branch_empty" = "true"
+       then
+               local_branch="${branch:=HEAD}"
+       fi
+
        if test -z "$force" && ! git add --dry-run --ignore-missing "$sm_path" > /dev/null 2>&1
        then
                eval_gettextln "The following path is ignored by one of your .gitignore files:
@@ -366,6 +379,10 @@ Use -f if you really want to add it." >&2
 
        git config -f .gitmodules submodule."$sm_path".path "$sm_path" &&
        git config -f .gitmodules submodule."$sm_path".url "$repo" &&
+       if test -n "$local_branch"
+       then
+               git config -f .gitmodules submodule."$sm_path".branch "$local_branch"
+       fi &&
        git add --force .gitmodules ||
        die "$(eval_gettext "Failed to register submodule '\$sm_path'")"
 }
@@ -488,6 +505,13 @@ cmd_init()
                test -n "$(git config submodule."$name".update)" ||
                git config submodule."$name".update "$upd" ||
                die "$(eval_gettext "Failed to register update mode for submodule path '\$sm_path'")"
+
+               # Copy "branch" setting when it is not set yet
+               branch="$(git config -f .gitmodules submodule."$name".branch)"
+               test -z "$branch" ||
+               test -n "$(git config submodule."$name".branch)" ||
+               git config submodule."$name".branch "$branch" ||
+               die "$(eval_gettext "Failed to register branch for submodule path '\$sm_path'")"
        done
 }
 
@@ -1107,7 +1131,15 @@ do
 done
 
 # No command word defaults to "status"
-test -n "$command" || command=status
+if test -z "$command"
+then
+    if test $# = 0
+    then
+       command=status
+    else
+       usage
+    fi
+fi
 
 # "-b branch" is accepted only by "add"
 if test -n "$branch" && test "$command" != add