{"thread":{"id":"36906","subject":"[PATCH 00/32] Split index resend","startedAt":"2014-06-13T12:19:19Z","lastAt":"2014-06-13T12:19:51Z","messageCount":33,"participants":["Nguyễn Thái Ngọc Duy"],"isPatch":true,"patchVersion":1,"patchTotal":32},"messages":[{"id":"244094","messageId":"1402661991-14977-1-git-send-email-pclouds@gmail.com","threadId":"36906","inReplyTo":null,"subject":"[PATCH 00/32] Split index resend","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2014-06-13T12:19:19Z","receivedAt":"2014-06-13T12:19:19Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"This is basically what's in 'pu' with fixup patches squashed in (also\nresend is a good way to get people's eyes on it one more time).\nThere's also another minor change that SOMETHING_CHANGED now has value\n1.  This is the usual value of cache_changed before this series. So if\nanother in-flight series update cache_changed the old way, nothing's\nsubtly broken.\n\nNguyễn Thái Ngọc Duy (32):\n  ewah: fix constness of ewah_read_mmap\n  ewah: delete unused ewah_read_mmap_native declaration\n  sequencer: do not update/refresh index if the lock cannot be held\n  read-cache: new API write_locked_index instead of write_index/write_cache\n  read-cache: relocate and unexport commit_locked_index()\n  read-cache: store in-memory flags in the first 12 bits of ce_flags\n  read-cache: be strict about \"changed\" in remove_marked_cache_entries()\n  read-cache: be specific what part of the index has changed\n  update-index: be specific what part of the index has changed\n  resolve-undo: be specific what part of the index has changed\n  unpack-trees: be specific what part of the index has changed\n  cache-tree: mark istate->cache_changed on cache tree invalidation\n  cache-tree: mark istate->cache_changed on cache tree update\n  cache-tree: mark istate->cache_changed on prime_cache_tree()\n  entry.c: update cache_changed if refresh_cache is set in checkout_entry()\n  read-cache: save index SHA-1 after reading\n  read-cache: split-index mode\n  read-cache: mark new entries for split index\n  read-cache: save deleted entries in split index\n  read-cache: mark updated entries for split index\n  split-index: the writing part\n  split-index: the reading part\n  split-index: do not invalidate cache-tree at read time\n  split-index: strip pathname of on-disk replaced entries\n  update-index: new options to enable/disable split index mode\n  update-index --split-index: do not split if $GIT_DIR is read only\n  rev-parse: add --shared-index-path to get shared index path\n  read-tree: force split-index mode off on --index-output\n  read-tree: note about dropping split-index mode or index version\n  read-cache: force split index mode with GIT_TEST_SPLIT_INDEX\n  t2104: make sure split index mode is off for the version test\n  t1700: new tests for split-index mode\n\n .gitignore                               |   1 +\n Documentation/git-rev-parse.txt          |   4 +\n Documentation/git-update-index.txt       |  11 ++\n Documentation/gitrepository-layout.txt   |   4 +\n Documentation/technical/index-format.txt |  35 ++++\n Makefile                                 |   2 +\n builtin/add.c                            |   6 +-\n builtin/apply.c                          |  17 +-\n builtin/blame.c                          |   2 +-\n builtin/checkout-index.c                 |   4 +-\n builtin/checkout.c                       |  12 +-\n builtin/clone.c                          |   7 +-\n builtin/commit.c                         |  33 ++--\n builtin/merge.c                          |  12 +-\n builtin/mv.c                             |   7 +-\n builtin/read-tree.c                      |  18 +-\n builtin/reset.c                          |   7 +-\n builtin/rev-parse.c                      |  10 +\n builtin/rm.c                             |   7 +-\n builtin/update-index.c                   |  33 +++-\n cache-tree.c                             |  52 ++---\n cache-tree.h                             |   6 +-\n cache.h                                  |  28 ++-\n entry.c                                  |   3 +\n ewah/ewah_io.c                           |   4 +-\n ewah/ewok.h                              |   3 +-\n lockfile.c                               |  20 --\n merge-recursive.c                        |  11 +-\n merge.c                                  |   7 +-\n read-cache.c                             | 270 ++++++++++++++++++++++---\n rerere.c                                 |   3 +-\n resolve-undo.c                           |   2 +-\n sequencer.c                              |  16 +-\n split-index.c (new)                      | 328 +++++++++++++++++++++++++++++++\n split-index.h (new)                      |  35 ++++\n t/t1700-split-index.sh (new +x)          | 194 ++++++++++++++++++\n t/t2104-update-index-skip-worktree.sh    |   2 +\n test-dump-cache-tree.c                   |   7 +-\n test-dump-split-index.c (new)            |  34 ++++\n test-scrap-cache-tree.c                  |   5 +-\n unpack-trees.c                           |  18 +-\n 41 files changed, 1088 insertions(+), 192 deletions(-)\n create mode 100644 split-index.c\n create mode 100644 split-index.h\n create mode 100755 t/t1700-split-index.sh\n create mode 100644 test-dump-split-index.c\n\n-- \n1.9.1.346.ga2b5940\n"},{"id":"244095","messageId":"1402661991-14977-2-git-send-email-pclouds@gmail.com","threadId":"36906","inReplyTo":"1402661991-14977-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 01/32] ewah: fix constness of ewah_read_mmap","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2014-06-13T12:19:20Z","receivedAt":"2014-06-13T12:19:20Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n ewah/ewah_io.c | 4 ++--\n ewah/ewok.h    | 2 +-\n 2 files changed, 3 insertions(+), 3 deletions(-)\n\ndiff --git a/ewah/ewah_io.c b/ewah/ewah_io.c\nindex f7f700e..1c2d7af 100644\n--- a/ewah/ewah_io.c\n+++ b/ewah/ewah_io.c\n@@ -110,9 +110,9 @@ int ewah_serialize(struct ewah_bitmap *self, int fd)\n \treturn ewah_serialize_to(self, write_helper, (void *)(intptr_t)fd);\n }\n \n-int ewah_read_mmap(struct ewah_bitmap *self, void *map, size_t len)\n+int ewah_read_mmap(struct ewah_bitmap *self, const void *map, size_t len)\n {\n-\tuint8_t *ptr = map;\n+\tconst uint8_t *ptr = map;\n \tsize_t i;\n \n \tself->bit_size = get_be32(ptr);\ndiff --git a/ewah/ewok.h b/ewah/ewok.h\nindex 43adeb5..0556ca5 100644\n--- a/ewah/ewok.h\n+++ b/ewah/ewok.h\n@@ -99,7 +99,7 @@ int ewah_serialize(struct ewah_bitmap *self, int fd);\n int ewah_serialize_native(struct ewah_bitmap *self, int fd);\n \n int ewah_deserialize(struct ewah_bitmap *self, int fd);\n-int ewah_read_mmap(struct ewah_bitmap *self, void *map, size_t len);\n+int ewah_read_mmap(struct ewah_bitmap *self, const void *map, size_t len);\n int ewah_read_mmap_native(struct ewah_bitmap *self, void *map, size_t len);\n \n uint32_t ewah_checksum(struct ewah_bitmap *self);\n-- \n1.9.1.346.ga2b5940\n"},{"id":"244096","messageId":"1402661991-14977-3-git-send-email-pclouds@gmail.com","threadId":"36906","inReplyTo":"1402661991-14977-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 02/32] ewah: delete unused ewah_read_mmap_native declaration","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2014-06-13T12:19:21Z","receivedAt":"2014-06-13T12:19:21Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n ewah/ewok.h | 1 -\n 1 file changed, 1 deletion(-)\n\ndiff --git a/ewah/ewok.h b/ewah/ewok.h\nindex 0556ca5..f6ad190 100644\n--- a/ewah/ewok.h\n+++ b/ewah/ewok.h\n@@ -100,7 +100,6 @@ int ewah_serialize_native(struct ewah_bitmap *self, int fd);\n \n int ewah_deserialize(struct ewah_bitmap *self, int fd);\n int ewah_read_mmap(struct ewah_bitmap *self, const void *map, size_t len);\n-int ewah_read_mmap_native(struct ewah_bitmap *self, void *map, size_t len);\n \n uint32_t ewah_checksum(struct ewah_bitmap *self);\n \n-- \n1.9.1.346.ga2b5940\n"},{"id":"244097","messageId":"1402661991-14977-4-git-send-email-pclouds@gmail.com","threadId":"36906","inReplyTo":"1402661991-14977-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 03/32] sequencer: do not update/refresh index if the lock cannot be held","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2014-06-13T12:19:22Z","receivedAt":"2014-06-13T12:19:22Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n sequencer.c | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/sequencer.c b/sequencer.c\nindex bde5f04..7b886a6 100644\n--- a/sequencer.c\n+++ b/sequencer.c\n@@ -679,7 +679,7 @@ static void read_and_refresh_cache(struct replay_opts *opts)\n \tif (read_index_preload(&the_index, NULL) < 0)\n \t\tdie(_(\"git %s: failed to read the index\"), action_name(opts));\n \trefresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL, NULL);\n-\tif (the_index.cache_changed) {\n+\tif (the_index.cache_changed && index_fd >= 0) {\n \t\tif (write_index(&the_index, index_fd) ||\n \t\t    commit_locked_index(&index_lock))\n \t\t\tdie(_(\"git %s: failed to refresh the index\"), action_name(opts));\n-- \n1.9.1.346.ga2b5940\n"},{"id":"244098","messageId":"1402661991-14977-5-git-send-email-pclouds@gmail.com","threadId":"36906","inReplyTo":"1402661991-14977-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 04/32] read-cache: new API write_locked_index instead of write_index/write_cache","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2014-06-13T12:19:23Z","receivedAt":"2014-06-13T12:19:23Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/add.c            |  6 ++----\n builtin/apply.c          |  9 ++++-----\n builtin/checkout-index.c |  3 +--\n builtin/checkout.c       | 11 ++++-------\n builtin/clone.c          |  7 +++----\n builtin/commit.c         | 33 ++++++++++++++-------------------\n builtin/merge.c          | 12 ++++--------\n builtin/mv.c             |  7 +++----\n builtin/read-tree.c      |  7 +++----\n builtin/reset.c          |  5 ++---\n builtin/rm.c             |  7 +++----\n builtin/update-index.c   |  3 +--\n cache-tree.c             |  3 +--\n cache.h                  |  6 ++++--\n merge-recursive.c        |  7 +++----\n merge.c                  |  7 +++----\n read-cache.c             | 28 ++++++++++++++++++++++++----\n rerere.c                 |  3 +--\n sequencer.c              | 10 ++++------\n test-scrap-cache-tree.c  |  5 ++---\n 20 files changed, 86 insertions(+), 93 deletions(-)\n\ndiff --git a/builtin/add.c b/builtin/add.c\nindex 459208a..4baf3a5 100644\n--- a/builtin/add.c\n+++ b/builtin/add.c\n@@ -299,7 +299,6 @@ static int add_files(struct dir_struct *dir, int flags)\n int cmd_add(int argc, const char **argv, const char *prefix)\n {\n \tint exit_status = 0;\n-\tint newfd;\n \tstruct pathspec pathspec;\n \tstruct dir_struct dir;\n \tint flags;\n@@ -345,7 +344,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)\n \tadd_new_files = !take_worktree_changes && !refresh_only;\n \trequire_pathspec = !take_worktree_changes;\n \n-\tnewfd = hold_locked_index(&lock_file, 1);\n+\thold_locked_index(&lock_file, 1);\n \n \tflags = ((verbose ? ADD_CACHE_VERBOSE : 0) |\n \t\t (show_only ? ADD_CACHE_PRETEND : 0) |\n@@ -443,8 +442,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)\n \n finish:\n \tif (active_cache_changed) {\n-\t\tif (write_cache(newfd, active_cache, active_nr) ||\n-\t\t    commit_locked_index(&lock_file))\n+\t\tif (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))\n \t\t\tdie(_(\"Unable to write new index file\"));\n \t}\n \ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 87439fa..5e13444 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -3644,7 +3644,7 @@ static void build_fake_ancestor(struct patch *list, const char *filename)\n {\n \tstruct patch *patch;\n \tstruct index_state result = { NULL };\n-\tint fd;\n+\tstatic struct lock_file lock;\n \n \t/* Once we start supporting the reverse patch, it may be\n \t * worth showing the new sha1 prefix, but until then...\n@@ -3682,8 +3682,8 @@ static void build_fake_ancestor(struct patch *list, const char *filename)\n \t\t\tdie (\"Could not add %s to temporary index\", name);\n \t}\n \n-\tfd = open(filename, O_WRONLY | O_CREAT, 0666);\n-\tif (fd < 0 || write_index(&result, fd) || close(fd))\n+\thold_lock_file_for_update(&lock, filename, LOCK_DIE_ON_ERROR);\n+\tif (write_locked_index(&result, &lock, COMMIT_LOCK))\n \t\tdie (\"Could not write temporary index to %s\", filename);\n \n \tdiscard_index(&result);\n@@ -4501,8 +4501,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)\n \t}\n \n \tif (update_index) {\n-\t\tif (write_cache(newfd, active_cache, active_nr) ||\n-\t\t    commit_locked_index(&lock_file))\n+\t\tif (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))\n \t\t\tdie(_(\"Unable to write new index file\"));\n \t}\n \ndiff --git a/builtin/checkout-index.c b/builtin/checkout-index.c\nindex 61e75eb..9e49bf2 100644\n--- a/builtin/checkout-index.c\n+++ b/builtin/checkout-index.c\n@@ -279,8 +279,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)\n \t\tcheckout_all(prefix, prefix_length);\n \n \tif (0 <= newfd &&\n-\t    (write_cache(newfd, active_cache, active_nr) ||\n-\t     commit_locked_index(&lock_file)))\n+\t    write_locked_index(&the_index, &lock_file, COMMIT_LOCK))\n \t\tdie(\"Unable to write new index file\");\n \treturn 0;\n }\ndiff --git a/builtin/checkout.c b/builtin/checkout.c\nindex 07cf555..944a634 100644\n--- a/builtin/checkout.c\n+++ b/builtin/checkout.c\n@@ -225,7 +225,6 @@ static int checkout_paths(const struct checkout_opts *opts,\n \tint flag;\n \tstruct commit *head;\n \tint errs = 0;\n-\tint newfd;\n \tstruct lock_file *lock_file;\n \n \tif (opts->track != BRANCH_TRACK_UNSPECIFIED)\n@@ -256,7 +255,7 @@ static int checkout_paths(const struct checkout_opts *opts,\n \n \tlock_file = xcalloc(1, sizeof(struct lock_file));\n \n-\tnewfd = hold_locked_index(lock_file, 1);\n+\thold_locked_index(lock_file, 1);\n \tif (read_cache_preload(&opts->pathspec) < 0)\n \t\treturn error(_(\"corrupt index file\"));\n \n@@ -352,8 +351,7 @@ static int checkout_paths(const struct checkout_opts *opts,\n \t\t}\n \t}\n \n-\tif (write_cache(newfd, active_cache, active_nr) ||\n-\t    commit_locked_index(lock_file))\n+\tif (write_locked_index(&the_index, lock_file, COMMIT_LOCK))\n \t\tdie(_(\"unable to write new index file\"));\n \n \tread_ref_full(\"HEAD\", rev, 0, &flag);\n@@ -444,8 +442,8 @@ static int merge_working_tree(const struct checkout_opts *opts,\n {\n \tint ret;\n \tstruct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));\n-\tint newfd = hold_locked_index(lock_file, 1);\n \n+\thold_locked_index(lock_file, 1);\n \tif (read_cache_preload(NULL) < 0)\n \t\treturn error(_(\"corrupt index file\"));\n \n@@ -553,8 +551,7 @@ static int merge_working_tree(const struct checkout_opts *opts,\n \t\t}\n \t}\n \n-\tif (write_cache(newfd, active_cache, active_nr) ||\n-\t    commit_locked_index(lock_file))\n+\tif (write_locked_index(&the_index, lock_file, COMMIT_LOCK))\n \t\tdie(_(\"unable to write new index file\"));\n \n \tif (!opts->force && !opts->quiet)\ndiff --git a/builtin/clone.c b/builtin/clone.c\nindex 9b3c04d..48f91f5 100644\n--- a/builtin/clone.c\n+++ b/builtin/clone.c\n@@ -616,7 +616,7 @@ static int checkout(void)\n \tstruct unpack_trees_options opts;\n \tstruct tree *tree;\n \tstruct tree_desc t;\n-\tint err = 0, fd;\n+\tint err = 0;\n \n \tif (option_no_checkout)\n \t\treturn 0;\n@@ -640,7 +640,7 @@ static int checkout(void)\n \tsetup_work_tree();\n \n \tlock_file = xcalloc(1, sizeof(struct lock_file));\n-\tfd = hold_locked_index(lock_file, 1);\n+\thold_locked_index(lock_file, 1);\n \n \tmemset(&opts, 0, sizeof opts);\n \topts.update = 1;\n@@ -656,8 +656,7 @@ static int checkout(void)\n \tif (unpack_trees(1, &t, &opts) < 0)\n \t\tdie(_(\"unable to checkout working tree\"));\n \n-\tif (write_cache(fd, active_cache, active_nr) ||\n-\t    commit_locked_index(lock_file))\n+\tif (write_locked_index(&the_index, lock_file, COMMIT_LOCK))\n \t\tdie(_(\"unable to write new index file\"));\n \n \terr |= run_hook_le(NULL, \"post-checkout\", sha1_to_hex(null_sha1),\ndiff --git a/builtin/commit.c b/builtin/commit.c\nindex 9cfef6c..243b0c3 100644\n--- a/builtin/commit.c\n+++ b/builtin/commit.c\n@@ -305,7 +305,6 @@ static void refresh_cache_or_die(int refresh_flags)\n static char *prepare_index(int argc, const char **argv, const char *prefix,\n \t\t\t   const struct commit *current_head, int is_status)\n {\n-\tint fd;\n \tstruct string_list partial;\n \tstruct pathspec pathspec;\n \tint refresh_flags = REFRESH_QUIET;\n@@ -321,12 +320,11 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,\n \n \tif (interactive) {\n \t\tchar *old_index_env = NULL;\n-\t\tfd = hold_locked_index(&index_lock, 1);\n+\t\thold_locked_index(&index_lock, 1);\n \n \t\trefresh_cache_or_die(refresh_flags);\n \n-\t\tif (write_cache(fd, active_cache, active_nr) ||\n-\t\t    close_lock_file(&index_lock))\n+\t\tif (write_locked_index(&the_index, &index_lock, CLOSE_LOCK))\n \t\t\tdie(_(\"unable to create temporary index\"));\n \n \t\told_index_env = getenv(INDEX_ENVIRONMENT);\n@@ -360,12 +358,11 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,\n \t * (B) on failure, rollback the real index.\n \t */\n \tif (all || (also && pathspec.nr)) {\n-\t\tfd = hold_locked_index(&index_lock, 1);\n+\t\thold_locked_index(&index_lock, 1);\n \t\tadd_files_to_cache(also ? prefix : NULL, &pathspec, 0);\n \t\trefresh_cache_or_die(refresh_flags);\n \t\tupdate_main_cache_tree(WRITE_TREE_SILENT);\n-\t\tif (write_cache(fd, active_cache, active_nr) ||\n-\t\t    close_lock_file(&index_lock))\n+\t\tif (write_locked_index(&the_index, &index_lock, CLOSE_LOCK))\n \t\t\tdie(_(\"unable to write new_index file\"));\n \t\tcommit_style = COMMIT_NORMAL;\n \t\treturn index_lock.filename;\n@@ -381,12 +378,12 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,\n \t * We still need to refresh the index here.\n \t */\n \tif (!only && !pathspec.nr) {\n-\t\tfd = hold_locked_index(&index_lock, 1);\n+\t\thold_locked_index(&index_lock, 1);\n \t\trefresh_cache_or_die(refresh_flags);\n \t\tif (active_cache_changed) {\n \t\t\tupdate_main_cache_tree(WRITE_TREE_SILENT);\n-\t\t\tif (write_cache(fd, active_cache, active_nr) ||\n-\t\t\t    commit_locked_index(&index_lock))\n+\t\t\tif (write_locked_index(&the_index, &index_lock,\n+\t\t\t\t\t       COMMIT_LOCK))\n \t\t\t\tdie(_(\"unable to write new_index file\"));\n \t\t} else {\n \t\t\trollback_lock_file(&index_lock);\n@@ -432,24 +429,22 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,\n \tif (read_cache() < 0)\n \t\tdie(_(\"cannot read the index\"));\n \n-\tfd = hold_locked_index(&index_lock, 1);\n+\thold_locked_index(&index_lock, 1);\n \tadd_remove_files(&partial);\n \trefresh_cache(REFRESH_QUIET);\n-\tif (write_cache(fd, active_cache, active_nr) ||\n-\t    close_lock_file(&index_lock))\n+\tif (write_locked_index(&the_index, &index_lock, CLOSE_LOCK))\n \t\tdie(_(\"unable to write new_index file\"));\n \n-\tfd = hold_lock_file_for_update(&false_lock,\n-\t\t\t\t       git_path(\"next-index-%\"PRIuMAX,\n-\t\t\t\t\t\t(uintmax_t) getpid()),\n-\t\t\t\t       LOCK_DIE_ON_ERROR);\n+\thold_lock_file_for_update(&false_lock,\n+\t\t\t\t  git_path(\"next-index-%\"PRIuMAX,\n+\t\t\t\t\t   (uintmax_t) getpid()),\n+\t\t\t\t  LOCK_DIE_ON_ERROR);\n \n \tcreate_base_index(current_head);\n \tadd_remove_files(&partial);\n \trefresh_cache(REFRESH_QUIET);\n \n-\tif (write_cache(fd, active_cache, active_nr) ||\n-\t    close_lock_file(&false_lock))\n+\tif (write_locked_index(&the_index, &false_lock, CLOSE_LOCK))\n \t\tdie(_(\"unable to write temporary index file\"));\n \n \tdiscard_cache();\ndiff --git a/builtin/merge.c b/builtin/merge.c\nindex 66d8843..bf770b6 100644\n--- a/builtin/merge.c\n+++ b/builtin/merge.c\n@@ -657,14 +657,12 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,\n \t\t\t      struct commit_list *remoteheads,\n \t\t\t      struct commit *head, const char *head_arg)\n {\n-\tint index_fd;\n \tstruct lock_file *lock = xcalloc(1, sizeof(struct lock_file));\n \n-\tindex_fd = hold_locked_index(lock, 1);\n+\thold_locked_index(lock, 1);\n \trefresh_cache(REFRESH_QUIET);\n \tif (active_cache_changed &&\n-\t\t\t(write_cache(index_fd, active_cache, active_nr) ||\n-\t\t\t commit_locked_index(lock)))\n+\t    write_locked_index(&the_index, lock, COMMIT_LOCK))\n \t\treturn error(_(\"Unable to write index.\"));\n \trollback_lock_file(lock);\n \n@@ -672,7 +670,6 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,\n \t\tint clean, x;\n \t\tstruct commit *result;\n \t\tstruct lock_file *lock = xcalloc(1, sizeof(struct lock_file));\n-\t\tint index_fd;\n \t\tstruct commit_list *reversed = NULL;\n \t\tstruct merge_options o;\n \t\tstruct commit_list *j;\n@@ -700,12 +697,11 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,\n \t\tfor (j = common; j; j = j->next)\n \t\t\tcommit_list_insert(j->item, &reversed);\n \n-\t\tindex_fd = hold_locked_index(lock, 1);\n+\t\thold_locked_index(lock, 1);\n \t\tclean = merge_recursive(&o, head,\n \t\t\t\tremoteheads->item, reversed, &result);\n \t\tif (active_cache_changed &&\n-\t\t\t\t(write_cache(index_fd, active_cache, active_nr) ||\n-\t\t\t\t commit_locked_index(lock)))\n+\t\t    write_locked_index(&the_index, lock, COMMIT_LOCK))\n \t\t\tdie (_(\"unable to write %s\"), get_index_file());\n \t\trollback_lock_file(lock);\n \t\treturn clean ? 0 : 1;\ndiff --git a/builtin/mv.c b/builtin/mv.c\nindex 2a7243f..db40777 100644\n--- a/builtin/mv.c\n+++ b/builtin/mv.c\n@@ -63,7 +63,7 @@ static struct lock_file lock_file;\n \n int cmd_mv(int argc, const char **argv, const char *prefix)\n {\n-\tint i, newfd, gitmodules_modified = 0;\n+\tint i, gitmodules_modified = 0;\n \tint verbose = 0, show_only = 0, force = 0, ignore_errors = 0;\n \tstruct option builtin_mv_options[] = {\n \t\tOPT__VERBOSE(&verbose, N_(\"be verbose\")),\n@@ -85,7 +85,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)\n \tif (--argc < 1)\n \t\tusage_with_options(builtin_mv_usage, builtin_mv_options);\n \n-\tnewfd = hold_locked_index(&lock_file, 1);\n+\thold_locked_index(&lock_file, 1);\n \tif (read_cache() < 0)\n \t\tdie(_(\"index file corrupt\"));\n \n@@ -275,8 +275,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)\n \t\tstage_updated_gitmodules();\n \n \tif (active_cache_changed) {\n-\t\tif (write_cache(newfd, active_cache, active_nr) ||\n-\t\t    commit_locked_index(&lock_file))\n+\t\tif (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))\n \t\t\tdie(_(\"Unable to write new index file\"));\n \t}\n \ndiff --git a/builtin/read-tree.c b/builtin/read-tree.c\nindex 0d7ef84..f26d90f 100644\n--- a/builtin/read-tree.c\n+++ b/builtin/read-tree.c\n@@ -99,7 +99,7 @@ static struct lock_file lock_file;\n \n int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)\n {\n-\tint i, newfd, stage = 0;\n+\tint i, stage = 0;\n \tunsigned char sha1[20];\n \tstruct tree_desc t[MAX_UNPACK_TREES];\n \tstruct unpack_trees_options opts;\n@@ -149,7 +149,7 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)\n \targc = parse_options(argc, argv, unused_prefix, read_tree_options,\n \t\t\t     read_tree_usage, 0);\n \n-\tnewfd = hold_locked_index(&lock_file, 1);\n+\thold_locked_index(&lock_file, 1);\n \n \tprefix_set = opts.prefix ? 1 : 0;\n \tif (1 < opts.merge + opts.reset + prefix_set)\n@@ -233,8 +233,7 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)\n \tif (nr_trees == 1 && !opts.prefix)\n \t\tprime_cache_tree(&active_cache_tree, trees[0]);\n \n-\tif (write_cache(newfd, active_cache, active_nr) ||\n-\t    commit_locked_index(&lock_file))\n+\tif (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))\n \t\tdie(\"unable to write new index file\");\n \treturn 0;\n }\ndiff --git a/builtin/reset.c b/builtin/reset.c\nindex f4e0875..0c56d28 100644\n--- a/builtin/reset.c\n+++ b/builtin/reset.c\n@@ -351,7 +351,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)\n \n \tif (reset_type != SOFT) {\n \t\tstruct lock_file *lock = xcalloc(1, sizeof(*lock));\n-\t\tint newfd = hold_locked_index(lock, 1);\n+\t\thold_locked_index(lock, 1);\n \t\tif (reset_type == MIXED) {\n \t\t\tint flags = quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN;\n \t\t\tif (read_from_tree(&pathspec, sha1, intent_to_add))\n@@ -367,8 +367,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)\n \t\t\t\tdie(_(\"Could not reset index file to revision '%s'.\"), rev);\n \t\t}\n \n-\t\tif (write_cache(newfd, active_cache, active_nr) ||\n-\t\t    commit_locked_index(lock))\n+\t\tif (write_locked_index(&the_index, lock, COMMIT_LOCK))\n \t\t\tdie(_(\"Could not write new index file.\"));\n \t}\n \ndiff --git a/builtin/rm.c b/builtin/rm.c\nindex 960634d..bc6490b 100644\n--- a/builtin/rm.c\n+++ b/builtin/rm.c\n@@ -278,7 +278,7 @@ static struct option builtin_rm_options[] = {\n \n int cmd_rm(int argc, const char **argv, const char *prefix)\n {\n-\tint i, newfd;\n+\tint i;\n \tstruct pathspec pathspec;\n \tchar *seen;\n \n@@ -293,7 +293,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)\n \tif (!index_only)\n \t\tsetup_work_tree();\n \n-\tnewfd = hold_locked_index(&lock_file, 1);\n+\thold_locked_index(&lock_file, 1);\n \n \tif (read_cache() < 0)\n \t\tdie(_(\"index file corrupt\"));\n@@ -427,8 +427,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)\n \t}\n \n \tif (active_cache_changed) {\n-\t\tif (write_cache(newfd, active_cache, active_nr) ||\n-\t\t    commit_locked_index(&lock_file))\n+\t\tif (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))\n \t\t\tdie(_(\"Unable to write new index file\"));\n \t}\n \ndiff --git a/builtin/update-index.c b/builtin/update-index.c\nindex ba54e19..42cbe4b 100644\n--- a/builtin/update-index.c\n+++ b/builtin/update-index.c\n@@ -921,8 +921,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)\n \t\t\t\texit(128);\n \t\t\tunable_to_lock_index_die(get_index_file(), lock_error);\n \t\t}\n-\t\tif (write_cache(newfd, active_cache, active_nr) ||\n-\t\t    commit_locked_index(lock_file))\n+\t\tif (write_locked_index(&the_index, lock_file, COMMIT_LOCK))\n \t\t\tdie(\"Unable to write new index file\");\n \t}\n \ndiff --git a/cache-tree.c b/cache-tree.c\nindex 7fa524a..52f8692 100644\n--- a/cache-tree.c\n+++ b/cache-tree.c\n@@ -595,8 +595,7 @@ int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix)\n \t\t\t\t      active_nr, flags) < 0)\n \t\t\treturn WRITE_TREE_UNMERGED_INDEX;\n \t\tif (0 <= newfd) {\n-\t\t\tif (!write_cache(newfd, active_cache, active_nr) &&\n-\t\t\t    !commit_lock_file(lock_file))\n+\t\t\tif (!write_locked_index(&the_index, lock_file, COMMIT_LOCK))\n \t\t\t\tnewfd = -1;\n \t\t}\n \t\t/* Not being able to write is fine -- we are only interested\ndiff --git a/cache.h b/cache.h\nindex 107ac61..9cc2b97 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -301,7 +301,6 @@ extern void free_name_hash(struct index_state *istate);\n #define read_cache_preload(pathspec) read_index_preload(&the_index, (pathspec))\n #define is_cache_unborn() is_index_unborn(&the_index)\n #define read_cache_unmerged() read_index_unmerged(&the_index)\n-#define write_cache(newfd, cache, entries) write_index(&the_index, (newfd))\n #define discard_cache() discard_index(&the_index)\n #define unmerged_cache() unmerged_index(&the_index)\n #define cache_name_pos(name, namelen) index_name_pos(&the_index,(name),(namelen))\n@@ -456,12 +455,15 @@ extern int daemonize(void);\n \t} while (0)\n \n /* Initialize and use the cache information */\n+struct lock_file;\n extern int read_index(struct index_state *);\n extern int read_index_preload(struct index_state *, const struct pathspec *pathspec);\n extern int read_index_from(struct index_state *, const char *path);\n extern int is_index_unborn(struct index_state *);\n extern int read_index_unmerged(struct index_state *);\n-extern int write_index(struct index_state *, int newfd);\n+#define COMMIT_LOCK\t\t(1 << 0)\n+#define CLOSE_LOCK\t\t(1 << 1)\n+extern int write_locked_index(struct index_state *, struct lock_file *lock, unsigned flags);\n extern int discard_index(struct index_state *);\n extern int unmerged_index(const struct index_state *);\n extern int verify_path(const char *path);\ndiff --git a/merge-recursive.c b/merge-recursive.c\nindex 4177092..442c1ec 100644\n--- a/merge-recursive.c\n+++ b/merge-recursive.c\n@@ -1986,7 +1986,7 @@ int merge_recursive_generic(struct merge_options *o,\n \t\t\t    const unsigned char **base_list,\n \t\t\t    struct commit **result)\n {\n-\tint clean, index_fd;\n+\tint clean;\n \tstruct lock_file *lock = xcalloc(1, sizeof(struct lock_file));\n \tstruct commit *head_commit = get_ref(head, o->branch1);\n \tstruct commit *next_commit = get_ref(merge, o->branch2);\n@@ -2003,12 +2003,11 @@ int merge_recursive_generic(struct merge_options *o,\n \t\t}\n \t}\n \n-\tindex_fd = hold_locked_index(lock, 1);\n+\thold_locked_index(lock, 1);\n \tclean = merge_recursive(o, head_commit, next_commit, ca,\n \t\t\tresult);\n \tif (active_cache_changed &&\n-\t\t\t(write_cache(index_fd, active_cache, active_nr) ||\n-\t\t\t commit_locked_index(lock)))\n+\t    write_locked_index(&the_index, lock, COMMIT_LOCK))\n \t\treturn error(_(\"Unable to write index.\"));\n \n \treturn clean ? 0 : 1;\ndiff --git a/merge.c b/merge.c\nindex 70f1000..610725c 100644\n--- a/merge.c\n+++ b/merge.c\n@@ -66,13 +66,13 @@ int checkout_fast_forward(const unsigned char *head,\n \tstruct tree *trees[MAX_UNPACK_TREES];\n \tstruct unpack_trees_options opts;\n \tstruct tree_desc t[MAX_UNPACK_TREES];\n-\tint i, fd, nr_trees = 0;\n+\tint i, nr_trees = 0;\n \tstruct dir_struct dir;\n \tstruct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));\n \n \trefresh_cache(REFRESH_QUIET);\n \n-\tfd = hold_locked_index(lock_file, 1);\n+\thold_locked_index(lock_file, 1);\n \n \tmemset(&trees, 0, sizeof(trees));\n \tmemset(&opts, 0, sizeof(opts));\n@@ -105,8 +105,7 @@ int checkout_fast_forward(const unsigned char *head,\n \t}\n \tif (unpack_trees(nr_trees, t, &opts))\n \t\treturn -1;\n-\tif (write_cache(fd, active_cache, active_nr) ||\n-\t\tcommit_locked_index(lock_file))\n+\tif (write_locked_index(&the_index, lock_file, COMMIT_LOCK))\n \t\tdie(_(\"unable to write new index file\"));\n \treturn 0;\n }\ndiff --git a/read-cache.c b/read-cache.c\nindex ba13353..44d4732 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -1779,13 +1779,11 @@ static int has_racy_timestamp(struct index_state *istate)\n void update_index_if_able(struct index_state *istate, struct lock_file *lockfile)\n {\n \tif ((istate->cache_changed || has_racy_timestamp(istate)) &&\n-\t    !write_index(istate, lockfile->fd))\n-\t\tcommit_locked_index(lockfile);\n-\telse\n+\t    write_locked_index(istate, lockfile, COMMIT_LOCK))\n \t\trollback_lock_file(lockfile);\n }\n \n-int write_index(struct index_state *istate, int newfd)\n+static int do_write_index(struct index_state *istate, int newfd)\n {\n \tgit_SHA_CTX c;\n \tstruct cache_header hdr;\n@@ -1877,6 +1875,28 @@ int write_index(struct index_state *istate, int newfd)\n \treturn 0;\n }\n \n+static int do_write_locked_index(struct index_state *istate, struct lock_file *lock,\n+\t\t\t\t unsigned flags)\n+{\n+\tint ret = do_write_index(istate, lock->fd);\n+\tif (ret)\n+\t\treturn ret;\n+\tassert((flags & (COMMIT_LOCK | CLOSE_LOCK)) !=\n+\t       (COMMIT_LOCK | CLOSE_LOCK));\n+\tif (flags & COMMIT_LOCK)\n+\t\treturn commit_locked_index(lock);\n+\telse if (flags & CLOSE_LOCK)\n+\t\treturn close_lock_file(lock);\n+\telse\n+\t\treturn ret;\n+}\n+\n+int write_locked_index(struct index_state *istate, struct lock_file *lock,\n+\t\t       unsigned flags)\n+{\n+\treturn do_write_locked_index(istate, lock, flags);\n+}\n+\n /*\n  * Read the index file that is potentially unmerged into given\n  * index_state, dropping any unmerged entries.  Returns true if\ndiff --git a/rerere.c b/rerere.c\nindex d55aa8a..ffc6a5b 100644\n--- a/rerere.c\n+++ b/rerere.c\n@@ -492,8 +492,7 @@ static int update_paths(struct string_list *update)\n \t}\n \n \tif (!status && active_cache_changed) {\n-\t\tif (write_cache(fd, active_cache, active_nr) ||\n-\t\t    commit_locked_index(&index_lock))\n+\t\tif (write_locked_index(&the_index, &index_lock, COMMIT_LOCK))\n \t\t\tdie(\"Unable to write new index file\");\n \t} else if (fd >= 0)\n \t\trollback_lock_file(&index_lock);\ndiff --git a/sequencer.c b/sequencer.c\nindex 7b886a6..5ebc461 100644\n--- a/sequencer.c\n+++ b/sequencer.c\n@@ -294,11 +294,11 @@ static int do_recursive_merge(struct commit *base, struct commit *next,\n {\n \tstruct merge_options o;\n \tstruct tree *result, *next_tree, *base_tree, *head_tree;\n-\tint clean, index_fd;\n+\tint clean;\n \tconst char **xopt;\n \tstatic struct lock_file index_lock;\n \n-\tindex_fd = hold_locked_index(&index_lock, 1);\n+\thold_locked_index(&index_lock, 1);\n \n \tread_cache();\n \n@@ -319,8 +319,7 @@ static int do_recursive_merge(struct commit *base, struct commit *next,\n \t\t\t    next_tree, base_tree, &result);\n \n \tif (active_cache_changed &&\n-\t    (write_cache(index_fd, active_cache, active_nr) ||\n-\t     commit_locked_index(&index_lock)))\n+\t    write_locked_index(&the_index, &index_lock, COMMIT_LOCK))\n \t\t/* TRANSLATORS: %s will be \"revert\" or \"cherry-pick\" */\n \t\tdie(_(\"%s: Unable to write new index file\"), action_name(opts));\n \trollback_lock_file(&index_lock);\n@@ -680,8 +679,7 @@ static void read_and_refresh_cache(struct replay_opts *opts)\n \t\tdie(_(\"git %s: failed to read the index\"), action_name(opts));\n \trefresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL, NULL);\n \tif (the_index.cache_changed && index_fd >= 0) {\n-\t\tif (write_index(&the_index, index_fd) ||\n-\t\t    commit_locked_index(&index_lock))\n+\t\tif (write_locked_index(&the_index, &index_lock, COMMIT_LOCK))\n \t\t\tdie(_(\"git %s: failed to refresh the index\"), action_name(opts));\n \t}\n \trollback_lock_file(&index_lock);\ndiff --git a/test-scrap-cache-tree.c b/test-scrap-cache-tree.c\nindex 4728013..9ebcbca 100644\n--- a/test-scrap-cache-tree.c\n+++ b/test-scrap-cache-tree.c\n@@ -6,12 +6,11 @@ static struct lock_file index_lock;\n \n int main(int ac, char **av)\n {\n-\tint fd = hold_locked_index(&index_lock, 1);\n+\thold_locked_index(&index_lock, 1);\n \tif (read_cache() < 0)\n \t\tdie(\"unable to read index file\");\n \tactive_cache_tree = NULL;\n-\tif (write_cache(fd, active_cache, active_nr)\n-\t    || commit_lock_file(&index_lock))\n+\tif (write_locked_index(&the_index, &index_lock, COMMIT_LOCK))\n \t\tdie(\"unable to write index file\");\n \treturn 0;\n }\n-- \n1.9.1.346.ga2b5940\n"},{"id":"244099","messageId":"1402661991-14977-6-git-send-email-pclouds@gmail.com","threadId":"36906","inReplyTo":"1402661991-14977-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 05/32] read-cache: relocate and unexport commit_locked_index()","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2014-06-13T12:19:24Z","receivedAt":"2014-06-13T12:19:24Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"This function is now only used by write_locked_index(). Move it to\nread-cache.c (because read-cache.c will need to be aware of\nalternate_index_output later) and unexport it.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n cache.h      |  1 -\n lockfile.c   | 20 --------------------\n read-cache.c | 20 ++++++++++++++++++++\n 3 files changed, 20 insertions(+), 21 deletions(-)\n\ndiff --git a/cache.h b/cache.h\nindex 9cc2b97..e44048c 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -552,7 +552,6 @@ extern int commit_lock_file(struct lock_file *);\n extern void update_index_if_able(struct index_state *, struct lock_file *);\n \n extern int hold_locked_index(struct lock_file *, int);\n-extern int commit_locked_index(struct lock_file *);\n extern void set_alternate_index_output(const char *);\n extern int close_lock_file(struct lock_file *);\n extern void rollback_lock_file(struct lock_file *);\ndiff --git a/lockfile.c b/lockfile.c\nindex 8fbcb6a..b706614 100644\n--- a/lockfile.c\n+++ b/lockfile.c\n@@ -5,7 +5,6 @@\n #include \"sigchain.h\"\n \n static struct lock_file *lock_file_list;\n-static const char *alternate_index_output;\n \n static void remove_lock_file(void)\n {\n@@ -252,25 +251,6 @@ int hold_locked_index(struct lock_file *lk, int die_on_error)\n \t\t\t\t\t : 0);\n }\n \n-void set_alternate_index_output(const char *name)\n-{\n-\talternate_index_output = name;\n-}\n-\n-int commit_locked_index(struct lock_file *lk)\n-{\n-\tif (alternate_index_output) {\n-\t\tif (lk->fd >= 0 && close_lock_file(lk))\n-\t\t\treturn -1;\n-\t\tif (rename(lk->filename, alternate_index_output))\n-\t\t\treturn -1;\n-\t\tlk->filename[0] = 0;\n-\t\treturn 0;\n-\t}\n-\telse\n-\t\treturn commit_lock_file(lk);\n-}\n-\n void rollback_lock_file(struct lock_file *lk)\n {\n \tif (lk->filename[0]) {\ndiff --git a/read-cache.c b/read-cache.c\nindex 44d4732..a7b48a9 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -36,6 +36,7 @@ static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,\n #define CACHE_EXT_RESOLVE_UNDO 0x52455543 /* \"REUC\" */\n \n struct index_state the_index;\n+static const char *alternate_index_output;\n \n static void set_index_entry(struct index_state *istate, int nr, struct cache_entry *ce)\n {\n@@ -1875,6 +1876,25 @@ static int do_write_index(struct index_state *istate, int newfd)\n \treturn 0;\n }\n \n+void set_alternate_index_output(const char *name)\n+{\n+\talternate_index_output = name;\n+}\n+\n+static int commit_locked_index(struct lock_file *lk)\n+{\n+\tif (alternate_index_output) {\n+\t\tif (lk->fd >= 0 && close_lock_file(lk))\n+\t\t\treturn -1;\n+\t\tif (rename(lk->filename, alternate_index_output))\n+\t\t\treturn -1;\n+\t\tlk->filename[0] = 0;\n+\t\treturn 0;\n+\t} else {\n+\t\treturn commit_lock_file(lk);\n+\t}\n+}\n+\n static int do_write_locked_index(struct index_state *istate, struct lock_file *lock,\n \t\t\t\t unsigned flags)\n {\n-- \n1.9.1.346.ga2b5940\n"},{"id":"244100","messageId":"1402661991-14977-7-git-send-email-pclouds@gmail.com","threadId":"36906","inReplyTo":"1402661991-14977-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 06/32] read-cache: store in-memory flags in the first 12 bits of ce_flags","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2014-06-13T12:19:25Z","receivedAt":"2014-06-13T12:19:25Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"We're running out of room for in-memory flags. But since b60e188\n(Strip namelen out of ce_flags into a ce_namelen field - 2012-07-11),\nwe copy the namelen (first 12 bits) to ce_namelen field. So those bits\nare free to use. Just make sure we do not accidentally write any\nin-memory flags back.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n cache.h      | 2 +-\n read-cache.c | 2 +-\n 2 files changed, 2 insertions(+), 2 deletions(-)\n\ndiff --git a/cache.h b/cache.h\nindex e44048c..57ad318 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -145,7 +145,7 @@ struct cache_entry {\n #define CE_STAGESHIFT 12\n \n /*\n- * Range 0xFFFF0000 in ce_flags is divided into\n+ * Range 0xFFFF0FFF in ce_flags is divided into\n  * two parts: in-memory flags and on-disk ones.\n  * Flags in CE_EXTENDED_FLAGS will get saved on-disk\n  * if you want to save a new flag, add it in\ndiff --git a/read-cache.c b/read-cache.c\nindex a7b48a9..5e8c06c 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -1702,7 +1702,7 @@ static char *copy_cache_entry_to_ondisk(struct ondisk_cache_entry *ondisk,\n \tondisk->size = htonl(ce->ce_stat_data.sd_size);\n \thashcpy(ondisk->sha1, ce->sha1);\n \n-\tflags = ce->ce_flags;\n+\tflags = ce->ce_flags & ~CE_NAMEMASK;\n \tflags |= (ce_namelen(ce) >= CE_NAMEMASK ? CE_NAMEMASK : ce_namelen(ce));\n \tondisk->flags = htons(flags);\n \tif (ce->ce_flags & CE_EXTENDED) {\n-- \n1.9.1.346.ga2b5940\n"},{"id":"244101","messageId":"1402661991-14977-8-git-send-email-pclouds@gmail.com","threadId":"36906","inReplyTo":"1402661991-14977-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 07/32] read-cache: be strict about \"changed\" in remove_marked_cache_entries()","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2014-06-13T12:19:26Z","receivedAt":"2014-06-13T12:19:26Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"remove_marked_cache_entries() deletes entries marked with\nCE_REMOVE. But if there is no such entry, do not mark the index as\n\"changed\" because that could trigger an index update unnecessarily.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n read-cache.c | 2 ++\n 1 file changed, 2 insertions(+)\n\ndiff --git a/read-cache.c b/read-cache.c\nindex 5e8c06c..c0c2e39 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -510,6 +510,8 @@ void remove_marked_cache_entries(struct index_state *istate)\n \t\telse\n \t\t\tce_array[j++] = ce_array[i];\n \t}\n+\tif (j == istate->cache_nr)\n+\t\treturn;\n \tistate->cache_changed = 1;\n \tistate->cache_nr = j;\n }\n-- \n1.9.1.346.ga2b5940\n"},{"id":"244102","messageId":"1402661991-14977-9-git-send-email-pclouds@gmail.com","threadId":"36906","inReplyTo":"1402661991-14977-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 08/32] read-cache: be specific what part of the index has changed","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2014-06-13T12:19:27Z","receivedAt":"2014-06-13T12:19:27Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"cache entry additions, removals and modifications are separated\nout. The rest of changes are still in the catch-all flag\nSOMETHING_CHANGED, which would be more specific later.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/update-index.c |  6 +++---\n cache.h                |  5 +++++\n read-cache.c           | 11 ++++++-----\n resolve-undo.c         |  2 +-\n 4 files changed, 15 insertions(+), 9 deletions(-)\n\ndiff --git a/builtin/update-index.c b/builtin/update-index.c\nindex 42cbe4b..d2654d6 100644\n--- a/builtin/update-index.c\n+++ b/builtin/update-index.c\n@@ -56,7 +56,7 @@ static int mark_ce_flags(const char *path, int flag, int mark)\n \t\telse\n \t\t\tactive_cache[pos]->ce_flags &= ~flag;\n \t\tcache_tree_invalidate_path(active_cache_tree, path);\n-\t\tactive_cache_changed = 1;\n+\t\tactive_cache_changed = SOMETHING_CHANGED;\n \t\treturn 0;\n \t}\n \treturn -1;\n@@ -268,7 +268,7 @@ static void chmod_path(int flip, const char *path)\n \t\tgoto fail;\n \t}\n \tcache_tree_invalidate_path(active_cache_tree, path);\n-\tactive_cache_changed = 1;\n+\tactive_cache_changed = SOMETHING_CHANGED;\n \treport(\"chmod %cx '%s'\", flip, path);\n \treturn;\n  fail:\n@@ -889,7 +889,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)\n \t\t\t    INDEX_FORMAT_LB, INDEX_FORMAT_UB);\n \n \t\tif (the_index.version != preferred_index_format)\n-\t\t\tactive_cache_changed = 1;\n+\t\t\tactive_cache_changed = SOMETHING_CHANGED;\n \t\tthe_index.version = preferred_index_format;\n \t}\n \ndiff --git a/cache.h b/cache.h\nindex 57ad318..31d4541 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -268,6 +268,11 @@ static inline unsigned int canon_mode(unsigned int mode)\n \n #define cache_entry_size(len) (offsetof(struct cache_entry,name) + (len) + 1)\n \n+#define SOMETHING_CHANGED\t(1 << 0) /* unclassified changes go here */\n+#define CE_ENTRY_CHANGED\t(1 << 1)\n+#define CE_ENTRY_REMOVED\t(1 << 2)\n+#define CE_ENTRY_ADDED\t\t(1 << 3)\n+\n struct index_state {\n \tstruct cache_entry **cache;\n \tunsigned int version;\ndiff --git a/read-cache.c b/read-cache.c\nindex c0c2e39..035c72e 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -51,7 +51,7 @@ static void replace_index_entry(struct index_state *istate, int nr, struct cache\n \tremove_name_hash(istate, old);\n \tfree(old);\n \tset_index_entry(istate, nr, ce);\n-\tistate->cache_changed = 1;\n+\tistate->cache_changed |= CE_ENTRY_CHANGED;\n }\n \n void rename_index_entry_at(struct index_state *istate, int nr, const char *new_name)\n@@ -482,7 +482,7 @@ int remove_index_entry_at(struct index_state *istate, int pos)\n \trecord_resolve_undo(istate, ce);\n \tremove_name_hash(istate, ce);\n \tfree(ce);\n-\tistate->cache_changed = 1;\n+\tistate->cache_changed |= CE_ENTRY_REMOVED;\n \tistate->cache_nr--;\n \tif (pos >= istate->cache_nr)\n \t\treturn 0;\n@@ -512,7 +512,7 @@ void remove_marked_cache_entries(struct index_state *istate)\n \t}\n \tif (j == istate->cache_nr)\n \t\treturn;\n-\tistate->cache_changed = 1;\n+\tistate->cache_changed |= CE_ENTRY_REMOVED;\n \tistate->cache_nr = j;\n }\n \n@@ -1002,7 +1002,7 @@ int add_index_entry(struct index_state *istate, struct cache_entry *ce, int opti\n \t\t\tistate->cache + pos,\n \t\t\t(istate->cache_nr - pos - 1) * sizeof(ce));\n \tset_index_entry(istate, pos, ce);\n-\tistate->cache_changed = 1;\n+\tistate->cache_changed |= CE_ENTRY_ADDED;\n \treturn 0;\n }\n \n@@ -1101,6 +1101,7 @@ static struct cache_entry *refresh_cache_ent(struct index_state *istate,\n \t    !(ce->ce_flags & CE_VALID))\n \t\tupdated->ce_flags &= ~CE_VALID;\n \n+\t/* istate->cache_changed is updated in the caller */\n \treturn updated;\n }\n \n@@ -1182,7 +1183,7 @@ int refresh_index(struct index_state *istate, unsigned int flags,\n \t\t\t\t * means the index is not valid anymore.\n \t\t\t\t */\n \t\t\t\tce->ce_flags &= ~CE_VALID;\n-\t\t\t\tistate->cache_changed = 1;\n+\t\t\t\tistate->cache_changed |= CE_ENTRY_CHANGED;\n \t\t\t}\n \t\t\tif (quiet)\n \t\t\t\tcontinue;\ndiff --git a/resolve-undo.c b/resolve-undo.c\nindex 44c697c..e9dff57 100644\n--- a/resolve-undo.c\n+++ b/resolve-undo.c\n@@ -110,7 +110,7 @@ void resolve_undo_clear_index(struct index_state *istate)\n \tstring_list_clear(resolve_undo, 1);\n \tfree(resolve_undo);\n \tistate->resolve_undo = NULL;\n-\tistate->cache_changed = 1;\n+\tistate->cache_changed = SOMETHING_CHANGED;\n }\n \n int unmerge_index_entry_at(struct index_state *istate, int pos)\n-- \n1.9.1.346.ga2b5940\n"},{"id":"244103","messageId":"1402661991-14977-10-git-send-email-pclouds@gmail.com","threadId":"36906","inReplyTo":"1402661991-14977-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 09/32] update-index: be specific what part of the index has changed","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2014-06-13T12:19:28Z","receivedAt":"2014-06-13T12:19:28Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/update-index.c | 6 +++---\n 1 file changed, 3 insertions(+), 3 deletions(-)\n\ndiff --git a/builtin/update-index.c b/builtin/update-index.c\nindex d2654d6..e0e881b 100644\n--- a/builtin/update-index.c\n+++ b/builtin/update-index.c\n@@ -56,7 +56,7 @@ static int mark_ce_flags(const char *path, int flag, int mark)\n \t\telse\n \t\t\tactive_cache[pos]->ce_flags &= ~flag;\n \t\tcache_tree_invalidate_path(active_cache_tree, path);\n-\t\tactive_cache_changed = SOMETHING_CHANGED;\n+\t\tactive_cache_changed |= CE_ENTRY_CHANGED;\n \t\treturn 0;\n \t}\n \treturn -1;\n@@ -268,7 +268,7 @@ static void chmod_path(int flip, const char *path)\n \t\tgoto fail;\n \t}\n \tcache_tree_invalidate_path(active_cache_tree, path);\n-\tactive_cache_changed = SOMETHING_CHANGED;\n+\tactive_cache_changed |= CE_ENTRY_CHANGED;\n \treport(\"chmod %cx '%s'\", flip, path);\n \treturn;\n  fail:\n@@ -889,7 +889,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)\n \t\t\t    INDEX_FORMAT_LB, INDEX_FORMAT_UB);\n \n \t\tif (the_index.version != preferred_index_format)\n-\t\t\tactive_cache_changed = SOMETHING_CHANGED;\n+\t\t\tactive_cache_changed |= SOMETHING_CHANGED;\n \t\tthe_index.version = preferred_index_format;\n \t}\n \n-- \n1.9.1.346.ga2b5940\n"},{"id":"244104","messageId":"1402661991-14977-11-git-send-email-pclouds@gmail.com","threadId":"36906","inReplyTo":"1402661991-14977-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 10/32] resolve-undo: be specific what part of the index has changed","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2014-06-13T12:19:29Z","receivedAt":"2014-06-13T12:19:29Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n cache.h        | 1 +\n resolve-undo.c | 2 +-\n 2 files changed, 2 insertions(+), 1 deletion(-)\n\ndiff --git a/cache.h b/cache.h\nindex 31d4541..976f2e0 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -272,6 +272,7 @@ static inline unsigned int canon_mode(unsigned int mode)\n #define CE_ENTRY_CHANGED\t(1 << 1)\n #define CE_ENTRY_REMOVED\t(1 << 2)\n #define CE_ENTRY_ADDED\t\t(1 << 3)\n+#define RESOLVE_UNDO_CHANGED\t(1 << 4)\n \n struct index_state {\n \tstruct cache_entry **cache;\ndiff --git a/resolve-undo.c b/resolve-undo.c\nindex e9dff57..468a2eb 100644\n--- a/resolve-undo.c\n+++ b/resolve-undo.c\n@@ -110,7 +110,7 @@ void resolve_undo_clear_index(struct index_state *istate)\n \tstring_list_clear(resolve_undo, 1);\n \tfree(resolve_undo);\n \tistate->resolve_undo = NULL;\n-\tistate->cache_changed = SOMETHING_CHANGED;\n+\tistate->cache_changed |= RESOLVE_UNDO_CHANGED;\n }\n \n int unmerge_index_entry_at(struct index_state *istate, int pos)\n-- \n1.9.1.346.ga2b5940\n"},{"id":"244106","messageId":"1402661991-14977-12-git-send-email-pclouds@gmail.com","threadId":"36906","inReplyTo":"1402661991-14977-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 11/32] unpack-trees: be specific what part of the index has changed","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2014-06-13T12:19:30Z","receivedAt":"2014-06-13T12:19:30Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n unpack-trees.c | 8 ++++++--\n 1 file changed, 6 insertions(+), 2 deletions(-)\n\ndiff --git a/unpack-trees.c b/unpack-trees.c\nindex 97fc995..a722685 100644\n--- a/unpack-trees.c\n+++ b/unpack-trees.c\n@@ -246,7 +246,9 @@ static int verify_absent_sparse(const struct cache_entry *ce,\n \t\t\t\tenum unpack_trees_error_types,\n \t\t\t\tstruct unpack_trees_options *o);\n \n-static int apply_sparse_checkout(struct cache_entry *ce, struct unpack_trees_options *o)\n+static int apply_sparse_checkout(struct index_state *istate,\n+\t\t\t\t struct cache_entry *ce,\n+\t\t\t\t struct unpack_trees_options *o)\n {\n \tint was_skip_worktree = ce_skip_worktree(ce);\n \n@@ -254,6 +256,8 @@ static int apply_sparse_checkout(struct cache_entry *ce, struct unpack_trees_opt\n \t\tce->ce_flags |= CE_SKIP_WORKTREE;\n \telse\n \t\tce->ce_flags &= ~CE_SKIP_WORKTREE;\n+\tif (was_skip_worktree != ce_skip_worktree(ce))\n+\t\tistate->cache_changed |= CE_ENTRY_CHANGED;\n \n \t/*\n \t * if (!was_skip_worktree && !ce_skip_worktree()) {\n@@ -1131,7 +1135,7 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options\n \t\t\t\tret = -1;\n \t\t\t}\n \n-\t\t\tif (apply_sparse_checkout(ce, o)) {\n+\t\t\tif (apply_sparse_checkout(&o->result, ce, o)) {\n \t\t\t\tif (!o->show_all_errors)\n \t\t\t\t\tgoto return_failed;\n \t\t\t\tret = -1;\n-- \n1.9.1.346.ga2b5940\n"},{"id":"244105","messageId":"1402661991-14977-13-git-send-email-pclouds@gmail.com","threadId":"36906","inReplyTo":"1402661991-14977-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 12/32] cache-tree: mark istate->cache_changed on cache tree invalidation","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2014-06-13T12:19:31Z","receivedAt":"2014-06-13T12:19:31Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/blame.c        |  2 +-\n builtin/update-index.c |  4 ++--\n cache-tree.c           | 15 +++++++++++----\n cache-tree.h           |  2 +-\n cache.h                |  1 +\n read-cache.c           |  6 +++---\n unpack-trees.c         |  2 +-\n 7 files changed, 20 insertions(+), 12 deletions(-)\n\ndiff --git a/builtin/blame.c b/builtin/blame.c\nindex 88cb799..914d919 100644\n--- a/builtin/blame.c\n+++ b/builtin/blame.c\n@@ -2126,7 +2126,7 @@ static struct commit *fake_working_tree_commit(struct diff_options *opt,\n \t * right now, but someday we might optimize diff-index --cached\n \t * with cache-tree information.\n \t */\n-\tcache_tree_invalidate_path(active_cache_tree, path);\n+\tcache_tree_invalidate_path(&the_index, path);\n \n \treturn commit;\n }\ndiff --git a/builtin/update-index.c b/builtin/update-index.c\nindex e0e881b..fa3c441 100644\n--- a/builtin/update-index.c\n+++ b/builtin/update-index.c\n@@ -55,7 +55,7 @@ static int mark_ce_flags(const char *path, int flag, int mark)\n \t\t\tactive_cache[pos]->ce_flags |= flag;\n \t\telse\n \t\t\tactive_cache[pos]->ce_flags &= ~flag;\n-\t\tcache_tree_invalidate_path(active_cache_tree, path);\n+\t\tcache_tree_invalidate_path(&the_index, path);\n \t\tactive_cache_changed |= CE_ENTRY_CHANGED;\n \t\treturn 0;\n \t}\n@@ -267,7 +267,7 @@ static void chmod_path(int flip, const char *path)\n \tdefault:\n \t\tgoto fail;\n \t}\n-\tcache_tree_invalidate_path(active_cache_tree, path);\n+\tcache_tree_invalidate_path(&the_index, path);\n \tactive_cache_changed |= CE_ENTRY_CHANGED;\n \treport(\"chmod %cx '%s'\", flip, path);\n \treturn;\ndiff --git a/cache-tree.c b/cache-tree.c\nindex 52f8692..23ddc73 100644\n--- a/cache-tree.c\n+++ b/cache-tree.c\n@@ -98,7 +98,7 @@ struct cache_tree_sub *cache_tree_sub(struct cache_tree *it, const char *path)\n \treturn find_subtree(it, path, pathlen, 1);\n }\n \n-void cache_tree_invalidate_path(struct cache_tree *it, const char *path)\n+static int do_invalidate_path(struct cache_tree *it, const char *path)\n {\n \t/* a/b/c\n \t * ==> invalidate self\n@@ -116,7 +116,7 @@ void cache_tree_invalidate_path(struct cache_tree *it, const char *path)\n #endif\n \n \tif (!it)\n-\t\treturn;\n+\t\treturn 0;\n \tslash = strchrnul(path, '/');\n \tnamelen = slash - path;\n \tit->entry_count = -1;\n@@ -137,11 +137,18 @@ void cache_tree_invalidate_path(struct cache_tree *it, const char *path)\n \t\t\t\t(it->subtree_nr - pos - 1));\n \t\t\tit->subtree_nr--;\n \t\t}\n-\t\treturn;\n+\t\treturn 1;\n \t}\n \tdown = find_subtree(it, path, namelen, 0);\n \tif (down)\n-\t\tcache_tree_invalidate_path(down->cache_tree, slash + 1);\n+\t\tdo_invalidate_path(down->cache_tree, slash + 1);\n+\treturn 1;\n+}\n+\n+void cache_tree_invalidate_path(struct index_state *istate, const char *path)\n+{\n+\tif (do_invalidate_path(istate->cache_tree, path))\n+\t\tistate->cache_changed |= CACHE_TREE_CHANGED;\n }\n \n static int verify_cache(const struct cache_entry * const *cache,\ndiff --git a/cache-tree.h b/cache-tree.h\nindex f1923ad..dfbcfab 100644\n--- a/cache-tree.h\n+++ b/cache-tree.h\n@@ -23,7 +23,7 @@ struct cache_tree {\n \n struct cache_tree *cache_tree(void);\n void cache_tree_free(struct cache_tree **);\n-void cache_tree_invalidate_path(struct cache_tree *, const char *);\n+void cache_tree_invalidate_path(struct index_state *, const char *);\n struct cache_tree_sub *cache_tree_sub(struct cache_tree *, const char *);\n \n void cache_tree_write(struct strbuf *, struct cache_tree *root);\ndiff --git a/cache.h b/cache.h\nindex 976f2e0..b4edbcd 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -273,6 +273,7 @@ static inline unsigned int canon_mode(unsigned int mode)\n #define CE_ENTRY_REMOVED\t(1 << 2)\n #define CE_ENTRY_ADDED\t\t(1 << 3)\n #define RESOLVE_UNDO_CHANGED\t(1 << 4)\n+#define CACHE_TREE_CHANGED\t(1 << 5)\n \n struct index_state {\n \tstruct cache_entry **cache;\ndiff --git a/read-cache.c b/read-cache.c\nindex 035c72e..d1ad227 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -65,7 +65,7 @@ void rename_index_entry_at(struct index_state *istate, int nr, const char *new_n\n \tnew->ce_namelen = namelen;\n \tmemcpy(new->name, new_name, namelen + 1);\n \n-\tcache_tree_invalidate_path(istate->cache_tree, old->name);\n+\tcache_tree_invalidate_path(istate, old->name);\n \tremove_index_entry_at(istate, nr);\n \tadd_index_entry(istate, new, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);\n }\n@@ -521,7 +521,7 @@ int remove_file_from_index(struct index_state *istate, const char *path)\n \tint pos = index_name_pos(istate, path, strlen(path));\n \tif (pos < 0)\n \t\tpos = -pos-1;\n-\tcache_tree_invalidate_path(istate->cache_tree, path);\n+\tcache_tree_invalidate_path(istate, path);\n \twhile (pos < istate->cache_nr && !strcmp(istate->cache[pos]->name, path))\n \t\tremove_index_entry_at(istate, pos);\n \treturn 0;\n@@ -939,7 +939,7 @@ static int add_index_entry_with_check(struct index_state *istate, struct cache_e\n \tint skip_df_check = option & ADD_CACHE_SKIP_DFCHECK;\n \tint new_only = option & ADD_CACHE_NEW_ONLY;\n \n-\tcache_tree_invalidate_path(istate->cache_tree, ce->name);\n+\tcache_tree_invalidate_path(istate, ce->name);\n \tpos = index_name_stage_pos(istate, ce->name, ce_namelen(ce), ce_stage(ce));\n \n \t/* existing match? Just replace it. */\ndiff --git a/unpack-trees.c b/unpack-trees.c\nindex a722685..3beff8a 100644\n--- a/unpack-trees.c\n+++ b/unpack-trees.c\n@@ -1263,7 +1263,7 @@ static void invalidate_ce_path(const struct cache_entry *ce,\n \t\t\t       struct unpack_trees_options *o)\n {\n \tif (ce)\n-\t\tcache_tree_invalidate_path(o->src_index->cache_tree, ce->name);\n+\t\tcache_tree_invalidate_path(o->src_index, ce->name);\n }\n \n /*\n-- \n1.9.1.346.ga2b5940\n"},{"id":"244107","messageId":"1402661991-14977-14-git-send-email-pclouds@gmail.com","threadId":"36906","inReplyTo":"1402661991-14977-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 13/32] cache-tree: mark istate->cache_changed on cache tree update","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2014-06-13T12:19:32Z","receivedAt":"2014-06-13T12:19:32Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n cache-tree.c           | 25 +++++++++++--------------\n cache-tree.h           |  2 +-\n merge-recursive.c      |  4 +---\n sequencer.c            |  4 +---\n test-dump-cache-tree.c |  7 ++++---\n 5 files changed, 18 insertions(+), 24 deletions(-)\n\ndiff --git a/cache-tree.c b/cache-tree.c\nindex 23ddc73..18055f1 100644\n--- a/cache-tree.c\n+++ b/cache-tree.c\n@@ -151,7 +151,7 @@ void cache_tree_invalidate_path(struct index_state *istate, const char *path)\n \t\tistate->cache_changed |= CACHE_TREE_CHANGED;\n }\n \n-static int verify_cache(const struct cache_entry * const *cache,\n+static int verify_cache(struct cache_entry **cache,\n \t\t\tint entries, int flags)\n {\n \tint i, funny;\n@@ -236,7 +236,7 @@ int cache_tree_fully_valid(struct cache_tree *it)\n }\n \n static int update_one(struct cache_tree *it,\n-\t\t      const struct cache_entry * const *cache,\n+\t\t      struct cache_entry **cache,\n \t\t      int entries,\n \t\t      const char *base,\n \t\t      int baselen,\n@@ -398,18 +398,19 @@ static int update_one(struct cache_tree *it,\n \treturn i;\n }\n \n-int cache_tree_update(struct cache_tree *it,\n-\t\t      const struct cache_entry * const *cache,\n-\t\t      int entries,\n-\t\t      int flags)\n+int cache_tree_update(struct index_state *istate, int flags)\n {\n-\tint i, skip;\n-\ti = verify_cache(cache, entries, flags);\n+\tstruct cache_tree *it = istate->cache_tree;\n+\tstruct cache_entry **cache = istate->cache;\n+\tint entries = istate->cache_nr;\n+\tint skip, i = verify_cache(cache, entries, flags);\n+\n \tif (i)\n \t\treturn i;\n \ti = update_one(it, cache, entries, \"\", 0, &skip, flags);\n \tif (i < 0)\n \t\treturn i;\n+\tistate->cache_changed |= CACHE_TREE_CHANGED;\n \treturn 0;\n }\n \n@@ -597,9 +598,7 @@ int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix)\n \n \twas_valid = cache_tree_fully_valid(active_cache_tree);\n \tif (!was_valid) {\n-\t\tif (cache_tree_update(active_cache_tree,\n-\t\t\t\t      (const struct cache_entry * const *)active_cache,\n-\t\t\t\t      active_nr, flags) < 0)\n+\t\tif (cache_tree_update(&the_index, flags) < 0)\n \t\t\treturn WRITE_TREE_UNMERGED_INDEX;\n \t\tif (0 <= newfd) {\n \t\t\tif (!write_locked_index(&the_index, lock_file, COMMIT_LOCK))\n@@ -698,7 +697,5 @@ int update_main_cache_tree(int flags)\n {\n \tif (!the_index.cache_tree)\n \t\tthe_index.cache_tree = cache_tree();\n-\treturn cache_tree_update(the_index.cache_tree,\n-\t\t\t\t (const struct cache_entry * const *)the_index.cache,\n-\t\t\t\t the_index.cache_nr, flags);\n+\treturn cache_tree_update(&the_index, flags);\n }\ndiff --git a/cache-tree.h b/cache-tree.h\nindex dfbcfab..154b357 100644\n--- a/cache-tree.h\n+++ b/cache-tree.h\n@@ -30,7 +30,7 @@ void cache_tree_write(struct strbuf *, struct cache_tree *root);\n struct cache_tree *cache_tree_read(const char *buffer, unsigned long size);\n \n int cache_tree_fully_valid(struct cache_tree *);\n-int cache_tree_update(struct cache_tree *, const struct cache_entry * const *, int, int);\n+int cache_tree_update(struct index_state *, int);\n \n int update_main_cache_tree(int);\n \ndiff --git a/merge-recursive.c b/merge-recursive.c\nindex 442c1ec..0b5d34d 100644\n--- a/merge-recursive.c\n+++ b/merge-recursive.c\n@@ -265,9 +265,7 @@ struct tree *write_tree_from_memory(struct merge_options *o)\n \t\tactive_cache_tree = cache_tree();\n \n \tif (!cache_tree_fully_valid(active_cache_tree) &&\n-\t    cache_tree_update(active_cache_tree,\n-\t\t\t      (const struct cache_entry * const *)active_cache,\n-\t\t\t      active_nr, 0) < 0)\n+\t    cache_tree_update(&the_index, 0) < 0)\n \t\tdie(_(\"error building trees\"));\n \n \tresult = lookup_tree(active_cache_tree->sha1);\ndiff --git a/sequencer.c b/sequencer.c\nindex 5ebc461..4b709db 100644\n--- a/sequencer.c\n+++ b/sequencer.c\n@@ -371,9 +371,7 @@ static int is_index_unchanged(void)\n \t\tactive_cache_tree = cache_tree();\n \n \tif (!cache_tree_fully_valid(active_cache_tree))\n-\t\tif (cache_tree_update(active_cache_tree,\n-\t\t\t\t      (const struct cache_entry * const *)active_cache,\n-\t\t\t\t      active_nr, 0))\n+\t\tif (cache_tree_update(&the_index, 0))\n \t\t\treturn error(_(\"Unable to update cache tree\\n\"));\n \n \treturn !hashcmp(active_cache_tree->sha1, head_commit->tree->object.sha1);\ndiff --git a/test-dump-cache-tree.c b/test-dump-cache-tree.c\nindex 47eab97..330ba4f 100644\n--- a/test-dump-cache-tree.c\n+++ b/test-dump-cache-tree.c\n@@ -56,11 +56,12 @@ static int dump_cache_tree(struct cache_tree *it,\n \n int main(int ac, char **av)\n {\n+\tstruct index_state istate;\n \tstruct cache_tree *another = cache_tree();\n \tif (read_cache() < 0)\n \t\tdie(\"unable to read index file\");\n-\tcache_tree_update(another,\n-\t\t\t  (const struct cache_entry * const *)active_cache,\n-\t\t\t  active_nr, WRITE_TREE_DRY_RUN);\n+\tistate = the_index;\n+\tistate.cache_tree = another;\n+\tcache_tree_update(&istate, WRITE_TREE_DRY_RUN);\n \treturn dump_cache_tree(active_cache_tree, another, \"\");\n }\n-- \n1.9.1.346.ga2b5940\n"},{"id":"244108","messageId":"1402661991-14977-15-git-send-email-pclouds@gmail.com","threadId":"36906","inReplyTo":"1402661991-14977-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 14/32] cache-tree: mark istate->cache_changed on prime_cache_tree()","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2014-06-13T12:19:33Z","receivedAt":"2014-06-13T12:19:33Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/read-tree.c | 2 +-\n builtin/reset.c     | 2 +-\n cache-tree.c        | 9 +++++----\n cache-tree.h        | 2 +-\n 4 files changed, 8 insertions(+), 7 deletions(-)\n\ndiff --git a/builtin/read-tree.c b/builtin/read-tree.c\nindex f26d90f..3204c62 100644\n--- a/builtin/read-tree.c\n+++ b/builtin/read-tree.c\n@@ -231,7 +231,7 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)\n \t * what came from the tree.\n \t */\n \tif (nr_trees == 1 && !opts.prefix)\n-\t\tprime_cache_tree(&active_cache_tree, trees[0]);\n+\t\tprime_cache_tree(&the_index, trees[0]);\n \n \tif (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))\n \t\tdie(\"unable to write new index file\");\ndiff --git a/builtin/reset.c b/builtin/reset.c\nindex 0c56d28..234b2eb 100644\n--- a/builtin/reset.c\n+++ b/builtin/reset.c\n@@ -84,7 +84,7 @@ static int reset_index(const unsigned char *sha1, int reset_type, int quiet)\n \n \tif (reset_type == MIXED || reset_type == HARD) {\n \t\ttree = parse_tree_indirect(sha1);\n-\t\tprime_cache_tree(&active_cache_tree, tree);\n+\t\tprime_cache_tree(&the_index, tree);\n \t}\n \n \treturn 0;\ndiff --git a/cache-tree.c b/cache-tree.c\nindex 18055f1..c53f7de 100644\n--- a/cache-tree.c\n+++ b/cache-tree.c\n@@ -654,11 +654,12 @@ static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)\n \tit->entry_count = cnt;\n }\n \n-void prime_cache_tree(struct cache_tree **it, struct tree *tree)\n+void prime_cache_tree(struct index_state *istate, struct tree *tree)\n {\n-\tcache_tree_free(it);\n-\t*it = cache_tree();\n-\tprime_cache_tree_rec(*it, tree);\n+\tcache_tree_free(&istate->cache_tree);\n+\tistate->cache_tree = cache_tree();\n+\tprime_cache_tree_rec(istate->cache_tree, tree);\n+\tistate->cache_changed |= CACHE_TREE_CHANGED;\n }\n \n /*\ndiff --git a/cache-tree.h b/cache-tree.h\nindex 154b357..b47ccec 100644\n--- a/cache-tree.h\n+++ b/cache-tree.h\n@@ -46,7 +46,7 @@ int update_main_cache_tree(int);\n #define WRITE_TREE_PREFIX_ERROR (-3)\n \n int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix);\n-void prime_cache_tree(struct cache_tree **, struct tree *);\n+void prime_cache_tree(struct index_state *, struct tree *);\n \n extern int cache_tree_matches_traversal(struct cache_tree *, struct name_entry *ent, struct traverse_info *info);\n \n-- \n1.9.1.346.ga2b5940\n"},{"id":"244109","messageId":"1402661991-14977-16-git-send-email-pclouds@gmail.com","threadId":"36906","inReplyTo":"1402661991-14977-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 15/32] entry.c: update cache_changed if refresh_cache is set in checkout_entry()","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2014-06-13T12:19:34Z","receivedAt":"2014-06-13T12:19:34Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Other fill_stat_cache_info() is on new entries, which should set\nCE_ENTRY_ADDED in cache_changed, so we're safe.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/apply.c          | 8 +++++---\n builtin/checkout-index.c | 1 +\n builtin/checkout.c       | 1 +\n cache.h                  | 1 +\n entry.c                  | 2 ++\n unpack-trees.c           | 1 +\n 6 files changed, 11 insertions(+), 3 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 5e13444..adca035 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -3084,13 +3084,15 @@ static void prepare_fn_table(struct patch *patch)\n \t}\n }\n \n-static int checkout_target(struct cache_entry *ce, struct stat *st)\n+static int checkout_target(struct index_state *istate,\n+\t\t\t   struct cache_entry *ce, struct stat *st)\n {\n \tstruct checkout costate;\n \n \tmemset(&costate, 0, sizeof(costate));\n \tcostate.base_dir = \"\";\n \tcostate.refresh_cache = 1;\n+\tcostate.istate = istate;\n \tif (checkout_entry(ce, &costate, NULL) || lstat(ce->name, st))\n \t\treturn error(_(\"cannot checkout %s\"), ce->name);\n \treturn 0;\n@@ -3257,7 +3259,7 @@ static int load_current(struct image *image, struct patch *patch)\n \tif (lstat(name, &st)) {\n \t\tif (errno != ENOENT)\n \t\t\treturn error(_(\"%s: %s\"), name, strerror(errno));\n-\t\tif (checkout_target(ce, &st))\n+\t\tif (checkout_target(&the_index, ce, &st))\n \t\t\treturn -1;\n \t}\n \tif (verify_index_match(ce, &st))\n@@ -3411,7 +3413,7 @@ static int check_preimage(struct patch *patch, struct cache_entry **ce, struct s\n \t\t}\n \t\t*ce = active_cache[pos];\n \t\tif (stat_ret < 0) {\n-\t\t\tif (checkout_target(*ce, st))\n+\t\t\tif (checkout_target(&the_index, *ce, st))\n \t\t\t\treturn -1;\n \t\t}\n \t\tif (!cached && verify_index_match(*ce, st))\ndiff --git a/builtin/checkout-index.c b/builtin/checkout-index.c\nindex 9e49bf2..05edd9e 100644\n--- a/builtin/checkout-index.c\n+++ b/builtin/checkout-index.c\n@@ -135,6 +135,7 @@ static int option_parse_u(const struct option *opt,\n \tint *newfd = opt->value;\n \n \tstate.refresh_cache = 1;\n+\tstate.istate = &the_index;\n \tif (*newfd < 0)\n \t\t*newfd = hold_locked_index(&lock_file, 1);\n \treturn 0;\ndiff --git a/builtin/checkout.c b/builtin/checkout.c\nindex 944a634..146ab91 100644\n--- a/builtin/checkout.c\n+++ b/builtin/checkout.c\n@@ -336,6 +336,7 @@ static int checkout_paths(const struct checkout_opts *opts,\n \tmemset(&state, 0, sizeof(state));\n \tstate.force = 1;\n \tstate.refresh_cache = 1;\n+\tstate.istate = &the_index;\n \tfor (pos = 0; pos < active_nr; pos++) {\n \t\tstruct cache_entry *ce = active_cache[pos];\n \t\tif (ce->ce_flags & CE_MATCHED) {\ndiff --git a/cache.h b/cache.h\nindex b4edbcd..bf09d1c 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -1063,6 +1063,7 @@ extern int split_ident_line(struct ident_split *, const char *, int);\n extern int ident_cmp(const struct ident_split *, const struct ident_split *);\n \n struct checkout {\n+\tstruct index_state *istate;\n \tconst char *base_dir;\n \tint base_dir_len;\n \tunsigned force:1,\ndiff --git a/entry.c b/entry.c\nindex 77c6882..d913c1d 100644\n--- a/entry.c\n+++ b/entry.c\n@@ -210,9 +210,11 @@ static int write_entry(struct cache_entry *ce,\n \n finish:\n \tif (state->refresh_cache) {\n+\t\tassert(state->istate);\n \t\tif (!fstat_done)\n \t\t\tlstat(ce->name, &st);\n \t\tfill_stat_cache_info(ce, &st);\n+\t\tstate->istate->cache_changed |= CE_ENTRY_CHANGED;\n \t}\n \treturn 0;\n }\ndiff --git a/unpack-trees.c b/unpack-trees.c\nindex 3beff8a..26f65c7 100644\n--- a/unpack-trees.c\n+++ b/unpack-trees.c\n@@ -1029,6 +1029,7 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options\n \tstate.force = 1;\n \tstate.quiet = 1;\n \tstate.refresh_cache = 1;\n+\tstate.istate = &o->result;\n \n \tmemset(&el, 0, sizeof(el));\n \tif (!core_apply_sparse_checkout || !o->update)\n-- \n1.9.1.346.ga2b5940\n"},{"id":"244110","messageId":"1402661991-14977-17-git-send-email-pclouds@gmail.com","threadId":"36906","inReplyTo":"1402661991-14977-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 16/32] read-cache: save index SHA-1 after reading","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2014-06-13T12:19:35Z","receivedAt":"2014-06-13T12:19:35Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Also update SHA-1 after writing. If we do not do that, the second\nread_index() will see \"initialized\" variable already set and not read\n.git/index again, which is fine, except istate->sha1 now has a stale\nvalue.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n cache.h        | 1 +\n read-cache.c   | 6 ++++--\n unpack-trees.c | 1 +\n 3 files changed, 6 insertions(+), 2 deletions(-)\n\ndiff --git a/cache.h b/cache.h\nindex bf09d1c..41cdcd0 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -286,6 +286,7 @@ struct index_state {\n \t\t initialized : 1;\n \tstruct hashmap name_hash;\n \tstruct hashmap dir_hash;\n+\tunsigned char sha1[20];\n };\n \n extern struct index_state the_index;\ndiff --git a/read-cache.c b/read-cache.c\nindex d1ad227..b4653c0 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -1481,6 +1481,7 @@ int read_index_from(struct index_state *istate, const char *path)\n \tif (verify_hdr(hdr, mmap_size) < 0)\n \t\tgoto unmap;\n \n+\thashcpy(istate->sha1, (const unsigned char *)hdr + mmap_size - 20);\n \tistate->version = ntohl(hdr->hdr_version);\n \tistate->cache_nr = ntohl(hdr->hdr_entries);\n \tistate->cache_alloc = alloc_nr(istate->cache_nr);\n@@ -1616,7 +1617,7 @@ static int write_index_ext_header(git_SHA_CTX *context, int fd,\n \t\t(ce_write(context, fd, &sz, 4) < 0)) ? -1 : 0;\n }\n \n-static int ce_flush(git_SHA_CTX *context, int fd)\n+static int ce_flush(git_SHA_CTX *context, int fd, unsigned char *sha1)\n {\n \tunsigned int left = write_buffer_len;\n \n@@ -1634,6 +1635,7 @@ static int ce_flush(git_SHA_CTX *context, int fd)\n \n \t/* Append the SHA1 signature at the end */\n \tgit_SHA1_Final(write_buffer + left, context);\n+\thashcpy(sha1, write_buffer + left);\n \tleft += 20;\n \treturn (write_in_full(fd, write_buffer, left) != left) ? -1 : 0;\n }\n@@ -1872,7 +1874,7 @@ static int do_write_index(struct index_state *istate, int newfd)\n \t\t\treturn -1;\n \t}\n \n-\tif (ce_flush(&c, newfd) || fstat(newfd, &st))\n+\tif (ce_flush(&c, newfd, istate->sha1) || fstat(newfd, &st))\n \t\treturn -1;\n \tistate->timestamp.sec = (unsigned int)st.st_mtime;\n \tistate->timestamp.nsec = ST_MTIME_NSEC(st);\ndiff --git a/unpack-trees.c b/unpack-trees.c\nindex 26f65c7..f594932 100644\n--- a/unpack-trees.c\n+++ b/unpack-trees.c\n@@ -1046,6 +1046,7 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options\n \to->result.timestamp.sec = o->src_index->timestamp.sec;\n \to->result.timestamp.nsec = o->src_index->timestamp.nsec;\n \to->result.version = o->src_index->version;\n+\thashcpy(o->result.sha1, o->src_index->sha1);\n \to->merge_size = len;\n \tmark_all_ce_unused(o->src_index);\n \n-- \n1.9.1.346.ga2b5940\n"},{"id":"244111","messageId":"1402661991-14977-18-git-send-email-pclouds@gmail.com","threadId":"36906","inReplyTo":"1402661991-14977-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 17/32] read-cache: split-index mode","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2014-06-13T12:19:36Z","receivedAt":"2014-06-13T12:19:36Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"This split-index mode is designed to keep write cost proportional to\nthe number of changes the user has made, not the size of the work\ntree. (Read cost is another matter, to be dealt separately.)\n\nThis mode stores index info in a pair of $GIT_DIR/index and\n$GIT_DIR/sharedindex.<SHA-1>. sharedindex is large and unchanged over\ntime while \"index\" is smaller and updated often. Format details are in\nindex-format.txt, although not everything is implemented in this\npatch.\n\nShared indexes are not automatically removed, because it's unclear if\nthe shared index is needed by any (even temporary) indexes by just\nlooking at it. After a while you'll collect stale shared indexes. The\ngood news is one shared index is useable for long, until\n$GIT_DIR/index becomes too big and sluggish that the new shared index\nmust be created.\n\nThe safest way to clean shared indexes is to turn off split index\nmode, so shared files are all garbage, delete them all, then turn on\nsplit index mode again.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n Documentation/gitrepository-layout.txt   |  4 ++\n Documentation/technical/index-format.txt | 35 ++++++++++++\n Makefile                                 |  1 +\n cache.h                                  |  3 +\n read-cache.c                             | 96 ++++++++++++++++++++++++++++++--\n split-index.c (new)                      | 90 ++++++++++++++++++++++++++++++\n split-index.h (new)                      | 25 +++++++++\n unpack-trees.c                           |  4 ++\n 8 files changed, 253 insertions(+), 5 deletions(-)\n create mode 100644 split-index.c\n create mode 100644 split-index.h\n\ndiff --git a/Documentation/gitrepository-layout.txt b/Documentation/gitrepository-layout.txt\nindex 17d2ea6..79653f3 100644\n--- a/Documentation/gitrepository-layout.txt\n+++ b/Documentation/gitrepository-layout.txt\n@@ -155,6 +155,10 @@ index::\n \tThe current index file for the repository.  It is\n \tusually not found in a bare repository.\n \n+sharedindex.<SHA-1>::\n+\tThe shared index part, to be referenced by $GIT_DIR/index and\n+\tother temporary index files. Only valid in split index mode.\n+\n info::\n \tAdditional information about the repository is recorded\n \tin this directory.\ndiff --git a/Documentation/technical/index-format.txt b/Documentation/technical/index-format.txt\nindex f352a9b..fe6f316 100644\n--- a/Documentation/technical/index-format.txt\n+++ b/Documentation/technical/index-format.txt\n@@ -129,6 +129,9 @@ Git index format\n   (Version 4) In version 4, the padding after the pathname does not\n   exist.\n \n+  Interpretation of index entries in split index mode is completely\n+  different. See below for details.\n+\n == Extensions\n \n === Cached tree\n@@ -198,3 +201,35 @@ Git index format\n   - At most three 160-bit object names of the entry in stages from 1 to 3\n     (nothing is written for a missing stage).\n \n+=== Split index\n+\n+  In split index mode, the majority of index entries could be stored\n+  in a separate file. This extension records the changes to be made on\n+  top of that to produce the final index.\n+\n+  The signature for this extension is { 'l', 'i, 'n', 'k' }.\n+\n+  The extension consists of:\n+\n+  - 160-bit SHA-1 of the shared index file. The shared index file path\n+    is $GIT_DIR/sharedindex.<SHA-1>. If all 160 bits are zero, the\n+    index does not require a shared index file.\n+\n+  - An ewah-encoded delete bitmap, each bit represents an entry in the\n+    shared index. If a bit is set, its corresponding entry in the\n+    shared index will be removed from the final index.  Note, because\n+    a delete operation changes index entry positions, but we do need\n+    original positions in replace phase, it's best to just mark\n+    entries for removal, then do a mass deletion after replacement.\n+\n+  - An ewah-encoded replace bitmap, each bit represents an entry in\n+    the shared index. If a bit is set, its corresponding entry in the\n+    shared index will be replaced with an entry in this index\n+    file. All replaced entries are stored in sorted order in this\n+    index. The first \"1\" bit in the replace bitmap corresponds to the\n+    first index entry, the second \"1\" bit to the second entry and so\n+    on. Replaced entries may have empty path names to save space.\n+\n+  The remaining index entries after replaced ones will be added to the\n+  final index. These added entries are also sorted by entry namme then\n+  stage.\ndiff --git a/Makefile b/Makefile\nindex 81e8214..94ad3ce 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -887,6 +887,7 @@ LIB_OBJS += sha1_name.o\n LIB_OBJS += shallow.o\n LIB_OBJS += sideband.o\n LIB_OBJS += sigchain.o\n+LIB_OBJS += split-index.o\n LIB_OBJS += strbuf.o\n LIB_OBJS += streaming.o\n LIB_OBJS += string-list.o\ndiff --git a/cache.h b/cache.h\nindex 41cdcd0..47fe374 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -135,6 +135,7 @@ struct cache_entry {\n \tunsigned int ce_mode;\n \tunsigned int ce_flags;\n \tunsigned int ce_namelen;\n+\tunsigned int index;\t/* for link extension */\n \tunsigned char sha1[20];\n \tchar name[FLEX_ARRAY]; /* more */\n };\n@@ -275,12 +276,14 @@ static inline unsigned int canon_mode(unsigned int mode)\n #define RESOLVE_UNDO_CHANGED\t(1 << 4)\n #define CACHE_TREE_CHANGED\t(1 << 5)\n \n+struct split_index;\n struct index_state {\n \tstruct cache_entry **cache;\n \tunsigned int version;\n \tunsigned int cache_nr, cache_alloc, cache_changed;\n \tstruct string_list *resolve_undo;\n \tstruct cache_tree *cache_tree;\n+\tstruct split_index *split_index;\n \tstruct cache_time timestamp;\n \tunsigned name_hash_initialized : 1,\n \t\t initialized : 1;\ndiff --git a/read-cache.c b/read-cache.c\nindex b4653c0..90a3f09 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -14,6 +14,7 @@\n #include \"resolve-undo.h\"\n #include \"strbuf.h\"\n #include \"varint.h\"\n+#include \"split-index.h\"\n \n static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,\n \t\t\t\t\t       unsigned int options);\n@@ -34,6 +35,10 @@ static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,\n #define CACHE_EXT(s) ( (s[0]<<24)|(s[1]<<16)|(s[2]<<8)|(s[3]) )\n #define CACHE_EXT_TREE 0x54524545\t/* \"TREE\" */\n #define CACHE_EXT_RESOLVE_UNDO 0x52455543 /* \"REUC\" */\n+#define CACHE_EXT_LINK 0x6c696e6b\t  /* \"link\" */\n+\n+/* changes that can be kept in $GIT_DIR/index (basically all extensions) */\n+#define EXTMASK (RESOLVE_UNDO_CHANGED | CACHE_TREE_CHANGED)\n \n struct index_state the_index;\n static const char *alternate_index_output;\n@@ -63,6 +68,7 @@ void rename_index_entry_at(struct index_state *istate, int nr, const char *new_n\n \tcopy_cache_entry(new, old);\n \tnew->ce_flags &= ~CE_HASHED;\n \tnew->ce_namelen = namelen;\n+\tnew->index = 0;\n \tmemcpy(new->name, new_name, namelen + 1);\n \n \tcache_tree_invalidate_path(istate, old->name);\n@@ -1335,6 +1341,10 @@ static int read_index_extension(struct index_state *istate,\n \tcase CACHE_EXT_RESOLVE_UNDO:\n \t\tistate->resolve_undo = resolve_undo_read(data, sz);\n \t\tbreak;\n+\tcase CACHE_EXT_LINK:\n+\t\tif (read_link_extension(istate, data, sz))\n+\t\t\treturn -1;\n+\t\tbreak;\n \tdefault:\n \t\tif (*ext < 'A' || 'Z' < *ext)\n \t\t\treturn error(\"index uses %.4s extension, which we do not understand\",\n@@ -1369,6 +1379,7 @@ static struct cache_entry *cache_entry_from_ondisk(struct ondisk_cache_entry *on\n \tce->ce_stat_data.sd_size  = get_be32(&ondisk->size);\n \tce->ce_flags = flags & ~CE_NAMEMASK;\n \tce->ce_namelen = len;\n+\tce->index = 0;\n \thashcpy(ce->sha1, ondisk->sha1);\n \tmemcpy(ce->name, name, len);\n \tce->name[len] = '\\0';\n@@ -1443,7 +1454,8 @@ static struct cache_entry *create_from_disk(struct ondisk_cache_entry *ondisk,\n }\n \n /* remember to discard_cache() before reading a different cache! */\n-int read_index_from(struct index_state *istate, const char *path)\n+static int do_read_index(struct index_state *istate, const char *path,\n+\t\t\t int must_exist)\n {\n \tint fd, i;\n \tstruct stat st;\n@@ -1460,9 +1472,9 @@ int read_index_from(struct index_state *istate, const char *path)\n \tistate->timestamp.nsec = 0;\n \tfd = open(path, O_RDONLY);\n \tif (fd < 0) {\n-\t\tif (errno == ENOENT)\n+\t\tif (!must_exist && errno == ENOENT)\n \t\t\treturn 0;\n-\t\tdie_errno(\"index file open failed\");\n+\t\tdie_errno(\"%s: index file open failed\", path);\n \t}\n \n \tif (fstat(fd, &st))\n@@ -1535,6 +1547,42 @@ unmap:\n \tdie(\"index file corrupt\");\n }\n \n+int read_index_from(struct index_state *istate, const char *path)\n+{\n+\tstruct split_index *split_index;\n+\tint ret;\n+\n+\t/* istate->initialized covers both .git/index and .git/sharedindex.xxx */\n+\tif (istate->initialized)\n+\t\treturn istate->cache_nr;\n+\n+\tret = do_read_index(istate, path, 0);\n+\tsplit_index = istate->split_index;\n+\tif (!split_index)\n+\t\treturn ret;\n+\n+\tif (is_null_sha1(split_index->base_sha1))\n+\t\treturn ret;\n+\tif (istate->cache_nr)\n+\t\tdie(\"index in split-index mode must contain no entries\");\n+\n+\tif (split_index->base)\n+\t\tdiscard_index(split_index->base);\n+\telse\n+\t\tsplit_index->base = xcalloc(1, sizeof(*split_index->base));\n+\tret = do_read_index(split_index->base,\n+\t\t\t    git_path(\"sharedindex.%s\",\n+\t\t\t\t     sha1_to_hex(split_index->base_sha1)), 1);\n+\tif (hashcmp(split_index->base_sha1, split_index->base->sha1))\n+\t\tdie(\"broken index, expect %s in %s, got %s\",\n+\t\t    sha1_to_hex(split_index->base_sha1),\n+\t\t    git_path(\"sharedindex.%s\",\n+\t\t\t\t     sha1_to_hex(split_index->base_sha1)),\n+\t\t    sha1_to_hex(split_index->base->sha1));\n+\tmerge_base_index(istate);\n+\treturn ret;\n+}\n+\n int is_index_unborn(struct index_state *istate)\n {\n \treturn (!istate->cache_nr && !istate->timestamp.sec);\n@@ -1544,8 +1592,15 @@ int discard_index(struct index_state *istate)\n {\n \tint i;\n \n-\tfor (i = 0; i < istate->cache_nr; i++)\n+\tfor (i = 0; i < istate->cache_nr; i++) {\n+\t\tif (istate->cache[i]->index &&\n+\t\t    istate->split_index &&\n+\t\t    istate->split_index->base &&\n+\t\t    istate->cache[i]->index <= istate->split_index->base->cache_nr &&\n+\t\t    istate->cache[i] == istate->split_index->base->cache[istate->cache[i]->index - 1])\n+\t\t\tcontinue;\n \t\tfree(istate->cache[i]);\n+\t}\n \tresolve_undo_clear_index(istate);\n \tistate->cache_nr = 0;\n \tistate->cache_changed = 0;\n@@ -1557,6 +1612,7 @@ int discard_index(struct index_state *istate)\n \tfree(istate->cache);\n \tistate->cache = NULL;\n \tistate->cache_alloc = 0;\n+\tdiscard_split_index(istate);\n \treturn 0;\n }\n \n@@ -1852,6 +1908,17 @@ static int do_write_index(struct index_state *istate, int newfd)\n \tstrbuf_release(&previous_name_buf);\n \n \t/* Write extension data here */\n+\tif (istate->split_index) {\n+\t\tstruct strbuf sb = STRBUF_INIT;\n+\n+\t\terr = write_link_extension(&sb, istate) < 0 ||\n+\t\t\twrite_index_ext_header(&c, newfd, CACHE_EXT_LINK,\n+\t\t\t\t\t       sb.len) < 0 ||\n+\t\t\tce_write(&c, newfd, sb.buf, sb.len) < 0;\n+\t\tstrbuf_release(&sb);\n+\t\tif (err)\n+\t\t\treturn -1;\n+\t}\n \tif (istate->cache_tree) {\n \t\tstruct strbuf sb = STRBUF_INIT;\n \n@@ -1916,10 +1983,29 @@ static int do_write_locked_index(struct index_state *istate, struct lock_file *l\n \t\treturn ret;\n }\n \n+static int write_split_index(struct index_state *istate,\n+\t\t\t     struct lock_file *lock,\n+\t\t\t     unsigned flags)\n+{\n+\tint ret;\n+\tprepare_to_write_split_index(istate);\n+\tret = do_write_locked_index(istate, lock, flags);\n+\tfinish_writing_split_index(istate);\n+\treturn ret;\n+}\n+\n int write_locked_index(struct index_state *istate, struct lock_file *lock,\n \t\t       unsigned flags)\n {\n-\treturn do_write_locked_index(istate, lock, flags);\n+\tstruct split_index *si = istate->split_index;\n+\n+\tif (!si || (istate->cache_changed & ~EXTMASK)) {\n+\t\tif (si)\n+\t\t\thashclr(si->base_sha1);\n+\t\treturn do_write_locked_index(istate, lock, flags);\n+\t}\n+\n+\treturn write_split_index(istate, lock, flags);\n }\n \n /*\ndiff --git a/split-index.c b/split-index.c\nnew file mode 100644\nindex 0000000..63b52bb\n--- /dev/null\n+++ b/split-index.c\n@@ -0,0 +1,90 @@\n+#include \"cache.h\"\n+#include \"split-index.h\"\n+\n+struct split_index *init_split_index(struct index_state *istate)\n+{\n+\tif (!istate->split_index) {\n+\t\tistate->split_index = xcalloc(1, sizeof(*istate->split_index));\n+\t\tistate->split_index->refcount = 1;\n+\t}\n+\treturn istate->split_index;\n+}\n+\n+int read_link_extension(struct index_state *istate,\n+\t\t\t const void *data_, unsigned long sz)\n+{\n+\tconst unsigned char *data = data_;\n+\tstruct split_index *si;\n+\tif (sz < 20)\n+\t\treturn error(\"corrupt link extension (too short)\");\n+\tsi = init_split_index(istate);\n+\thashcpy(si->base_sha1, data);\n+\tdata += 20;\n+\tsz -= 20;\n+\tif (sz)\n+\t\treturn error(\"garbage at the end of link extension\");\n+\treturn 0;\n+}\n+\n+int write_link_extension(struct strbuf *sb,\n+\t\t\t struct index_state *istate)\n+{\n+\tstruct split_index *si = istate->split_index;\n+\tstrbuf_add(sb, si->base_sha1, 20);\n+\treturn 0;\n+}\n+\n+static void mark_base_index_entries(struct index_state *base)\n+{\n+\tint i;\n+\t/*\n+\t * To keep track of the shared entries between\n+\t * istate->base->cache[] and istate->cache[], base entry\n+\t * position is stored in each base entry. All positions start\n+\t * from 1 instead of 0, which is resrved to say \"this is a new\n+\t * entry\".\n+\t */\n+\tfor (i = 0; i < base->cache_nr; i++)\n+\t\tbase->cache[i]->index = i + 1;\n+}\n+\n+void merge_base_index(struct index_state *istate)\n+{\n+\tstruct split_index *si = istate->split_index;\n+\n+\tmark_base_index_entries(si->base);\n+\tistate->cache_nr = si->base->cache_nr;\n+\tALLOC_GROW(istate->cache, istate->cache_nr, istate->cache_alloc);\n+\tmemcpy(istate->cache, si->base->cache,\n+\t       sizeof(*istate->cache) * istate->cache_nr);\n+}\n+\n+void prepare_to_write_split_index(struct index_state *istate)\n+{\n+\tstruct split_index *si = init_split_index(istate);\n+\t/* take cache[] out temporarily */\n+\tsi->saved_cache_nr = istate->cache_nr;\n+\tistate->cache_nr = 0;\n+}\n+\n+void finish_writing_split_index(struct index_state *istate)\n+{\n+\tstruct split_index *si = init_split_index(istate);\n+\tistate->cache_nr = si->saved_cache_nr;\n+}\n+\n+void discard_split_index(struct index_state *istate)\n+{\n+\tstruct split_index *si = istate->split_index;\n+\tif (!si)\n+\t\treturn;\n+\tistate->split_index = NULL;\n+\tsi->refcount--;\n+\tif (si->refcount)\n+\t\treturn;\n+\tif (si->base) {\n+\t\tdiscard_index(si->base);\n+\t\tfree(si->base);\n+\t}\n+\tfree(si);\n+}\ndiff --git a/split-index.h b/split-index.h\nnew file mode 100644\nindex 0000000..8d74041\n--- /dev/null\n+++ b/split-index.h\n@@ -0,0 +1,25 @@\n+#ifndef SPLIT_INDEX_H\n+#define SPLIT_INDEX_H\n+\n+struct index_state;\n+struct strbuf;\n+\n+struct split_index {\n+\tunsigned char base_sha1[20];\n+\tstruct index_state *base;\n+\tunsigned int saved_cache_nr;\n+\tint refcount;\n+};\n+\n+struct split_index *init_split_index(struct index_state *istate);\n+int read_link_extension(struct index_state *istate,\n+\t\t\tconst void *data, unsigned long sz);\n+int write_link_extension(struct strbuf *sb,\n+\t\t\t struct index_state *istate);\n+void move_cache_to_base_index(struct index_state *istate);\n+void merge_base_index(struct index_state *istate);\n+void prepare_to_write_split_index(struct index_state *istate);\n+void finish_writing_split_index(struct index_state *istate);\n+void discard_split_index(struct index_state *istate);\n+\n+#endif\ndiff --git a/unpack-trees.c b/unpack-trees.c\nindex f594932..a941f7c 100644\n--- a/unpack-trees.c\n+++ b/unpack-trees.c\n@@ -8,6 +8,7 @@\n #include \"progress.h\"\n #include \"refs.h\"\n #include \"attr.h\"\n+#include \"split-index.h\"\n \n /*\n  * Error messages expected by scripts out of plumbing commands such as\n@@ -1046,6 +1047,9 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options\n \to->result.timestamp.sec = o->src_index->timestamp.sec;\n \to->result.timestamp.nsec = o->src_index->timestamp.nsec;\n \to->result.version = o->src_index->version;\n+\to->result.split_index = o->src_index->split_index;\n+\tif (o->result.split_index)\n+\t\to->result.split_index->refcount++;\n \thashcpy(o->result.sha1, o->src_index->sha1);\n \to->merge_size = len;\n \tmark_all_ce_unused(o->src_index);\n-- \n1.9.1.346.ga2b5940\n"},{"id":"244112","messageId":"1402661991-14977-19-git-send-email-pclouds@gmail.com","threadId":"36906","inReplyTo":"1402661991-14977-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 18/32] read-cache: mark new entries for split index","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2014-06-13T12:19:37Z","receivedAt":"2014-06-13T12:19:37Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Make sure entry addition does not lead to unifying the index. We don't\nneed to explicitly keep track of new entries. If ce->index is zero,\nthey're new. Otherwise it's unlikely that they are new, but we'll do a\nthorough check later at writing time.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n read-cache.c | 3 ++-\n 1 file changed, 2 insertions(+), 1 deletion(-)\n\ndiff --git a/read-cache.c b/read-cache.c\nindex 90a3f09..52a27b3 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -38,7 +38,8 @@ static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,\n #define CACHE_EXT_LINK 0x6c696e6b\t  /* \"link\" */\n \n /* changes that can be kept in $GIT_DIR/index (basically all extensions) */\n-#define EXTMASK (RESOLVE_UNDO_CHANGED | CACHE_TREE_CHANGED)\n+#define EXTMASK (RESOLVE_UNDO_CHANGED | CACHE_TREE_CHANGED | \\\n+\t\t CE_ENTRY_ADDED)\n \n struct index_state the_index;\n static const char *alternate_index_output;\n-- \n1.9.1.346.ga2b5940\n"},{"id":"244113","messageId":"1402661991-14977-20-git-send-email-pclouds@gmail.com","threadId":"36906","inReplyTo":"1402661991-14977-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 19/32] read-cache: save deleted entries in split index","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2014-06-13T12:19:38Z","receivedAt":"2014-06-13T12:19:38Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Entries that belong to the base index should not be freed. Mark\nCE_REMOVE to track them.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n read-cache.c  | 14 ++++++++------\n split-index.c | 12 ++++++++++++\n split-index.h |  1 +\n 3 files changed, 21 insertions(+), 6 deletions(-)\n\ndiff --git a/read-cache.c b/read-cache.c\nindex 52a27b3..c437585 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -39,7 +39,7 @@ static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,\n \n /* changes that can be kept in $GIT_DIR/index (basically all extensions) */\n #define EXTMASK (RESOLVE_UNDO_CHANGED | CACHE_TREE_CHANGED | \\\n-\t\t CE_ENTRY_ADDED)\n+\t\t CE_ENTRY_ADDED | CE_ENTRY_REMOVED)\n \n struct index_state the_index;\n static const char *alternate_index_output;\n@@ -488,7 +488,7 @@ int remove_index_entry_at(struct index_state *istate, int pos)\n \n \trecord_resolve_undo(istate, ce);\n \tremove_name_hash(istate, ce);\n-\tfree(ce);\n+\tsave_or_free_index_entry(istate, ce);\n \tistate->cache_changed |= CE_ENTRY_REMOVED;\n \tistate->cache_nr--;\n \tif (pos >= istate->cache_nr)\n@@ -512,7 +512,7 @@ void remove_marked_cache_entries(struct index_state *istate)\n \tfor (i = j = 0; i < istate->cache_nr; i++) {\n \t\tif (ce_array[i]->ce_flags & CE_REMOVE) {\n \t\t\tremove_name_hash(istate, ce_array[i]);\n-\t\t\tfree(ce_array[i]);\n+\t\t\tsave_or_free_index_entry(istate, ce_array[i]);\n \t\t}\n \t\telse\n \t\t\tce_array[j++] = ce_array[i];\n@@ -577,7 +577,9 @@ static int different_name(struct cache_entry *ce, struct cache_entry *alias)\n  * So we use the CE_ADDED flag to verify that the alias was an old\n  * one before we accept it as\n  */\n-static struct cache_entry *create_alias_ce(struct cache_entry *ce, struct cache_entry *alias)\n+static struct cache_entry *create_alias_ce(struct index_state *istate,\n+\t\t\t\t\t   struct cache_entry *ce,\n+\t\t\t\t\t   struct cache_entry *alias)\n {\n \tint len;\n \tstruct cache_entry *new;\n@@ -590,7 +592,7 @@ static struct cache_entry *create_alias_ce(struct cache_entry *ce, struct cache_\n \tnew = xcalloc(1, cache_entry_size(len));\n \tmemcpy(new->name, alias->name, len);\n \tcopy_cache_entry(new, ce);\n-\tfree(ce);\n+\tsave_or_free_index_entry(istate, ce);\n \treturn new;\n }\n \n@@ -683,7 +685,7 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,\n \t\tset_object_name_for_intent_to_add_entry(ce);\n \n \tif (ignore_case && alias && different_name(ce, alias))\n-\t\tce = create_alias_ce(ce, alias);\n+\t\tce = create_alias_ce(istate, ce, alias);\n \tce->ce_flags |= CE_ADDED;\n \n \t/* It was suspected to be racily clean, but it turns out to be Ok */\ndiff --git a/split-index.c b/split-index.c\nindex 63b52bb..2bb5d55 100644\n--- a/split-index.c\n+++ b/split-index.c\n@@ -88,3 +88,15 @@ void discard_split_index(struct index_state *istate)\n \t}\n \tfree(si);\n }\n+\n+void save_or_free_index_entry(struct index_state *istate, struct cache_entry *ce)\n+{\n+\tif (ce->index &&\n+\t    istate->split_index &&\n+\t    istate->split_index->base &&\n+\t    ce->index <= istate->split_index->base->cache_nr &&\n+\t    ce == istate->split_index->base->cache[ce->index - 1])\n+\t\tce->ce_flags |= CE_REMOVE;\n+\telse\n+\t\tfree(ce);\n+}\ndiff --git a/split-index.h b/split-index.h\nindex 8d74041..5302118 100644\n--- a/split-index.h\n+++ b/split-index.h\n@@ -12,6 +12,7 @@ struct split_index {\n };\n \n struct split_index *init_split_index(struct index_state *istate);\n+void save_or_free_index_entry(struct index_state *istate, struct cache_entry *ce);\n int read_link_extension(struct index_state *istate,\n \t\t\tconst void *data, unsigned long sz);\n int write_link_extension(struct strbuf *sb,\n-- \n1.9.1.346.ga2b5940\n"},{"id":"244114","messageId":"1402661991-14977-21-git-send-email-pclouds@gmail.com","threadId":"36906","inReplyTo":"1402661991-14977-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 20/32] read-cache: mark updated entries for split index","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2014-06-13T12:19:39Z","receivedAt":"2014-06-13T12:19:39Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"The large part of this patch just follows CE_ENTRY_CHANGED\nmarks. replace_index_entry() is updated to update\nsplit_index->base->cache[] as well so base->cache[] does not reference\nto a freed entry.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/update-index.c |  2 ++\n cache.h                |  2 ++\n entry.c                |  1 +\n read-cache.c           |  5 ++++-\n split-index.c          | 15 +++++++++++++++\n split-index.h          |  3 +++\n unpack-trees.c         |  4 +++-\n 7 files changed, 30 insertions(+), 2 deletions(-)\n\ndiff --git a/builtin/update-index.c b/builtin/update-index.c\nindex fa3c441..f7a19c4 100644\n--- a/builtin/update-index.c\n+++ b/builtin/update-index.c\n@@ -55,6 +55,7 @@ static int mark_ce_flags(const char *path, int flag, int mark)\n \t\t\tactive_cache[pos]->ce_flags |= flag;\n \t\telse\n \t\t\tactive_cache[pos]->ce_flags &= ~flag;\n+\t\tactive_cache[pos]->ce_flags |= CE_UPDATE_IN_BASE;\n \t\tcache_tree_invalidate_path(&the_index, path);\n \t\tactive_cache_changed |= CE_ENTRY_CHANGED;\n \t\treturn 0;\n@@ -268,6 +269,7 @@ static void chmod_path(int flip, const char *path)\n \t\tgoto fail;\n \t}\n \tcache_tree_invalidate_path(&the_index, path);\n+\tce->ce_flags |= CE_UPDATE_IN_BASE;\n \tactive_cache_changed |= CE_ENTRY_CHANGED;\n \treport(\"chmod %cx '%s'\", flip, path);\n \treturn;\ndiff --git a/cache.h b/cache.h\nindex 47fe374..295bf9d 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -169,6 +169,8 @@ struct cache_entry {\n /* used to temporarily mark paths matched by pathspecs */\n #define CE_MATCHED           (1 << 26)\n \n+#define CE_UPDATE_IN_BASE    (1 << 27)\n+\n /*\n  * Extended on-disk flags\n  */\ndiff --git a/entry.c b/entry.c\nindex d913c1d..1eda8e9 100644\n--- a/entry.c\n+++ b/entry.c\n@@ -214,6 +214,7 @@ finish:\n \t\tif (!fstat_done)\n \t\t\tlstat(ce->name, &st);\n \t\tfill_stat_cache_info(ce, &st);\n+\t\tce->ce_flags |= CE_UPDATE_IN_BASE;\n \t\tstate->istate->cache_changed |= CE_ENTRY_CHANGED;\n \t}\n \treturn 0;\ndiff --git a/read-cache.c b/read-cache.c\nindex c437585..7966fb7 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -39,7 +39,7 @@ static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,\n \n /* changes that can be kept in $GIT_DIR/index (basically all extensions) */\n #define EXTMASK (RESOLVE_UNDO_CHANGED | CACHE_TREE_CHANGED | \\\n-\t\t CE_ENTRY_ADDED | CE_ENTRY_REMOVED)\n+\t\t CE_ENTRY_ADDED | CE_ENTRY_REMOVED | CE_ENTRY_CHANGED)\n \n struct index_state the_index;\n static const char *alternate_index_output;\n@@ -54,9 +54,11 @@ static void replace_index_entry(struct index_state *istate, int nr, struct cache\n {\n \tstruct cache_entry *old = istate->cache[nr];\n \n+\treplace_index_entry_in_base(istate, old, ce);\n \tremove_name_hash(istate, old);\n \tfree(old);\n \tset_index_entry(istate, nr, ce);\n+\tce->ce_flags |= CE_UPDATE_IN_BASE;\n \tistate->cache_changed |= CE_ENTRY_CHANGED;\n }\n \n@@ -1192,6 +1194,7 @@ int refresh_index(struct index_state *istate, unsigned int flags,\n \t\t\t\t * means the index is not valid anymore.\n \t\t\t\t */\n \t\t\t\tce->ce_flags &= ~CE_VALID;\n+\t\t\t\tce->ce_flags |= CE_UPDATE_IN_BASE;\n \t\t\t\tistate->cache_changed |= CE_ENTRY_CHANGED;\n \t\t\t}\n \t\t\tif (quiet)\ndiff --git a/split-index.c b/split-index.c\nindex 2bb5d55..b36c73b 100644\n--- a/split-index.c\n+++ b/split-index.c\n@@ -100,3 +100,18 @@ void save_or_free_index_entry(struct index_state *istate, struct cache_entry *ce\n \telse\n \t\tfree(ce);\n }\n+\n+void replace_index_entry_in_base(struct index_state *istate,\n+\t\t\t\t struct cache_entry *old,\n+\t\t\t\t struct cache_entry *new)\n+{\n+\tif (old->index &&\n+\t    istate->split_index &&\n+\t    istate->split_index->base &&\n+\t    old->index <= istate->split_index->base->cache_nr) {\n+\t\tnew->index = old->index;\n+\t\tif (old != istate->split_index->base->cache[new->index - 1])\n+\t\t\tfree(istate->split_index->base->cache[new->index - 1]);\n+\t\tistate->split_index->base->cache[new->index - 1] = new;\n+\t}\n+}\ndiff --git a/split-index.h b/split-index.h\nindex 5302118..812e510 100644\n--- a/split-index.h\n+++ b/split-index.h\n@@ -13,6 +13,9 @@ struct split_index {\n \n struct split_index *init_split_index(struct index_state *istate);\n void save_or_free_index_entry(struct index_state *istate, struct cache_entry *ce);\n+void replace_index_entry_in_base(struct index_state *istate,\n+\t\t\t\t struct cache_entry *old,\n+\t\t\t\t struct cache_entry *new);\n int read_link_extension(struct index_state *istate,\n \t\t\tconst void *data, unsigned long sz);\n int write_link_extension(struct strbuf *sb,\ndiff --git a/unpack-trees.c b/unpack-trees.c\nindex a941f7c..4a9cdf2 100644\n--- a/unpack-trees.c\n+++ b/unpack-trees.c\n@@ -257,8 +257,10 @@ static int apply_sparse_checkout(struct index_state *istate,\n \t\tce->ce_flags |= CE_SKIP_WORKTREE;\n \telse\n \t\tce->ce_flags &= ~CE_SKIP_WORKTREE;\n-\tif (was_skip_worktree != ce_skip_worktree(ce))\n+\tif (was_skip_worktree != ce_skip_worktree(ce)) {\n+\t\tce->ce_flags |= CE_UPDATE_IN_BASE;\n \t\tistate->cache_changed |= CE_ENTRY_CHANGED;\n+\t}\n \n \t/*\n \t * if (!was_skip_worktree && !ce_skip_worktree()) {\n-- \n1.9.1.346.ga2b5940\n"},{"id":"244115","messageId":"1402661991-14977-22-git-send-email-pclouds@gmail.com","threadId":"36906","inReplyTo":"1402661991-14977-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 21/32] split-index: the writing part","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2014-06-13T12:19:40Z","receivedAt":"2014-06-13T12:19:40Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"prepare_to_write_split_index() does the major work, classifying\ndeleted, updated and added entries. write_link_extension() then just\nwrites it down.\n\nAn observation is, deleting an entry, then adding it back is recorded\nas \"entry X is deleted, entry X is added\", not \"entry X is replaced\".\nThis is simpler, with small overhead: a replaced entry is stored\nwithout its path, a new entry is store with its path.\n\nA note about unpack_trees() and the deduplication code inside\nprepare_to_write_split_index(). Usually tracking updated/removed\nentries via read-cache API is enough. unpack_trees() manipulates the\nindex in a different way: it throws the entire source index out,\nbuilds up a new one, copying/duplicating entries (using dup_entry)\nfrom the source index over if necessary, then returns the new index.\n\nA naive solution would be marking the entire source index \"deleted\"\nand add their duplicates as new. That could bring $GIT_DIR/index back\nto the original size. So we try harder and memcmp() between the\noriginal and the duplicate to see if it needs updating.\n\nWe could avoid memcmp() too, by avoiding duplicating the original\nentry in dup_entry(). The performance gain this way is within noise\nlevel and it complicates unpack-trees.c. So memcmp() is the preferred\nway to deal with deduplication.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n split-index.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--\n split-index.h |   4 +++\n 2 files changed, 103 insertions(+), 2 deletions(-)\n\ndiff --git a/split-index.c b/split-index.c\nindex b36c73b..5708807 100644\n--- a/split-index.c\n+++ b/split-index.c\n@@ -1,5 +1,6 @@\n #include \"cache.h\"\n #include \"split-index.h\"\n+#include \"ewah/ewok.h\"\n \n struct split_index *init_split_index(struct index_state *istate)\n {\n@@ -26,11 +27,22 @@ int read_link_extension(struct index_state *istate,\n \treturn 0;\n }\n \n+static int write_strbuf(void *user_data, const void *data, size_t len)\n+{\n+\tstruct strbuf *sb = user_data;\n+\tstrbuf_add(sb, data, len);\n+\treturn len;\n+}\n+\n int write_link_extension(struct strbuf *sb,\n \t\t\t struct index_state *istate)\n {\n \tstruct split_index *si = istate->split_index;\n \tstrbuf_add(sb, si->base_sha1, 20);\n+\tif (!si->delete_bitmap && !si->replace_bitmap)\n+\t\treturn 0;\n+\tewah_serialize_to(si->delete_bitmap, write_strbuf, sb);\n+\tewah_serialize_to(si->replace_bitmap, write_strbuf, sb);\n \treturn 0;\n }\n \n@@ -62,14 +74,99 @@ void merge_base_index(struct index_state *istate)\n void prepare_to_write_split_index(struct index_state *istate)\n {\n \tstruct split_index *si = init_split_index(istate);\n-\t/* take cache[] out temporarily */\n+\tstruct cache_entry **entries = NULL, *ce;\n+\tint i, nr_entries = 0, nr_alloc = 0;\n+\n+\tsi->delete_bitmap = ewah_new();\n+\tsi->replace_bitmap = ewah_new();\n+\n+\tif (si->base) {\n+\t\t/* Go through istate->cache[] and mark CE_MATCHED to\n+\t\t * entry with positive index. We'll go through\n+\t\t * base->cache[] later to delete all entries in base\n+\t\t * that are not marked eith either CE_MATCHED or\n+\t\t * CE_UPDATE_IN_BASE. If istate->cache[i] is a\n+\t\t * duplicate, deduplicate it.\n+\t\t */\n+\t\tfor (i = 0; i < istate->cache_nr; i++) {\n+\t\t\tstruct cache_entry *base;\n+\t\t\t/* namelen is checked separately */\n+\t\t\tconst unsigned int ondisk_flags =\n+\t\t\t\tCE_STAGEMASK | CE_VALID | CE_EXTENDED_FLAGS;\n+\t\t\tunsigned int ce_flags, base_flags, ret;\n+\t\t\tce = istate->cache[i];\n+\t\t\tif (!ce->index)\n+\t\t\t\tcontinue;\n+\t\t\tif (ce->index > si->base->cache_nr) {\n+\t\t\t\tce->index = 0;\n+\t\t\t\tcontinue;\n+\t\t\t}\n+\t\t\tce->ce_flags |= CE_MATCHED; /* or \"shared\" */\n+\t\t\tbase = si->base->cache[ce->index - 1];\n+\t\t\tif (ce == base)\n+\t\t\t\tcontinue;\n+\t\t\tif (ce->ce_namelen != base->ce_namelen ||\n+\t\t\t    strcmp(ce->name, base->name)) {\n+\t\t\t\tce->index = 0;\n+\t\t\t\tcontinue;\n+\t\t\t}\n+\t\t\tce_flags = ce->ce_flags;\n+\t\t\tbase_flags = base->ce_flags;\n+\t\t\t/* only on-disk flags matter */\n+\t\t\tce->ce_flags   &= ondisk_flags;\n+\t\t\tbase->ce_flags &= ondisk_flags;\n+\t\t\tret = memcmp(&ce->ce_stat_data, &base->ce_stat_data,\n+\t\t\t\t     offsetof(struct cache_entry, name) -\n+\t\t\t\t     offsetof(struct cache_entry, ce_stat_data));\n+\t\t\tce->ce_flags = ce_flags;\n+\t\t\tbase->ce_flags = base_flags;\n+\t\t\tif (ret)\n+\t\t\t\tce->ce_flags |= CE_UPDATE_IN_BASE;\n+\t\t\tfree(base);\n+\t\t\tsi->base->cache[ce->index - 1] = ce;\n+\t\t}\n+\t\tfor (i = 0; i < si->base->cache_nr; i++) {\n+\t\t\tce = si->base->cache[i];\n+\t\t\tif ((ce->ce_flags & CE_REMOVE) ||\n+\t\t\t    !(ce->ce_flags & CE_MATCHED))\n+\t\t\t\tewah_set(si->delete_bitmap, i);\n+\t\t\telse if (ce->ce_flags & CE_UPDATE_IN_BASE) {\n+\t\t\t\tewah_set(si->replace_bitmap, i);\n+\t\t\t\tALLOC_GROW(entries, nr_entries+1, nr_alloc);\n+\t\t\t\tentries[nr_entries++] = ce;\n+\t\t\t}\n+\t\t}\n+\t}\n+\n+\tfor (i = 0; i < istate->cache_nr; i++) {\n+\t\tce = istate->cache[i];\n+\t\tif ((!si->base || !ce->index) && !(ce->ce_flags & CE_REMOVE)) {\n+\t\t\tALLOC_GROW(entries, nr_entries+1, nr_alloc);\n+\t\t\tentries[nr_entries++] = ce;\n+\t\t}\n+\t\tce->ce_flags &= ~CE_MATCHED;\n+\t}\n+\n+\t/*\n+\t * take cache[] out temporarily, put entries[] in its place\n+\t * for writing\n+\t */\n+\tsi->saved_cache = istate->cache;\n \tsi->saved_cache_nr = istate->cache_nr;\n-\tistate->cache_nr = 0;\n+\tistate->cache = entries;\n+\tistate->cache_nr = nr_entries;\n }\n \n void finish_writing_split_index(struct index_state *istate)\n {\n \tstruct split_index *si = init_split_index(istate);\n+\n+\tewah_free(si->delete_bitmap);\n+\tewah_free(si->replace_bitmap);\n+\tsi->delete_bitmap = NULL;\n+\tsi->replace_bitmap = NULL;\n+\tfree(istate->cache);\n+\tistate->cache = si->saved_cache;\n \tistate->cache_nr = si->saved_cache_nr;\n }\n \ndiff --git a/split-index.h b/split-index.h\nindex 812e510..53b778f 100644\n--- a/split-index.h\n+++ b/split-index.h\n@@ -3,10 +3,14 @@\n \n struct index_state;\n struct strbuf;\n+struct ewah_bitmap;\n \n struct split_index {\n \tunsigned char base_sha1[20];\n \tstruct index_state *base;\n+\tstruct ewah_bitmap *delete_bitmap;\n+\tstruct ewah_bitmap *replace_bitmap;\n+\tstruct cache_entry **saved_cache;\n \tunsigned int saved_cache_nr;\n \tint refcount;\n };\n-- \n1.9.1.346.ga2b5940\n"},{"id":"244116","messageId":"1402661991-14977-23-git-send-email-pclouds@gmail.com","threadId":"36906","inReplyTo":"1402661991-14977-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 22/32] split-index: the reading part","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2014-06-13T12:19:41Z","receivedAt":"2014-06-13T12:19:41Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"CE_REMOVE'd entries are removed here because only parts of the code\nbase (unpack_trees in fact) test this bit when they look for the\npresence of an entry. Leaving them may confuse the code ignores this\nbit and expects to see a real entry.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n read-cache.c  |  2 --\n split-index.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--\n split-index.h |  2 ++\n 3 files changed, 84 insertions(+), 4 deletions(-)\n\ndiff --git a/read-cache.c b/read-cache.c\nindex 7966fb7..1a7ef7f 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -1569,8 +1569,6 @@ int read_index_from(struct index_state *istate, const char *path)\n \n \tif (is_null_sha1(split_index->base_sha1))\n \t\treturn ret;\n-\tif (istate->cache_nr)\n-\t\tdie(\"index in split-index mode must contain no entries\");\n \n \tif (split_index->base)\n \t\tdiscard_index(split_index->base);\ndiff --git a/split-index.c b/split-index.c\nindex 5708807..b03a250 100644\n--- a/split-index.c\n+++ b/split-index.c\n@@ -16,13 +16,27 @@ int read_link_extension(struct index_state *istate,\n {\n \tconst unsigned char *data = data_;\n \tstruct split_index *si;\n+\tint ret;\n+\n \tif (sz < 20)\n \t\treturn error(\"corrupt link extension (too short)\");\n \tsi = init_split_index(istate);\n \thashcpy(si->base_sha1, data);\n \tdata += 20;\n \tsz -= 20;\n-\tif (sz)\n+\tif (!sz)\n+\t\treturn 0;\n+\tsi->delete_bitmap = ewah_new();\n+\tret = ewah_read_mmap(si->delete_bitmap, data, sz);\n+\tif (ret < 0)\n+\t\treturn error(\"corrupt delete bitmap in link extension\");\n+\tdata += ret;\n+\tsz -= ret;\n+\tsi->replace_bitmap = ewah_new();\n+\tret = ewah_read_mmap(si->replace_bitmap, data, sz);\n+\tif (ret < 0)\n+\t\treturn error(\"corrupt replace bitmap in link extension\");\n+\tif (ret != sz)\n \t\treturn error(\"garbage at the end of link extension\");\n \treturn 0;\n }\n@@ -60,15 +74,81 @@ static void mark_base_index_entries(struct index_state *base)\n \t\tbase->cache[i]->index = i + 1;\n }\n \n+static void mark_entry_for_delete(size_t pos, void *data)\n+{\n+\tstruct index_state *istate = data;\n+\tif (pos >= istate->cache_nr)\n+\t\tdie(\"position for delete %d exceeds base index size %d\",\n+\t\t    (int)pos, istate->cache_nr);\n+\tistate->cache[pos]->ce_flags |= CE_REMOVE;\n+\tistate->split_index->nr_deletions = 1;\n+}\n+\n+static void replace_entry(size_t pos, void *data)\n+{\n+\tstruct index_state *istate = data;\n+\tstruct split_index *si = istate->split_index;\n+\tstruct cache_entry *dst, *src;\n+\tif (pos >= istate->cache_nr)\n+\t\tdie(\"position for replacement %d exceeds base index size %d\",\n+\t\t    (int)pos, istate->cache_nr);\n+\tif (si->nr_replacements >= si->saved_cache_nr)\n+\t\tdie(\"too many replacements (%d vs %d)\",\n+\t\t    si->nr_replacements, si->saved_cache_nr);\n+\tdst = istate->cache[pos];\n+\tif (dst->ce_flags & CE_REMOVE)\n+\t\tdie(\"entry %d is marked as both replaced and deleted\",\n+\t\t    (int)pos);\n+\tsrc = si->saved_cache[si->nr_replacements];\n+\tsrc->index = pos + 1;\n+\tsrc->ce_flags |= CE_UPDATE_IN_BASE;\n+\tfree(dst);\n+\tdst = src;\n+\tsi->nr_replacements++;\n+}\n+\n void merge_base_index(struct index_state *istate)\n {\n \tstruct split_index *si = istate->split_index;\n+\tunsigned int i;\n \n \tmark_base_index_entries(si->base);\n-\tistate->cache_nr = si->base->cache_nr;\n+\n+\tsi->saved_cache\t    = istate->cache;\n+\tsi->saved_cache_nr  = istate->cache_nr;\n+\tistate->cache_nr    = si->base->cache_nr;\n+\tistate->cache\t    = NULL;\n+\tistate->cache_alloc = 0;\n \tALLOC_GROW(istate->cache, istate->cache_nr, istate->cache_alloc);\n \tmemcpy(istate->cache, si->base->cache,\n \t       sizeof(*istate->cache) * istate->cache_nr);\n+\n+\tsi->nr_deletions = 0;\n+\tsi->nr_replacements = 0;\n+\tewah_each_bit(si->replace_bitmap, replace_entry, istate);\n+\tewah_each_bit(si->delete_bitmap, mark_entry_for_delete, istate);\n+\tif (si->nr_deletions)\n+\t\tremove_marked_cache_entries(istate);\n+\n+\tfor (i = si->nr_replacements; i < si->saved_cache_nr; i++) {\n+\t\tadd_index_entry(istate, si->saved_cache[i],\n+\t\t\t\tADD_CACHE_OK_TO_ADD |\n+\t\t\t\t/*\n+\t\t\t\t * we may have to replay what\n+\t\t\t\t * merge-recursive.c:update_stages()\n+\t\t\t\t * does, which has this flag on\n+\t\t\t\t */\n+\t\t\t\tADD_CACHE_SKIP_DFCHECK);\n+\t\tsi->saved_cache[i] = NULL;\n+\t}\n+\n+\tewah_free(si->delete_bitmap);\n+\tewah_free(si->replace_bitmap);\n+\tfree(si->saved_cache);\n+\tsi->delete_bitmap  = NULL;\n+\tsi->replace_bitmap = NULL;\n+\tsi->saved_cache\t   = NULL;\n+\tsi->saved_cache_nr = 0;\n }\n \n void prepare_to_write_split_index(struct index_state *istate)\ndiff --git a/split-index.h b/split-index.h\nindex 53b778f..c1324f5 100644\n--- a/split-index.h\n+++ b/split-index.h\n@@ -12,6 +12,8 @@ struct split_index {\n \tstruct ewah_bitmap *replace_bitmap;\n \tstruct cache_entry **saved_cache;\n \tunsigned int saved_cache_nr;\n+\tunsigned int nr_deletions;\n+\tunsigned int nr_replacements;\n \tint refcount;\n };\n \n-- \n1.9.1.346.ga2b5940\n"},{"id":"244117","messageId":"1402661991-14977-24-git-send-email-pclouds@gmail.com","threadId":"36906","inReplyTo":"1402661991-14977-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 23/32] split-index: do not invalidate cache-tree at read time","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2014-06-13T12:19:42Z","receivedAt":"2014-06-13T12:19:42Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"We are sure that after merge_base_index() is done. cache-tree can\nstill be used with the final index. So don't destroy cache tree.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n cache.h       | 1 +\n read-cache.c  | 3 ++-\n split-index.c | 1 +\n 3 files changed, 4 insertions(+), 1 deletion(-)\n\ndiff --git a/cache.h b/cache.h\nindex 295bf9d..70fd8ed 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -488,6 +488,7 @@ extern int index_name_pos(const struct index_state *, const char *name, int name\n #define ADD_CACHE_SKIP_DFCHECK 4\t/* Ok to skip DF conflict checks */\n #define ADD_CACHE_JUST_APPEND 8\t\t/* Append only; tree.c::read_tree() */\n #define ADD_CACHE_NEW_ONLY 16\t\t/* Do not replace existing ones */\n+#define ADD_CACHE_KEEP_CACHE_TREE 32\t/* Do not invalidate cache-tree */\n extern int add_index_entry(struct index_state *, struct cache_entry *ce, int option);\n extern void rename_index_entry_at(struct index_state *, int pos, const char *new_name);\n extern int remove_index_entry_at(struct index_state *, int pos);\ndiff --git a/read-cache.c b/read-cache.c\nindex 1a7ef7f..d5f70a0 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -950,7 +950,8 @@ static int add_index_entry_with_check(struct index_state *istate, struct cache_e\n \tint skip_df_check = option & ADD_CACHE_SKIP_DFCHECK;\n \tint new_only = option & ADD_CACHE_NEW_ONLY;\n \n-\tcache_tree_invalidate_path(istate, ce->name);\n+\tif (!(option & ADD_CACHE_KEEP_CACHE_TREE))\n+\t\tcache_tree_invalidate_path(istate, ce->name);\n \tpos = index_name_stage_pos(istate, ce->name, ce_namelen(ce), ce_stage(ce));\n \n \t/* existing match? Just replace it. */\ndiff --git a/split-index.c b/split-index.c\nindex b03a250..33c0c4b 100644\n--- a/split-index.c\n+++ b/split-index.c\n@@ -133,6 +133,7 @@ void merge_base_index(struct index_state *istate)\n \tfor (i = si->nr_replacements; i < si->saved_cache_nr; i++) {\n \t\tadd_index_entry(istate, si->saved_cache[i],\n \t\t\t\tADD_CACHE_OK_TO_ADD |\n+\t\t\t\tADD_CACHE_KEEP_CACHE_TREE |\n \t\t\t\t/*\n \t\t\t\t * we may have to replay what\n \t\t\t\t * merge-recursive.c:update_stages()\n-- \n1.9.1.346.ga2b5940\n"},{"id":"244118","messageId":"1402661991-14977-25-git-send-email-pclouds@gmail.com","threadId":"36906","inReplyTo":"1402661991-14977-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 24/32] split-index: strip pathname of on-disk replaced entries","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2014-06-13T12:19:43Z","receivedAt":"2014-06-13T12:19:43Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"We know the positions of replaced entries via the replace bitmap in\n\"link\" extension, so the \"name\" path does not have to be stored (it's\nstill in the shared index). With this, we also have a way to\ndistinguish additions vs replacements at load time and can catch\nbroken \"link\" extensions.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n cache.h       |  1 +\n read-cache.c  | 10 ++++++++++\n split-index.c | 14 ++++++++++++--\n 3 files changed, 23 insertions(+), 2 deletions(-)\n\ndiff --git a/cache.h b/cache.h\nindex 70fd8ed..ddb7cd2 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -170,6 +170,7 @@ struct cache_entry {\n #define CE_MATCHED           (1 << 26)\n \n #define CE_UPDATE_IN_BASE    (1 << 27)\n+#define CE_STRIP_NAME        (1 << 28)\n \n /*\n  * Extended on-disk flags\ndiff --git a/read-cache.c b/read-cache.c\nindex d5f70a0..af475dc 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -1787,9 +1787,15 @@ static int ce_write_entry(git_SHA_CTX *c, int fd, struct cache_entry *ce,\n {\n \tint size;\n \tstruct ondisk_cache_entry *ondisk;\n+\tint saved_namelen = saved_namelen; /* compiler workaround */\n \tchar *name;\n \tint result;\n \n+\tif (ce->ce_flags & CE_STRIP_NAME) {\n+\t\tsaved_namelen = ce_namelen(ce);\n+\t\tce->ce_namelen = 0;\n+\t}\n+\n \tif (!previous_name) {\n \t\tsize = ondisk_ce_size(ce);\n \t\tondisk = xcalloc(1, size);\n@@ -1821,6 +1827,10 @@ static int ce_write_entry(git_SHA_CTX *c, int fd, struct cache_entry *ce,\n \t\tstrbuf_splice(previous_name, common, to_remove,\n \t\t\t      ce->name + common, ce_namelen(ce) - common);\n \t}\n+\tif (ce->ce_flags & CE_STRIP_NAME) {\n+\t\tce->ce_namelen = saved_namelen;\n+\t\tce->ce_flags &= ~CE_STRIP_NAME;\n+\t}\n \n \tresult = ce_write(c, fd, ondisk, size);\n \tfree(ondisk);\ndiff --git a/split-index.c b/split-index.c\nindex 33c0c4b..ee3246f 100644\n--- a/split-index.c\n+++ b/split-index.c\n@@ -89,6 +89,7 @@ static void replace_entry(size_t pos, void *data)\n \tstruct index_state *istate = data;\n \tstruct split_index *si = istate->split_index;\n \tstruct cache_entry *dst, *src;\n+\n \tif (pos >= istate->cache_nr)\n \t\tdie(\"position for replacement %d exceeds base index size %d\",\n \t\t    (int)pos, istate->cache_nr);\n@@ -100,10 +101,14 @@ static void replace_entry(size_t pos, void *data)\n \t\tdie(\"entry %d is marked as both replaced and deleted\",\n \t\t    (int)pos);\n \tsrc = si->saved_cache[si->nr_replacements];\n+\tif (ce_namelen(src))\n+\t\tdie(\"corrupt link extension, entry %d should have \"\n+\t\t    \"zero length name\", (int)pos);\n \tsrc->index = pos + 1;\n \tsrc->ce_flags |= CE_UPDATE_IN_BASE;\n-\tfree(dst);\n-\tdst = src;\n+\tsrc->ce_namelen = dst->ce_namelen;\n+\tcopy_cache_entry(dst, src);\n+\tfree(src);\n \tsi->nr_replacements++;\n }\n \n@@ -131,6 +136,9 @@ void merge_base_index(struct index_state *istate)\n \t\tremove_marked_cache_entries(istate);\n \n \tfor (i = si->nr_replacements; i < si->saved_cache_nr; i++) {\n+\t\tif (!ce_namelen(si->saved_cache[i]))\n+\t\t\tdie(\"corrupt link extension, entry %d should \"\n+\t\t\t    \"have non-zero length name\", i);\n \t\tadd_index_entry(istate, si->saved_cache[i],\n \t\t\t\tADD_CACHE_OK_TO_ADD |\n \t\t\t\tADD_CACHE_KEEP_CACHE_TREE |\n@@ -213,6 +221,7 @@ void prepare_to_write_split_index(struct index_state *istate)\n \t\t\t\tewah_set(si->delete_bitmap, i);\n \t\t\telse if (ce->ce_flags & CE_UPDATE_IN_BASE) {\n \t\t\t\tewah_set(si->replace_bitmap, i);\n+\t\t\t\tce->ce_flags |= CE_STRIP_NAME;\n \t\t\t\tALLOC_GROW(entries, nr_entries+1, nr_alloc);\n \t\t\t\tentries[nr_entries++] = ce;\n \t\t\t}\n@@ -222,6 +231,7 @@ void prepare_to_write_split_index(struct index_state *istate)\n \tfor (i = 0; i < istate->cache_nr; i++) {\n \t\tce = istate->cache[i];\n \t\tif ((!si->base || !ce->index) && !(ce->ce_flags & CE_REMOVE)) {\n+\t\t\tassert(!(ce->ce_flags & CE_STRIP_NAME));\n \t\t\tALLOC_GROW(entries, nr_entries+1, nr_alloc);\n \t\t\tentries[nr_entries++] = ce;\n \t\t}\n-- \n1.9.1.346.ga2b5940\n"},{"id":"244119","messageId":"1402661991-14977-26-git-send-email-pclouds@gmail.com","threadId":"36906","inReplyTo":"1402661991-14977-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 25/32] update-index: new options to enable/disable split index mode","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2014-06-13T12:19:44Z","receivedAt":"2014-06-13T12:19:44Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"If you have a large work tree but only make changes in a subset, then\n$GIT_DIR/index's size should be stable after a while. If you change\nbranches that touch something else, $GIT_DIR/index's size may grow\nlarge that it becomes as slow as the unified index. Do --split-index\nagain occasionally to force all changes back to the shared index and\nkeep $GIT_DIR/index small.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n Documentation/git-update-index.txt | 11 +++++++\n builtin/update-index.c             | 18 ++++++++++\n cache.h                            |  1 +\n read-cache.c                       | 67 ++++++++++++++++++++++++++++++++++----\n split-index.c                      | 23 +++++++++++++\n 5 files changed, 114 insertions(+), 6 deletions(-)\n\ndiff --git a/Documentation/git-update-index.txt b/Documentation/git-update-index.txt\nindex d6de4a0..dfc09d9 100644\n--- a/Documentation/git-update-index.txt\n+++ b/Documentation/git-update-index.txt\n@@ -161,6 +161,17 @@ may not support it yet.\n \tOnly meaningful with `--stdin` or `--index-info`; paths are\n \tseparated with NUL character instead of LF.\n \n+--split-index::\n+--no-split-index::\n+\tEnable or disable split index mode. If enabled, the index is\n+\tsplit into two files, $GIT_DIR/index and $GIT_DIR/sharedindex.<SHA-1>.\n+\tChanges are accumulated in $GIT_DIR/index while the shared\n+\tindex file contains all index entries stays unchanged. If\n+\tsplit-index mode is already enabled and `--split-index` is\n+\tgiven again, all changes in $GIT_DIR/index are pushed back to\n+\tthe shared index file. This mode is designed for very large\n+\tindexes that take a signficant amount of time to read or write.\n+\n \\--::\n \tDo not interpret any more arguments as options.\n \ndiff --git a/builtin/update-index.c b/builtin/update-index.c\nindex f7a19c4..b0503f4 100644\n--- a/builtin/update-index.c\n+++ b/builtin/update-index.c\n@@ -13,6 +13,7 @@\n #include \"parse-options.h\"\n #include \"pathspec.h\"\n #include \"dir.h\"\n+#include \"split-index.h\"\n \n /*\n  * Default to not allowing changes to the list of files. The\n@@ -742,6 +743,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)\n \tchar set_executable_bit = 0;\n \tstruct refresh_params refresh_args = {0, &has_errors};\n \tint lock_error = 0;\n+\tint split_index = -1;\n \tstruct lock_file *lock_file;\n \tstruct parse_opt_ctx_t ctx;\n \tint parseopt_state = PARSE_OPT_UNKNOWN;\n@@ -824,6 +826,8 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)\n \t\t\tresolve_undo_clear_callback},\n \t\tOPT_INTEGER(0, \"index-version\", &preferred_index_format,\n \t\t\tN_(\"write index in this format\")),\n+\t\tOPT_BOOL(0, \"split-index\", &split_index,\n+\t\t\tN_(\"enable or disable split index\")),\n \t\tOPT_END()\n \t};\n \n@@ -917,6 +921,20 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)\n \t\tstrbuf_release(&buf);\n \t}\n \n+\tif (split_index > 0) {\n+\t\tinit_split_index(&the_index);\n+\t\tthe_index.cache_changed |= SPLIT_INDEX_ORDERED;\n+\t} else if (!split_index && the_index.split_index) {\n+\t\t/*\n+\t\t * can't discard_split_index(&the_index); because that\n+\t\t * will destroy split_index->base->cache[], which may\n+\t\t * be shared with the_index.cache[]. So yeah we're\n+\t\t * leaking a bit here.\n+\t\t */\n+\t\tthe_index.split_index = NULL;\n+\t\tthe_index.cache_changed |= SOMETHING_CHANGED;\n+\t}\n+\n \tif (active_cache_changed) {\n \t\tif (newfd < 0) {\n \t\t\tif (refresh_args.flags & REFRESH_QUIET)\ndiff --git a/cache.h b/cache.h\nindex ddb7cd2..7523c28 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -278,6 +278,7 @@ static inline unsigned int canon_mode(unsigned int mode)\n #define CE_ENTRY_ADDED\t\t(1 << 3)\n #define RESOLVE_UNDO_CHANGED\t(1 << 4)\n #define CACHE_TREE_CHANGED\t(1 << 5)\n+#define SPLIT_INDEX_ORDERED\t(1 << 6)\n \n struct split_index;\n struct index_state {\ndiff --git a/read-cache.c b/read-cache.c\nindex af475dc..8ecb959 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -15,6 +15,7 @@\n #include \"strbuf.h\"\n #include \"varint.h\"\n #include \"split-index.h\"\n+#include \"sigchain.h\"\n \n static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,\n \t\t\t\t\t       unsigned int options);\n@@ -39,7 +40,8 @@ static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,\n \n /* changes that can be kept in $GIT_DIR/index (basically all extensions) */\n #define EXTMASK (RESOLVE_UNDO_CHANGED | CACHE_TREE_CHANGED | \\\n-\t\t CE_ENTRY_ADDED | CE_ENTRY_REMOVED | CE_ENTRY_CHANGED)\n+\t\t CE_ENTRY_ADDED | CE_ENTRY_REMOVED | CE_ENTRY_CHANGED | \\\n+\t\t SPLIT_INDEX_ORDERED)\n \n struct index_state the_index;\n static const char *alternate_index_output;\n@@ -1860,7 +1862,8 @@ void update_index_if_able(struct index_state *istate, struct lock_file *lockfile\n \t\trollback_lock_file(lockfile);\n }\n \n-static int do_write_index(struct index_state *istate, int newfd)\n+static int do_write_index(struct index_state *istate, int newfd,\n+\t\t\t  int strip_extensions)\n {\n \tgit_SHA_CTX c;\n \tstruct cache_header hdr;\n@@ -1923,7 +1926,7 @@ static int do_write_index(struct index_state *istate, int newfd)\n \tstrbuf_release(&previous_name_buf);\n \n \t/* Write extension data here */\n-\tif (istate->split_index) {\n+\tif (!strip_extensions && istate->split_index) {\n \t\tstruct strbuf sb = STRBUF_INIT;\n \n \t\terr = write_link_extension(&sb, istate) < 0 ||\n@@ -1934,7 +1937,7 @@ static int do_write_index(struct index_state *istate, int newfd)\n \t\tif (err)\n \t\t\treturn -1;\n \t}\n-\tif (istate->cache_tree) {\n+\tif (!strip_extensions && istate->cache_tree) {\n \t\tstruct strbuf sb = STRBUF_INIT;\n \n \t\tcache_tree_write(&sb, istate->cache_tree);\n@@ -1944,7 +1947,7 @@ static int do_write_index(struct index_state *istate, int newfd)\n \t\tif (err)\n \t\t\treturn -1;\n \t}\n-\tif (istate->resolve_undo) {\n+\tif (!strip_extensions && istate->resolve_undo) {\n \t\tstruct strbuf sb = STRBUF_INIT;\n \n \t\tresolve_undo_write(&sb, istate->resolve_undo);\n@@ -1985,7 +1988,7 @@ static int commit_locked_index(struct lock_file *lk)\n static int do_write_locked_index(struct index_state *istate, struct lock_file *lock,\n \t\t\t\t unsigned flags)\n {\n-\tint ret = do_write_index(istate, lock->fd);\n+\tint ret = do_write_index(istate, lock->fd, 0);\n \tif (ret)\n \t\treturn ret;\n \tassert((flags & (COMMIT_LOCK | CLOSE_LOCK)) !=\n@@ -2009,6 +2012,52 @@ static int write_split_index(struct index_state *istate,\n \treturn ret;\n }\n \n+static char *temporary_sharedindex;\n+\n+static void remove_temporary_sharedindex(void)\n+{\n+\tif (temporary_sharedindex) {\n+\t\tunlink_or_warn(temporary_sharedindex);\n+\t\tfree(temporary_sharedindex);\n+\t\ttemporary_sharedindex = NULL;\n+\t}\n+}\n+\n+static void remove_temporary_sharedindex_on_signal(int signo)\n+{\n+\tremove_temporary_sharedindex();\n+\tsigchain_pop(signo);\n+\traise(signo);\n+}\n+\n+static int write_shared_index(struct index_state *istate)\n+{\n+\tstruct split_index *si = istate->split_index;\n+\tstatic int installed_handler;\n+\tint fd, ret;\n+\n+\ttemporary_sharedindex = git_pathdup(\"sharedindex_XXXXXX\");\n+\tfd = xmkstemp(temporary_sharedindex);\n+\tif (!installed_handler) {\n+\t\tatexit(remove_temporary_sharedindex);\n+\t\tsigchain_push_common(remove_temporary_sharedindex_on_signal);\n+\t}\n+\tmove_cache_to_base_index(istate);\n+\tret = do_write_index(si->base, fd, 1);\n+\tclose(fd);\n+\tif (ret) {\n+\t\tremove_temporary_sharedindex();\n+\t\treturn ret;\n+\t}\n+\tret = rename(temporary_sharedindex,\n+\t\t     git_path(\"sharedindex.%s\", sha1_to_hex(si->base->sha1)));\n+\tfree(temporary_sharedindex);\n+\ttemporary_sharedindex = NULL;\n+\tif (!ret)\n+\t\thashcpy(si->base_sha1, si->base->sha1);\n+\treturn ret;\n+}\n+\n int write_locked_index(struct index_state *istate, struct lock_file *lock,\n \t\t       unsigned flags)\n {\n@@ -2020,6 +2069,12 @@ int write_locked_index(struct index_state *istate, struct lock_file *lock,\n \t\treturn do_write_locked_index(istate, lock, flags);\n \t}\n \n+\tif (istate->cache_changed & SPLIT_INDEX_ORDERED) {\n+\t\tint ret = write_shared_index(istate);\n+\t\tif (ret)\n+\t\t\treturn ret;\n+\t}\n+\n \treturn write_split_index(istate, lock, flags);\n }\n \ndiff --git a/split-index.c b/split-index.c\nindex ee3246f..21485e2 100644\n--- a/split-index.c\n+++ b/split-index.c\n@@ -74,6 +74,29 @@ static void mark_base_index_entries(struct index_state *base)\n \t\tbase->cache[i]->index = i + 1;\n }\n \n+void move_cache_to_base_index(struct index_state *istate)\n+{\n+\tstruct split_index *si = istate->split_index;\n+\tint i;\n+\n+\t/*\n+\t * do not delete old si->base, its index entries may be shared\n+\t * with istate->cache[]. Accept a bit of leaking here because\n+\t * this code is only used by short-lived update-index.\n+\t */\n+\tsi->base = xcalloc(1, sizeof(*si->base));\n+\tsi->base->version = istate->version;\n+\t/* zero timestamp disables racy test in ce_write_index() */\n+\tsi->base->timestamp = istate->timestamp;\n+\tALLOC_GROW(si->base->cache, istate->cache_nr, si->base->cache_alloc);\n+\tsi->base->cache_nr = istate->cache_nr;\n+\tmemcpy(si->base->cache, istate->cache,\n+\t       sizeof(*istate->cache) * istate->cache_nr);\n+\tmark_base_index_entries(si->base);\n+\tfor (i = 0; i < si->base->cache_nr; i++)\n+\t\tsi->base->cache[i]->ce_flags &= ~CE_UPDATE_IN_BASE;\n+}\n+\n static void mark_entry_for_delete(size_t pos, void *data)\n {\n \tstruct index_state *istate = data;\n-- \n1.9.1.346.ga2b5940\n"},{"id":"244122","messageId":"1402661991-14977-27-git-send-email-pclouds@gmail.com","threadId":"36906","inReplyTo":"1402661991-14977-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 26/32] update-index --split-index: do not split if $GIT_DIR is read only","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2014-06-13T12:19:45Z","receivedAt":"2014-06-13T12:19:45Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"If $GIT_DIR is read only, we can't write $GIT_DIR/sharedindex. This\ncould happen when $GIT_INDEX_FILE is set to somehwere outside\n$GIT_DIR.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n read-cache.c | 13 ++++++++++---\n 1 file changed, 10 insertions(+), 3 deletions(-)\n\ndiff --git a/read-cache.c b/read-cache.c\nindex 8ecb959..aa848e1 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -2030,14 +2030,21 @@ static void remove_temporary_sharedindex_on_signal(int signo)\n \traise(signo);\n }\n \n-static int write_shared_index(struct index_state *istate)\n+static int write_shared_index(struct index_state *istate,\n+\t\t\t      struct lock_file *lock, unsigned flags)\n {\n \tstruct split_index *si = istate->split_index;\n \tstatic int installed_handler;\n \tint fd, ret;\n \n \ttemporary_sharedindex = git_pathdup(\"sharedindex_XXXXXX\");\n-\tfd = xmkstemp(temporary_sharedindex);\n+\tfd = mkstemp(temporary_sharedindex);\n+\tif (fd < 0) {\n+\t\tfree(temporary_sharedindex);\n+\t\ttemporary_sharedindex = NULL;\n+\t\thashclr(si->base_sha1);\n+\t\treturn do_write_locked_index(istate, lock, flags);\n+\t}\n \tif (!installed_handler) {\n \t\tatexit(remove_temporary_sharedindex);\n \t\tsigchain_push_common(remove_temporary_sharedindex_on_signal);\n@@ -2070,7 +2077,7 @@ int write_locked_index(struct index_state *istate, struct lock_file *lock,\n \t}\n \n \tif (istate->cache_changed & SPLIT_INDEX_ORDERED) {\n-\t\tint ret = write_shared_index(istate);\n+\t\tint ret = write_shared_index(istate, lock, flags);\n \t\tif (ret)\n \t\t\treturn ret;\n \t}\n-- \n1.9.1.346.ga2b5940\n"},{"id":"244121","messageId":"1402661991-14977-28-git-send-email-pclouds@gmail.com","threadId":"36906","inReplyTo":"1402661991-14977-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 27/32] rev-parse: add --shared-index-path to get shared index path","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2014-06-13T12:19:46Z","receivedAt":"2014-06-13T12:19:46Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Normally scripts do not have to be aware about split indexes because\nall shared indexes are in $GIT_DIR. A simple \"mv $tmp_index\n$GIT_DIR/somewhere\" is enough. Scripts that generate temporary indexes\nand move them across repos must be aware about split index and copy\nthe shared file as well. This option enables that.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n Documentation/git-rev-parse.txt |  4 ++++\n builtin/rev-parse.c             | 10 ++++++++++\n 2 files changed, 14 insertions(+)\n\ndiff --git a/Documentation/git-rev-parse.txt b/Documentation/git-rev-parse.txt\nindex 987395d..9bd76a5 100644\n--- a/Documentation/git-rev-parse.txt\n+++ b/Documentation/git-rev-parse.txt\n@@ -245,6 +245,10 @@ print a message to stderr and exit with nonzero status.\n --show-toplevel::\n \tShow the absolute path of the top-level directory.\n \n+--shared-index-path::\n+\tShow the path to the shared index file in split index mode, or\n+\tempty if not in split-index mode.\n+\n Other Options\n ~~~~~~~~~~~~~\n \ndiff --git a/builtin/rev-parse.c b/builtin/rev-parse.c\nindex 1a6122d..8102aaa 100644\n--- a/builtin/rev-parse.c\n+++ b/builtin/rev-parse.c\n@@ -11,6 +11,7 @@\n #include \"parse-options.h\"\n #include \"diff.h\"\n #include \"revision.h\"\n+#include \"split-index.h\"\n \n #define DO_REVS\t\t1\n #define DO_NOREV\t2\n@@ -775,6 +776,15 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)\n \t\t\t\t\t\t: \"false\");\n \t\t\t\tcontinue;\n \t\t\t}\n+\t\t\tif (!strcmp(arg, \"--shared-index-path\")) {\n+\t\t\t\tif (read_cache() < 0)\n+\t\t\t\t\tdie(_(\"Could not read the index\"));\n+\t\t\t\tif (the_index.split_index) {\n+\t\t\t\t\tconst unsigned char *sha1 = the_index.split_index->base_sha1;\n+\t\t\t\t\tputs(git_path(\"sharedindex.%s\", sha1_to_hex(sha1)));\n+\t\t\t\t}\n+\t\t\t\tcontinue;\n+\t\t\t}\n \t\t\tif (starts_with(arg, \"--since=\")) {\n \t\t\t\tshow_datestring(\"--max-age=\", arg+8);\n \t\t\t\tcontinue;\n-- \n1.9.1.346.ga2b5940\n"},{"id":"244123","messageId":"1402661991-14977-29-git-send-email-pclouds@gmail.com","threadId":"36906","inReplyTo":"1402661991-14977-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 28/32] read-tree: force split-index mode off on --index-output","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2014-06-13T12:19:47Z","receivedAt":"2014-06-13T12:19:47Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Just a (paranoid?) safety measure..\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n read-cache.c | 3 ++-\n 1 file changed, 2 insertions(+), 1 deletion(-)\n\ndiff --git a/read-cache.c b/read-cache.c\nindex aa848e1..b1027f7 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -2070,7 +2070,8 @@ int write_locked_index(struct index_state *istate, struct lock_file *lock,\n {\n \tstruct split_index *si = istate->split_index;\n \n-\tif (!si || (istate->cache_changed & ~EXTMASK)) {\n+\tif (!si || alternate_index_output ||\n+\t    (istate->cache_changed & ~EXTMASK)) {\n \t\tif (si)\n \t\t\thashclr(si->base_sha1);\n \t\treturn do_write_locked_index(istate, lock, flags);\n-- \n1.9.1.346.ga2b5940\n"},{"id":"244124","messageId":"1402661991-14977-30-git-send-email-pclouds@gmail.com","threadId":"36906","inReplyTo":"1402661991-14977-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 29/32] read-tree: note about dropping split-index mode or index version","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2014-06-13T12:19:48Z","receivedAt":"2014-06-13T12:19:48Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/read-tree.c | 9 +++++++++\n 1 file changed, 9 insertions(+)\n\ndiff --git a/builtin/read-tree.c b/builtin/read-tree.c\nindex 3204c62..e7e1c33 100644\n--- a/builtin/read-tree.c\n+++ b/builtin/read-tree.c\n@@ -155,6 +155,15 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)\n \tif (1 < opts.merge + opts.reset + prefix_set)\n \t\tdie(\"Which one? -m, --reset, or --prefix?\");\n \n+\t/*\n+\t * NEEDSWORK\n+\t *\n+\t * The old index should be read anyway even if we're going to\n+\t * destroy all index entries because we still need to preserve\n+\t * certain information such as index version or split-index\n+\t * mode.\n+\t */\n+\n \tif (opts.reset || opts.merge || opts.prefix) {\n \t\tif (read_cache_unmerged() && (opts.prefix || opts.merge))\n \t\t\tdie(\"You need to resolve your current index first\");\n-- \n1.9.1.346.ga2b5940\n"},{"id":"244125","messageId":"1402661991-14977-31-git-send-email-pclouds@gmail.com","threadId":"36906","inReplyTo":"1402661991-14977-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 30/32] read-cache: force split index mode with GIT_TEST_SPLIT_INDEX","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2014-06-13T12:19:49Z","receivedAt":"2014-06-13T12:19:49Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"This could be used to run the whole test suite with split\nindexes. Index splitting is carried out at random. \"git read-tree\"\nalso resets the index and forces splitting at the next update.\n\nI had a lot of headaches with the test suite, which proves it\nexercises split index pretty good.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n read-cache.c | 10 +++++++++-\n 1 file changed, 9 insertions(+), 1 deletion(-)\n\ndiff --git a/read-cache.c b/read-cache.c\nindex b1027f7..12335a3 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -1885,8 +1885,11 @@ static int do_write_index(struct index_state *istate, int newfd,\n \t\t}\n \t}\n \n-\tif (!istate->version)\n+\tif (!istate->version) {\n \t\tistate->version = get_index_format_default();\n+\t\tif (getenv(\"GIT_TEST_SPLIT_INDEX\"))\n+\t\t\tinit_split_index(istate);\n+\t}\n \n \t/* demote version 3 to version 2 when the latter suffices */\n \tif (istate->version == 3 || istate->version == 2)\n@@ -2077,6 +2080,11 @@ int write_locked_index(struct index_state *istate, struct lock_file *lock,\n \t\treturn do_write_locked_index(istate, lock, flags);\n \t}\n \n+\tif (getenv(\"GIT_TEST_SPLIT_INDEX\")) {\n+\t\tint v = si->base_sha1[0];\n+\t\tif ((v & 15) < 6)\n+\t\t\tistate->cache_changed |= SPLIT_INDEX_ORDERED;\n+\t}\n \tif (istate->cache_changed & SPLIT_INDEX_ORDERED) {\n \t\tint ret = write_shared_index(istate, lock, flags);\n \t\tif (ret)\n-- \n1.9.1.346.ga2b5940\n"},{"id":"244126","messageId":"1402661991-14977-32-git-send-email-pclouds@gmail.com","threadId":"36906","inReplyTo":"1402661991-14977-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 31/32] t2104: make sure split index mode is off for the version test","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2014-06-13T12:19:50Z","receivedAt":"2014-06-13T12:19:50Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Version tests only make sense when all entries are in the same file,\nso we can see if version is downgraded to 2 if 3 is not required.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n t/t2104-update-index-skip-worktree.sh | 2 ++\n 1 file changed, 2 insertions(+)\n\ndiff --git a/t/t2104-update-index-skip-worktree.sh b/t/t2104-update-index-skip-worktree.sh\nindex 29c1fb1..cc830da 100755\n--- a/t/t2104-update-index-skip-worktree.sh\n+++ b/t/t2104-update-index-skip-worktree.sh\n@@ -7,6 +7,8 @@ test_description='skip-worktree bit test'\n \n . ./test-lib.sh\n \n+sane_unset GIT_TEST_SPLIT_INDEX\n+\n test_set_index_version 3\n \n cat >expect.full <<EOF\n-- \n1.9.1.346.ga2b5940\n"},{"id":"244127","messageId":"1402661991-14977-33-git-send-email-pclouds@gmail.com","threadId":"36906","inReplyTo":"1402661991-14977-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 32/32] t1700: new tests for split-index mode","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2014-06-13T12:19:51Z","receivedAt":"2014-06-13T12:19:51Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n .gitignore                      |   1 +\n Makefile                        |   1 +\n cache.h                         |   2 +\n read-cache.c                    |   3 +-\n t/t1700-split-index.sh (new +x) | 194 ++++++++++++++++++++++++++++++++++++++++\n test-dump-split-index.c (new)   |  34 +++++++\n 6 files changed, 233 insertions(+), 2 deletions(-)\n create mode 100755 t/t1700-split-index.sh\n create mode 100644 test-dump-split-index.c\n\ndiff --git a/.gitignore b/.gitignore\nindex dc600f9..70992a4 100644\n--- a/.gitignore\n+++ b/.gitignore\n@@ -180,6 +180,7 @@\n /test-date\n /test-delta\n /test-dump-cache-tree\n+/test-dump-split-index\n /test-scrap-cache-tree\n /test-genrandom\n /test-hashmap\ndiff --git a/Makefile b/Makefile\nindex 94ad3ce..028749b 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -565,6 +565,7 @@ TEST_PROGRAMS_NEED_X += test-ctype\n TEST_PROGRAMS_NEED_X += test-date\n TEST_PROGRAMS_NEED_X += test-delta\n TEST_PROGRAMS_NEED_X += test-dump-cache-tree\n+TEST_PROGRAMS_NEED_X += test-dump-split-index\n TEST_PROGRAMS_NEED_X += test-genrandom\n TEST_PROGRAMS_NEED_X += test-hashmap\n TEST_PROGRAMS_NEED_X += test-index-version\ndiff --git a/cache.h b/cache.h\nindex 7523c28..c6b7770 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -473,6 +473,8 @@ extern int daemonize(void);\n struct lock_file;\n extern int read_index(struct index_state *);\n extern int read_index_preload(struct index_state *, const struct pathspec *pathspec);\n+extern int do_read_index(struct index_state *istate, const char *path,\n+\t\t\t int must_exist); /* for testting only! */\n extern int read_index_from(struct index_state *, const char *path);\n extern int is_index_unborn(struct index_state *);\n extern int read_index_unmerged(struct index_state *);\ndiff --git a/read-cache.c b/read-cache.c\nindex 12335a3..342fe52 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -1463,8 +1463,7 @@ static struct cache_entry *create_from_disk(struct ondisk_cache_entry *ondisk,\n }\n \n /* remember to discard_cache() before reading a different cache! */\n-static int do_read_index(struct index_state *istate, const char *path,\n-\t\t\t int must_exist)\n+int do_read_index(struct index_state *istate, const char *path, int must_exist)\n {\n \tint fd, i;\n \tstruct stat st;\ndiff --git a/t/t1700-split-index.sh b/t/t1700-split-index.sh\nnew file mode 100755\nindex 0000000..94fb473\n--- /dev/null\n+++ b/t/t1700-split-index.sh\n@@ -0,0 +1,194 @@\n+#!/bin/sh\n+\n+test_description='split index mode tests'\n+\n+. ./test-lib.sh\n+\n+# We need total control of index splitting here\n+sane_unset GIT_TEST_SPLIT_INDEX\n+\n+test_expect_success 'enable split index' '\n+\tgit update-index --split-index &&\n+\ttest-dump-split-index .git/index >actual &&\n+\tcat >expect <<EOF &&\n+own 8299b0bcd1ac364e5f1d7768efb62fa2da79a339\n+base 39d890139ee5356c7ef572216cebcd27aa41f9df\n+replacements:\n+deletions:\n+EOF\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'add one file' '\n+\t: >one &&\n+\tgit update-index --add one &&\n+\tgit ls-files --stage >ls-files.actual &&\n+\tcat >ls-files.expect <<EOF &&\n+100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0\tone\n+EOF\n+\ttest_cmp ls-files.expect ls-files.actual &&\n+\n+\ttest-dump-split-index .git/index | sed \"/^own/d\" >actual &&\n+\tcat >expect <<EOF &&\n+base 39d890139ee5356c7ef572216cebcd27aa41f9df\n+100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0\tone\n+replacements:\n+deletions:\n+EOF\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'disable split index' '\n+\tgit update-index --no-split-index &&\n+\tgit ls-files --stage >ls-files.actual &&\n+\tcat >ls-files.expect <<EOF &&\n+100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0\tone\n+EOF\n+\ttest_cmp ls-files.expect ls-files.actual &&\n+\n+\tBASE=`test-dump-split-index .git/index | grep \"^own\" | sed \"s/own/base/\"` &&\n+\ttest-dump-split-index .git/index | sed \"/^own/d\" >actual &&\n+\tcat >expect <<EOF &&\n+not a split index\n+EOF\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'enable split index again, \"one\" now belongs to base index\"' '\n+\tgit update-index --split-index &&\n+\tgit ls-files --stage >ls-files.actual &&\n+\tcat >ls-files.expect <<EOF &&\n+100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0\tone\n+EOF\n+\ttest_cmp ls-files.expect ls-files.actual &&\n+\n+\ttest-dump-split-index .git/index | sed \"/^own/d\" >actual &&\n+\tcat >expect <<EOF &&\n+$BASE\n+replacements:\n+deletions:\n+EOF\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'modify original file, base index untouched' '\n+\techo modified >one &&\n+\tgit update-index one &&\n+\tgit ls-files --stage >ls-files.actual &&\n+\tcat >ls-files.expect <<EOF &&\n+100644 2e0996000b7e9019eabcad29391bf0f5c7702f0b 0\tone\n+EOF\n+\ttest_cmp ls-files.expect ls-files.actual &&\n+\n+\ttest-dump-split-index .git/index | sed \"/^own/d\" >actual &&\n+\tq_to_tab >expect <<EOF &&\n+$BASE\n+100644 2e0996000b7e9019eabcad29391bf0f5c7702f0b 0Q\n+replacements: 0\n+deletions:\n+EOF\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'add another file, which stays index' '\n+\t: >two &&\n+\tgit update-index --add two &&\n+\tgit ls-files --stage >ls-files.actual &&\n+\tcat >ls-files.expect <<EOF &&\n+100644 2e0996000b7e9019eabcad29391bf0f5c7702f0b 0\tone\n+100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0\ttwo\n+EOF\n+\ttest_cmp ls-files.expect ls-files.actual &&\n+\n+\ttest-dump-split-index .git/index | sed \"/^own/d\" >actual &&\n+\tq_to_tab >expect <<EOF &&\n+$BASE\n+100644 2e0996000b7e9019eabcad29391bf0f5c7702f0b 0Q\n+100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0\ttwo\n+replacements: 0\n+deletions:\n+EOF\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'remove file not in base index' '\n+\tgit update-index --force-remove two &&\n+\tgit ls-files --stage >ls-files.actual &&\n+\tcat >ls-files.expect <<EOF &&\n+100644 2e0996000b7e9019eabcad29391bf0f5c7702f0b 0\tone\n+EOF\n+\ttest_cmp ls-files.expect ls-files.actual &&\n+\n+\ttest-dump-split-index .git/index | sed \"/^own/d\" >actual &&\n+\tq_to_tab >expect <<EOF &&\n+$BASE\n+100644 2e0996000b7e9019eabcad29391bf0f5c7702f0b 0Q\n+replacements: 0\n+deletions:\n+EOF\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'remove file in base index' '\n+\tgit update-index --force-remove one &&\n+\tgit ls-files --stage >ls-files.actual &&\n+\tcat >ls-files.expect <<EOF &&\n+EOF\n+\ttest_cmp ls-files.expect ls-files.actual &&\n+\n+\ttest-dump-split-index .git/index | sed \"/^own/d\" >actual &&\n+\tcat >expect <<EOF &&\n+$BASE\n+replacements:\n+deletions: 0\n+EOF\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'add original file back' '\n+\t: >one &&\n+\tgit update-index --add one &&\n+\tgit ls-files --stage >ls-files.actual &&\n+\tcat >ls-files.expect <<EOF &&\n+100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0\tone\n+EOF\n+\ttest_cmp ls-files.expect ls-files.actual &&\n+\n+\ttest-dump-split-index .git/index | sed \"/^own/d\" >actual &&\n+\tcat >expect <<EOF &&\n+$BASE\n+100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0\tone\n+replacements:\n+deletions: 0\n+EOF\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'add new file' '\n+\t: >two &&\n+\tgit update-index --add two &&\n+\tgit ls-files --stage >actual &&\n+\tcat >expect <<EOF &&\n+100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0\tone\n+100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0\ttwo\n+EOF\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'unify index, two files remain' '\n+\tgit update-index --no-split-index &&\n+\tgit ls-files --stage >ls-files.actual &&\n+\tcat >ls-files.expect <<EOF &&\n+100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0\tone\n+100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0\ttwo\n+EOF\n+\ttest_cmp ls-files.expect ls-files.actual\n+\n+\ttest-dump-split-index .git/index | sed \"/^own/d\" >actual &&\n+\tcat >expect <<EOF &&\n+not a split index\n+EOF\n+\ttest_cmp expect actual\n+'\n+\n+test_done\ndiff --git a/test-dump-split-index.c b/test-dump-split-index.c\nnew file mode 100644\nindex 0000000..9cf3112\n--- /dev/null\n+++ b/test-dump-split-index.c\n@@ -0,0 +1,34 @@\n+#include \"cache.h\"\n+#include \"split-index.h\"\n+#include \"ewah/ewok.h\"\n+\n+static void show_bit(size_t pos, void *data)\n+{\n+\tprintf(\" %d\", (int)pos);\n+}\n+\n+int main(int ac, char **av)\n+{\n+\tstruct split_index *si;\n+\tint i;\n+\n+\tdo_read_index(&the_index, av[1], 1);\n+\tprintf(\"own %s\\n\", sha1_to_hex(the_index.sha1));\n+\tsi = the_index.split_index;\n+\tif (!si) {\n+\t\tprintf(\"not a split index\\n\");\n+\t\treturn 0;\n+\t}\n+\tprintf(\"base %s\\n\", sha1_to_hex(si->base_sha1));\n+\tfor (i = 0; i < the_index.cache_nr; i++) {\n+\t\tstruct cache_entry *ce = the_index.cache[i];\n+\t\tprintf(\"%06o %s %d\\t%s\\n\", ce->ce_mode,\n+\t\t       sha1_to_hex(ce->sha1), ce_stage(ce), ce->name);\n+\t}\n+\tprintf(\"replacements:\");\n+\tewah_each_bit(si->replace_bitmap, show_bit, NULL);\n+\tprintf(\"\\ndeletions:\");\n+\tewah_each_bit(si->delete_bitmap, show_bit, NULL);\n+\tprintf(\"\\n\");\n+\treturn 0;\n+}\n-- \n1.9.1.346.ga2b5940\n"}]}