{"thread":{"id":"137","subject":"[PATCH 7/8] read-tree.c: add INDEX_FILE_DIRECTORY support","startedAt":"2005-04-19T09:09:39Z","lastAt":"2005-04-19T09:09:39Z","messageCount":1,"participants":["Zach Welch"],"isPatch":true,"patchVersion":1,"patchTotal":8},"messages":[{"id":"805","messageId":"mailbox-23311-1113901779-735349@spoon","threadId":"137","inReplyTo":null,"subject":"[PATCH 7/8] read-tree.c: add INDEX_FILE_DIRECTORY support","fromName":"Zach Welch","fromEmail":"zw@superlucidity.net","sentAt":"2005-04-19T09:09:39Z","receivedAt":"2005-04-19T09:09:39Z","isPatch":true,"sender":{"key":"zw@superlucidity.net","avatar":null},"body":"This patch give read-tree the ability for the index directory to be \noverridden by the INDEX_FILE_DIRECTORY environment variable.\n\nThis patch applies on top of:\n        [PATCH 0/8] init-db.c cleanup, add INDEX_FILE_DIRECTORY support\n        [PATCH 1/8] init-db.c: [RESEND] remove redundant getenv call\n        [PATCH 2/8] init-db.c: [RESEND] make init-db work with common objects\n        [PATCH 3/8] init-db.c: refactor directory creation\n        [PATCH 4/8] init-db.c: add INDEX_FILE_DIRECTORY support\n        [PATCH 5/8] init-db.c: refactor mkdir logic\n        [PATCH 6/8] read-cache.c: add INDEX_FILE_DIRECTORY support\n read-tree.c |   33 +++++++++++++++++++++++++--------\n 1 files changed, 25 insertions(+), 8 deletions(-)\nSigned-Off-By: Zach Welch <zw@superlucidity.net>\n\n\nread-tree.c: 42556c82def1d23f21116a2c1b3e7ae27c0605c5\n--- a/read-tree.c\n+++ b/read-tree.c\n@@ -65,12 +65,12 @@ static int read_tree(unsigned char *sha1\n \treturn 0;\n }\n \n-static int remove_lock = 0;\n+static char *index_lock = NULL;\n \n static void remove_lock_file(void)\n {\n-\tif (remove_lock)\n-\t\tunlink(\".git/index.lock\");\n+\tif (index_lock)\n+\t\tunlink(index_lock);\n }\n \n static int same(struct cache_entry *a, struct cache_entry *b)\n@@ -154,14 +154,27 @@ static void trivially_merge_cache(struct\n \n int main(int argc, char **argv)\n {\n-\tint i, newfd;\n+\tint i, newfd, len;\n \tunsigned char sha1[20];\n+\tchar *index_file, *index_path;\n \n-\tnewfd = open(\".git/index.lock\", O_RDWR | O_CREAT | O_EXCL, 0600);\n+\tindex_path = getenv(INDEX_ENVIRONMENT);\n+\tif (!index_path)\n+\t\tindex_path = DEFAULT_INDEX_ENVIRONMENT;\n+\n+\tlen = strlen(index_path);\n+\tindex_file = malloc(len + 7);\n+\tif (!index_file) error(\"out of memory\");\n+\tsprintf(index_file, \"%s/index\", index_path);\n+\n+\tindex_lock = malloc(len + 12);\n+\tif (!index_lock) error(\"out of memory\");\n+\tsprintf(index_lock, \"%s/index.lock\", index_path);\n+\n+\tnewfd = open(index_lock, O_RDWR | O_CREAT | O_EXCL, 0600);\n \tif (newfd < 0)\n \t\tdie(\"unable to create new cachefile\");\n \tatexit(remove_lock_file);\n-\tremove_lock = 1;\n \n \tfor (i = 1; i < argc; i++) {\n \t\tconst char *arg = argv[i];\n@@ -182,8 +195,12 @@ int main(int argc, char **argv)\n \tif (stage == 4)\n \t\ttrivially_merge_cache(active_cache, active_nr);\n \tif (write_cache(newfd, active_cache, active_nr) ||\n-\t    rename(\".git/index.lock\", \".git/index\"))\n+\t    rename(index_lock, index_file))\n \t\tdie(\"unable to write new index file\");\n-\tremove_lock = 0;\n+\n+\tfree(index_file);\n+\tfree(index_lock);\n+\tindex_lock = NULL;\n+\n \treturn 0;\n }\n"}]}