From: Jon Seymour Date: Sun, 27 May 2012 15:34:07 GMT Subject: [PATCH v7 5/9] submodule: extract normalize_path into standalone function Message-ID: <1338132851-23497-6-git-send-email-jon.seymour@gmail.com> In-Reply-To: <1338132851-23497-1-git-send-email-jon.seymour@gmail.com> Extract the normalize_path function so that it can be re-used elsewhere. Signed-off-by: Jon Seymour --- git-submodule.sh | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/git-submodule.sh b/git-submodule.sh index 64a70d6..dbbc905 100755 --- a/git-submodule.sh +++ b/git-submodule.sh @@ -176,6 +176,21 @@ module_clone() (clear_local_git_env; cd "$sm_path" && GIT_WORK_TREE=. git config core.worktree "$rel/$b") } +normalize_path() +{ + # normalize path: + # multiple //; leading ./; /./; /../; trailing / + printf '%s/\n' "$1" | + sed -e ' + s|//*|/|g + s|^\(\./\)*|| + s|/\./|/|g + :start + s|\([^/]*\)/\.\./|| + tstart + s|/*$|| + ' +} # # Add a new submodule to the working tree, .gitmodules and the index # @@ -250,18 +265,7 @@ cmd_add() ;; esac - # normalize path: - # multiple //; leading ./; /./; /../; trailing / - sm_path=$(printf '%s/\n' "$sm_path" | - sed -e ' - s|//*|/|g - s|^\(\./\)*|| - s|/\./|/|g - :start - s|\([^/]*\)/\.\./|| - tstart - s|/*$|| - ') + sm_path="$(normalize_path "$sm_path")" git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 && die "$(eval_gettext "'\$sm_path' already exists in the index")" -- 1.7.10.2.656.g24a6219