git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Use git config file for committer name and email info
@ 2005-10-12  0:54 Linus Torvalds
  0 siblings, 0 replies; only message in thread
From: Linus Torvalds @ 2005-10-12  0:54 UTC (permalink / raw)
  To: Junio C Hamano, Git Mailing List


This starts using the "user.name" and "user.email" config variables if
they exist as the default name and email when committing.  This means
that you don't have to use the GIT_COMMITTER_EMAIL environment variable
to override your email - you can just edit the config file instead.

The patch looks bigger than it is because it makes the default name and
email information non-static and renames it appropriately.  And it moves
the common git environment variables into a new library file, so that
you can link against libgit.a and get the git environment without having
to link in zlib and libcrypt.

In short, most of it is renaming and moving, the real change core is
just a few new lines in "git_default_config()" that copies the user
config values to the new base.

It also changes "git-var -l" to list the config variables.

Signed-off-by: Linus Torvalds <torvalds@osdl.org>
---

With this, you can now have a .git/config file that has something like 
this in it:

	[user]
	   name = Linus Torvalds
	   email = torvalds@osdl.org

	[core]
	   filemode = false

and it will do the obvious thing. The "user.name" and "user.email" 
variables will be used by default for the name information, overriding any 
system information from /etc/passwd etc.

NOTE! The GIT_{COMMITTER|AUTHOR}_NAME and GIT_{COMMITTER|AUTHOR}_EMAIL 
environment variables continue to override any config file information. 

Also, we should probably verify that the name/email is valid (ie doesn't 
contain any of the characters "<>\n" that could cause invalid commit 
objects).

The fact that "git-var -l" now also shows all config info can be useful. 

For example, scripts can use it to pick up stuff from the config file, ie 
we could make "git tag" get the default key from there by adding something 
like

	key=$(git-var -l | sed -n '/^user\.key=/ { s/user.key=//;p;q}')

to the script, which again allows us to have a convenient default value in 
the config file without littering us up with more environment variables.

Hmm?

This hasn't gotten a ton of testing, but I did do a test-commit with this, 
and it all seems to work.

---
 Makefile      |    2 +-
 cache.h       |    4 +++
 commit-tree.c |    4 ++-
 config.c      |   10 ++++++++
 environment.c |   75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 ident.c       |   30 +++++++++++------------
 read-cache.c  |    1 -
 sha1_file.c   |   59 ---------------------------------------------
 var.c         |   11 ++++++++
 9 files changed, 118 insertions(+), 78 deletions(-)

diff --git a/Makefile b/Makefile
index 8697d52..ee3a752 100644
--- a/Makefile
+++ b/Makefile
@@ -158,7 +158,7 @@ LIB_OBJS = \
 	object.o pack-check.o patch-delta.o path.o pkt-line.o \
 	quote.o read-cache.o refs.o run-command.o \
 	server-info.o setup.o sha1_file.o sha1_name.o strbuf.o \
-	tag.o tree.o usage.o config.o $(DIFF_OBJS)
+	tag.o tree.o usage.o config.o environment.o $(DIFF_OBJS)
 
 LIBS = $(LIB_FILE)
 LIBS += -lz
diff --git a/cache.h b/cache.h
index 41cc22c..1a7e047 100644
--- a/cache.h
+++ b/cache.h
@@ -382,4 +382,8 @@ extern int git_config(config_fn_t fn);
 extern int git_config_int(const char *, const char *);
 extern int git_config_bool(const char *, const char *);
 
+#define MAX_GITNAME (1000)
+extern char git_default_email[MAX_GITNAME];
+extern char git_default_name[MAX_GITNAME];
+
 #endif /* CACHE_H */
diff --git a/commit-tree.c b/commit-tree.c
index b1ef0b5..030fb70 100644
--- a/commit-tree.c
+++ b/commit-tree.c
@@ -89,6 +89,9 @@ int main(int argc, char **argv)
 	char *buffer;
 	unsigned int size;
 
+	setup_ident();
+	git_config(git_default_config);
+
 	if (argc < 2 || get_sha1_hex(argv[1], tree_sha1) < 0)
 		usage(commit_tree_usage);
 
@@ -104,7 +107,6 @@ int main(int argc, char **argv)
 	}
 	if (!parents)
 		fprintf(stderr, "Committing initial tree %s\n", argv[1]);
