Merge branch 'mg/maint-submodule-normalize-path'
authorJunio C Hamano <gitster@pobox.com>
Wed, 11 Mar 2009 20:50:29 +0000 (13:50 -0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 11 Mar 2009 20:50:29 +0000 (13:50 -0700)
* mg/maint-submodule-normalize-path:
git submodule: Fix adding of submodules at paths with ./, .. and //
git submodule: Add test cases for git submodule add

1  2 
git-submodule.sh
t/t7400-submodule-basic.sh
diff --combined git-submodule.sh
index 204aab671ef78edc24acff4019a2f40a71a59020,68d6afd15c94844c2435b65a197d678aa52314c9..0a27232b90456b5471ee64d5dcf9965ad73f28af
@@@ -5,7 -5,7 +5,7 @@@
  # Copyright (c) 2007 Lars Hjemli
  
  USAGE="[--quiet] [--cached] \
 -[add <repo> [-b branch] <path>]|[status|init|update [-i|--init]|summary [-n|--summary-limit <n>] [<commit>]] \
 +[add <repo> [-b branch] <path>]|[status|init|update [-i|--init] [-N|--no-fetch]|summary [-n|--summary-limit <n>] [<commit>]] \
  [--] [<path>...]|[foreach <command>]|[sync [--] [<path>...]]"
  OPTIONS_SPEC=
  . git-sh-setup
@@@ -16,7 -16,6 +16,7 @@@ command
  branch=
  quiet=
  cached=
 +nofetch=
  
  #
  # print stuff on stdout unless -q was specified
@@@ -60,7 -59,7 +60,7 @@@ resolve_relative_url (
  #
  module_list()
  {
 -      git ls-files --stage -- "$@" | grep '^160000 '
 +      git ls-files --error-unmatch --stage -- "$@" | grep '^160000 '
  }
  
  #
@@@ -167,9 -166,18 +167,18 @@@ cmd_add(
        ;;
        esac
  
-       # strip trailing slashes from path
-       path=$(echo "$path" | sed -e 's|/*$||')
+       # normalize path:
+       # multiple //; leading ./; /./; /../; trailing /
+       path=$(printf '%s/\n' "$path" |
+               sed -e '
+                       s|//*|/|g
+                       s|^\(\./\)*||
+                       s|/\./|/|g
+                       :start
+                       s|\([^/]*\)/\.\./||
+                       tstart
+                       s|/*$||
+               ')
        git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
        die "'$path' already exists in the index"
  
@@@ -301,10 -309,6 +310,10 @@@ cmd_update(
                        shift
                        cmd_init "$@" || return
                        ;;
 +              -N|--no-fetch)
 +                      shift
 +                      nofetch=1
 +                      ;;
                --)
                        shift
                        break
                        then
                                force="-f"
                        fi
 -                      (unset GIT_DIR; cd "$path" && git-fetch &&
 -                              git-checkout $force -q "$sha1") ||
 +
 +                      if test -z "$nofetch"
 +                      then
 +                              (unset GIT_DIR; cd "$path" &&
 +                                      git-fetch) ||
 +                              die "Unable to fetch in submodule path '$path'"
 +                      fi
 +
 +                      (unset GIT_DIR; cd "$path" &&
 +                                git-checkout $force -q "$sha1") ||
                        die "Unable to checkout '$sha1' in submodule path '$path'"
  
                        say "Submodule path '$path': checked out '$sha1'"
index b8cb2df6670a18ebf54fecbb97a48d0c07a06e2b,21c19a28cfdcef1478a97168cae8b6d6cae597a1..af690ec6c1f36871dbd0044d22ab78ab95103541
@@@ -47,6 -47,55 +47,55 @@@ test_expect_success 'Prepare submodule 
        GIT_CONFIG=.gitmodules git config submodule.example.url git://example.com/init.git
  '
  
+ test_expect_success 'Prepare submodule add testing' '
+       submodurl=$(pwd)
+       (
+               mkdir addtest &&
+               cd addtest &&
+               git init
+       )
+ '
+ test_expect_success 'submodule add' '
+       (
+               cd addtest &&
+               git submodule add "$submodurl" submod &&
+               git submodule init
+       )
+ '
+ test_expect_success 'submodule add with ./ in path' '
+       (
+               cd addtest &&
+               git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
+               git submodule init
+       )
+ '
+ test_expect_success 'submodule add with // in path' '
+       (
+               cd addtest &&
+               git submodule add "$submodurl" slashslashsubmod///frotz// &&
+               git submodule init
+       )
+ '
+ test_expect_success 'submodule add with /.. in path' '
+       (
+               cd addtest &&
+               git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
+               git submodule init
+       )
+ '
+ test_expect_success 'submodule add with ./, /.. and // in path' '
+       (
+               cd addtest &&
+               git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
+               git submodule init
+       )
+ '
  test_expect_success 'status should fail for unmapped paths' '
        if git submodule status
        then
@@@ -234,17 -283,4 +283,17 @@@ test_expect_success 'gracefully add sub
  
  '
  
 +test_expect_success 'ls-files gracefully handles trailing slash' '
 +
 +      test "init" = "$(git ls-files init/)"
 +
 +'
 +
 +test_expect_success 'submodule <invalid-path> warns' '
 +
 +      git submodule no-such-submodule 2> output.err &&
 +      grep "^error: .*no-such-submodule" output.err
 +
 +'
 +
  test_done