Skip to contentSkip to conversation

introduce GIT_CACHE_DIRECTORY to plumbing

PATCH New Zach Welch 1 participant 4 messages Jun 15, 2016

lore
Share & tools
lore

Zach Welch

author
#0 lore raw

cover introduce GIT_CACHE_DIRECTORY to plumbing

The first 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.

The second patch renames the DB_ENVIRONMENT symbols to match. Note, the name changes came from an off-list comment from Linus, after my sendpatches script accidentally bombed him with a half dozen copies of the init-db patch series summary. His comment inspired these last two.

The third patch renames SHA1_FILE_DIRECTORY as GIT_OBJECT_DIRECTORY, to match the "GIT_CACHE_DIRECTORY" name introduced in the first patch. This was done last and seperately so it can be applied after a little bit of notice. We're not at a point of need to be backwards compatible.

This require my latest init-db cleanups be applied first, otherwise you will get a rejection in init-db.c.

There are 3 patches in this series:
[PATCH 1/3] add GIT_CACHE_DIRECTORY support [PATCH 2/3] rename object directory symbols [PATCH 3/3] rename SHA1_FILE_DIRECTORY

author
#1 lore raw

PATCH 1/3 add GIT_CACHE_DIRECTORY support

 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(-)

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

cache.h +3 -0
@@ -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

init-db.c +7 -2
@@ -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

read-cache.c +13 -2
@@ -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

read-tree.c +26 -9
@@ -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

update-cache.c +24 -9
@@ -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;
 }
author
#2 lore raw

PATCH 2/3 rename object directory symbols

In reply to Zach Welch

This patch applies on top of:
[PATCH 1/3] add GIT_CACHE_DIRECTORY support

 cache.h      |    4 ++--
 fsck-cache.c |    2 +-
 init-db.c    |    4 ++--
 ls-tree.c    |    4 ++--
 read-cache.c |    4 ++--
 sha1_file.c  |    2 +-
 6 files changed, 10 insertions(+), 10 deletions(-)

Rename the DB_ENVIRONMENT symbols to match the newly introduced GIT_CACHE_ENVIROMENT symbols.

cache.h: 1fca894f485471d51c6a72c16e02df6d56d0052f

cache.h +2 -2
@@ -84,8 +84,8 @@ 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"
+#define GIT_OBJECT_ENVIRONMENT "SHA1_FILE_DIRECTORY"
+#define DEFAULT_GIT_OBJECT_ENVIRONMENT ".git/objects"
 
 #define alloc_nr(x) (((x)+16)*3/2)
 

fsck-cache.c: cf39b7e054d9685fde7004ec767cd098b97e8ce7

fsck-cache.c +1 -1
@@ -135,7 +135,7 @@ int main(int argc, char **argv)
 	int i, heads;
 	char *sha1_dir;
 
-	sha1_dir = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT;
+	sha1_dir = getenv(GIT_OBJECT_ENVIRONMENT) ? : DEFAULT_GIT_OBJECT_ENVIRONMENT;
 	for (i = 0; i < 256; i++) {
 		static char dir[4096];
 		sprintf(dir, "%s/%02x", sha1_dir, i);

init-db.c: 9e693a0a914512c5574f394222cfc75496e3453a

init-db.c +2 -2
@@ -33,9 +33,9 @@ int main(int argc, char **argv)
 	}	
 	safe_create_dir(git_dir);
 
-	sha1_dir = getenv(DB_ENVIRONMENT);
+	sha1_dir = getenv(GIT_OBJECT_ENVIRONMENT);
 	if (!sha1_dir) {
-		sha1_dir = DEFAULT_DB_ENVIRONMENT;
+		sha1_dir = DEFAULT_GIT_OBJECT_ENVIRONMENT;
 		fprintf(stderr, "defaulting to local storage area\n");
 	}
 	len = strlen(sha1_dir);

ls-tree.c: 936bb19a5525046a5a784d5f14c3ea7da406cc62

ls-tree.c +2 -2
@@ -105,9 +105,9 @@ int main(int argc, char **argv)
 		usage(ls_tree_usage);
 	if (get_sha1_hex(argv[1], sha1) < 0)
 		usage(ls_tree_usage);
-	sha1_file_directory = getenv(DB_ENVIRONMENT);
+	sha1_file_directory = getenv(GIT_OBJECT_ENVIRONMENT);
 	if (!sha1_file_directory)
-		sha1_file_directory = DEFAULT_DB_ENVIRONMENT;
+		sha1_file_directory = DEFAULT_GIT_OBJECT_ENVIRONMENT;
 	if (list(sha1) < 0)
 		die("list failed");
 	return 0;

read-cache.c: 9eee23097b9406548765ec6fc77e61788317df19

read-cache.c +2 -2
@@ -186,9 +186,9 @@ int read_cache(void)
 		return error("more than one cachefile");
 	errno = ENOENT;
 
-	sha1_file_directory = getenv(DB_ENVIRONMENT);
+	sha1_file_directory = getenv(GIT_OBJECT_ENVIRONMENT);
 	if (!sha1_file_directory)
-		sha1_file_directory = DEFAULT_DB_ENVIRONMENT;
+		sha1_file_directory = DEFAULT_GIT_OBJECT_ENVIRONMENT;
 	if (access(sha1_file_directory, X_OK) < 0)
 		return error("no access to SHA1 file directory");
 

sha1_file.c: c4591cd2168ae2e42c1fc9878be8befbfa1a8afa

sha1_file.c +1 -1
@@ -61,7 +61,7 @@ char *sha1_file_name(const unsigned char
 	static char *name, *base;
 
 	if (!base) {
-		char *sha1_file_directory = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT;
+		char *sha1_file_directory = getenv(GIT_OBJECT_ENVIRONMENT) ? : DEFAULT_GIT_OBJECT_ENVIRONMENT;
 		int len = strlen(sha1_file_directory);
 		base = malloc(len + 60);
 		memcpy(base, sha1_file_directory, len);
author
#3 lore raw

PATCH 3/3 rename SHA1_FILE_DIRECTORY

In reply to Zach Welch

This patch applies on top of:
[PATCH 1/3] add GIT_CACHE_DIRECTORY support [PATCH 2/3] rename object directory symbols

 cache.h |    2 +-
 1 files changed, 1 insertion(+), 1 deletion(-)

Rename SHA1_FILE_DIRECTORY to GIT_OBJECT_DIRECTORY. Scripts that used to pass this setting will need to be updated.

cache.h: 218bec12fab3fb57ad03fafccedd4398c64c3646

cache.h +1 -1
@@ -84,7 +84,7 @@ unsigned int active_nr, active_alloc;
 #define GIT_CACHE_ENVIRONMENT "GIT_CACHE_DIRECTORY"
 #define DEFAULT_GIT_CACHE_ENVIRONMENT ".git"
 
-#define GIT_OBJECT_ENVIRONMENT "SHA1_FILE_DIRECTORY"
+#define GIT_OBJECT_ENVIRONMENT "GIT_OBJECT_DIRECTORY"
 #define DEFAULT_GIT_OBJECT_ENVIRONMENT ".git/objects"
 
 #define alloc_nr(x) (((x)+16)*3/2)