@matheusbsbs / matheus-bagliano / commits / d4264ca323
Fork 0

git-submodule - Allow adding a submodule in-place

When working in the top-level project, it is useful to create a new submodule as a git repo in a subdirectory, then add that submodule to the top-level in place. This patch allows "git submodule add <intended url> subdir" to add the existing subdir to the current project. The presumption is the user will later push / clone the subdir to the <intended url> so that future submodule init / updates will work. Absent this patch, "git submodule add" insists upon cloning the subdir from a repository at the given url, which is fine for adding an existing project in, but less useful when adding a new submodule from scratch to an existing project. The former functionality remains, and the clone is attempted if the subdir does not already exist as a valid git repo. Signed-off-by: Mark Levedahl <mlevedahl@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Mark Levedahl committed Mar 4, 2008 at 20:15 UTC d4264ca3233e206ea927bc94befda530683b5836
2 files changed +33 -22
Documentation/git-submodule.txt
+3 -2
@@ -18,8 +18,9 @@ COMMANDS
18 --------
19 add::
20 Add the given repository as a submodule at the given path
21 - to the changeset to be committed next. In particular, the
22 - repository is cloned at the specified path, added to the
21 + to the changeset to be committed next. If path is a valid
22 + repository within the project, it is added as is. Otherwise,
23 + repository is cloned at the specified path. path is added to the
24 changeset and registered in .gitmodules. If no path is
25 specified, the path is deduced from the repository specification.
26 If the repository url begins with ./ or ../, it is stored as
git-submodule.sh
+30 -20
@@ -153,20 +153,6 @@ cmd_add()
153 usage
154 fi
155
156 - case "$repo" in
157 - ./*|../*)
158 - # dereference source url relative to parent's url
159 - realrepo="$(resolve_relative_url $repo)" ;;
160 - *)
161 - # Turn the source into an absolute path if
162 - # it is local
163 - if base=$(get_repo_base "$repo"); then
164 - repo="$base"
165 - fi
166 - realrepo=$repo
167 - ;;
168 - esac
169 -
156 # Guess path from repo if not specified or strip trailing slashes
157 if test -z "$path"; then
158 path=$(echo "$repo" | sed -e 's|/*$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
@@ -174,15 +160,39 @@ cmd_add()
160 path=$(echo "$path" | sed -e 's|/*$||')
161 fi
162
177 - test -e "$path" &&
178 - die "'$path' already exists"
179 -
163 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
164 die "'$path' already exists in the index"
165
183 - module_clone "$path" "$realrepo" || exit
184 - (unset GIT_DIR; cd "$path" && git checkout -q ${branch:+-b "$branch" "origin/$branch"}) ||
185 - die "Unable to checkout submodule '$path'"
166 + # perhaps the path exists and is already a git repo, else clone it
167 + if test -e "$path"
168 + then
169 + if test -d "$path/.git" &&
170 + test "$(unset GIT_DIR; cd $path; git rev-parse --git-dir)" = ".git"
171 + then
172 + echo "Adding existing repo at '$path' to the index"
173 + else
174 + die "'$path' already exists and is not a valid git repo"
175 + fi
176 + else
177 + case "$repo" in
178 + ./*|../*)
179 + # dereference source url relative to parent's url
180 + realrepo="$(resolve_relative_url $repo)" ;;
181 + *)
182 + # Turn the source into an absolute path if
183 + # it is local
184 + if base=$(get_repo_base "$repo"); then
185 + repo="$base"
186 + fi
187 + realrepo=$repo
188 + ;;
189 + esac
190 +
191 + module_clone "$path" "$realrepo" || exit
192 + (unset GIT_DIR; cd "$path" && git checkout -q ${branch:+-b "$branch" "origin/$branch"}) ||
193 + die "Unable to checkout submodule '$path'"
194 + fi
195 +
196 git add "$path" ||
197 die "Failed to add submodule '$path'"
198