Skip to contentSkip to conversation

init-db.c cleanup, add INDEX_FILE_DIRECTORY support

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

lore
Share & tools
lore

Zach Welch

author
#0 lore raw

cover init-db.c cleanup, add INDEX_FILE_DIRECTORY support

Hi all,

I'm working on an OO perl alternative to Petr's git scripts, which I'm currently calling "yogi" (your other git interface). While I'm not ready to release that just yet, I wanted to start floating some patches to the core plumbing to support the respective packages' potentially divergent demands. For those die-hard perl hackers, I can float you a copy of the package off-list if you're interested; I'm hoping to finish a public 0.0.1 release by the end of the week.

The first two patches in the series are already in the pasky.git repository, but Linus hasn't merged them yet. The are included only because the next few patches expect them to be in place.

The third patch in the series prepares for the forth patch by factoring the object directory detection and creation functionality. The fifth patch makes one final pass at cleaning up init-db. The 3rd and 5th patches aren't particularly valuable unless the remaining patches are also applied, but they do make the code a bit prettier. To me, at least.

The remaining patches (4,6,7,8) add the ability for the '.git' index directory to be overridden in the same manner as the object directory. This allows me to create my own independent '.yogi' trees, the very notion of which may cause this whole series to be henceforth flamed into oblivion.

Here's to hoping otherwise....

Cheers,

Zach Welch
Superlucidity Services

These patches are based off commit 5b53d3a08d64198d26d4f2323f235790c04aeaab.

There are 8 patches in this series:
[PATCH 1/8] init-db.c: [RESEND] remove redundant getenv call [PATCH 2/8] init-db.c: [RESEND] make init-db work with common objects [PATCH 3/8] init-db.c: refactor directory creation
[PATCH 4/8] init-db.c: add INDEX_FILE_DIRECTORY support [PATCH 5/8] init-db.c: refactor mkdir logic
[PATCH 6/8] read-cache.c: add INDEX_FILE_DIRECTORY support [PATCH 7/8] read-tree.c: add INDEX_FILE_DIRECTORY support [PATCH 8/8] update-cache.c: add INDEX_FILE_DIRECTORY support

author
#1 lore raw

PATCH 1/8 init-db.c: [RESEND] remove redundant getenv call

init-db calls getenv(DB_ENVIRONMENT) twice. Once should be enough.

This patch applies on top of:
[PATCH 0/8] init-db.c cleanup, add INDEX_FILE_DIRECTORY support

 init-db.c |    3 +--
 1 files changed, 1 insertion(+), 2 deletions(-)
init-db.c +1 -1
@@ -7,7 +7,7 @@
 
 int main(int argc, char **argv)
 {
-	char *sha1_dir = getenv(DB_ENVIRONMENT), *path;
+	char *sha1_dir, *path;
 	int len, i;
 
 	if (mkdir(".git", 0755) < 0) {

-

author
#2 lore raw

PATCH 2/8 init-db.c: [RESEND] make init-db work with common objects

In reply to Zach Welch

This makes init-db work for common object database.

This patch applies on top of:
[PATCH 0/8] init-db.c cleanup, add INDEX_FILE_DIRECTORY support [PATCH 1/8] init-db.c: [RESEND] remove redundant getenv call

 init-db.c |    2 +-
 1 files changed, 1 insertion(+), 1 deletion(-)

Author: Aaron Straus <[email protected]>

init-db.c: aa00fbb1b95624f6c30090a17354c9c08a6ac596

init-db.c +1 -1
@@ -24,7 +24,7 @@ int main(int argc, char **argv)
 	sha1_dir = getenv(DB_ENVIRONMENT);
 	if (sha1_dir) {
 		struct stat st;
-		if (!stat(sha1_dir, &st) < 0 && S_ISDIR(st.st_mode))
+		if (!stat(sha1_dir, &st) && S_ISDIR(st.st_mode))
 			return 0;
 		fprintf(stderr, "DB_ENVIRONMENT set to bad directory %s: ", sha1_dir);
 	}
author
#3 lore raw

PATCH 3/8 init-db.c: refactor directory creation

In reply to Zach Welch

This patch factors the init-db directory creation into a new function, which is then reused in the next patch.

This patch applies on top of:
[PATCH 0/8] init-db.c cleanup, add INDEX_FILE_DIRECTORY support [PATCH 1/8] init-db.c: [RESEND] remove redundant getenv call [PATCH 2/8] init-db.c: [RESEND] make init-db work with common objects

 init-db.c |   61 ++++++++++++++++++++++++++++-----------------------
 1 files changed, 34 insertions(+), 27 deletions(-)
init-db.c +32 -0
@@ -4,43 +4,50 @@
  * Copyright (C) Linus Torvalds, 2005
  */
 #include "cache.h"
+/*
+ * If you want to, you can share the DB area with any number of branches.
+ * That has advantages: you can save space by sharing all the SHA1 objects.
+ * On the other hand, it might just make lookup slower and messier. You
+ * be the judge.  The default case is to have a DB per managed directory.
+ */
+
+static char* init_dir(char *env, char *std, char *label, int *len)
+{
+	char *dir;
+	dir = getenv(env);
+	if (dir) {
+		struct stat st;
+		if (stat(dir, &st) < 0 || !S_ISDIR(st.st_mode)) {
+			fprintf(stderr, "%s set to bad directory %s: ", env, dir);
+			exit(1);
+		}
+	} 
+	else {
+		dir = std;
+		fprintf(stderr, "defaulting to private %s area\n", label);
+	}
+	if (mkdir(dir, 0755) < 0) {
+		if (errno != EEXIST) {
+			perror(dir);
+			exit(1);
+		}
+	}
+	if (len)
+		*len = strlen(dir);
+	return dir;
+}
 
 int main(int argc, char **argv)
 {
	char *sha1_dir, *path;

int len, i;

if (mkdir(".git", 0755) < 0) {
perror("unable to create .git directory"); exit(1);
}
-
- /*
- * If you want to, you can share the DB area with any number of branches.
- * That has advantages: you can save space by sharing all the SHA1 objects.
- * On the other hand, it might just make lookup slower and messier. You
- * be the judge.
- */
- sha1_dir = getenv(DB_ENVIRONMENT);
- if (sha1_dir) {
- struct stat st;
- if (!stat(sha1_dir, &st) && S_ISDIR(st.st_mode))
- return 0;
- fprintf(stderr, "DB_ENVIRONMENT set to bad directory %s: ", sha1_dir);
- }
-
- /*
- * The default case is to have a DB per managed directory.
- */
- sha1_dir = DEFAULT_DB_ENVIRONMENT;
- fprintf(stderr, "defaulting to private storage area\n");
- len = strlen(sha1_dir);
- if (mkdir(sha1_dir, 0755) < 0) {
- if (errno != EEXIST) {
- perror(sha1_dir);
- exit(1);
- }
- }
+ sha1_dir = init_dir(DB_ENVIRONMENT, DEFAULT_DB_ENVIRONMENT, "storage", &len);
+
path = malloc(len + 40);
memcpy(path, sha1_dir, len);
for (i = 0; i < 256; i++) {

author
#4 lore raw

PATCH 4/8 init-db.c: add INDEX_FILE_DIRECTORY support

In reply to Zach Welch

This patch give init-db the ability for the index directory to be overridden by the INDEX_FILE_DIRECTORY environment variable.

This patch applies on top of:
[PATCH 0/8] init-db.c cleanup, add INDEX_FILE_DIRECTORY support [PATCH 1/8] init-db.c: [RESEND] remove redundant getenv call [PATCH 2/8] init-db.c: [RESEND] make init-db work with common objects [PATCH 3/8] init-db.c: refactor directory creation

 cache.h   |    3 +++
 init-db.c |    5 +----
 2 files changed, 4 insertions(+), 4 deletions(-)
cache.h +3 -0
@@ -81,6 +81,9 @@
 struct cache_entry **active_cache;
 unsigned int active_nr, active_alloc;
 
+#define INDEX_ENVIRONMENT "INDEX_FILE_DIRECTORY"
+#define DEFAULT_INDEX_ENVIRONMENT ".git"
+
 #define DB_ENVIRONMENT "SHA1_FILE_DIRECTORY"
 #define DEFAULT_DB_ENVIRONMENT ".git/objects"
 
init-db.c +1 -4
@@ -42,10 +42,7 @@
 	char *sha1_dir, *path;
 	int len, i;
 
-	if (mkdir(".git", 0755) < 0) {
-		perror("unable to create .git directory");
-		exit(1);
-	}
+	(void) init_dir(INDEX_ENVIRONMENT, DEFAULT_INDEX_ENVIRONMENT, "index", NULL);
 	sha1_dir = init_dir(DB_ENVIRONMENT, DEFAULT_DB_ENVIRONMENT, "storage", &len);
 	
 	path = malloc(len + 40);
author
#5 lore raw

PATCH 5/8 init-db.c: refactor mkdir logic

In reply to Zach Welch

Move redundant mkdir call logic into helper function.

This patch applies on top of:
[PATCH 0/8] init-db.c cleanup, add INDEX_FILE_DIRECTORY support [PATCH 1/8] init-db.c: [RESEND] remove redundant getenv call [PATCH 2/8] init-db.c: [RESEND] make init-db work with common objects [PATCH 3/8] init-db.c: refactor directory creation
[PATCH 4/8] init-db.c: add INDEX_FILE_DIRECTORY support

 init-db.c |   24 ++++++++++++------------
 1 files changed, 12 insertions(+), 12 deletions(-)
init-db.c +12 -12
@@ -11,6 +11,16 @@
  * be the judge.  The default case is to have a DB per managed directory.
  */
 
+static void create_dir(char *path) 
+{
+	if (mkdir(dir, 0755) < 0) {
+		if (errno != EEXIST) {
+			perror(dir);
+			exit(1);
+		}
+	}
+}
+
 static char* init_dir(char *env, char *std, char *label, int *len)
 {
 	char *dir;
@@ -26,12 +36,7 @@
 		dir = std;
 		fprintf(stderr, "defaulting to private %s area\n", label);
 	}
-	if (mkdir(dir, 0755) < 0) {
-		if (errno != EEXIST) {
-			perror(dir);
-			exit(1);
-		}
-	}
+	create_dir(dir);
 	if (len)
 		*len = strlen(dir);
 	return dir;
@@ -49,12 +54,7 @@
 	memcpy(path, sha1_dir, len);
 	for (i = 0; i < 256; i++) {
 		sprintf(path+len, "/%02x", i);
-		if (mkdir(path, 0755) < 0) {
-			if (errno != EEXIST) {
-				perror(path);
-				exit(1);
-			}
-		}
+		create_dir(path);
 	}
 	return 0;
 }
author
#6 lore raw

PATCH 1/1 init-db.c: fix stupid typo

Fix a stupid typo from the last mkdir refactorng patch.

 init-db.c |    2 +-
 1 files changed, 1 insertion(+), 1 deletion(-)
init-db.c +1 -1
@@ -11,7 +11,7 @@
  * be the judge.  The default case is to have a DB per managed directory.
  */
 
-static void create_dir(char *path) 
+static void create_dir(char *dir) 
 {
 	if (mkdir(dir, 0755) < 0) {
 		if (errno != EEXIST) {

There are 0 patches in this series:

author
#7 lore raw

PATCH 6/8 read-cache.c: add INDEX_FILE_DIRECTORY support

In reply to Zach Welch

This patch give read-cache the ability for the index directory to be overridden by the INDEX_FILE_DIRECTORY environment variable.

This patch applies on top of:
[PATCH 0/8] init-db.c cleanup, add INDEX_FILE_DIRECTORY support [PATCH 1/8] init-db.c: [RESEND] remove redundant getenv call [PATCH 2/8] init-db.c: [RESEND] make init-db work with common objects [PATCH 3/8] init-db.c: refactor directory creation
[PATCH 4/8] init-db.c: add INDEX_FILE_DIRECTORY support [PATCH 5/8] init-db.c: refactor mkdir logic

 read-cache.c |   15 +++++++++++++--
 1 files changed, 13 insertions(+), 2 deletions(-)

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(INDEX_ENVIRONMENT);
+	if (!index_path)
+		index_path = DEFAULT_INDEX_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");
 
author
#8 lore raw

PATCH 7/8 read-tree.c: add INDEX_FILE_DIRECTORY support

In reply to Zach Welch

This patch give read-tree the ability for the index directory to be overridden by the INDEX_FILE_DIRECTORY environment variable.

This patch applies on top of:
[PATCH 0/8] init-db.c cleanup, add INDEX_FILE_DIRECTORY support [PATCH 1/8] init-db.c: [RESEND] remove redundant getenv call [PATCH 2/8] init-db.c: [RESEND] make init-db work with common objects [PATCH 3/8] init-db.c: refactor directory creation
[PATCH 4/8] init-db.c: add INDEX_FILE_DIRECTORY support [PATCH 5/8] init-db.c: refactor mkdir logic
[PATCH 6/8] read-cache.c: add INDEX_FILE_DIRECTORY support

 read-tree.c |   33 +++++++++++++++++++++++++--------
 1 files changed, 25 insertions(+), 8 deletions(-)

read-tree.c: 42556c82def1d23f21116a2c1b3e7ae27c0605c5

read-tree.c +25 -8
@@ -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 same(struct cache_entry *a, struct cache_entry *b)
@@ -154,14 +154,27 @@ static void trivially_merge_cache(struct
 
 int main(int argc, char **argv)
 {
-	int i, newfd;
+	int i, newfd, len;
 	unsigned char sha1[20];
+	char *index_file, *index_path;
 
-	newfd = open(".git/index.lock", O_RDWR | O_CREAT | O_EXCL, 0600);
+	index_path = getenv(INDEX_ENVIRONMENT);
+	if (!index_path)
+		index_path = DEFAULT_INDEX_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;
 
 	for (i = 1; i < argc; i++) {
 		const char *arg = argv[i];
@@ -182,8 +195,12 @@ int main(int argc, char **argv)
 	if (stage == 4)
 		trivially_merge_cache(active_cache, active_nr);
 	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;
 }
author
#9 lore raw

PATCH 8/8 update-cache.c: add INDEX_FILE_DIRECTORY support

In reply to Zach Welch

This patch give update-cache the ability for the index directory to be overridden by the INDEX_FILE_DIRECTORY environment variable.

This patch applies on top of:
[PATCH 0/8] init-db.c cleanup, add INDEX_FILE_DIRECTORY support [PATCH 1/8] init-db.c: [RESEND] remove redundant getenv call [PATCH 2/8] init-db.c: [RESEND] make init-db work with common objects [PATCH 3/8] init-db.c: refactor directory creation
[PATCH 4/8] init-db.c: add INDEX_FILE_DIRECTORY support [PATCH 5/8] init-db.c: refactor mkdir logic
[PATCH 6/8] read-cache.c: add INDEX_FILE_DIRECTORY support [PATCH 7/8] read-tree.c: add INDEX_FILE_DIRECTORY support

 update-cache.c |   33 ++++++++++++++++++++++++---------
 1 files changed, 24 insertions(+), 9 deletions(-)

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(INDEX_ENVIRONMENT);
+	if (!index_path)
+		index_path = DEFAULT_INDEX_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;
 }