-	setup_ident();
 
 	init_buffer(&buffer, &size);
 	add_buffer(&buffer, &size, "tree %s\n", sha1_to_hex(tree_sha1));
diff --git a/config.c b/config.c
index 510456c..cf80358 100644
--- a/config.c
+++ b/config.c
@@ -207,6 +207,16 @@ int git_default_config(const char *var, 
 		return 0;
 	}
 
+	if (!strcmp(var, "user.name")) {
+		strncpy(git_default_name, value, sizeof(git_default_name));
+		return 0;
+	}
+
+	if (!strcmp(var, "user.email")) {
+		strncpy(git_default_email, value, sizeof(git_default_email));
+		return 0;
+	}
+
 	/* Add other config variables here.. */
 	return 0;
 }
diff --git a/environment.c b/environment.c
new file mode 100644
index 0000000..1dc7af5
--- /dev/null
+++ b/environment.c
@@ -0,0 +1,75 @@
+/*
+ * We put all the git config variables in this same object
+ * file, so that programs can link against the config parser
+ * without having to link against all the rest of git.
+ *
+ * In particular, no need to bring in libz etc unless needed,
+ * even if you might want to know where the git directory etc
+ * are.
+ */
+#include "cache.h"
+
+char git_default_email[MAX_GITNAME];
+char git_default_name[MAX_GITNAME];
+int trust_executable_bit = 1;
+
+static char *git_dir, *git_object_dir, *git_index_file, *git_refs_dir,
+	*git_graft_file;
+static void setup_git_env(void)
+{
+	git_dir = getenv(GIT_DIR_ENVIRONMENT);
+	if (!git_dir)
+		git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
+	git_object_dir = getenv(DB_ENVIRONMENT);
+	if (!git_object_dir) {
+		git_object_dir = xmalloc(strlen(git_dir) + 9);
+		sprintf(git_object_dir, "%s/objects", git_dir);
+	}
+	git_refs_dir = xmalloc(strlen(git_dir) + 6);
+	sprintf(git_refs_dir, "%s/refs", git_dir);
+	git_index_file = getenv(INDEX_ENVIRONMENT);
+	if (!git_index_file) {
+		git_index_file = xmalloc(strlen(git_dir) + 7);
+		sprintf(git_index_file, "%s/index", git_dir);
+	}
+	git_graft_file = getenv(GRAFT_ENVIRONMENT);
+	if (!git_graft_file)
+		git_graft_file = strdup(git_path("info/grafts"));
+}
+
+char *get_git_dir(void)
+{
+	if (!git_dir)
+		setup_git_env();
+	return git_dir;
+}
+
+char *get_object_directory(void)
+{
+	if (!git_object_dir)
+		setup_git_env();
+	return git_object_dir;
+}
+
+char *get_refs_directory(void)
+{
+	if (!git_refs_dir)
+		setup_git_env();
+	return git_refs_dir;
+}
+
+char *get_index_file(void)
+{
+	if (!git_index_file)
+		setup_git_env();
+	return git_index_file;
+}
+
+char *get_graft_file(void)
+{
+	if (!git_graft_file)
+		setup_git_env();
+	return git_graft_file;
+}
+
+
diff --git a/ident.c b/ident.c
index 562f5f1..7a9f567 100644
--- a/ident.c
+++ b/ident.c
@@ -11,9 +11,7 @@
 #include <time.h>
 #include <ctype.h>
 
