From 17712991a59824a8d22d5115c0c154d3122fc17b Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Mon, 10 Oct 2005 16:31:08 -0700 Subject: Add ".git/config" file parser This is a first cut at a very simple parser for a git config file. The format of the file is a simple ini-file like thing, with simple variable/value pairs. You can (and should) make the variables have a simple single-level scope, ie a valid file looks something like this: # # This is the config file, and # a '#' or ';' character indicates # a comment # ; core variables [core] ; Don't trust file modes filemode = false ; Our diff algorithm [diff] external = "/usr/local/bin/gnu-diff -u" renames = true which parses into three variables: "core.filemode" is associated with the string "false", and "diff.external" gets the appropriate quoted value. Right now we only react to one variable: "core.filemode" is a boolean that decides if we should care about the 0100 (user-execute) bit of the stat information. Even that is just a parsing demonstration - this doesn't actually implement that st_mode compare logic itself. Different programs can react to different config options, although they should always fall back to calling "git_default_config()" on any config option name that they don't recognize. Signed-off-by: Linus Torvalds Signed-off-by: Junio C Hamano --- read-cache.c | 1 + 1 file changed, 1 insertion(+) (limited to 'read-cache.c') diff --git a/read-cache.c b/read-cache.c index d2aebdd6bc..c7f3b26862 100644 --- a/read-cache.c +++ b/read-cache.c @@ -5,6 +5,7 @@ */ #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; -- cgit v1.3 From 3e09cdfd114651fc61656dbd45d5ec3d9352cb2b Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Tue, 11 Oct 2005 18:45:33 -0700 Subject: Use core.filemode. With "[core] filemode = false", you can tell git to ignore differences in the working tree file only in executable bit. * "git-update-index --refresh" does not say "needs update" if index entry and working tree file differs only in executable bit. * "git-update-index" on an existing path takes executable bit from the existing index entry, if the path and index entry are both regular files. * "git-diff-files" and "git-diff-index" without --cached flag pretend the path on the filesystem has the same executable bit as the existing index entry, if the path and index entry are both regular files. If you are on a filesystem with unreliable mode bits, you may need to force the executable bit after registering the path in the index. * "git-update-index --chmod=+x foo" flips the executable bit of the index file entry for path "foo" on. Use "--chmod=-x" to flip it off. Note that --chmod only works in index file and does not look at nor update the working tree. So if you are on a filesystem and do not have working executable bit, you would do: 1. set the appropriate .git/config option; 2. "git-update-index --add new-file.c" 3. "git-ls-files --stage new-file.c" to see if it has the desired mode bits. If not, e.g. to drop executable bit picked up from the filesystem, say "git-update-index --chmod=-x new-file.c". Signed-off-by: Junio C Hamano --- diff-files.c | 10 ++++++++-- diff-index.c | 11 +++++++++-- read-cache.c | 12 ++++++++---- update-index.c | 46 +++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 70 insertions(+), 9 deletions(-) (limited to 'read-cache.c') diff --git a/diff-files.c b/diff-files.c index 96d2c7f19f..8a8f9b6dc7 100644 --- a/diff-files.c +++ b/diff-files.c @@ -81,7 +81,7 @@ int main(int argc, const char **argv) for (i = 0; i < entries; i++) { struct stat st; - unsigned int oldmode; + unsigned int oldmode, newmode; struct cache_entry *ce = active_cache[i]; int changed; @@ -111,7 +111,13 @@ int main(int argc, const char **argv) if (!changed && !diff_options.find_copies_harder) continue; oldmode = ntohl(ce->ce_mode); - show_modified(oldmode, DIFF_FILE_CANON_MODE(st.st_mode), + + newmode = DIFF_FILE_CANON_MODE(st.st_mode); + if (!trust_executable_bit && + S_ISREG(newmode) && S_ISREG(oldmode) && + ((newmode ^ oldmode) == 0111)) + newmode = oldmode; + show_modified(oldmode, newmode, ce->sha1, (changed ? null_sha1 : ce->sha1), ce->name); } diff --git a/diff-index.c b/diff-index.c index 62b36cc8ee..c9a9f4c74d 100644 --- a/diff-index.c +++ b/diff-index.c @@ -15,7 +15,7 @@ static void show_file(const char *prefix, } static int get_stat_data(struct cache_entry *ce, - unsigned char **sha1p, unsigned int *modep) + unsigned char ** sha1p, unsigned int *modep) { unsigned char *sha1 = ce->sha1; unsigned int mode = ce->ce_mode; @@ -35,6 +35,10 @@ static int get_stat_data(struct cache_entry *ce, changed = ce_match_stat(ce, &st); if (changed) { mode = create_ce_mode(st.st_mode); + if (!trust_executable_bit && + S_ISREG(mode) && S_ISREG(ce->ce_mode) && + ((mode ^ ce->ce_mode) == 0111)) + mode = ce->ce_mode; sha1 = no_sha1; } } @@ -49,7 +53,9 @@ static void show_new_file(struct cache_entry *new) unsigned char *sha1; unsigned int mode; - /* New file in the index: it might actually be different in the working copy */ + /* New file in the index: it might actually be different in + * the working copy. + */ if (get_stat_data(new, &sha1, &mode) < 0) return; @@ -174,6 +180,7 @@ int main(int argc, const char **argv) int allow_options = 1; int i; + git_config(git_default_config); diff_setup(&diff_options); for (i = 1; i < argc; i++) { const char *arg = argv[i]; diff --git a/read-cache.c b/read-cache.c index c7f3b26862..4ed369acf8 100644 --- a/read-cache.c +++ b/read-cache.c @@ -36,8 +36,11 @@ int ce_match_stat(struct cache_entry *ce, struct stat *st) switch (ntohl(ce->ce_mode) & S_IFMT) { case S_IFREG: changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED : 0; - /* We consider only the owner x bit to be relevant for "mode changes" */ - if (0100 & (ntohl(ce->ce_mode) ^ st->st_mode)) + /* We consider only the owner x bit to be relevant for + * "mode changes" + */ + if (trust_executable_bit && + (0100 & (ntohl(ce->ce_mode) ^ st->st_mode))) changed |= MODE_CHANGED; break; case S_IFLNK: @@ -393,7 +396,7 @@ int add_cache_entry(struct cache_entry *ce, int option) int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK; pos = cache_name_pos(ce->name, ntohs(ce->ce_flags)); - /* existing match? Just replace it */ + /* existing match? Just replace it. */ if (pos >= 0) { active_cache_changed = 1; active_cache[pos] = ce; @@ -416,7 +419,8 @@ int add_cache_entry(struct cache_entry *ce, int option) if (!ok_to_add) return -1; - if (!skip_df_check && check_file_directory_conflict(ce, pos, ok_to_replace)) { + if (!skip_df_check && + check_file_directory_conflict(ce, pos, ok_to_replace)) { if (!ok_to_replace) return -1; pos = cache_name_pos(ce->name, ntohs(ce->ce_flags)); diff --git a/update-index.c b/update-index.c index 01b4088ad6..1eeb45dbb1 100644 --- a/update-index.c +++ b/update-index.c @@ -67,13 +67,23 @@ static int add_file_to_cache(const char *path) return error("lstat(\"%s\"): %s", path, strerror(errno)); } + namelen = strlen(path); size = cache_entry_size(namelen); ce = xmalloc(size); memset(ce, 0, size); memcpy(ce->name, path, namelen); fill_stat_cache_info(ce, &st); + ce->ce_mode = create_ce_mode(st.st_mode); + if (!trust_executable_bit) { + /* If there is an existing entry, pick the mode bits + * from it. + */ + int pos = cache_name_pos(path, namelen); + if (0 <= pos) + ce->ce_mode = active_cache[pos]->ce_mode; + } ce->ce_flags = htons(namelen); if (index_path(ce->sha1, path, &st, !info_only)) @@ -253,8 +263,32 @@ static int add_cacheinfo(const char *arg1, const char *arg2, const char *arg3) return add_cache_entry(ce, option); } -static struct cache_file cache_file; +static int chmod_path(int flip, const char *path) +{ + int pos; + struct cache_entry *ce; + unsigned int mode; + pos = cache_name_pos(path, strlen(path)); + if (pos < 0) + return -1; + ce = active_cache[pos]; + mode = ntohl(ce->ce_mode); + if (!S_ISREG(mode)) + return -1; + switch (flip) { + case '+': + ce->ce_mode |= htonl(0111); break; + case '-': + ce->ce_mode &= htonl(~0111); break; + default: + return -1; + } + active_cache_changed = 1; + return 0; +} + +static struct cache_file cache_file; static void update_one(const char *path, const char *prefix, int prefix_length) { @@ -328,6 +362,8 @@ int main(int argc, const char **argv) const char *prefix = setup_git_directory(); int prefix_length = prefix ? strlen(prefix) : 0; + git_config(git_default_config); + newfd = hold_index_file_for_update(&cache_file, get_index_file()); if (newfd < 0) die("unable to create new cachefile"); @@ -376,6 +412,14 @@ int main(int argc, const char **argv) i += 3; continue; } + if (!strcmp(path, "--chmod=-x") || + !strcmp(path, "--chmod=+x")) { + if (argc <= i+1) + die("git-update-index: %s ", path); + if (chmod_path(path[8], argv[++i])) + die("git-update-index: %s cannot chmod %s", path, argv[i]); + continue; + } if (!strcmp(path, "--info-only")) { info_only = 1; continue; -- cgit v1.3 From e1b10391eabdaaa4c89c53099dd96d5f9d978719 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Tue, 11 Oct 2005 18:47:34 -0700 Subject: 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 Signed-off-by: Junio C Hamano --- 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(-) create mode 100644 environment.c (limited to 'read-cache.c') diff --git a/Makefile b/Makefile index d345c5d116..5e7d0555ea 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 41cc22c1a1..1a7e047d76 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 b1ef0b590a..030fb704e5 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 510456ceb5..cf803580c9 100644 --- a/config.c +++ b/config.c @@ -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; } diff --git a/environment.c b/environment.c new file mode 100644 index 0000000000..1dc7af56cf --- /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 562f5f1816..7a9f5672eb 100644 --- a/ident.c +++ b/ident.c @@ -11,9 +11,7 @@ #include #include -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)); diff --git a/read-cache.c b/read-cache.c index 4ed369acf8..6932736203 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 6e3ea232ee..f059004909 100644 --- a/sha1_file.c +++ b/sha1_file.c @@ -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; diff --git a/var.c b/var.c index 3f13126cb8..51cf86a584 100644 --- a/var.c +++ b/var.c @@ -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); -- cgit v1.3