cache.h | 3 +++ init-db.c | 9 +++++++-- read-cache.c | 15 +++++++++++++-- read-tree.c | 35 ++++++++++++++++++++++++++--------- update-cache.c | 33 ++++++++++++++++++++++++--------- 5 files changed, 73 insertions(+), 22 deletions(-)
Signed-Off-By: Zach Welch <[EMAIL PROTECTED]> This patch introduces the GIT_CACHE_DIRECTORY to the C plumbing. Without this patch, the index file and its lock are always placed in './.git'. Scripts wishing to run these commands from a different working directory can use this support to override the cache directory. This require my latest init-db cleanups be applied first, otherwise you will get a rejection in init-db.c. cache.h: 5948db759b3f6fb5ade3b027f202330f71a8cb6a --- a/cache.h +++ b/cache.h @@ -81,6 +81,9 @@ const char *sha1_file_directory; struct cache_entry **active_cache; unsigned int active_nr, active_alloc; +#define GIT_CACHE_ENVIRONMENT "GIT_CACHE_DIRECTORY" +#define DEFAULT_GIT_CACHE_ENVIRONMENT ".git" + #define DB_ENVIRONMENT "SHA1_FILE_DIRECTORY" #define DEFAULT_DB_ENVIRONMENT ".git/objects" init-db.c: 14beb35657de229a61673198bfc4e009daafca15 --- a/init-db.c +++ b/init-db.c @@ -23,10 +23,15 @@ void safe_create_dir(char *dir) */ int main(int argc, char **argv) { - char *sha1_dir, *path; + char *sha1_dir, *path, *git_dir; int len, i; - safe_create_dir(".git"); + git_dir = getenv(GIT_CACHE_ENVIRONMENT); + if (!git_dir) { + git_dir = DEFAULT_GIT_CACHE_ENVIRONMENT; + fprintf(stderr, "defaulting to local cache area\n"); + } + safe_create_dir(git_dir); sha1_dir = getenv(DB_ENVIRONMENT); if (!sha1_dir) { read-cache.c: edaadf3e1c0714735ca8d80301dd644aa0f9cd2a --- a/read-cache.c +++ b/read-cache.c @@ -174,22 +174,33 @@ static int verify_hdr(struct cache_heade int read_cache(void) { - int fd, i; + int fd, i, len; struct stat st; unsigned long size, offset; void *map; struct cache_header *hdr; + char *index_path, *index_file; errno = EBUSY; if (active_cache) return error("more than one cachefile"); errno = ENOENT; + sha1_file_directory = getenv(DB_ENVIRONMENT); if (!sha1_file_directory) sha1_file_directory = DEFAULT_DB_ENVIRONMENT; if (access(sha1_file_directory, X_OK) < 0) return error("no access to SHA1 file directory"); - fd = open(".git/index", O_RDONLY); + + index_path = getenv(GIT_CACHE_ENVIRONMENT); + if (!index_path) + index_path = DEFAULT_GIT_CACHE_ENVIRONMENT; + len = strlen(index_path); + index_file = malloc(len + 7); + if (!index_file) error("out of memory"); + sprintf(index_file, "%s/index", index_path); + + fd = open(index_file, O_RDONLY); if (fd < 0) return (errno == ENOENT) ? 0 : error("open failed"); read-tree.c: 9bcba2d567e1c86ae967d383cc081e6947d00a13 --- a/read-tree.c +++ b/read-tree.c @@ -65,12 +65,12 @@ static int read_tree(unsigned char *sha1 return 0; } -static int remove_lock = 0; +static char *index_lock = NULL; static void remove_lock_file(void) { - if (remove_lock) - unlink(".git/index.lock"); + if (index_lock) + unlink(index_lock); } static int path_matches(struct cache_entry *a, struct cache_entry *b) @@ -205,14 +205,27 @@ static void merge_stat_info(struct cache int main(int argc, char **argv) { - int i, newfd, merge; + int i, newfd, len, merge; unsigned char sha1[20]; - - newfd = open(".git/index.lock", O_RDWR | O_CREAT | O_EXCL, 0600); + char *index_file, *index_path; + + index_path = getenv(GIT_CACHE_ENVIRONMENT); + if (!index_path) + index_path = DEFAULT_GIT_CACHE_ENVIRONMENT; + + len = strlen(index_path); + index_file = malloc(len + 7); + if (!index_file) error("out of memory"); + sprintf(index_file, "%s/index", index_path); + + index_lock = malloc(len + 12); + if (!index_lock) error("out of memory"); + sprintf(index_lock, "%s/index.lock", index_path); + + newfd = open(index_lock, O_RDWR | O_CREAT | O_EXCL, 0600); if (newfd < 0) die("unable to create new cachefile"); atexit(remove_lock_file); - remove_lock = 1; merge = 0; for (i = 1; i < argc; i++) { @@ -253,8 +266,12 @@ int main(int argc, char **argv) } } if (write_cache(newfd, active_cache, active_nr) || - rename(".git/index.lock", ".git/index")) + rename(index_lock, index_file)) die("unable to write new index file"); - remove_lock = 0; + + free(index_file); + free(index_lock); + index_lock = NULL; + return 0; } update-cache.c: 0d16b36d7d074e9f0a2811a40e16e9823a628ec9 --- a/update-cache.c +++ b/update-cache.c @@ -270,25 +270,37 @@ static int add_cacheinfo(char *arg1, cha return add_cache_entry(ce, allow_add); } -static int remove_lock = 0; +static char *index_lock = NULL; static void remove_lock_file(void) { - if (remove_lock) - unlink(".git/index.lock"); + if (index_lock) + unlink(index_lock); } int main(int argc, char **argv) { - int i, newfd, entries; + int i, newfd, entries, len; int allow_options = 1; + char *index_file, *index_path; - newfd = open(".git/index.lock", O_RDWR | O_CREAT | O_EXCL, 0600); + index_path = getenv(GIT_CACHE_ENVIRONMENT); + if (!index_path) + index_path = DEFAULT_GIT_CACHE_ENVIRONMENT; + + len = strlen(index_path); + index_file = malloc(len + 7); + if (!index_file) error("out of memory"); + sprintf(index_file, "%s/index", index_path); + + index_lock = malloc(len + 12); + if (!index_lock) error("out of memory"); + sprintf(index_lock, "%s/index.lock", index_path); + + newfd = open(index_lock, O_RDWR | O_CREAT | O_EXCL, 0600); if (newfd < 0) die("unable to create new cachefile"); - atexit(remove_lock_file); - remove_lock = 1; entries = read_cache(); if (entries < 0) @@ -330,9 +342,12 @@ int main(int argc, char **argv) die("Unable to add %s to database", path); } if (write_cache(newfd, active_cache, active_nr) || - rename(".git/index.lock", ".git/index")) + rename(index_lock, index_file)) die("Unable to write new cachefile"); - remove_lock = 0; + free(index_file); + free(index_lock); + index_lock = NULL; + return 0; } - To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to [EMAIL PROTECTED] More majordomo info at http://vger.kernel.org/majordomo-info.html