-static char real_email[1000];
-static char real_name[1000];
-static char real_date[50];
+static char git_default_date[50];
 
 static void copy_gecos(struct passwd *w, char *name, int sz)
 {
@@ -58,22 +56,22 @@ int setup_ident(void)
 		die("You don't exist. Go away!");
 
 	/* Get the name ("gecos") */
-	copy_gecos(pw, real_name, sizeof(real_name));
+	copy_gecos(pw, git_default_name, sizeof(git_default_name));
 
 	/* Make up a fake email address (name + '@' + hostname [+ '.' + domainname]) */
 	len = strlen(pw->pw_name);
-	if (len > sizeof(real_email)/2)
+	if (len > sizeof(git_default_email)/2)
 		die("Your sysadmin must hate you!");
-	memcpy(real_email, pw->pw_name, len);
-	real_email[len++] = '@';
-	gethostname(real_email + len, sizeof(real_email) - len);
-	if (!strchr(real_email+len, '.')) {
-		len = strlen(real_email);
-		real_email[len++] = '.';
-		getdomainname(real_email+len, sizeof(real_email)-len);
+	memcpy(git_default_email, pw->pw_name, len);
+	git_default_email[len++] = '@';
+	gethostname(git_default_email + len, sizeof(git_default_email) - len);
+	if (!strchr(git_default_email+len, '.')) {
+		len = strlen(git_default_email);
+		git_default_email[len++] = '.';
+		getdomainname(git_default_email+len, sizeof(git_default_email)-len);
 	}
 	/* And set the default date */
-	datestamp(real_date, sizeof(real_date));
+	datestamp(git_default_date, sizeof(git_default_date));
 	return 0;
 }
 
@@ -159,10 +157,10 @@ char *get_ident(const char *name, const 
 	int i;
 
 	if (!name)
-		name = real_name;
+		name = git_default_name;
 	if (!email)
-		email = real_email;
-	strcpy(date, real_date);
+		email = git_default_email;
+	strcpy(date, git_default_date);
 	if (date_str)
 		parse_date(date_str, date, sizeof(date));
 
diff --git a/read-cache.c b/read-cache.c
index c7f3b26..d2aebdd 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -5,7 +5,6 @@
  */
 #include "cache.h"
 
-int trust_executable_bit = 1;
 struct cache_entry **active_cache = NULL;
 unsigned int active_nr = 0, active_alloc = 0, active_cache_changed = 0;
 
diff --git a/sha1_file.c b/sha1_file.c
index 6e3ea23..f059004 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -48,65 +48,6 @@ int get_sha1_hex(const char *hex, unsign
 	return 0;
 }
 
-static char *git_dir, *git_object_dir, *git_index_file, *git_refs_dir,
-	*git_graft_file;
-static void setup_git_env(void)
-{
-	git_dir = getenv(GIT_DIR_ENVIRONMENT);
-	if (!git_dir)
-		git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
-	git_object_dir = getenv(DB_ENVIRONMENT);
-	if (!git_object_dir) {
-		git_object_dir = xmalloc(strlen(git_dir) + 9);
-		sprintf(git_object_dir, "%s/objects", git_dir);
-	}
-	git_refs_dir = xmalloc(strlen(git_dir) + 6);
-	sprintf(git_refs_dir, "%s/refs", git_dir);
-	git_index_file = getenv(INDEX_ENVIRONMENT);
-	if (!git_index_file) {
-		git_index_file = xmalloc(strlen(git_dir) + 7);
-		sprintf(git_index_file, "%s/index", git_dir);
-	}
-	git_graft_file = getenv(GRAFT_ENVIRONMENT);
-	if (!git_graft_file)
-		git_graft_file = strdup(git_path("info/grafts"));
-}
-
-char *get_git_dir(void)
-{
-	if (!git_dir)
-		setup_git_env();
-	return git_dir;
-}
-
-char *get_object_directory(void)
-{
-	if (!git_object_dir)
-		setup_git_env();
-	return git_object_dir;
-}
-
-char *get_refs_directory(void)
-{
-	if (!git_refs_dir)
-		setup_git_env();
-	return git_refs_dir;
-}
-
-char *get_index_file(void)
-{
-	if (!git_index_file)
-		setup_git_env();
-	return git_index_file;
-}
-
-char *get_graft_file(void)
-{
-	if (!git_graft_file)
-		setup_git_env();
-	return git_graft_file;
-}
-
 int safe_create_leading_directories(char *path)
 {
 	char *pos = path;
diff --git a/var.c b/var.c
index 3f13126..51cf86a 100644
--- a/var.c
+++ b/var.c
@@ -42,6 +42,15 @@ static const char *read_var(const char *
 	return val;
 }
 
+static int show_config(const char *var, const char *value)
+{
+	if (value)
+		printf("%s=%s\n", var, value);
+	else
+		printf("%s\n", var);
+	return git_default_config(var, value);
+}
+
 int main(int argc, char **argv)
 {
 	const char *val;
@@ -52,9 +61,11 @@ int main(int argc, char **argv)
 	val = NULL;
 
 	if (strcmp(argv[1], "-l") == 0) {
+		git_config(show_config);
 		list_vars();
 		return 0;
 	}
+	git_config(git_default_config);
 	val = read_var(argv[1]);
 	if (!val)
 		usage(var_usage);

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2005-10-12  0:55 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-10-12  0:54 Use git config file for committer name and email info Linus Torvalds

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).