From 61d531a0a6b5232d2cd694fff815e35fba677751 Mon Sep 17 00:00:00 2001 From: Ben Peart Date: Fri, 16 Mar 2018 17:47:27 -0400 Subject: [PATCH 1/4] read-cache: add post-indexchanged hook --- cache.h | 3 ++- read-cache.c | 11 +++++++++-- unpack-trees.c | 2 ++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/cache.h b/cache.h index dc9c155b0d2671..3c5b64d8047a30 100644 --- a/cache.h +++ b/cache.h @@ -329,7 +329,8 @@ 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; struct hashmap name_hash; struct hashmap dir_hash; struct object_id oid; diff --git a/read-cache.c b/read-cache.c index 96aeb36179d34b..f348e5eec03026 100644 --- a/read-cache.c +++ b/read-cache.c @@ -18,6 +18,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" @@ -2657,8 +2658,14 @@ 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-indexchanged", istate->updated_workdir ? "1" : "0", NULL); + istate->updated_workdir = 0; + + return ret; } static int write_split_index(struct index_state *istate, diff --git a/unpack-trees.c b/unpack-trees.c index 852ffa3bc8c072..98f6f20888b4fb 100644 --- a/unpack-trees.c +++ b/unpack-trees.c @@ -1607,6 +1607,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 { From 80eabc53ea0baa82edad1f834f46630f14a48982 Mon Sep 17 00:00:00 2001 From: Ben Peart Date: Mon, 19 Mar 2018 13:16:48 -0400 Subject: [PATCH 2/4] read-cache: post-indexchanged hook add skip-worktree bit changing support --- builtin/reset.c | 4 +++- builtin/update-index.c | 2 ++ cache.h | 3 ++- read-cache.c | 5 ++++- 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/builtin/reset.c b/builtin/reset.c index 61027b0d961ff0..ba65e45cb40439 100644 --- a/builtin/reset.c +++ b/builtin/reset.c @@ -454,9 +454,11 @@ 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; - if (get_git_work_tree()) + if (get_git_work_tree()) { refresh_index(&the_index, flags, NULL, NULL, _("Unstaged changes after reset:")); + the_index.updated_skipworktree = 1; + } } else { int err = reset_index(&oid, reset_type, quiet); if (reset_type == KEEP && !err) diff --git a/builtin/update-index.c b/builtin/update-index.c index fe84003b4fa05c..82efd579499830 100644 --- a/builtin/update-index.c +++ b/builtin/update-index.c @@ -1052,6 +1052,8 @@ int cmd_update_index(int argc, const char **argv, const char *prefix) entries = read_cache(); if (entries < 0) die("cache corrupted"); + + the_index.updated_skipworktree = 1; /* * Custom copy of parse_options() because we want to handle diff --git a/cache.h b/cache.h index 3c5b64d8047a30..b74ee27a3c5b03 100644 --- a/cache.h +++ b/cache.h @@ -330,7 +330,8 @@ struct index_state { unsigned name_hash_initialized : 1, initialized : 1, drop_cache_tree : 1, - updated_workdir : 1; + updated_workdir : 1, + updated_skipworktree : 1; struct hashmap name_hash; struct hashmap dir_hash; struct object_id oid; diff --git a/read-cache.c b/read-cache.c index f348e5eec03026..6d2be5f743a4c9 100644 --- a/read-cache.c +++ b/read-cache.c @@ -2662,8 +2662,11 @@ static int do_write_locked_index(struct index_state *istate, struct lock_file *l else ret = close_lock_file_gently(lock); - run_hook_le(NULL, "post-indexchanged", istate->updated_workdir ? "1" : "0", NULL); + run_hook_le(NULL, "post-indexchanged", + istate->updated_workdir ? "1" : "0", + istate->updated_skipworktree ? "1" : "0", NULL); istate->updated_workdir = 0; + istate->updated_skipworktree = 0; return ret; } From ce4e661965452c62b967ab3789a67845ba59e9c7 Mon Sep 17 00:00:00 2001 From: Ben Peart Date: Mon, 19 Mar 2018 14:50:18 -0400 Subject: [PATCH 3/4] read-cache: add test for post-indexchanged hook --- t/t7113-post-index-changed-hook.sh | 136 +++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100755 t/t7113-post-index-changed-hook.sh diff --git a/t/t7113-post-index-changed-hook.sh b/t/t7113-post-index-changed-hook.sh new file mode 100755 index 00000000000000..8c6b0972ca8c72 --- /dev/null +++ b/t/t7113-post-index-changed-hook.sh @@ -0,0 +1,136 @@ +#!/bin/sh + +test_description='post index changed 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-indexchanged <<-\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 && + 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-indexchanged <<-\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 + git checkout master && + test_path_is_file testsuccess && rm -f testsuccess && + test_path_is_missing testfailure && + git checkout HEAD && + test_path_is_file testsuccess && rm -f testsuccess && + test_path_is_missing testfailure && + 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-indexchanged <<-\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 + git reset --mixed HEAD~1 && + test_path_is_file testsuccess && rm -f testsuccess && + test_path_is_missing testfailure && + git hash-object -w --stdin 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 From 12b8b62e0ff5c3a8c67f4e4861114d2889587878 Mon Sep 17 00:00:00 2001 From: Kevin Willford Date: Tue, 30 Oct 2018 13:38:47 -0600 Subject: [PATCH 4/4] Add documentation for the post-indexchanged hook --- Documentation/githooks.txt | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Documentation/githooks.txt b/Documentation/githooks.txt index 87f9ad2a77a97a..1a90bc01d704c9 100644 --- a/Documentation/githooks.txt +++ b/Documentation/githooks.txt @@ -512,6 +512,24 @@ of the working directory and are separated by a single NUL. Full paths The exit status determines whether git will use the data from the hook. On error, git will abort the command with an error message. +post-indexchanged +~~~~~~~~~~~~~~~~~ + +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