[PATCH] Added initialize and update support for submodule in git clone

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This patch adds support for initializing and updating submodules when
a repo is cloned. The advantage it adds is, the user actually does not
have to know whether it has a module or not and if it does to what
depth and path. For this I added a option -w or --with-submodule for
initializing and updating during clone stage.

I am working on project that has multiple maven modules and some of
the modules have separate GIT repo and needs to be linked with the
main project after maturity; we wanted that when developers clone the
main repo it should fetch all the submodules at once.

Following is the diff with git-clone 1.5.3.7; I also attached the diff
and modified file in the attachment. I would like to add that I am not
fully sure about unsetting the GIT_DIR, GIT_CONFIG and GIT_WORK_TREE
and resetting them and thus added them to the comment. If there is a
better way to achieve it I would gratefully incorporate it.

diff --git a/git-clone b/git-clone
index 204a769..63510c4 100755
--- a/git-clone
+++ b/git-clone
@@ -14,7 +14,7 @@ die() {
 }

 usage() {
-	die "Usage: $0 [--template=<template_directory>] [--reference
<reference-repo>] [--bare] [-l [-s]] [-q] [-u <upload-pack>] [--origin
<name>] [--depth <n>] [-n] <repo> [<dir>]"
+	die "Usage: $0 [--template=<template_directory>] [--reference
<reference-repo>] [--bare] [-l [-s]] [-q] [-w|--with-submodule] [-u
<upload-pack>] [--origin <name>] [--depth <n>] [-n] <repo> [<dir>]"
 }

 get_repo_base() {
@@ -90,6 +90,29 @@ Perhaps git-update-server-info needs to be run there?"
 	fi
 }

+initializeSubModule() {
+	if [ ! -d "$1"/.git ]; then
+		git-submodule-test init "$1"; git-submodule-test update "$1"
+	fi
+}
+
+initSubModules() {
+	current_dir=`pwd`
+	dir_path="$current_dir:$dir_path"
+	initializeSubModule "$1"
+        cd "$1"
+	if [ -f .gitmodules ]; then
+                for sub_mod_path in `grep "path =" .gitmodules | awk
'{print $3}'`; do
+			initSubModules "$sub_mod_path"
+                done
+        fi
+	old_dir=$(echo $dir_path | cut -d':' -f1-1)
+	length_old_dir=`expr "$old_dir" : '.*'`
+	cd $old_dir
+	index=$(echo "$length_old_dir+2" | bc)
+	dir_path=`echo $dir_path $index | awk '{print substr($1, $2)}'`
+}
+
 quiet=
 local=no
 use_local_hardlink=yes
@@ -106,6 +129,7 @@ depth=
 no_progress=
 local_explicitly_asked_for=
 test -t 1 || no_progress=--no-progress
+with_submodule=0
 while
 	case "$#,$1" in
 	0,*) break ;;
@@ -156,6 +180,7 @@ while
 		upload_pack="--upload-pack=$1" ;;
 	*,--upload-pack=*)
 		upload_pack=--upload-pack=$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
+	*,-w|*,--w|*,--wi|*,--wit|*,--with|*,--with-s|*,--with-su|*,--with-sub|*,--with-subm|*,--with-submo|*,--with-submod|*,--with-submodu|*,--with-submodul|*,--with-submodule)
with_submodule=1 ;;
 	1,--depth) usage;;
 	*,--depth)
 		shift
@@ -465,4 +490,38 @@ then
 fi
 rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"

+# The following section initializes the submodules of the repo
+if [ $with_submodule -eq 1  ]; then
+	if [ -f .gitmodules ]; then
+
+		# Need to know either what is the alternate
+		# or what else to backup and unset
+		# If this is not done in that case recursion
+		# in a subdirectory of the top level fails
+		# Thus the old values are backed up and restored
+		# after recursion
+		OLD_GIT_DIR="$GIT_DIR"
+		OLD_GIT_CONFIG="$GIT_CONFIG"
+		OLD_GIT_WORK_TREE="$GIT_WORK_TREE"
+		unset GIT_DIR
+		unset GIT_CONFIG
+		unset GIT_WORK_TREE
+
+		# The following loop iterates over the submodules
+		# visible from he top of the module
+	        for mod_path in `grep "path =" .gitmodules | awk '{print $3}'`; do
+                        initSubModules "$mod_path"
+                done
+
+		GIT_DIR="$OLD_GIT_DIR"
+		GIT_CONFIG="$OLD_GIT_CONFIG"
+		GIT_WORK_TREE="$OLD_GIT_WORK_TREE"
+
+		# The following export is probably not required
+		export GIT_DIR
+		export GIT_CONFIG
+		export GIT_WORK_TREE
+	fi
+fi
+
 trap - 0

