+ config_file_name = NULL;
+ }
+ return ret;
+}
+
+int git_config(config_fn_t fn)
+{
+ return git_config_from_file(fn, git_path("config"));
+}
+
+/*
+ * Find all the stuff for git_config_set() below.
+ */
+
+#define MAX_MATCHES 512
+
+static struct {
+ int baselen;
+ char* key;
+ int do_not_match;
+ regex_t* value_regex;
+ int multi_replace;
+ off_t offset[MAX_MATCHES];
+ enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state;
+ int seen;
+} store;
+
+static int matches(const char* key, const char* value)
+{
+ return !strcmp(key, store.key) &&
+ (store.value_regex == NULL ||
+ (store.do_not_match ^
+ !regexec(store.value_regex, value, 0, NULL, 0)));
+}
+
+static int store_aux(const char* key, const char* value)
+{
+ switch (store.state) {
+ case KEY_SEEN:
+ if (matches(key, value)) {
+ if (store.seen == 1 && store.multi_replace == 0) {
+ fprintf(stderr,
+ "Warning: %s has multiple values\n",
+ key);
+ } else if (store.seen >= MAX_MATCHES) {
+ fprintf(stderr, "Too many matches\n");
+ return 1;
+ }
+
+ store.offset[store.seen] = ftell(config_file);
+ store.seen++;
+ }
+ break;
+ case SECTION_SEEN:
+ if (strncmp(key, store.key, store.baselen+1)) {
+ store.state = SECTION_END_SEEN;
+ break;
+ } else
+ /* do not increment matches: this is no match */
+ store.offset[store.seen] = ftell(config_file);
+ /* fallthru */
+ case SECTION_END_SEEN:
+ case START:
+ if (matches(key, value)) {
+ store.offset[store.seen] = ftell(config_file);
+ store.state = KEY_SEEN;
+ store.seen++;
+ } else if(!strncmp(key, store.key, store.baselen))
+ store.state = SECTION_SEEN;
+ }
+ return 0;
+}
+
+static void store_write_section(int fd, const char* key)
+{
+ write(fd, "[", 1);
+ write(fd, key, store.baselen);
+ write(fd, "]\n", 2);
+}
+
+static void store_write_pair(int fd, const char* key, const char* value)
+{
+ int i;
+
+ write(fd, "\t", 1);
+ write(fd, key+store.baselen+1,
+ strlen(key+store.baselen+1));
+ write(fd, " = ", 3);
+ 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);
+}
+
+static int find_beginning_of_line(const char* contents, int size,
+ int offset_, int* found_bracket)
+{
+ int equal_offset = size, bracket_offset = size;
+ int offset;
+
+ for (offset = offset_-2; offset > 0
+ && contents[offset] != '\n'; offset--)
+ switch (contents[offset]) {
+ case '=': equal_offset = offset; break;
+ case ']': bracket_offset = offset; break;
+ }
+ if (bracket_offset < equal_offset) {
+ *found_bracket = 1;
+ offset = bracket_offset+1;
+ } else
+ offset++;
+
+ return offset;
+}
+
+int git_config_set(const char* key, const char* value)
+{
+ return git_config_set_multivar(key, value, NULL, 0);
+}
+
+/*
+ * If value==NULL, unset in (remove from) config,
+ * if value_regex!=NULL, disregard key/value pairs where value does not match.
+ * if multi_replace==0, nothing, or only one matching key/value is replaced,
+ * else all matching key/values (regardless how many) are removed,
+ * before the new pair is written.
+ *
+ * Returns 0 on success.
+ *
+ * This function does this:
+ *
+ * - it locks the config file by creating ".git/config.lock"
+ *
+ * - it then parses the config using store_aux() as validator to find
+ * the position on the key/value pair to replace. If it is to be unset,
+ * it must be found exactly once.
+ *
+ * - the config file is mmap()ed and the part before the match (if any) is
+ * written to the lock file, then the changed part and the rest.
+ *
+ * - the config file is removed and the lock file rename()d to it.
+ *
+ */
+int git_config_set_multivar(const char* key, const char* value,
+ const char* value_regex, int multi_replace)
+{
+ int i;
+ int fd, in_fd;
+ int ret;
+ char* config_filename = strdup(git_path("config"));
+ char* lock_file = strdup(git_path("config.lock"));
+ const char* last_dot = strrchr(key, '.');
+
+ /*
+ * Since "key" actually contains the section name and the real
+ * key name separated by a dot, we have to know where the dot is.
+ */
+
+ if (last_dot == NULL) {
+ fprintf(stderr, "key does not contain a section: %s\n", key);
+ ret = 2;
+ goto out_free;