Merge branch 'bp/post-index-change-hook'
authorJunio C Hamano <gitster@pobox.com>
Thu, 25 Apr 2019 07:41:10 +0000 (16:41 +0900)
committerJunio C Hamano <gitster@pobox.com>
Thu, 25 Apr 2019 07:41:11 +0000 (16:41 +0900)
A new hook "post-index-change" is called when the on-disk index
file changes, which can help e.g. a virtualized working tree
implementation.

* bp/post-index-change-hook:
read-cache: add post-index-change hook

Documentation/githooks.txt
builtin/reset.c
builtin/update-index.c
cache.h
read-cache.c
t/t7113-post-index-change-hook.sh [new file with mode: 0755]
unpack-trees.c
index 5bf653c111d07e0958840e38187320a433b20d07..786e778ab8223a0ee02a44c8c756652fcf147205 100644 (file)
@@ -496,6 +496,24 @@ This hook is invoked by `git-p4 submit`. It takes no parameters and nothing
 from standard input. Exiting with non-zero status from this script prevent
 `git-p4 submit` from launching. Run `git-p4 submit --help` for details.
 
+post-index-change
+~~~~~~~~~~~~~~~~~
+
+This hook is invoked when the index is written in read-cache.c
+do_write_locked_index.
+
+The first parameter passed to the hook is the indicator for the
+working directory being updated.  "1" meaning working directory
+was updated or "0" when the working directory was not updated.
+
+The second parameter passed to the hook is the indicator for whether
+or not the index was updated and the skip-worktree bit could have
+changed.  "1" meaning skip-worktree bits could have been updated
+and "0" meaning they were not.
+
+Only one parameter should be set to "1" when the hook runs.  The hook
+running passing "1", "1" should not be possible.
+
 GIT
 ---
 Part of the linkgit:git[1] suite
index 7882829a95d8294ea5bb5bcba3335e651233d79a..26ef9a7bd03ac8925e1cb350c49e5e327999ba52 100644 (file)
@@ -386,6 +386,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
                        int flags = quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN;
                        if (read_from_tree(&pathspec, &oid, intent_to_add))
                                return 1;
