grep: fix worktree case in submodules
[gitweb.git] / t / t7814-grep-recurse-submodules.sh
index fa475d52fa32bc257a215573943c84df6532bc8d..48f2b13e7d7905b092ed852c38a2534d3ee31bf2 100755 (executable)
@@ -380,11 +380,7 @@ test_expect_success 'grep --recurse-submodules should pass the pattern type alon
        fi
 '
 
-# Recursing down into nested submodules which do not have .gitmodules in their
-# working tree does not work yet. This is because config_from_gitmodules()
-# uses get_oid() and the latter is still not able to get objects from an
-# arbitrary repository (the nested submodule, in this case).
-test_expect_failure 'grep --recurse-submodules with submodules without .gitmodules in the working tree' '
+test_expect_success 'grep --recurse-submodules with submodules without .gitmodules in the working tree' '
        test_when_finished "git -C submodule checkout .gitmodules" &&
        rm submodule/.gitmodules &&
        git grep --recurse-submodules -e "(.|.)[\d]" >actual &&
@@ -396,4 +392,25 @@ test_expect_failure 'grep --recurse-submodules with submodules without .gitmodul
        test_cmp expect actual
 '
 
+reset_and_clean () {
+       git reset --hard &&
+       git clean -fd &&
+       git submodule foreach --recursive 'git reset --hard' &&
+       git submodule foreach --recursive 'git clean -fd'
+}
+
+test_expect_success 'grep --recurse-submodules without --cached considers worktree modifications' '
+       reset_and_clean &&
+       echo "A modified line in submodule" >>submodule/a &&
+       echo "submodule/a:A modified line in submodule" >expect &&
+       git grep --recurse-submodules "A modified line in submodule" >actual &&
+       test_cmp expect actual
+'
+
+test_expect_success 'grep --recurse-submodules with --cached ignores worktree modifications' '
+       reset_and_clean &&
+       echo "A modified line in submodule" >>submodule/a &&
+       test_must_fail git grep --recurse-submodules --cached "A modified line in submodule" >actual 2>&1 &&
+       test_must_be_empty actual
+'
 test_done