-- 
Imran M Yousuf

Attachment: git-clone
Description: Binary data

diff --git a/git-clone b/git-clone
index 204a769..63510c4 100755
--- a/git-clone
+++ b/git-clone
@@ -14,7 +14,7 @@ die() {
 }
 
 usage() {
-	die "Usage: $0 [--template=<template_directory>] [--reference <reference-repo>] [--bare] [-l [-s]] [-q] [-u <upload-pack>] [--origin <name>] [--depth <n>] [-n] <repo> [<dir>]"
+	die "Usage: $0 [--template=<template_directory>] [--reference <reference-repo>] [--bare] [-l [-s]] [-q] [-w|--with-submodule] [-u <upload-pack>] [--origin <name>] [--depth <n>] [-n] <repo> [<dir>]"
 }
 
 get_repo_base() {
@@ -90,6 +90,29 @@ Perhaps git-update-server-info needs to be run there?"
 	fi
 }
 
+initializeSubModule() {
+	if [ ! -d "$1"/.git ]; then
+		git-submodule-test init "$1"; git-submodule-test update "$1"
+	fi
+}
+
+initSubModules() {
+	current_dir=`pwd`
+	dir_path="$current_dir:$dir_path"
+	initializeSubModule "$1"
+        cd "$1"
+	if [ -f .gitmodules ]; then
+                for sub_mod_path in `grep "path =" .gitmodules | awk '{print $3}'`; do
+			initSubModules "$sub_mod_path"
+                done
+        fi
+	old_dir=$(echo $dir_path | cut -d':' -f1-1)
+	length_old_dir=`expr "$old_dir" : '.*'`
+	cd $old_dir
+	index=$(echo "$length_old_dir+2" | bc)
+	dir_path=`echo $dir_path $index | awk '{print substr($1, $2)}'`
+}
+
 quiet=
 local=no
 use_local_hardlink=yes
@@ -106,6 +129,7 @@ depth=
 no_progress=
 local_explicitly_asked_for=
 test -t 1 || no_progress=--no-progress
+with_submodule=0
 while
 	case "$#,$1" in
 	0,*) break ;;
@@ -156,6 +180,7 @@ while
 		upload_pack="--upload-pack=$1" ;;
 	*,--upload-pack=*)
 		upload_pack=--upload-pack=$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
+	*,-w|*,--w|*,--wi|*,--wit|*,--with|*,--with-s|*,--with-su|*,--with-sub|*,--with-subm|*,--with-submo|*,--with-submod|*,--with-submodu|*,--with-submodul|*,--with-submodule) with_submodule=1 ;;
 	1,--depth) usage;;
 	*,--depth)
 		shift
@@ -465,4 +490,38 @@ then
 fi
 rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"
 
+# The following section initializes the submodules of the repo
+if [ $with_submodule -eq 1  ]; then
+	if [ -f .gitmodules ]; then
+
+		# Need to know either what is the alternate 
+		# or what else to backup and unset
+		# If this is not done in that case recursion 
+		# in a subdirectory of the top level fails
+		# Thus the old values are backed up and restored
+		# after recursion
+		OLD_GIT_DIR="$GIT_DIR"
+		OLD_GIT_CONFIG="$GIT_CONFIG"
+		OLD_GIT_WORK_TREE="$GIT_WORK_TREE"
+		unset GIT_DIR
+		unset GIT_CONFIG
+		unset GIT_WORK_TREE
+
+		# The following loop iterates over the submodules
+		# visible from he top of the module
+	        for mod_path in `grep "path =" .gitmodules | awk '{print $3}'`; do
+                        initSubModules "$mod_path"
+                done
+
+		GIT_DIR="$OLD_GIT_DIR"
+		GIT_CONFIG="$OLD_GIT_CONFIG"
+		GIT_WORK_TREE="$OLD_GIT_WORK_TREE"
+
+		# The following export is probably not required
+		export GIT_DIR
+		export GIT_CONFIG
+		export GIT_WORK_TREE
+	fi
+fi
+
 trap - 0

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux