Skip to contentSkip to conversation

init-db.c cleanup and fixes

PATCH In review Zach Welch 2 participants 7 messages Jun 15, 2016

lore
Share & tools
lore

Zach Welch

author
#0 lore raw

cover init-db.c cleanup and fixes

Linus,

I see you pulled the first two patches of my last series into your tree, so I know I had your attention briefly. I wanted to see what I can do to help the rest of the changes get in, so....

I realized last night as I was going to bed that the third patch might not be accepted because it changes the behaviour slightly, nevermind that they were - by comparison with today's alternative - plain ugly.

For what it's worth, init-db is practically useless for my package without the second change in this series. Currently, I've implemented init-db in pure perl, but I'd like to use init-db.

As such, I started from scratch, and came up with a much simpler series of patches. Please continue to ignore the previous series, but consider these new patches in their stead.

New GIT_FILE_DIRECTORY patches will follow seperately.

Cheers,

Zach Welch
Superlucidity Services

These patches were based off commit 4e1778c8ceeaea340a2a7f62fc65736da327ec05.

There are 3 patches in this series:
[PATCH 1/3] init-db.c: cleanup comments
[PATCH 2/3] init-db.c: normalize env var handling. [PATCH 3/3] init-db.c: create and use safe_create_dir helper

author
#1 lore raw

PATCH 1/3 init-db.c: cleanup comments

 init-db.c |   15 ++++++---------
 1 files changed, 6 insertions(+), 9 deletions(-)

Consolidate comments at top of main.

init-db.c +6 -9
@@ -5,6 +5,12 @@
  */
 #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 one DB per managed directory.
+ */
 int main(int argc, char **argv)
 {
 	char *sha1_dir, *path;
@@ -15,12 +21,6 @@ int main(int argc, char **argv)
 		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;
@@ -29,9 +29,6 @@ int main(int argc, char **argv)
 		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);
author
#2 lore raw

PATCH 2/3 init-db.c: normalize env var handling.

In reply to Zach Welch

This patch applies on top of:
[PATCH 1/3] init-db.c: cleanup comments

 init-db.c |   11 +++--------
 1 files changed, 3 insertions(+), 8 deletions(-)

Normalize init-db environment variable handling, allowing the creation of object directories with something other than DEFAULT_DB_ENVIRONMENT.

init-db.c +3 -8
@@ -22,15 +22,10 @@ int main(int argc, char **argv)
 	}
 
 	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);
+	if (!sha1_dir) {
+		sha1_dir = DEFAULT_DB_ENVIRONMENT;
+		fprintf(stderr, "defaulting to local storage area\n");
 	}
-
-	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) {
#3 lore raw

Re: [PATCH 2/3] init-db.c: normalize env var handling.

init-db.c

Quoted 14 lines · Zach Welch

This patch applies on top of:
[PATCH 1/3] init-db.c: cleanup comments

 init-db.c |   11 +++--------
 1 files changed, 3 insertions(+), 8 deletions(-)

Normalize init-db environment variable handling, allowing the creation of object directories with something other than DEFAULT_DB_ENVIRONMENT.

init-db.c +0 -0

For future reference, this is in the wrong order.

You should have "checkin comment" first, then "signed-off-by", then a line with three dashes, and then "administrative trivia".

Ie I'd much rather see the email look like

Normalize init-db environment variable handling, allowing the creation of object directories with something other than DEFAULT_DB_ENVIRONMENT.

---
This patch applies on top of:
[PATCH 1/3] init-db.c: cleanup comments

init-db.c | 11 +++--------
1 files changed, 3 insertions(+), 8 deletions(-)

.. actual patch goes here ..

since otherwise I'll just have to edit it that way. I like seeing the administrative stuff (diffstat etc), but I don't want to have it in the commit message, and that's exactly what the "---" marker is for - my tools will automatically cut it off as if it was a signature (or the beginning of the patch).

Linus

author
#4 lore raw

Re: [PATCH 2/3] init-db.c: normalize env var handling.

Quoted 1 line · Linus Torvalds

For future reference, this is in the wrong order.

I feel even more abashed for my earlier scripting faux pas. Would you like me to resend them to you off-list?

Cheers,

Zach

#5 lore raw

Re: [PATCH 2/3] init-db.c: normalize env var handling.

Quoted 3 lines · Zach Welch

I feel even more abashed for my earlier scripting faux pas. Would you like me to resend them to you off-list?

No, I edited them and applied them (the first series, I'll have to think about the second one).

It's only when there are tens of patches that it gets really old really quickly to edit things by hand. Three I can handle ;)

Linus

author
#6 lore raw

PATCH 3/3 init-db.c: create and use safe_create_dir helper

In reply to Zach Welch

This patch applies on top of:
[PATCH 1/3] init-db.c: cleanup comments [PATCH 2/3] init-db.c: normalize env var handling.

 init-db.c |   30 ++++++++++++++----------------
 1 files changed, 14 insertions(+), 16 deletions(-)

Factor mkdir calls into common safe_create_dir subroutine.

init-db.c +14 -16
@@ -5,6 +5,16 @@
  */
 #include "cache.h"
 
+void safe_create_dir(char *dir)
+{
+	if (mkdir(dir, 0755) < 0) {
+		if (errno != EEXIST) {
+			perror(dir);
+			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.
@@ -16,10 +26,7 @@
 	char *sha1_dir, *path;
 	int len, i;
 
-	if (mkdir(".git", 0755) < 0) {
-		perror("unable to create .git directory");
-		exit(1);
-	}
+	safe_create_dir(".git");
 
 	sha1_dir = getenv(DB_ENVIRONMENT);
 	if (!sha1_dir) {
@@ -27,22 +34,13 @@
 		fprintf(stderr, "defaulting to local storage area\n");
 	}
 	len = strlen(sha1_dir);
-	if (mkdir(sha1_dir, 0755) < 0) {
-		if (errno != EEXIST) {
-			perror(sha1_dir);
-			exit(1);
-		}
-	}
 	path = malloc(len + 40);
 	memcpy(path, sha1_dir, len);
+
+	safe_create_dir(sha1_dir);
 	for (i = 0; i < 256; i++) {
 		sprintf(path+len, "/%02x", i);
-		if (mkdir(path, 0755) < 0) {
-			if (errno != EEXIST) {
-				perror(path);
-				exit(1);
-			}
-		}
+		safe_create_dir(path);
 	}
 	return 0;
 }