@matheusbsbs / matheus-bagliano / commits / e1b10391ea
Fork 0

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>

Linus Torvalds committed Oct 11, 2005 at 18:47 UTC e1b10391eabdaaa4c89c53099dd96d5f9d978719
9 files changed +118 -78
Makefile
+1 -1
@@ -158,7 +158,7 @@ LIB_OBJS = \
158 object.o pack-check.o patch-delta.o path.o pkt-line.o \
159 quote.o read-cache.o refs.o run-command.o \
160 server-info.o setup.o sha1_file.o sha1_name.o strbuf.o \
161 - tag.o tree.o usage.o config.o $(DIFF_OBJS)
161 + tag.o tree.o usage.o config.o environment.o $(DIFF_OBJS)
162
163 LIBS = $(LIB_FILE)
164 LIBS += -lz
cache.h
+4
@@ -382,4 +382,8 @@ extern int git_config(config_fn_t fn);
382 extern int git_config_int(const char *, const char *);
383 extern int git_config_bool(const char *, const char *);
384
385 +#define MAX_GITNAME (1000)
386 +extern char git_default_email[MAX_GITNAME];
387 +extern char git_default_name[MAX_GITNAME];
388 +
389 #endif /* CACHE_H */
commit-tree.c
+3 -1
@@ -89,6 +89,9 @@ int main(int argc, char **argv)
89 char *buffer;
90 unsigned int size;
91
92 + setup_ident();
93 + git_config(git_default_config);
94 +
95 if (argc < 2 || get_sha1_hex(argv[1], tree_sha1) < 0)
96 usage(commit_tree_usage);
97
@@ -104,7 +107,6 @@ int main(int argc, char **argv)
107 }
108 if (!parents)
109 fprintf(stderr, "Committing initial tree %s\n", argv[1]);
107 - setup_ident();
110
111 init_buffer(&buffer, &size);
112 add_buffer(&buffer, &size, "tree %s\n", sha1_to_hex(tree_sha1));
config.c
+10
@@ -207,6 +207,16 @@ int git_default_config(const char *var, const char *value)
207 return 0;
208 }
209
210 + if (!strcmp(var, "user.name")) {
211 + strncpy(git_default_name, value, sizeof(git_default_name));
212 + return 0;
213 + }
214 +
215 + if (!strcmp(var, "user.email")) {
216 + strncpy(git_default_email, value, sizeof(git_default_email));
217 + return 0;
218 + }
219 +
220 /* Add other config variables here.. */
221 return 0;
222 }
environment.c new
+75
@@ -0,0 +1,75 @@
1 +/*
2 + * We put all the git config variables in this same object
3 + * file, so that programs can link against the config parser
4 + * without having to link against all the rest of git.
5 + *
6 + * In particular, no need to bring in libz etc unless needed,
7 + * even if you might want to know where the git directory etc
8 + * are.
9 + */
10 +#include "cache.h"
11 +
12 +char git_default_email[MAX_GITNAME];
13 +char git_default_name[MAX_GITNAME];
14 +int trust_executable_bit = 1;
15 +
16 +static char *git_dir, *git_object_dir, *git_index_file, *git_refs_dir,
17 + *git_graft_file;
18 +static void setup_git_env(void)
19 +{
20 + git_dir = getenv(GIT_DIR_ENVIRONMENT);
21 + if (!git_dir)
22 + git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
23 + git_object_dir = getenv(DB_ENVIRONMENT);
24 + if (!git_object_dir) {
25 + git_object_dir = xmalloc(strlen(git_dir) + 9);
26 + sprintf(git_object_dir, "%s/objects", git_dir);
27 + }
28 + git_refs_dir = xmalloc(strlen(git_dir) + 6);
29 + sprintf(git_refs_dir, "%s/refs", git_dir);
30 + git_index_file = getenv(INDEX_ENVIRONMENT);
31 + if (!git_index_file) {
32 + git_index_file = xmalloc(strlen(git_dir) + 7);
33 + sprintf(git_index_file, "%s/index", git_dir);
34 + }
35 + git_graft_file = getenv(GRAFT_ENVIRONMENT);
36 + if (!git_graft_file)
37 + git_graft_file = strdup(git_path("info/grafts"));
38 +}
39 +
40 +char *get_git_dir(void)
41 +{
42 + if (!git_dir)
43 + setup_git_env();
44 + return git_dir;
45 +}
46 +
47 +char *get_object_directory(void)
48 +{
49 + if (!git_object_dir)
50 + setup_git_env();
51 + return git_object_dir;
52 +}
53 +
54 +char *get_refs_directory(void)
55 +{
56 + if (!git_refs_dir)
57 + setup_git_env();
58 + return git_refs_dir;
59 +}
60 +
61 +char *get_index_file(void)
62 +{
63 + if (!git_index_file)
64 + setup_git_env();
65 + return git_index_file;
66 +}
67 +
68 +char *get_graft_file(void)
69 +{
70 + if (!git_graft_file)
71 + setup_git_env();
72 + return git_graft_file;
73 +}
74 +
75 +
ident.c
+14 -16
@@ -11,9 +11,7 @@
11 #include <time.h>
12 #include <ctype.h>
13
14 -static char real_email[1000];
15 -static char real_name[1000];
16 -static char real_date[50];
14 +static char git_default_date[50];
15
16 static void copy_gecos(struct passwd *w, char *name, int sz)
17 {
@@ -58,22 +56,22 @@ int setup_ident(void)
56 die("You don't exist. Go away!");
57
58 /* Get the name ("gecos") */
61 - copy_gecos(pw, real_name, sizeof(real_name));
59 + copy_gecos(pw, git_default_name, sizeof(git_default_name));
60
61 /* Make up a fake email address (name + '@' + hostname [+ '.' + domainname]) */
62 len = strlen(pw->pw_name);
65 - if (len > sizeof(real_email)/2)
63 + if (len > sizeof(git_default_email)/2)
64 die("Your sysadmin must hate you!");
67 - memcpy(real_email, pw->pw_name, len);
68 - real_email[len++] = '@';
69 - gethostname(real_email + len, sizeof(real_email) - len);
70 - if (!strchr(real_email+len, '.')) {
71 - len = strlen(real_email);
72 - real_email[len++] = '.';
73 - getdomainname(real_email+len, sizeof(real_email)-len);
65 + memcpy(git_default_email, pw->pw_name, len);
66 + git_default_email[len++] = '@';
67 + gethostname(git_default_email + len, sizeof(git_default_email) - len);
68 + if (!strchr(git_default_email+len, '.')) {
69 + len = strlen(git_default_email);
70 + git_default_email[len++] = '.';
71 + getdomainname(git_default_email+len, sizeof(git_default_email)-len);
72 }
73 /* And set the default date */
76 - datestamp(real_date, sizeof(real_date));
74 + datestamp(git_default_date, sizeof(git_default_date));
75 return 0;
76 }
77
@@ -159,10 +157,10 @@ char *get_ident(const char *name, const char *email, const char *date_str)
157 int i;
158
159 if (!name)
162 - name = real_name;
160 + name = git_default_name;
161 if (!email)
164 - email = real_email;
165 - strcpy(date, real_date);
162 + email = git_default_email;
163 + strcpy(date, git_default_date);
164 if (date_str)
165 parse_date(date_str, date, sizeof(date));
166
read-cache.c
-1
@@ -5,7 +5,6 @@
5 */
6 #include "cache.h"
7
8 -int trust_executable_bit = 1;
8 struct cache_entry **active_cache = NULL;
9 unsigned int active_nr = 0, active_alloc = 0, active_cache_changed = 0;
10
sha1_file.c
-59
@@ -48,65 +48,6 @@ int get_sha1_hex(const char *hex, unsigned char *sha1)
48 return 0;
49 }
50
51 -static char *git_dir, *git_object_dir, *git_index_file, *git_refs_dir,
52 - *git_graft_file;
53 -static void setup_git_env(void)
54 -{
55 - git_dir = getenv(GIT_DIR_ENVIRONMENT);
56 - if (!git_dir)
57 - git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
58 - git_object_dir = getenv(DB_ENVIRONMENT);
59 - if (!git_object_dir) {
60 - git_object_dir = xmalloc(strlen(git_dir) + 9);
61 - sprintf(git_object_dir, "%s/objects", git_dir);
62 - }
63 - git_refs_dir = xmalloc(strlen(git_dir) + 6);
64 - sprintf(git_refs_dir, "%s/refs", git_dir);
65 - git_index_file = getenv(INDEX_ENVIRONMENT);
66 - if (!git_index_file) {
67 - git_index_file = xmalloc(strlen(git_dir) + 7);
68 - sprintf(git_index_file, "%s/index", git_dir);
69 - }
70 - git_graft_file = getenv(GRAFT_ENVIRONMENT);
71 - if (!git_graft_file)
72 - git_graft_file = strdup(git_path("info/grafts"));
73 -}
74 -
75 -char *get_git_dir(void)
76 -{
77 - if (!git_dir)
78 - setup_git_env();
79 - return git_dir;
80 -}
81 -
82 -char *get_object_directory(void)
83 -{
84 - if (!git_object_dir)
85 - setup_git_env();
86 - return git_object_dir;
87 -}
88 -
89 -char *get_refs_directory(void)
90 -{
91 - if (!git_refs_dir)
92 - setup_git_env();
93 - return git_refs_dir;
94 -}
95 -
96 -char *get_index_file(void)
97 -{
98 - if (!git_index_file)
99 - setup_git_env();
100 - return git_index_file;
101 -}
102 -
103 -char *get_graft_file(void)
104 -{
105 - if (!git_graft_file)
106 - setup_git_env();
107 - return git_graft_file;
108 -}
109 -
51 int safe_create_leading_directories(char *path)
52 {
53 char *pos = path;
var.c
+11
@@ -42,6 +42,15 @@ static const char *read_var(const char *var)
42 return val;
43 }
44
45 +static int show_config(const char *var, const char *value)
46 +{
47 + if (value)
48 + printf("%s=%s\n", var, value);
49 + else
50 + printf("%s\n", var);
51 + return git_default_config(var, value);
52 +}
53 +
54 int main(int argc, char **argv)
55 {
56 const char *val;
@@ -52,9 +61,11 @@ int main(int argc, char **argv)
61 val = NULL;
62
63 if (strcmp(argv[1], "-l") == 0) {
64 + git_config(show_config);
65 list_vars();
66 return 0;
67 }
68 + git_config(git_default_config);
69 val = read_var(argv[1]);
70 if (!val)
71 usage(var_usage);