{"thread":{"id":"159","subject":"[PATCH 1/3] add GIT_CACHE_DIRECTORY support","startedAt":"2005-04-20T04:32:06Z","lastAt":"2005-04-20T04:32:06Z","messageCount":1,"participants":["Zach Welch"],"isPatch":true,"patchVersion":1,"patchTotal":3},"messages":[{"id":"935","messageId":"mailbox-1370-1113971526-293449@spoon","threadId":"159","inReplyTo":null,"subject":"[PATCH 1/3] add GIT_CACHE_DIRECTORY support","fromName":"Zach Welch","fromEmail":"zw@superlucidity.net","sentAt":"2005-04-20T04:32:06Z","receivedAt":"2005-04-20T04:32:06Z","isPatch":true,"sender":{"key":"zw@superlucidity.net","avatar":null},"body":" cache.h        |    3 +++\n init-db.c      |    9 +++++++--\n read-cache.c   |   15 +++++++++++++--\n read-tree.c    |   35 ++++++++++++++++++++++++++---------\n update-cache.c |   33 ++++++++++++++++++++++++---------\n 5 files changed, 73 insertions(+), 22 deletions(-)\n\nSigned-Off-By: Zach Welch <zw@superlucidity.net>\n\nThis patch introduces the GIT_CACHE_DIRECTORY to the C plumbing.\nWithout this patch, the index file and its lock are always placed\nin './.git'.  Scripts wishing to run these commands from a different \nworking directory can use this support to override the cache directory.\n\nThis require my latest init-db cleanups be applied first, otherwise\nyou will get a rejection in init-db.c.\n\ncache.h: 5948db759b3f6fb5ade3b027f202330f71a8cb6a\n--- a/cache.h\n+++ b/cache.h\n@@ -81,6 +81,9 @@ const char *sha1_file_directory;\n struct cache_entry **active_cache;\n unsigned int active_nr, active_alloc;\n \n+#define GIT_CACHE_ENVIRONMENT \"GIT_CACHE_DIRECTORY\"\n+#define DEFAULT_GIT_CACHE_ENVIRONMENT \".git\"\n+\n #define DB_ENVIRONMENT \"SHA1_FILE_DIRECTORY\"\n #define DEFAULT_DB_ENVIRONMENT \".git/objects\"\n \ninit-db.c: 14beb35657de229a61673198bfc4e009daafca15\n--- a/init-db.c\n+++ b/init-db.c\n@@ -23,10 +23,15 @@ void safe_create_dir(char *dir)\n  */\n int main(int argc, char **argv)\n {\n-\tchar *sha1_dir, *path;\n+\tchar *sha1_dir, *path, *git_dir;\n \tint len, i;\n \n-\tsafe_create_dir(\".git\");\n+\tgit_dir = getenv(GIT_CACHE_ENVIRONMENT);\n+\tif (!git_dir) {\n+\t\tgit_dir = DEFAULT_GIT_CACHE_ENVIRONMENT;\n+\t\tfprintf(stderr, \"defaulting to local cache area\\n\");\n+\t}\t\n+\tsafe_create_dir(git_dir);\n \n \tsha1_dir = getenv(DB_ENVIRONMENT);\n \tif (!sha1_dir) {\nread-cache.c: edaadf3e1c0714735ca8d80301dd644aa0f9cd2a\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -174,22 +174,33 @@ static int verify_hdr(struct cache_heade\n \n int read_cache(void)\n {\n-\tint fd, i;\n+\tint fd, i, len;\n \tstruct stat st;\n \tunsigned long size, offset;\n \tvoid *map;\n \tstruct cache_header *hdr;\n+\tchar *index_path, *index_file;\n \n \terrno = EBUSY;\n \tif (active_cache)\n \t\treturn error(\"more than one cachefile\");\n \terrno = ENOENT;\n+\n \tsha1_file_directory = getenv(DB_ENVIRONMENT);\n \tif (!sha1_file_directory)\n \t\tsha1_file_directory = DEFAULT_DB_ENVIRONMENT;\n \tif (access(sha1_file_directory, X_OK) < 0)\n \t\treturn error(\"no access to SHA1 file directory\");\n-\tfd = open(\".git/index\", O_RDONLY);\n+\n+\tindex_path = getenv(GIT_CACHE_ENVIRONMENT);\n+\tif (!index_path)\n+\t\tindex_path = DEFAULT_GIT_CACHE_ENVIRONMENT;\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+\tfd = open(index_file, O_RDONLY);\n \tif (fd < 0)\n \t\treturn (errno == ENOENT) ? 0 : error(\"open failed\");\n \nread-tree.c: 9bcba2d567e1c86ae967d383cc081e6947d00a13\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 path_matches(struct cache_entry *a, struct cache_entry *b)\n@@ -205,14 +205,27 @@ static void merge_stat_info(struct cache\n \n int main(int argc, char **argv)\n {\n-\tint i, newfd, merge;\n+\tint i, newfd, len, merge;\n \tunsigned char sha1[20];\n-\n-\tnewfd = open(\".git/index.lock\", O_RDWR | O_CREAT | O_EXCL, 0600);\n+ \tchar *index_file, *index_path;\n+  \n+ \tindex_path = getenv(GIT_CACHE_ENVIRONMENT);\n+ \tif (!index_path)\n+ \t\tindex_path = DEFAULT_GIT_CACHE_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 \tmerge = 0;\n \tfor (i = 1; i < argc; i++) {\n@@ -253,8 +266,12 @@ int main(int argc, char **argv)\n \t\t}\n \t}\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 }\nupdate-cache.c: 0d16b36d7d074e9f0a2811a40e16e9823a628ec9\n--- a/update-cache.c\n+++ b/update-cache.c\n@@ -270,25 +270,37 @@ static int add_cacheinfo(char *arg1, cha\n \treturn add_cache_entry(ce, allow_add);\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 int main(int argc, char **argv)\n {\n-\tint i, newfd, entries;\n+\tint i, newfd, entries, len;\n \tint allow_options = 1;\n+\tchar *index_file, *index_path;\n \n-\tnewfd = open(\".git/index.lock\", O_RDWR | O_CREAT | O_EXCL, 0600);\n+\tindex_path = getenv(GIT_CACHE_ENVIRONMENT);\n+\tif (!index_path)\n+\t\tindex_path = DEFAULT_GIT_CACHE_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-\n \tatexit(remove_lock_file);\n-\tremove_lock = 1;\n \n \tentries = read_cache();\n \tif (entries < 0)\n@@ -330,9 +342,12 @@ int main(int argc, char **argv)\n \t\t\tdie(\"Unable to add %s to database\", path);\n \t}\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 cachefile\");\n \n-\tremove_lock = 0;\n+\tfree(index_file);\n+\tfree(index_lock);\n+\tindex_lock = NULL;\n+\n \treturn 0;\n }\n"}]}