X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=config.c;h=70d105567921fe75518d8d5fe3d3df4aad0659cd;hb=b51b8bbf146d17556226bff14f97957e84aa0207;hp=8cbdd178bd9e678774b65cc9ab2da6b169bdc1ca;hpb=fc1905bb9340304fb5980841fca24638028c1c5e;p=git.git diff --git a/config.c b/config.c index 8cbdd178b..70d105567 100644 --- a/config.c +++ b/config.c @@ -6,7 +6,6 @@ * */ #include "cache.h" -#include #define MAXNAME (256) @@ -239,6 +238,12 @@ int git_config_int(const char *name, const char *value) int val = strtol(value, &end, 0); if (!*end) return val; + if (!strcasecmp(end, "k")) + return val * 1024; + if (!strcasecmp(end, "m")) + return val * 1024 * 1024; + if (!strcasecmp(end, "g")) + return val * 1024 * 1024 * 1024; } die("bad config value for '%s' in %s", name, config_file_name); } @@ -264,6 +269,16 @@ int git_default_config(const char *var, const char *value) return 0; } + if (!strcmp(var, "core.symlinks")) { + has_symlinks = git_config_bool(var, value); + return 0; + } + + if (!strcmp(var, "core.bare")) { + is_bare_repository_cfg = git_config_bool(var, value); + return 0; + } + if (!strcmp(var, "core.ignorestat")) { assume_unchanged = git_config_bool(var, value); return 0; @@ -299,6 +314,37 @@ int git_default_config(const char *var, const char *value) return 0; } + if (!strcmp(var, "core.packedgitwindowsize")) { + int pgsz_x2 = getpagesize() * 2; + packed_git_window_size = git_config_int(var, value); + + /* This value must be multiple of (pagesize * 2) */ + packed_git_window_size /= pgsz_x2; + if (packed_git_window_size < 1) + packed_git_window_size = 1; + packed_git_window_size *= pgsz_x2; + return 0; + } + + if (!strcmp(var, "core.packedgitlimit")) { + packed_git_limit = git_config_int(var, value); + return 0; + } + + if (!strcmp(var, "core.deltabasecachelimit")) { + delta_base_cache_limit = git_config_int(var, value); + return 0; + } + + if (!strcmp(var, "core.autocrlf")) { + if (value && !strcasecmp(value, "input")) { + auto_crlf = -1; + return 0; + } + auto_crlf = git_config_bool(var, value); + return 0; + } + if (!strcmp(var, "user.name")) { strlcpy(git_default_name, value, sizeof(git_default_name)); return 0; @@ -310,10 +356,16 @@ int git_default_config(const char *var, const char *value) } if (!strcmp(var, "i18n.commitencoding")) { - strlcpy(git_commit_encoding, value, sizeof(git_commit_encoding)); + git_commit_encoding = xstrdup(value); + return 0; + } + + if (!strcmp(var, "i18n.logoutputencoding")) { + git_log_output_encoding = xstrdup(value); return 0; } + if (!strcmp(var, "pager.color") || !strcmp(var, "color.pager")) { pager_use_color = git_config_bool(var,value); return 0; @@ -350,10 +402,12 @@ int git_config(config_fn_t fn) * $GIT_CONFIG_LOCAL will make it process it in addition to the * global config file, the same way it would the per-repository * config file otherwise. */ - filename = getenv("GIT_CONFIG"); + filename = getenv(CONFIG_ENVIRONMENT); if (!filename) { + if (!access(ETC_GITCONFIG, R_OK)) + ret += git_config_from_file(fn, ETC_GITCONFIG); home = getenv("HOME"); - filename = getenv("GIT_CONFIG_LOCAL"); + filename = getenv(CONFIG_LOCAL_ENVIRONMENT); if (!filename) filename = repo_config = xstrdup(git_path("config")); } @@ -382,7 +436,7 @@ static struct { int do_not_match; regex_t* value_regex; int multi_replace; - off_t offset[MAX_MATCHES]; + size_t offset[MAX_MATCHES]; enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state; int seen; } store; @@ -438,7 +492,15 @@ static int store_aux(const char* key, const char* value) return 0; } -static void store_write_section(int fd, const char* key) +static int write_error() +{ + fprintf(stderr, "Failed to write new configuration file\n"); + + /* Same error code as "failed to rename". */ + return 4; +} + +static int store_write_section(int fd, const char* key) { const char *dot = strchr(key, '.'); int len1 = store.baselen, len2 = -1; @@ -452,44 +514,81 @@ static void store_write_section(int fd, const char* key) } } - write(fd, "[", 1); - write(fd, key, len1); + if (write_in_full(fd, "[", 1) != 1 || + write_in_full(fd, key, len1) != len1) + return 0; if (len2 >= 0) { - write(fd, " \"", 2); + if (write_in_full(fd, " \"", 2) != 2) + return 0; while (--len2 >= 0) { unsigned char c = *++dot; if (c == '"') - write(fd, "\\", 1); - write(fd, &c, 1); + if (write_in_full(fd, "\\", 1) != 1) + return 0; + if (write_in_full(fd, &c, 1) != 1) + return 0; } - write(fd, "\"", 1); + if (write_in_full(fd, "\"", 1) != 1) + return 0; } - write(fd, "]\n", 2); + if (write_in_full(fd, "]\n", 2) != 2) + return 0; + + return 1; } -static void store_write_pair(int fd, const char* key, const char* value) +static int store_write_pair(int fd, const char* key, const char* value) { int i; + int length = strlen(key+store.baselen+1); + int quote = 0; - write(fd, "\t", 1); - write(fd, key+store.baselen+1, - strlen(key+store.baselen+1)); - write(fd, " = ", 3); + /* Check to see if the value needs to be quoted. */ + if (value[0] == ' ') + quote = 1; + for (i = 0; value[i]; i++) + if (value[i] == ';' || value[i] == '#') + quote = 1; + if (value[i-1] == ' ') + quote = 1; + + if (write_in_full(fd, "\t", 1) != 1 || + write_in_full(fd, key+store.baselen+1, length) != length || + write_in_full(fd, " = ", 3) != 3) + return 0; + if (quote && write_in_full(fd, "\"", 1) != 1) + return 0; for (i = 0; value[i]; i++) switch (value[i]) { - case '\n': write(fd, "\\n", 2); break; - case '\t': write(fd, "\\t", 2); break; - case '"': case '\\': write(fd, "\\", 1); - default: write(fd, value+i, 1); - } - write(fd, "\n", 1); + case '\n': + if (write_in_full(fd, "\\n", 2) != 2) + return 0; + break; + case '\t': + if (write_in_full(fd, "\\t", 2) != 2) + return 0; + break; + case '"': + case '\\': + if (write_in_full(fd, "\\", 1) != 1) + return 0; + default: + if (write_in_full(fd, value+i, 1) != 1) + return 0; + break; + } + if (quote && write_in_full(fd, "\"", 1) != 1) + return 0; + if (write_in_full(fd, "\n", 1) != 1) + return 0; + return 1; } -static int find_beginning_of_line(const char* contents, int size, - int offset_, int* found_bracket) +static ssize_t find_beginning_of_line(const char* contents, size_t size, + size_t offset_, int* found_bracket) { - int equal_offset = size, bracket_offset = size; - int offset; + size_t equal_offset = size, bracket_offset = size; + ssize_t offset; for (offset = offset_-2; offset > 0 && contents[offset] != '\n'; offset--) @@ -544,9 +643,9 @@ int git_config_set_multivar(const char* key, const char* value, char* lock_file; const char* last_dot = strrchr(key, '.'); - config_filename = getenv("GIT_CONFIG"); + config_filename = getenv(CONFIG_ENVIRONMENT); if (!config_filename) { - config_filename = getenv("GIT_CONFIG_LOCAL"); + config_filename = getenv(CONFIG_LOCAL_ENVIRONMENT); if (!config_filename) config_filename = git_path("config"); } @@ -585,6 +684,11 @@ int git_config_set_multivar(const char* key, const char* value, goto out_free; } c = tolower(c); + } else if (c == '\n') { + fprintf(stderr, "invalid key (newline): %s\n", key); + free(store.key); + ret = 1; + goto out_free; } store.key[i] = c; } @@ -622,12 +726,14 @@ int git_config_set_multivar(const char* key, const char* value, } store.key = (char*)key; - store_write_section(fd, key); - store_write_pair(fd, key, value); - } else{ + if (!store_write_section(fd, key) || + !store_write_pair(fd, key, value)) + goto write_err_out; + } else { struct stat st; char* contents; - int i, copy_begin, copy_end, new_line = 0; + size_t contents_sz, copy_begin, copy_end; + int i, new_line = 0; if (value_regex == NULL) store.value_regex = NULL; @@ -684,7 +790,8 @@ int git_config_set_multivar(const char* key, const char* value, } fstat(in_fd, &st); - contents = mmap(NULL, st.st_size, PROT_READ, + contents_sz = xsize_t(st.st_size); + contents = xmmap(NULL, contents_sz, PROT_READ, MAP_PRIVATE, in_fd, 0); close(in_fd); @@ -693,37 +800,45 @@ int git_config_set_multivar(const char* key, const char* value, for (i = 0, copy_begin = 0; i < store.seen; i++) { if (store.offset[i] == 0) { - store.offset[i] = copy_end = st.st_size; + store.offset[i] = copy_end = contents_sz; } else if (store.state != KEY_SEEN) { copy_end = store.offset[i]; } else copy_end = find_beginning_of_line( - contents, st.st_size, + contents, contents_sz, store.offset[i]-2, &new_line); /* write the first part of the config */ if (copy_end > copy_begin) { - write(fd, contents + copy_begin, - copy_end - copy_begin); - if (new_line) - write(fd, "\n", 1); + if (write_in_full(fd, contents + copy_begin, + copy_end - copy_begin) < + copy_end - copy_begin) + goto write_err_out; + if (new_line && + write_in_full(fd, "\n", 1) != 1) + goto write_err_out; } copy_begin = store.offset[i]; } /* write the pair (value == NULL means unset) */ if (value != NULL) { - if (store.state == START) - store_write_section(fd, key); - store_write_pair(fd, key, value); + if (store.state == START) { + if (!store_write_section(fd, key)) + goto write_err_out; + } + if (!store_write_pair(fd, key, value)) + goto write_err_out; } /* write the rest of the config */ - if (copy_begin < st.st_size) - write(fd, contents + copy_begin, - st.st_size - copy_begin); + if (copy_begin < contents_sz) + if (write_in_full(fd, contents + copy_begin, + contents_sz - copy_begin) < + contents_sz - copy_begin) + goto write_err_out; - munmap(contents, st.st_size); + munmap(contents, contents_sz); unlink(config_filename); } @@ -744,19 +859,52 @@ out_free: free(lock_file); } return ret; + +write_err_out: + ret = write_error(); + goto out_free; + +} + +static int section_name_match (const char *buf, const char *name) +{ + int i = 0, j = 0, dot = 0; + for (; buf[i] && buf[i] != ']'; i++) { + if (!dot && isspace(buf[i])) { + dot = 1; + if (name[j++] != '.') + break; + for (i++; isspace(buf[i]); i++) + ; /* do nothing */ + if (buf[i] != '"') + break; + continue; + } + if (buf[i] == '\\' && dot) + i++; + else if (buf[i] == '"' && dot) { + for (i++; isspace(buf[i]); i++) + ; /* do_nothing */ + break; + } + if (buf[i] != name[j++]) + break; + } + return (buf[i] == ']' && name[j] == 0); } +/* if new_name == NULL, the section is removed instead */ int git_config_rename_section(const char *old_name, const char *new_name) { - int ret = 0; + int ret = 0, remove = 0; char *config_filename; struct lock_file *lock = xcalloc(sizeof(struct lock_file), 1); int out_fd; char buf[1024]; - config_filename = getenv("GIT_CONFIG"); + config_filename = getenv(CONFIG_ENVIRONMENT); if (!config_filename) { - config_filename = getenv("GIT_CONFIG_LOCAL"); + config_filename = getenv(CONFIG_LOCAL_ENVIRONMENT); if (!config_filename) config_filename = git_path("config"); } @@ -768,51 +916,44 @@ int git_config_rename_section(const char *old_name, const char *new_name) } if (!(config_file = fopen(config_filename, "rb"))) { - ret = error("Could not open config file!"); - goto out; + /* no config file means nothing to rename, no error */ + goto unlock_and_out; } while (fgets(buf, sizeof(buf), config_file)) { int i; + int length; for (i = 0; buf[i] && isspace(buf[i]); i++) ; /* do nothing */ if (buf[i] == '[') { /* it's a section */ - int j = 0, dot = 0; - for (i++; buf[i] && buf[i] != ']'; i++) { - if (!dot && isspace(buf[i])) { - dot = 1; - if (old_name[j++] != '.') - break; - for (i++; isspace(buf[i]); i++) - ; /* do nothing */ - if (buf[i] != '"') - break; + if (section_name_match (&buf[i+1], old_name)) { + ret++; + if (new_name == NULL) { + remove = 1; continue; } - if (buf[i] == '\\' && dot) - i++; - else if (buf[i] == '"' && dot) { - for (i++; isspace(buf[i]); i++) - ; /* do_nothing */ - break; - } - if (buf[i] != old_name[j++]) - break; - } - if (buf[i] == ']') { - /* old_name matches */ - ret++; store.baselen = strlen(new_name); - store_write_section(out_fd, new_name); + if (!store_write_section(out_fd, new_name)) { + ret = write_error(); + goto out; + } continue; } + remove = 0; + } + if (remove) + continue; + length = strlen(buf); + if (write_in_full(out_fd, buf, length) != length) { + ret = write_error(); + goto out; } - write(out_fd, buf, strlen(buf)); } fclose(config_file); + unlock_and_out: if (close(out_fd) || commit_lock_file(lock) < 0) - ret = error("Cannot commit config file!"); + ret = error("Cannot commit config file!"); out: free(config_filename); return ret;