Use git config file for committer name and email info

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>
Signed-off-by: Junio C Hamano <junkio@cox.net>
This commit is contained in:
Linus Torvalds 2005-10-11 18:47:34 -07:00 committed by Junio C Hamano
parent ec2d15118b
commit e1b10391ea
9 changed files with 118 additions and 78 deletions

View File

@ -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

View File

@ -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 */

View File

@ -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));

View File

@ -207,6 +207,16 @@ int git_default_config(const char *var, const char *value)
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;
}

75
environment.c Normal file
View File

@ -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;
}

30
ident.c
View File

@ -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 char *email, const char *date_str)
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));

View File

@ -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;

View File

@ -48,65 +48,6 @@ int get_sha1_hex(const char *hex, unsigned char *sha1)
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;

11
var.c
View File

@ -42,6 +42,15 @@ static const char *read_var(const char *var)
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);