]> git.ipfire.org Git - thirdparty/git.git/commitdiff
Teach Git to respect skip-worktree bit (reading part)
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>
Thu, 20 Aug 2009 13:46:58 +0000 (20:46 +0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 24 Aug 2009 00:13:32 +0000 (17:13 -0700)
grep: turn on --cached for files that is marked skip-worktree
ls-files: do not check for deleted file that is marked skip-worktree
update-index: ignore update request if it's skip-worktree, while still allows removing
diff*: skip worktree version

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin-commit.c
builtin-grep.c
builtin-ls-files.c
builtin-update-index.c
diff-lib.c
diff.c
read-cache.c
t/t7011-skip-worktree-reading.sh [new file with mode: 0755]

index 4bcce06fbffdf10ec701dfbc0b6b90a11513f89e..a0b1fd35cbacafd5afb79d3aa7a0e38e28960e8f 100644 (file)
@@ -180,6 +180,11 @@ static void add_remove_files(struct string_list *list)
        for (i = 0; i < list->nr; i++) {
                struct stat st;
                struct string_list_item *p = &(list->items[i]);
+               int pos = index_name_pos(&the_index, p->string, strlen(p->string));
+               struct cache_entry *ce = pos < 0 ? NULL : active_cache[pos];
+
+               if (ce && ce_skip_worktree(ce))
+                       continue;
 
                if (!lstat(p->string, &st)) {
                        if (add_to_cache(p->string, &st, 0))
index ad0e0a538539ff452d36f2e5776c39aa83c14137..813fe9778ae443edafac338d67299368631e821d 100644 (file)
@@ -517,7 +517,7 @@ static int grep_cache(struct grep_opt *opt, const char **paths, int cached,
                 * are identical, even if worktree file has been modified, so use
                 * cache version instead
                 */
-               if (cached || (ce->ce_flags & CE_VALID)) {
+               if (cached || (ce->ce_flags & CE_VALID) || ce_skip_worktree(ce)) {
                        if (ce_stage(ce))
                                continue;
                        hit |= grep_sha1(opt, ce->sha1, ce->name, 0);
index c1afbad453ae967efbe735b3f763ef17b3562d2e..ad7e44784f1b067f79333f22232e52e20642ab95 100644 (file)
@@ -194,6 +194,8 @@ static void show_files(struct dir_struct *dir, const char *prefix)
                                continue;
                        if (ce->ce_flags & CE_UPDATE)
                                continue;
+                       if (ce_skip_worktree(ce))
+                               continue;
                        err = lstat(ce->name, &st);
                        if (show_deleted && err)
                                show_ce_entry(tag_removed, ce);
index 5e97d0949716e4d25d4a9c42813db8a02db53eae..97b9ea61f7605970875776bd5c84c00abbb0f2f6 100644 (file)
@@ -172,29 +172,29 @@ static int process_directory(const char *path, int len, struct stat *st)
        return error("%s: is a directory - add files inside instead", path);
 }
 
-/*
- * Process a regular file
- */
-static int process_file(const char *path, int len, struct stat *st)
-{
-       int pos = cache_name_pos(path, len);
-       struct cache_entry *ce = pos < 0 ? NULL : active_cache[pos];
-
-       if (ce && S_ISGITLINK(ce->ce_mode))
-               return error("%s is already a gitlink, not replacing", path);
-
-       return add_one_path(ce, path, len, st);
-}
-
 static int process_path(const char *path)
 {
-       int len;
+       int pos, len;
        struct stat st;
+       struct cache_entry *ce;
 
        len = strlen(path);
        if (has_symlink_leading_path(path, len))
                return error("'%s' is beyond a symbolic link", path);
 
+       pos = cache_name_pos(path, len);
+       ce = pos < 0 ? NULL : active_cache[pos];
+       if (ce && ce_skip_worktree(ce)) {
+               /*
+                * working directory version is assumed "good"
+                * so updating it does not make sense.
+                * On the other hand, removing it from index should work
+                */
+               if (allow_remove && remove_file_from_cache(path))
+                       return error("%s: cannot remove from the index", path);
+               return 0;
+       }
+
        /*
         * First things first: get the stat information, to decide
         * what to do about the pathname!
@@ -205,7 +205,13 @@ static int process_path(const char *path)
        if (S_ISDIR(st.st_mode))
                return process_directory(path, len, &st);
 
-       return process_file(path, len, &st);
+       /*
+        * Process a regular file
+        */
+       if (ce && S_ISGITLINK(ce->ce_mode))
+               return error("%s is already a gitlink, not replacing", path);
+
+       return add_one_path(ce, path, len, &st);
 }
 
 static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
index 22da66ef145b3b1642b90e113a4659b43366d452..b0b379d9d208a9d70f67b6602bf1e19efbfc5674 100644 (file)
@@ -159,7 +159,7 @@ int run_diff_files(struct rev_info *revs, unsigned int option)
                                continue;
                }
 
-               if (ce_uptodate(ce))
+               if (ce_uptodate(ce) || ce_skip_worktree(ce))
                        continue;
 
                /* If CE_VALID is set, don't look at workdir for file removal */
@@ -339,7 +339,8 @@ static void do_oneway_diff(struct unpack_trees_options *o,
        int match_missing, cached;
 
        /* if the entry is not checked out, don't examine work tree */
-       cached = o->index_only || (idx && (idx->ce_flags & CE_VALID));
+       cached = o->index_only ||
+               (idx && ((idx->ce_flags & CE_VALID) || ce_skip_worktree(idx)));
        /*
         * Backward compatibility wart - "diff-index -m" does
         * not mean "do not ignore merges", but "match_missing".
diff --git a/diff.c b/diff.c
index cd35e0c2d7e29012e24cc8f5f49d9d351420b56e..3970df4afcbe62261757547bc7cc9570fd114ae9 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -1805,7 +1805,7 @@ static int reuse_worktree_file(const char *name, const unsigned char *sha1, int
         * If ce is marked as "assume unchanged", there is no
         * guarantee that work tree matches what we are looking for.
         */
-       if (ce->ce_flags & CE_VALID)
+       if ((ce->ce_flags & CE_VALID) || ce_skip_worktree(ce))
                return 0;
 
        /*
index 4e3e272ee409de66de2059a8be475fddcaa4dc28..5ee7d9da9c7674f2f7d16b64eaba9c21ae3d78c0 100644 (file)
@@ -265,7 +265,7 @@ int ie_match_stat(const struct index_state *istate,
         * If it's marked as always valid in the index, it's
         * valid whatever the checked-out copy says.
         */
-       if (!ignore_valid && (ce->ce_flags & CE_VALID))
+       if (!ignore_valid && ((ce->ce_flags & CE_VALID) || ce_skip_worktree(ce)))
                return 0;
 
        /*
@@ -1004,11 +1004,7 @@ static struct cache_entry *refresh_cache_ent(struct index_state *istate,
        if (ce_uptodate(ce))
                return ce;
 
-       /*
-        * CE_VALID means the user promised us that the change to
-        * the work tree does not matter and told us not to worry.
-        */
-       if (!ignore_valid && (ce->ce_flags & CE_VALID)) {
+       if (!ignore_valid && ((ce->ce_flags & CE_VALID) || ce_skip_worktree(ce))) {
                ce_mark_uptodate(ce);
                return ce;
        }
diff --git a/t/t7011-skip-worktree-reading.sh b/t/t7011-skip-worktree-reading.sh
new file mode 100755 (executable)
index 0000000..e996928
--- /dev/null
@@ -0,0 +1,163 @@
+#!/bin/sh
+#
+# Copyright (c) 2008 Nguyễn Thái Ngọc Duy
+#
+
+test_description='skip-worktree bit test'
+
+. ./test-lib.sh
+
+cat >expect.full <<EOF
+H 1
+H 2
+H init.t
+H sub/1
+H sub/2
+EOF
+
+cat >expect.skip <<EOF
+S 1
+H 2
+H init.t
+S sub/1
+H sub/2
+EOF
+
+NULL_SHA1=e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
+ZERO_SHA0=0000000000000000000000000000000000000000
+setup_absent() {
+       test -f 1 && rm 1
+       git update-index --remove 1 &&
+       git update-index --add --cacheinfo 100644 $NULL_SHA1 1 &&
+       git update-index --skip-worktree 1
+}
+
+test_absent() {
+       echo "100644 $NULL_SHA1 0       1" > expected &&
+       git ls-files --stage 1 > result &&
+       test_cmp expected result &&
+       test ! -f 1
+}
+
+setup_dirty() {
+       git update-index --force-remove 1 &&
+       echo dirty > 1 &&
+       git update-index --add --cacheinfo 100644 $NULL_SHA1 1 &&
+       git update-index --skip-worktree 1
+}
+
+test_dirty() {
+       echo "100644 $NULL_SHA1 0       1" > expected &&
+       git ls-files --stage 1 > result &&
+       test_cmp expected result &&
+       echo dirty > expected
+       test_cmp expected 1
+}
+
+test_expect_success 'setup' '
+       test_commit init &&
+       mkdir sub &&
+       touch ./1 ./2 sub/1 sub/2 &&
+       git add 1 2 sub/1 sub/2 &&
+       git update-index --skip-worktree 1 sub/1 &&
+       git ls-files -t > result &&
+       test_cmp expect.skip result
+'
+
+test_expect_success 'update-index' '
+       setup_absent &&
+       git update-index 1 &&
+       test_absent
+'
+
+test_expect_success 'update-index' '
+       setup_dirty &&
+       git update-index 1 &&
+       test_dirty
+'
+
+test_expect_success 'update-index --remove' '
+       setup_absent &&
+       git update-index --remove 1 &&
+       test -z "$(git ls-files 1)" &&
+       test ! -f 1
+'
+
+test_expect_success 'update-index --remove' '
+       setup_dirty &&
+       git update-index --remove 1 &&
+       test -z "$(git ls-files 1)" &&
+       echo dirty > expected &&
+       test_cmp expected 1
+'
+
+test_expect_success 'ls-files --delete' '
+       setup_absent &&
+       test -z "$(git ls-files -d)"
+'
+
+test_expect_success 'ls-files --delete' '
+       setup_dirty &&
+       test -z "$(git ls-files -d)"
+'
+
+test_expect_success 'ls-files --modified' '
+       setup_absent &&
+       test -z "$(git ls-files -m)"
+'
+
+test_expect_success 'ls-files --modified' '
+       setup_dirty &&
+       test -z "$(git ls-files -m)"
+'
+
+test_expect_success 'grep with skip-worktree file' '
+       git update-index --no-skip-worktree 1 &&
+       echo test > 1 &&
+       git update-index 1 &&
+       git update-index --skip-worktree 1 &&
+       rm 1 &&
+       test "$(git grep --no-ext-grep test)" = "1:test"
+'
+
+echo ":000000 100644 $ZERO_SHA0 $NULL_SHA1 A   1" > expected
+test_expect_success 'diff-index does not examine skip-worktree absent entries' '
+       setup_absent &&
+       git diff-index HEAD -- 1 > result &&
+       test_cmp expected result
+'
+
+test_expect_success 'diff-index does not examine skip-worktree dirty entries' '
+       setup_dirty &&
+       git diff-index HEAD -- 1 > result &&
+       test_cmp expected result
+'
+
+test_expect_success 'diff-files does not examine skip-worktree absent entries' '
+       setup_absent &&
+       test -z "$(git diff-files -- one)"
+'
+
+test_expect_success 'diff-files does not examine skip-worktree dirty entries' '
+       setup_dirty &&
+       test -z "$(git diff-files -- one)"
+'
+
+test_expect_success 'git-rm succeeds on skip-worktree absent entries' '
+       setup_absent &&
+       git rm 1
+'
+
+test_expect_failure 'commit on skip-worktree absent entries' '
+       git reset &&
+       setup_absent &&
+       test_must_fail git commit -m null 1
+'
+
+test_expect_failure 'commit on skip-worktree dirty entries' '
+       git reset &&
+       setup_dirty &&
+       test_must_fail git commit -m null 1
+'
+
+test_done