+                       the_index.updated_skipworktree = 1;
                        if (!quiet && get_git_work_tree()) {
                                uint64_t t_begin, t_delta_in_ms;
 
index 1b6c42f748fd52ece4cf5f109f92a50bc2a87be3..73fe04e1c2e45a46af6ea561be28e8831498b6d4 100644 (file)
@@ -1082,6 +1082,8 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
        if (entries < 0)
                die("cache corrupted");
 
+       the_index.updated_skipworktree = 1;
+
        /*
         * Custom copy of parse_options() because we want to handle
         * filename arguments as they come.
diff --git a/cache.h b/cache.h
index e3c2ab962822e784b934a2342584ca42a16a9439..e928fe9d3bd90b3ac2ca69bd31d281784287d536 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -339,7 +339,9 @@ struct index_state {
        struct cache_time timestamp;
        unsigned name_hash_initialized : 1,
                 initialized : 1,
-                drop_cache_tree : 1;
+                drop_cache_tree : 1,
+                updated_workdir : 1,
+                updated_skipworktree : 1;
        struct hashmap name_hash;
        struct hashmap dir_hash;
        struct object_id oid;
index 4dc6de1b55b0a6047e49f188b28c431f0167d7ef..c62eae651df112b94a8a17ae80e399687a570572 100644 (file)
@@ -17,6 +17,7 @@
 #include "commit.h"
 #include "blob.h"
 #include "resolve-undo.h"
+#include "run-command.h"
 #include "strbuf.h"
 #include "varint.h"
 #include "split-index.h"
@@ -3049,8 +3050,17 @@ static int do_write_locked_index(struct index_state *istate, struct lock_file *l
        if (ret)
                return ret;
        if (flags & COMMIT_LOCK)
-               return commit_locked_index(lock);
-       return close_lock_file_gently(lock);
+               ret = commit_locked_index(lock);
+       else
+               ret = close_lock_file_gently(lock);
+
+       run_hook_le(NULL, "post-index-change",
+                       istate->updated_workdir ? "1" : "0",
+                       istate->updated_skipworktree ? "1" : "0", NULL);
+       istate->updated_workdir = 0;
+       istate->updated_skipworktree = 0;
+
+       return ret;
 }
 
 static int write_split_index(struct index_state *istate,
diff --git a/t/t7113-post-index-change-hook.sh b/t/t7113-post-index-change-hook.sh
new file mode 100755 (executable)
index 0000000..f011ad7
--- /dev/null
@@ -0,0 +1,144 @@
+#!/bin/sh
+
+test_description='post index change hook'
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+       mkdir -p dir1 &&
+       touch dir1/file1.txt &&
+       echo testing >dir1/file2.txt &&
+       git add . &&
+       git commit -m "initial"
+'
+
+test_expect_success 'test status, add, commit, others trigger hook without flags set' '
+       mkdir -p .git/hooks &&
+       write_script .git/hooks/post-index-change <<-\EOF &&
+               if test "$1" -eq 1; then
+                       echo "Invalid combination of flags passed to hook; updated_workdir is set." >testfailure
+                       exit 1
+               fi
+               if test "$2" -eq 1; then
+                       echo "Invalid combination of flags passed to hook; updated_skipworktree is set." >testfailure
+                       exit 1
+               fi
+               if test -f ".git/index.lock"; then
+                       echo ".git/index.lock exists" >testfailure
+                       exit 3
+               fi
+               if ! test -f ".git/index"; then
+                       echo ".git/index does not exist" >testfailure
+                       exit 3
+               fi
+               echo "success" >testsuccess
+       EOF
+       mkdir -p dir2 &&
+       touch dir2/file1.txt &&
+       touch dir2/file2.txt &&
+       : force index to be dirty &&
+       test-tool chmtime +60 dir1/file1.txt &&
+       git status &&
+       test_path_is_file testsuccess && rm -f testsuccess &&
+       test_path_is_missing testfailure &&
+       git add . &&
+       test_path_is_file testsuccess && rm -f testsuccess &&
+       test_path_is_missing testfailure &&
+       git commit -m "second" &&
+       test_path_is_file testsuccess && rm -f testsuccess &&
+       test_path_is_missing testfailure &&
+       git checkout -- dir1/file1.txt &&
+       test_path_is_file testsuccess && rm -f testsuccess &&
+       test_path_is_missing testfailure &&
+       git update-index &&
+       test_path_is_missing testsuccess &&
+       test_path_is_missing testfailure &&
+       git reset --soft &&
+       test_path_is_missing testsuccess &&
+       test_path_is_missing testfailure
+'
+
+test_expect_success 'test checkout and reset trigger the hook' '
+       write_script .git/hooks/post-index-change <<-\EOF &&
+               if test "$1" -eq 1 && test "$2" -eq 1; then
+                       echo "Invalid combination of flags passed to hook; updated_workdir and updated_skipworktree are both set." >testfailure
+                       exit 1
+               fi
+               if test "$1" -eq 0 && test "$2" -eq 0; then
+                       echo "Invalid combination of flags passed to hook; neither updated_workdir or updated_skipworktree are set." >testfailure
+                       exit 2
+               fi
+               if test "$1" -eq 1; then
+                       if test -f ".git/index.lock"; then
+                               echo "updated_workdir set but .git/index.lock exists" >testfailure
+                               exit 3
+                       fi
+                       if ! test -f ".git/index"; then
+                               echo "updated_workdir set but .git/index does not exist" >testfailure
+                               exit 3
+                       fi
+               else
+                       echo "update_workdir should be set for checkout" >testfailure
+                       exit 4
+               fi
+               echo "success" >testsuccess
+       EOF
+       : force index to be dirty &&
+       test-tool chmtime +60 dir1/file1.txt &&
+       git checkout master &&
+       test_path_is_file testsuccess && rm -f testsuccess &&
+       test_path_is_missing testfailure &&
+       test-tool chmtime +60 dir1/file1.txt &&
+       git checkout HEAD &&
+       test_path_is_file testsuccess && rm -f testsuccess &&
+       test_path_is_missing testfailure &&
+       test-tool chmtime +60 dir1/file1.txt &&
+       git reset --hard &&
+       test_path_is_file testsuccess && rm -f testsuccess &&
+       test_path_is_missing testfailure &&
+       git checkout -B test &&
+       test_path_is_file testsuccess && rm -f testsuccess &&
+       test_path_is_missing testfailure
+'
+
+test_expect_success 'test reset --mixed and update-index triggers the hook' '
+       write_script .git/hooks/post-index-change <<-\EOF &&
+               if test "$1" -eq 1 && test "$2" -eq 1; then
+                       echo "Invalid combination of flags passed to hook; updated_workdir and updated_skipworktree are both set." >testfailure
+                       exit 1
+               fi
+               if test "$1" -eq 0 && test "$2" -eq 0; then
+                       echo "Invalid combination of flags passed to hook; neither updated_workdir or updated_skipworktree are set." >testfailure
+                       exit 2
+               fi
+               if test "$2" -eq 1; then
+                       if test -f ".git/index.lock"; then
+                               echo "updated_skipworktree set but .git/index.lock exists" >testfailure
+                               exit 3
+                       fi
+                       if ! test -f ".git/index"; then
+                               echo "updated_skipworktree set but .git/index does not exist" >testfailure
+                               exit 3
+                       fi
+               else
+                       echo "updated_skipworktree should be set for reset --mixed and update-index" >testfailure
+                       exit 4
+               fi
+               echo "success" >testsuccess
+       EOF
+       : force index to be dirty &&
+       test-tool chmtime +60 dir1/file1.txt &&
+       git reset --mixed --quiet HEAD~1 &&
+       test_path_is_file testsuccess && rm -f testsuccess &&
+       test_path_is_missing testfailure &&
+       git hash-object -w --stdin <dir1/file2.txt >expect &&
+       git update-index --cacheinfo 100644 "$(cat expect)" dir1/file1.txt &&
+       test_path_is_file testsuccess && rm -f testsuccess &&
+       test_path_is_missing testfailure &&
+       git update-index --skip-worktree dir1/file2.txt &&
+       git update-index --remove dir1/file2.txt &&
+       test_path_is_file testsuccess && rm -f testsuccess &&
+       test_path_is_missing testfailure
+'
+
+test_done
index 1ccd343cad92dfad3e66deb304811eaf7af9b4fc..c5ec30f25f40377b525094c8eba224b9eb131c73 100644 (file)
@@ -1618,6 +1618,8 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
                                                  WRITE_TREE_SILENT |
                                                  WRITE_TREE_REPAIR);
                }
+
+               o->result.updated_workdir = 1;
                discard_index(o->dst_index);
                *o->dst_index = o->result;
        } else {