blob: 56e99fc0f4750174299303b27237737e09549933 [file] [log] [blame]
Johannes Schindelin10bea152005-11-17 22:32:36 +01001/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 * Copyright (C) Johannes Schindelin, 2005
6 *
7 */
Linus Torvalds17712992005-10-10 16:31:08 -07008#include "cache.h"
9
10#define MAXNAME (256)
11
12static FILE *config_file;
Junio C Hamano0dccc7d2005-11-28 01:46:15 -080013static const char *config_file_name;
Linus Torvalds17712992005-10-10 16:31:08 -070014static int config_linenr;
Dana How960ccca2007-05-09 13:56:50 -070015static int zlib_compression_seen;
16
Linus Torvalds17712992005-10-10 16:31:08 -070017static int get_next_char(void)
18{
19 int c;
20 FILE *f;
21
22 c = '\n';
23 if ((f = config_file) != NULL) {
24 c = fgetc(f);
Junio C Hamanodb2c0752005-11-02 13:02:57 -080025 if (c == '\r') {
26 /* DOS like systems */
27 c = fgetc(f);
28 if (c != '\n') {
29 ungetc(c, f);
30 c = '\r';
31 }
32 }
Linus Torvalds17712992005-10-10 16:31:08 -070033 if (c == '\n')
34 config_linenr++;
35 if (c == EOF) {
36 config_file = NULL;
37 c = '\n';
38 }
39 }
40 return c;
41}
42
43static char *parse_value(void)
44{
45 static char value[1024];
46 int quote = 0, comment = 0, len = 0, space = 0;
47
48 for (;;) {
49 int c = get_next_char();
50 if (len >= sizeof(value))
51 return NULL;
52 if (c == '\n') {
53 if (quote)
54 return NULL;
55 value[len] = 0;
56 return value;
57 }
58 if (comment)
59 continue;
60 if (isspace(c) && !quote) {
61 space = 1;
62 continue;
63 }
Johannes Schindelin7ebdba62006-05-02 16:58:37 +020064 if (!quote) {
65 if (c == ';' || c == '#') {
66 comment = 1;
67 continue;
68 }
69 }
Linus Torvalds17712992005-10-10 16:31:08 -070070 if (space) {
71 if (len)
72 value[len++] = ' ';
73 space = 0;
74 }
75 if (c == '\\') {
76 c = get_next_char();
77 switch (c) {
78 case '\n':
79 continue;
80 case 't':
81 c = '\t';
82 break;
83 case 'b':
84 c = '\b';
85 break;
86 case 'n':
87 c = '\n';
88 break;
Linus Torvalds5cbb4012005-10-11 15:24:11 -070089 /* Some characters escape as themselves */
90 case '\\': case '"':
91 break;
92 /* Reject unknown escape sequences */
93 default:
94 return NULL;
Linus Torvalds17712992005-10-10 16:31:08 -070095 }
96 value[len++] = c;
97 continue;
98 }
99 if (c == '"') {
100 quote = 1-quote;
101 continue;
102 }
Linus Torvalds17712992005-10-10 16:31:08 -0700103 value[len++] = c;
104 }
105}
106
Linus Torvalds38c5afa2006-10-30 08:25:36 -0800107static inline int iskeychar(int c)
108{
109 return isalnum(c) || c == '-';
110}
111
Linus Torvalds17712992005-10-10 16:31:08 -0700112static int get_value(config_fn_t fn, char *name, unsigned int len)
113{
114 int c;
115 char *value;
116
117 /* Get the full name */
118 for (;;) {
119 c = get_next_char();
120 if (c == EOF)
121 break;
Linus Torvalds38c5afa2006-10-30 08:25:36 -0800122 if (!iskeychar(c))
Linus Torvalds17712992005-10-10 16:31:08 -0700123 break;
124 name[len++] = tolower(c);
125 if (len >= MAXNAME)
126 return -1;
127 }
128 name[len] = 0;
129 while (c == ' ' || c == '\t')
130 c = get_next_char();
131
132 value = NULL;
133 if (c != '\n') {
134 if (c != '=')
135 return -1;
136 value = parse_value();
137 if (!value)
138 return -1;
139 }
140 return fn(name, value);
141}
142
Linus Torvaldsd14f7762006-05-09 12:24:02 -0700143static int get_extended_base_var(char *name, int baselen, int c)
144{
145 do {
146 if (c == '\n')
147 return -1;
148 c = get_next_char();
149 } while (isspace(c));
150
151 /* We require the format to be '[base "extension"]' */
152 if (c != '"')
153 return -1;
154 name[baselen++] = '.';
155
156 for (;;) {
157 int c = get_next_char();
158 if (c == '\n')
159 return -1;
160 if (c == '"')
161 break;
162 if (c == '\\') {
163 c = get_next_char();
164 if (c == '\n')
165 return -1;
166 }
167 name[baselen++] = c;
168 if (baselen > MAXNAME / 2)
169 return -1;
170 }
171
172 /* Final ']' */
173 if (get_next_char() != ']')
174 return -1;
175 return baselen;
176}
177
Linus Torvalds17712992005-10-10 16:31:08 -0700178static int get_base_var(char *name)
179{
180 int baselen = 0;
181
182 for (;;) {
183 int c = get_next_char();
184 if (c == EOF)
185 return -1;
186 if (c == ']')
187 return baselen;
Linus Torvaldsd14f7762006-05-09 12:24:02 -0700188 if (isspace(c))
189 return get_extended_base_var(name, baselen, c);
Linus Torvalds38c5afa2006-10-30 08:25:36 -0800190 if (!iskeychar(c) && c != '.')
Linus Torvalds17712992005-10-10 16:31:08 -0700191 return -1;
192 if (baselen > MAXNAME / 2)
193 return -1;
194 name[baselen++] = tolower(c);
195 }
196}
197
198static int git_parse_file(config_fn_t fn)
199{
200 int comment = 0;
201 int baselen = 0;
202 static char var[MAXNAME];
203
204 for (;;) {
205 int c = get_next_char();
206 if (c == '\n') {
207 /* EOF? */
208 if (!config_file)
209 return 0;
210 comment = 0;
211 continue;
212 }
213 if (comment || isspace(c))
214 continue;
215 if (c == '#' || c == ';') {
216 comment = 1;
217 continue;
218 }
219 if (c == '[') {
220 baselen = get_base_var(var);
221 if (baselen <= 0)
222 break;
223 var[baselen++] = '.';
224 var[baselen] = 0;
225 continue;
226 }
227 if (!isalpha(c))
228 break;
Linus Torvalds128af9d2005-10-11 18:47:34 -0700229 var[baselen] = tolower(c);
Linus Torvalds17712992005-10-10 16:31:08 -0700230 if (get_value(fn, var, baselen+1) < 0)
231 break;
232 }
Junio C Hamano4f629532005-11-25 16:03:56 -0800233 die("bad config file line %d in %s", config_linenr, config_file_name);
Linus Torvalds17712992005-10-10 16:31:08 -0700234}
235
Brian Downing0b87b6e2007-07-12 08:32:26 -0500236static unsigned long get_unit_factor(const char *end)
237{
238 if (!*end)
239 return 1;
240 else if (!strcasecmp(end, "k"))
241 return 1024;
242 else if (!strcasecmp(end, "m"))
243 return 1024 * 1024;
244 else if (!strcasecmp(end, "g"))
245 return 1024 * 1024 * 1024;
246 die("unknown unit: '%s'", end);
247}
248
249int git_parse_long(const char *value, long *ret)
Linus Torvalds17712992005-10-10 16:31:08 -0700250{
251 if (value && *value) {
252 char *end;
Brian Downing0b87b6e2007-07-12 08:32:26 -0500253 long val = strtol(value, &end, 0);
254 *ret = val * get_unit_factor(end);
255 return 1;
Linus Torvalds17712992005-10-10 16:31:08 -0700256 }
Brian Downing0b87b6e2007-07-12 08:32:26 -0500257 return 0;
258}
259
260int git_parse_ulong(const char *value, unsigned long *ret)
261{
262 if (value && *value) {
263 char *end;
264 unsigned long val = strtoul(value, &end, 0);
265 *ret = val * get_unit_factor(end);
266 return 1;
267 }
268 return 0;
269}
270
271int git_config_int(const char *name, const char *value)
272{
273 long ret;
274 if (!git_parse_long(value, &ret))
275 die("bad config value for '%s' in %s", name, config_file_name);
276 return ret;
277}
278
279unsigned long git_config_ulong(const char *name, const char *value)
280{
281 unsigned long ret;
282 if (!git_parse_ulong(value, &ret))
283 die("bad config value for '%s' in %s", name, config_file_name);
284 return ret;
Linus Torvalds17712992005-10-10 16:31:08 -0700285}
286
287int git_config_bool(const char *name, const char *value)
288{
289 if (!value)
290 return 1;
291 if (!*value)
292 return 0;
Junio C Hamano624314f2006-07-03 18:48:23 -0700293 if (!strcasecmp(value, "true") || !strcasecmp(value, "yes"))
Linus Torvalds17712992005-10-10 16:31:08 -0700294 return 1;
Junio C Hamano624314f2006-07-03 18:48:23 -0700295 if (!strcasecmp(value, "false") || !strcasecmp(value, "no"))
Linus Torvalds17712992005-10-10 16:31:08 -0700296 return 0;
297 return git_config_int(name, value) != 0;
298}
299
300int git_default_config(const char *var, const char *value)
301{
302 /* This needs a better name */
303 if (!strcmp(var, "core.filemode")) {
304 trust_executable_bit = git_config_bool(var, value);
305 return 0;
306 }
307
Junio C Hamano9378c162007-06-24 15:11:24 -0700308 if (!strcmp(var, "core.quotepath")) {
309 quote_path_fully = git_config_bool(var, value);
310 return 0;
311 }
312
Johannes Sixt78a8d642007-03-02 22:11:30 +0100313 if (!strcmp(var, "core.symlinks")) {
314 has_symlinks = git_config_bool(var, value);
315 return 0;
316 }
317
Junio C Hamano7d1864c2007-01-07 02:00:28 -0800318 if (!strcmp(var, "core.bare")) {
319 is_bare_repository_cfg = git_config_bool(var, value);
320 return 0;
321 }
322
Junio C Hamano5f730762006-02-08 21:15:24 -0800323 if (!strcmp(var, "core.ignorestat")) {
324 assume_unchanged = git_config_bool(var, value);
325 return 0;
326 }
327
Junio C Hamanoe388c732006-05-02 00:40:24 -0700328 if (!strcmp(var, "core.prefersymlinkrefs")) {
329 prefer_symlink_refs = git_config_bool(var, value);
Johannes Schindelinf8348be2005-11-15 19:24:19 +0100330 return 0;
331 }
332
Shawn Pearce6de08ae2006-05-17 05:55:40 -0400333 if (!strcmp(var, "core.logallrefupdates")) {
334 log_all_ref_updates = git_config_bool(var, value);
335 return 0;
336 }
337
Junio C Hamano2f8acdb2006-03-20 18:45:47 -0800338 if (!strcmp(var, "core.warnambiguousrefs")) {
339 warn_ambiguous_refs = git_config_bool(var, value);
340 return 0;
341 }
342
Dana How960ccca2007-05-09 13:56:50 -0700343 if (!strcmp(var, "core.loosecompression")) {
Joachim B Haga12f6c302006-07-03 22:11:47 +0200344 int level = git_config_int(var, value);
345 if (level == -1)
346 level = Z_DEFAULT_COMPRESSION;
347 else if (level < 0 || level > Z_BEST_COMPRESSION)
348 die("bad zlib compression level %d", level);
349 zlib_compression_level = level;
Dana How960ccca2007-05-09 13:56:50 -0700350 zlib_compression_seen = 1;
351 return 0;
352 }
353
354 if (!strcmp(var, "core.compression")) {
355 int level = git_config_int(var, value);
356 if (level == -1)
357 level = Z_DEFAULT_COMPRESSION;
358 else if (level < 0 || level > Z_BEST_COMPRESSION)
359 die("bad zlib compression level %d", level);
360 core_compression_level = level;
361 core_compression_seen = 1;
362 if (!zlib_compression_seen)
363 zlib_compression_level = level;
Joachim B Haga12f6c302006-07-03 22:11:47 +0200364 return 0;
365 }
366
Shawn O. Pearce60bb8b12006-12-23 02:34:28 -0500367 if (!strcmp(var, "core.packedgitwindowsize")) {
Junio C Hamano5faaf242007-02-14 13:20:41 -0800368 int pgsz_x2 = getpagesize() * 2;
Shawn O. Pearce60bb8b12006-12-23 02:34:28 -0500369 packed_git_window_size = git_config_int(var, value);
Junio C Hamano5faaf242007-02-14 13:20:41 -0800370
371 /* This value must be multiple of (pagesize * 2) */
372 packed_git_window_size /= pgsz_x2;
373 if (packed_git_window_size < 1)
374 packed_git_window_size = 1;
375 packed_git_window_size *= pgsz_x2;
Shawn O. Pearce60bb8b12006-12-23 02:34:28 -0500376 return 0;
377 }
378
Shawn O. Pearce77ccc5b2006-12-23 02:33:35 -0500379 if (!strcmp(var, "core.packedgitlimit")) {
380 packed_git_limit = git_config_int(var, value);
381 return 0;
382 }
383
Shawn O. Pearce18bdec12007-03-19 01:14:37 -0400384 if (!strcmp(var, "core.deltabasecachelimit")) {
385 delta_base_cache_limit = git_config_int(var, value);
386 return 0;
387 }
388
Linus Torvalds6c510be2007-02-13 11:07:23 -0800389 if (!strcmp(var, "core.autocrlf")) {
Linus Torvaldsd7f46332007-02-13 18:16:12 -0800390 if (value && !strcasecmp(value, "input")) {
391 auto_crlf = -1;
392 return 0;
393 }
Linus Torvalds6c510be2007-02-13 11:07:23 -0800394 auto_crlf = git_config_bool(var, value);
395 return 0;
396 }
397
Linus Torvaldse1b10392005-10-11 18:47:34 -0700398 if (!strcmp(var, "user.name")) {
Peter Eriksen817151e2006-06-24 16:01:25 +0200399 strlcpy(git_default_name, value, sizeof(git_default_name));
Linus Torvaldse1b10392005-10-11 18:47:34 -0700400 return 0;
401 }
402
403 if (!strcmp(var, "user.email")) {
Peter Eriksen817151e2006-06-24 16:01:25 +0200404 strlcpy(git_default_email, value, sizeof(git_default_email));
Linus Torvaldse1b10392005-10-11 18:47:34 -0700405 return 0;
406 }
407
Junio C Hamano4e72dce2005-11-27 16:09:40 -0800408 if (!strcmp(var, "i18n.commitencoding")) {
Shawn O. Pearcedbb2b412007-03-15 21:02:51 -0400409 git_commit_encoding = xstrdup(value);
Junio C Hamano4e72dce2005-11-27 16:09:40 -0800410 return 0;
411 }
412
Junio C Hamanod2c11a32006-12-27 16:41:33 -0800413 if (!strcmp(var, "i18n.logoutputencoding")) {
Shawn O. Pearcedbb2b412007-03-15 21:02:51 -0400414 git_log_output_encoding = xstrdup(value);
Junio C Hamanod2c11a32006-12-27 16:41:33 -0800415 return 0;
416 }
417
418
Andy Parkinsa159ca02006-12-13 09:13:28 +0000419 if (!strcmp(var, "pager.color") || !strcmp(var, "color.pager")) {
Matthias Lederhoferaa086eb2006-07-30 00:27:43 +0200420 pager_use_color = git_config_bool(var,value);
421 return 0;
422 }
423
Brian Gernhardt54adf372007-07-03 14:18:11 -0400424 if (!strcmp(var, "core.pager")) {
425 pager_program = xstrdup(value);
426 return 0;
427 }
428
Johannes Schindelin4d87b9c2007-07-20 13:06:09 +0100429 if (!strcmp(var, "core.editor")) {
430 editor_program = xstrdup(value);
431 return 0;
432 }
433
Junio C Hamanodcf0c162007-11-16 17:05:02 -0800434 if (!strcmp(var, "core.excludesfile")) {
435 if (!value)
436 die("core.excludesfile without value");
437 excludes_file = xstrdup(value);
438 return 0;
439 }
440
Petr Baudis1ab661d2006-04-25 00:59:33 +0200441 /* Add other config variables here and to Documentation/config.txt. */
Linus Torvalds17712992005-10-10 16:31:08 -0700442 return 0;
443}
444
Junio C Hamano4f629532005-11-25 16:03:56 -0800445int git_config_from_file(config_fn_t fn, const char *filename)
Linus Torvalds17712992005-10-10 16:31:08 -0700446{
447 int ret;
Junio C Hamano4f629532005-11-25 16:03:56 -0800448 FILE *f = fopen(filename, "r");
Linus Torvalds17712992005-10-10 16:31:08 -0700449
450 ret = -1;
451 if (f) {
452 config_file = f;
Junio C Hamano4f629532005-11-25 16:03:56 -0800453 config_file_name = filename;
Linus Torvalds17712992005-10-10 16:31:08 -0700454 config_linenr = 1;
455 ret = git_parse_file(fn);
456 fclose(f);
Junio C Hamano4f629532005-11-25 16:03:56 -0800457 config_file_name = NULL;
Linus Torvalds17712992005-10-10 16:31:08 -0700458 }
459 return ret;
460}
Johannes Schindelin10bea152005-11-17 22:32:36 +0100461
Junio C Hamano4f629532005-11-25 16:03:56 -0800462int git_config(config_fn_t fn)
463{
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +0200464 int ret = 0;
465 char *repo_config = NULL;
466 const char *home = NULL, *filename;
467
468 /* $GIT_CONFIG makes git read _only_ the given config file,
469 * $GIT_CONFIG_LOCAL will make it process it in addition to the
470 * global config file, the same way it would the per-repository
471 * config file otherwise. */
Junio C Hamanod4ebc362006-12-19 01:28:15 -0800472 filename = getenv(CONFIG_ENVIRONMENT);
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +0200473 if (!filename) {
Johannes Schindelin32043c92007-02-14 12:48:14 +0100474 if (!access(ETC_GITCONFIG, R_OK))
475 ret += git_config_from_file(fn, ETC_GITCONFIG);
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +0200476 home = getenv("HOME");
Junio C Hamanod4ebc362006-12-19 01:28:15 -0800477 filename = getenv(CONFIG_LOCAL_ENVIRONMENT);
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +0200478 if (!filename)
Shawn Pearce9befac42006-09-02 00:16:31 -0400479 filename = repo_config = xstrdup(git_path("config"));
Petr Baudis7f29f7a2006-06-18 01:23:58 +0200480 }
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +0200481
482 if (home) {
Shawn Pearce9befac42006-09-02 00:16:31 -0400483 char *user_config = xstrdup(mkpath("%s/.gitconfig", home));
Johannes Schindeline33d0612006-06-20 09:51:09 +0200484 if (!access(user_config, R_OK))
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +0200485 ret = git_config_from_file(fn, user_config);
486 free(user_config);
487 }
488
489 ret += git_config_from_file(fn, filename);
Junio C Hamano4cac42b2006-08-27 21:19:39 -0700490 free(repo_config);
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +0200491 return ret;
Junio C Hamano4f629532005-11-25 16:03:56 -0800492}
493
Johannes Schindelin10bea152005-11-17 22:32:36 +0100494/*
495 * Find all the stuff for git_config_set() below.
496 */
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100497
498#define MAX_MATCHES 512
499
Johannes Schindelin10bea152005-11-17 22:32:36 +0100500static struct {
501 int baselen;
502 char* key;
Johannes Schindelinf98d8632005-11-20 13:24:18 +0100503 int do_not_match;
Johannes Schindelin10bea152005-11-17 22:32:36 +0100504 regex_t* value_regex;
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100505 int multi_replace;
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500506 size_t offset[MAX_MATCHES];
Johannes Schindelin10bea152005-11-17 22:32:36 +0100507 enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state;
508 int seen;
509} store;
510
Johannes Schindelinf98d8632005-11-20 13:24:18 +0100511static int matches(const char* key, const char* value)
512{
513 return !strcmp(key, store.key) &&
514 (store.value_regex == NULL ||
515 (store.do_not_match ^
516 !regexec(store.value_regex, value, 0, NULL, 0)));
517}
518
Johannes Schindelin10bea152005-11-17 22:32:36 +0100519static int store_aux(const char* key, const char* value)
520{
Junio C Hamanoae9ee412007-05-12 21:49:33 -0700521 const char *ep;
522 size_t section_len;
523
Johannes Schindelin10bea152005-11-17 22:32:36 +0100524 switch (store.state) {
525 case KEY_SEEN:
Johannes Schindelinf98d8632005-11-20 13:24:18 +0100526 if (matches(key, value)) {
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100527 if (store.seen == 1 && store.multi_replace == 0) {
Johannes Schindelin10bea152005-11-17 22:32:36 +0100528 fprintf(stderr,
529 "Warning: %s has multiple values\n",
530 key);
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100531 } else if (store.seen >= MAX_MATCHES) {
532 fprintf(stderr, "Too many matches\n");
533 return 1;
Johannes Schindelin10bea152005-11-17 22:32:36 +0100534 }
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100535
536 store.offset[store.seen] = ftell(config_file);
Johannes Schindelin10bea152005-11-17 22:32:36 +0100537 store.seen++;
538 }
539 break;
540 case SECTION_SEEN:
Junio C Hamanoae9ee412007-05-12 21:49:33 -0700541 /*
542 * What we are looking for is in store.key (both
543 * section and var), and its section part is baselen
544 * long. We found key (again, both section and var).
545 * We would want to know if this key is in the same
546 * section as what we are looking for. We already
547 * know we are in the same section as what should
548 * hold store.key.
549 */
550 ep = strrchr(key, '.');
551 section_len = ep - key;
552
553 if ((section_len != store.baselen) ||
554 memcmp(key, store.key, section_len+1)) {
Johannes Schindelin10bea152005-11-17 22:32:36 +0100555 store.state = SECTION_END_SEEN;
556 break;
Junio C Hamanoae9ee412007-05-12 21:49:33 -0700557 }
558
559 /*
560 * Do not increment matches: this is no match, but we
561 * just made sure we are in the desired section.
562 */
563 store.offset[store.seen] = ftell(config_file);
Johannes Schindelin10bea152005-11-17 22:32:36 +0100564 /* fallthru */
565 case SECTION_END_SEEN:
566 case START:
Johannes Schindelinf98d8632005-11-20 13:24:18 +0100567 if (matches(key, value)) {
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100568 store.offset[store.seen] = ftell(config_file);
Johannes Schindelin10bea152005-11-17 22:32:36 +0100569 store.state = KEY_SEEN;
570 store.seen++;
Linus Torvaldsd14f7762006-05-09 12:24:02 -0700571 } else {
572 if (strrchr(key, '.') - key == store.baselen &&
seanbdf0ef02006-05-06 14:14:02 -0400573 !strncmp(key, store.key, store.baselen)) {
sean93ddef32006-05-05 09:49:15 -0400574 store.state = SECTION_SEEN;
seanbdf0ef02006-05-06 14:14:02 -0400575 store.offset[store.seen] = ftell(config_file);
Linus Torvaldsd14f7762006-05-09 12:24:02 -0700576 }
seanbdf0ef02006-05-06 14:14:02 -0400577 }
Johannes Schindelin10bea152005-11-17 22:32:36 +0100578 }
579 return 0;
580}
581
Junio C Hamanob79d18c2007-06-13 01:22:51 -0700582static int write_error(void)
Andy Whitcroft480c9e52007-01-08 15:58:38 +0000583{
584 fprintf(stderr, "Failed to write new configuration file\n");
585
586 /* Same error code as "failed to rename". */
587 return 4;
588}
589
590static int store_write_section(int fd, const char* key)
Johannes Schindelin10bea152005-11-17 22:32:36 +0100591{
Linus Torvaldsd14f7762006-05-09 12:24:02 -0700592 const char *dot = strchr(key, '.');
593 int len1 = store.baselen, len2 = -1;
594
595 dot = strchr(key, '.');
596 if (dot) {
597 int dotlen = dot - key;
598 if (dotlen < len1) {
599 len2 = len1 - dotlen - 1;
600 len1 = dotlen;
601 }
602 }
603
Andy Whitcroft480c9e52007-01-08 15:58:38 +0000604 if (write_in_full(fd, "[", 1) != 1 ||
605 write_in_full(fd, key, len1) != len1)
606 return 0;
Linus Torvaldsd14f7762006-05-09 12:24:02 -0700607 if (len2 >= 0) {
Andy Whitcroft480c9e52007-01-08 15:58:38 +0000608 if (write_in_full(fd, " \"", 2) != 2)
609 return 0;
Linus Torvaldsd14f7762006-05-09 12:24:02 -0700610 while (--len2 >= 0) {
611 unsigned char c = *++dot;
612 if (c == '"')
Andy Whitcroft480c9e52007-01-08 15:58:38 +0000613 if (write_in_full(fd, "\\", 1) != 1)
614 return 0;
615 if (write_in_full(fd, &c, 1) != 1)
616 return 0;
Linus Torvaldsd14f7762006-05-09 12:24:02 -0700617 }
Andy Whitcroft480c9e52007-01-08 15:58:38 +0000618 if (write_in_full(fd, "\"", 1) != 1)
619 return 0;
Linus Torvaldsd14f7762006-05-09 12:24:02 -0700620 }
Andy Whitcroft480c9e52007-01-08 15:58:38 +0000621 if (write_in_full(fd, "]\n", 2) != 2)
622 return 0;
623
624 return 1;
Johannes Schindelin10bea152005-11-17 22:32:36 +0100625}
626
Andy Whitcroft480c9e52007-01-08 15:58:38 +0000627static int store_write_pair(int fd, const char* key, const char* value)
Johannes Schindelin10bea152005-11-17 22:32:36 +0100628{
629 int i;
Andy Whitcroft480c9e52007-01-08 15:58:38 +0000630 int length = strlen(key+store.baselen+1);
Brian Gernhardtcdd4fb12007-01-09 00:27:41 -0500631 int quote = 0;
632
633 /* Check to see if the value needs to be quoted. */
634 if (value[0] == ' ')
635 quote = 1;
636 for (i = 0; value[i]; i++)
637 if (value[i] == ';' || value[i] == '#')
638 quote = 1;
639 if (value[i-1] == ' ')
640 quote = 1;
Johannes Schindelin10bea152005-11-17 22:32:36 +0100641
Andy Whitcroft480c9e52007-01-08 15:58:38 +0000642 if (write_in_full(fd, "\t", 1) != 1 ||
643 write_in_full(fd, key+store.baselen+1, length) != length ||
644 write_in_full(fd, " = ", 3) != 3)
645 return 0;
Brian Gernhardtcdd4fb12007-01-09 00:27:41 -0500646 if (quote && write_in_full(fd, "\"", 1) != 1)
647 return 0;
Johannes Schindelin10bea152005-11-17 22:32:36 +0100648 for (i = 0; value[i]; i++)
649 switch (value[i]) {
Andy Whitcroft480c9e52007-01-08 15:58:38 +0000650 case '\n':
651 if (write_in_full(fd, "\\n", 2) != 2)
652 return 0;
653 break;
654 case '\t':
655 if (write_in_full(fd, "\\t", 2) != 2)
656 return 0;
657 break;
658 case '"':
659 case '\\':
660 if (write_in_full(fd, "\\", 1) != 1)
661 return 0;
662 default:
663 if (write_in_full(fd, value+i, 1) != 1)
664 return 0;
665 break;
666 }
Brian Gernhardtcdd4fb12007-01-09 00:27:41 -0500667 if (quote && write_in_full(fd, "\"", 1) != 1)
668 return 0;
Andy Whitcroft480c9e52007-01-08 15:58:38 +0000669 if (write_in_full(fd, "\n", 1) != 1)
670 return 0;
671 return 1;
Johannes Schindelin10bea152005-11-17 22:32:36 +0100672}
673
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500674static ssize_t find_beginning_of_line(const char* contents, size_t size,
675 size_t offset_, int* found_bracket)
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100676{
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500677 size_t equal_offset = size, bracket_offset = size;
678 ssize_t offset;
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100679
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700680 for (offset = offset_-2; offset > 0
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100681 && contents[offset] != '\n'; offset--)
682 switch (contents[offset]) {
683 case '=': equal_offset = offset; break;
684 case ']': bracket_offset = offset; break;
685 }
686 if (bracket_offset < equal_offset) {
687 *found_bracket = 1;
688 offset = bracket_offset+1;
689 } else
690 offset++;
691
692 return offset;
693}
694
Johannes Schindelin10bea152005-11-17 22:32:36 +0100695int git_config_set(const char* key, const char* value)
696{
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100697 return git_config_set_multivar(key, value, NULL, 0);
Johannes Schindelin10bea152005-11-17 22:32:36 +0100698}
699
700/*
701 * If value==NULL, unset in (remove from) config,
702 * if value_regex!=NULL, disregard key/value pairs where value does not match.
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100703 * if multi_replace==0, nothing, or only one matching key/value is replaced,
704 * else all matching key/values (regardless how many) are removed,
705 * before the new pair is written.
Johannes Schindelin10bea152005-11-17 22:32:36 +0100706 *
707 * Returns 0 on success.
708 *
709 * This function does this:
710 *
711 * - it locks the config file by creating ".git/config.lock"
712 *
713 * - it then parses the config using store_aux() as validator to find
714 * the position on the key/value pair to replace. If it is to be unset,
715 * it must be found exactly once.
716 *
717 * - the config file is mmap()ed and the part before the match (if any) is
718 * written to the lock file, then the changed part and the rest.
719 *
720 * - the config file is removed and the lock file rename()d to it.
721 *
722 */
723int git_config_set_multivar(const char* key, const char* value,
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100724 const char* value_regex, int multi_replace)
Johannes Schindelin10bea152005-11-17 22:32:36 +0100725{
Linus Torvaldsd14f7762006-05-09 12:24:02 -0700726 int i, dot;
Junio C Hamanof8ba6552006-05-07 21:27:30 -0700727 int fd = -1, in_fd;
Serge E. Hallyndafc88b2006-04-17 10:14:48 -0500728 int ret;
Johannes Schindelin9c3796f2006-06-20 00:51:58 +0200729 char* config_filename;
Bradford C. Smith6cbf9732007-07-26 12:55:28 -0400730 struct lock_file *lock = NULL;
Johannes Schindelinb17e6592005-11-20 21:22:19 +0100731 const char* last_dot = strrchr(key, '.');
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100732
Junio C Hamanod4ebc362006-12-19 01:28:15 -0800733 config_filename = getenv(CONFIG_ENVIRONMENT);
Johannes Schindelin9c3796f2006-06-20 00:51:58 +0200734 if (!config_filename) {
Junio C Hamanod4ebc362006-12-19 01:28:15 -0800735 config_filename = getenv(CONFIG_LOCAL_ENVIRONMENT);
Johannes Schindelin9c3796f2006-06-20 00:51:58 +0200736 if (!config_filename)
737 config_filename = git_path("config");
738 }
Shawn Pearce9befac42006-09-02 00:16:31 -0400739 config_filename = xstrdup(config_filename);
Johannes Schindelin9c3796f2006-06-20 00:51:58 +0200740
Johannes Schindelin10bea152005-11-17 22:32:36 +0100741 /*
742 * Since "key" actually contains the section name and the real
743 * key name separated by a dot, we have to know where the dot is.
744 */
Johannes Schindelinb17e6592005-11-20 21:22:19 +0100745
Serge E. Hallyndafc88b2006-04-17 10:14:48 -0500746 if (last_dot == NULL) {
Johannes Schindelin10bea152005-11-17 22:32:36 +0100747 fprintf(stderr, "key does not contain a section: %s\n", key);
Serge E. Hallyndafc88b2006-04-17 10:14:48 -0500748 ret = 2;
749 goto out_free;
Johannes Schindelin10bea152005-11-17 22:32:36 +0100750 }
Johannes Schindelinb17e6592005-11-20 21:22:19 +0100751 store.baselen = last_dot - key;
752
753 store.multi_replace = multi_replace;
Johannes Schindelin10bea152005-11-17 22:32:36 +0100754
755 /*
756 * Validate the key and while at it, lower case it for matching.
757 */
Jonas Fonseca2d7320d2006-09-01 00:32:39 +0200758 store.key = xmalloc(strlen(key) + 1);
Linus Torvaldsd14f7762006-05-09 12:24:02 -0700759 dot = 0;
760 for (i = 0; key[i]; i++) {
761 unsigned char c = key[i];
762 if (c == '.')
763 dot = 1;
764 /* Leave the extended basename untouched.. */
765 if (!dot || i > store.baselen) {
Linus Torvalds38c5afa2006-10-30 08:25:36 -0800766 if (!iskeychar(c) || (i == store.baselen+1 && !isalpha(c))) {
Linus Torvaldsd14f7762006-05-09 12:24:02 -0700767 fprintf(stderr, "invalid key: %s\n", key);
768 free(store.key);
769 ret = 1;
770 goto out_free;
771 }
772 c = tolower(c);
Johannes Schindelin6f716862007-01-20 02:25:37 +0100773 } else if (c == '\n') {
774 fprintf(stderr, "invalid key (newline): %s\n", key);
775 free(store.key);
776 ret = 1;
777 goto out_free;
Linus Torvaldsd14f7762006-05-09 12:24:02 -0700778 }
779 store.key[i] = c;
780 }
Johannes Schindelin3dd94e32005-11-21 11:18:20 +0100781 store.key[i] = 0;
Johannes Schindelin10bea152005-11-17 22:32:36 +0100782
783 /*
Bradford C. Smith6cbf9732007-07-26 12:55:28 -0400784 * The lock serves a purpose in addition to locking: the new
Johannes Schindelin10bea152005-11-17 22:32:36 +0100785 * contents of .git/config will be written into it.
786 */
Bradford C. Smith6cbf9732007-07-26 12:55:28 -0400787 lock = xcalloc(sizeof(struct lock_file), 1);
788 fd = hold_lock_file_for_update(lock, config_filename, 0);
789 if (fd < 0) {
Johannes Schindelin10bea152005-11-17 22:32:36 +0100790 fprintf(stderr, "could not lock config file\n");
791 free(store.key);
Serge E. Hallyndafc88b2006-04-17 10:14:48 -0500792 ret = -1;
793 goto out_free;
Johannes Schindelin10bea152005-11-17 22:32:36 +0100794 }
795
796 /*
797 * If .git/config does not exist yet, write a minimal version.
798 */
Alex Riesen88fb9582006-01-05 12:43:34 +0100799 in_fd = open(config_filename, O_RDONLY);
800 if ( in_fd < 0 ) {
Johannes Schindelin10bea152005-11-17 22:32:36 +0100801 free(store.key);
802
Alex Riesen88fb9582006-01-05 12:43:34 +0100803 if ( ENOENT != errno ) {
804 error("opening %s: %s", config_filename,
805 strerror(errno));
Serge E. Hallyndafc88b2006-04-17 10:14:48 -0500806 ret = 3; /* same as "invalid config file" */
807 goto out_free;
Alex Riesen88fb9582006-01-05 12:43:34 +0100808 }
Johannes Schindelin10bea152005-11-17 22:32:36 +0100809 /* if nothing to unset, error out */
810 if (value == NULL) {
Serge E. Hallyndafc88b2006-04-17 10:14:48 -0500811 ret = 5;
812 goto out_free;
Johannes Schindelin10bea152005-11-17 22:32:36 +0100813 }
814
815 store.key = (char*)key;
Andy Whitcroft480c9e52007-01-08 15:58:38 +0000816 if (!store_write_section(fd, key) ||
Junio C Hamano93c1e072007-01-11 13:16:26 -0800817 !store_write_pair(fd, key, value))
818 goto write_err_out;
819 } else {
Alex Riesen88fb9582006-01-05 12:43:34 +0100820 struct stat st;
Johannes Schindelin10bea152005-11-17 22:32:36 +0100821 char* contents;
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500822 size_t contents_sz, copy_begin, copy_end;
823 int i, new_line = 0;
Johannes Schindelin10bea152005-11-17 22:32:36 +0100824
825 if (value_regex == NULL)
826 store.value_regex = NULL;
827 else {
Johannes Schindelinf98d8632005-11-20 13:24:18 +0100828 if (value_regex[0] == '!') {
829 store.do_not_match = 1;
830 value_regex++;
831 } else
832 store.do_not_match = 0;
833
Jonas Fonseca2d7320d2006-09-01 00:32:39 +0200834 store.value_regex = (regex_t*)xmalloc(sizeof(regex_t));
Johannes Schindelin10bea152005-11-17 22:32:36 +0100835 if (regcomp(store.value_regex, value_regex,
836 REG_EXTENDED)) {
Alex Riesen7246ed42005-12-15 08:47:30 +0100837 fprintf(stderr, "Invalid pattern: %s\n",
Johannes Schindelin10bea152005-11-17 22:32:36 +0100838 value_regex);
839 free(store.value_regex);
Serge E. Hallyndafc88b2006-04-17 10:14:48 -0500840 ret = 6;
841 goto out_free;
Johannes Schindelin10bea152005-11-17 22:32:36 +0100842 }
843 }
844
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100845 store.offset[0] = 0;
Johannes Schindelin10bea152005-11-17 22:32:36 +0100846 store.state = START;
847 store.seen = 0;
848
849 /*
850 * After this, store.offset will contain the *end* offset
851 * of the last match, or remain at 0 if no match was found.
852 * As a side effect, we make sure to transform only a valid
853 * existing config file.
854 */
Johannes Schindelin9c3796f2006-06-20 00:51:58 +0200855 if (git_config_from_file(store_aux, config_filename)) {
Johannes Schindelin10bea152005-11-17 22:32:36 +0100856 fprintf(stderr, "invalid config file\n");
857 free(store.key);
858 if (store.value_regex != NULL) {
859 regfree(store.value_regex);
860 free(store.value_regex);
861 }
Serge E. Hallyndafc88b2006-04-17 10:14:48 -0500862 ret = 3;
863 goto out_free;
Johannes Schindelin10bea152005-11-17 22:32:36 +0100864 }
865
866 free(store.key);
867 if (store.value_regex != NULL) {
868 regfree(store.value_regex);
869 free(store.value_regex);
870 }
871
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100872 /* if nothing to unset, or too many matches, error out */
873 if ((store.seen == 0 && value == NULL) ||
874 (store.seen > 1 && multi_replace == 0)) {
Serge E. Hallyndafc88b2006-04-17 10:14:48 -0500875 ret = 5;
876 goto out_free;
Johannes Schindelin10bea152005-11-17 22:32:36 +0100877 }
878
Alex Riesen88fb9582006-01-05 12:43:34 +0100879 fstat(in_fd, &st);
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500880 contents_sz = xsize_t(st.st_size);
881 contents = xmmap(NULL, contents_sz, PROT_READ,
Johannes Schindelin10bea152005-11-17 22:32:36 +0100882 MAP_PRIVATE, in_fd, 0);
883 close(in_fd);
884
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100885 if (store.seen == 0)
886 store.seen = 1;
Johannes Schindelin10bea152005-11-17 22:32:36 +0100887
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100888 for (i = 0, copy_begin = 0; i < store.seen; i++) {
889 if (store.offset[i] == 0) {
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500890 store.offset[i] = copy_end = contents_sz;
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100891 } else if (store.state != KEY_SEEN) {
892 copy_end = store.offset[i];
Johannes Schindelin10bea152005-11-17 22:32:36 +0100893 } else
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100894 copy_end = find_beginning_of_line(
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500895 contents, contents_sz,
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100896 store.offset[i]-2, &new_line);
Johannes Schindelin10bea152005-11-17 22:32:36 +0100897
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100898 /* write the first part of the config */
899 if (copy_end > copy_begin) {
Junio C Hamano93c1e072007-01-11 13:16:26 -0800900 if (write_in_full(fd, contents + copy_begin,
901 copy_end - copy_begin) <
902 copy_end - copy_begin)
903 goto write_err_out;
904 if (new_line &&
905 write_in_full(fd, "\n", 1) != 1)
906 goto write_err_out;
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100907 }
908 copy_begin = store.offset[i];
909 }
Johannes Schindelin10bea152005-11-17 22:32:36 +0100910
911 /* write the pair (value == NULL means unset) */
912 if (value != NULL) {
Junio C Hamano93c1e072007-01-11 13:16:26 -0800913 if (store.state == START) {
914 if (!store_write_section(fd, key))
915 goto write_err_out;
Andy Whitcroft480c9e52007-01-08 15:58:38 +0000916 }
Junio C Hamano93c1e072007-01-11 13:16:26 -0800917 if (!store_write_pair(fd, key, value))
918 goto write_err_out;
Johannes Schindelin10bea152005-11-17 22:32:36 +0100919 }
920
921 /* write the rest of the config */
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500922 if (copy_begin < contents_sz)
Junio C Hamano93c1e072007-01-11 13:16:26 -0800923 if (write_in_full(fd, contents + copy_begin,
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500924 contents_sz - copy_begin) <
925 contents_sz - copy_begin)
Junio C Hamano93c1e072007-01-11 13:16:26 -0800926 goto write_err_out;
Johannes Schindelin10bea152005-11-17 22:32:36 +0100927
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500928 munmap(contents, contents_sz);
Johannes Schindelin10bea152005-11-17 22:32:36 +0100929 }
930
Bradford C. Smith6cbf9732007-07-26 12:55:28 -0400931 if (close(fd) || commit_lock_file(lock) < 0) {
932 fprintf(stderr, "Cannot commit config file!\n");
Serge E. Hallyndafc88b2006-04-17 10:14:48 -0500933 ret = 4;
934 goto out_free;
Johannes Schindelin10bea152005-11-17 22:32:36 +0100935 }
936
Bradford C. Smith6cbf9732007-07-26 12:55:28 -0400937 /* fd is closed, so don't try to close it below. */
938 fd = -1;
939 /*
940 * lock is committed, so don't try to roll it back below.
941 * NOTE: Since lockfile.c keeps a linked list of all created
942 * lock_file structures, it isn't safe to free(lock). It's
943 * better to just leave it hanging around.
944 */
945 lock = NULL;
Serge E. Hallyndafc88b2006-04-17 10:14:48 -0500946 ret = 0;
947
948out_free:
Junio C Hamanof8ba6552006-05-07 21:27:30 -0700949 if (0 <= fd)
950 close(fd);
Bradford C. Smith6cbf9732007-07-26 12:55:28 -0400951 if (lock)
952 rollback_lock_file(lock);
Junio C Hamano4cac42b2006-08-27 21:19:39 -0700953 free(config_filename);
Serge E. Hallyndafc88b2006-04-17 10:14:48 -0500954 return ret;
Junio C Hamano93c1e072007-01-11 13:16:26 -0800955
956write_err_out:
957 ret = write_error();
958 goto out_free;
959
Johannes Schindelin10bea152005-11-17 22:32:36 +0100960}
961
Paolo Bonzini118f8b22007-03-02 21:53:33 +0100962static int section_name_match (const char *buf, const char *name)
963{
964 int i = 0, j = 0, dot = 0;
965 for (; buf[i] && buf[i] != ']'; i++) {
966 if (!dot && isspace(buf[i])) {
967 dot = 1;
968 if (name[j++] != '.')
969 break;
970 for (i++; isspace(buf[i]); i++)
971 ; /* do nothing */
972 if (buf[i] != '"')
973 break;
974 continue;
975 }
976 if (buf[i] == '\\' && dot)
977 i++;
978 else if (buf[i] == '"' && dot) {
979 for (i++; isspace(buf[i]); i++)
980 ; /* do_nothing */
981 break;
982 }
983 if (buf[i] != name[j++])
984 break;
985 }
986 return (buf[i] == ']' && name[j] == 0);
987}
988
989/* if new_name == NULL, the section is removed instead */
Johannes Schindelin0667fcf2006-12-16 15:14:14 +0100990int git_config_rename_section(const char *old_name, const char *new_name)
991{
Paolo Bonzini118f8b22007-03-02 21:53:33 +0100992 int ret = 0, remove = 0;
Junio C Hamanofc1905b2006-12-19 21:55:27 -0800993 char *config_filename;
Johannes Schindelin0667fcf2006-12-16 15:14:14 +0100994 struct lock_file *lock = xcalloc(sizeof(struct lock_file), 1);
995 int out_fd;
996 char buf[1024];
997
Junio C Hamanod4ebc362006-12-19 01:28:15 -0800998 config_filename = getenv(CONFIG_ENVIRONMENT);
Johannes Schindelin0667fcf2006-12-16 15:14:14 +0100999 if (!config_filename) {
Junio C Hamanod4ebc362006-12-19 01:28:15 -08001000 config_filename = getenv(CONFIG_LOCAL_ENVIRONMENT);
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01001001 if (!config_filename)
1002 config_filename = git_path("config");
1003 }
1004 config_filename = xstrdup(config_filename);
1005 out_fd = hold_lock_file_for_update(lock, config_filename, 0);
Junio C Hamanofc1905b2006-12-19 21:55:27 -08001006 if (out_fd < 0) {
1007 ret = error("Could not lock config file!");
1008 goto out;
1009 }
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01001010
Junio C Hamanofc1905b2006-12-19 21:55:27 -08001011 if (!(config_file = fopen(config_filename, "rb"))) {
Geert Bosch01ebb9d2007-04-05 10:20:55 -04001012 /* no config file means nothing to rename, no error */
1013 goto unlock_and_out;
Junio C Hamanofc1905b2006-12-19 21:55:27 -08001014 }
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01001015
1016 while (fgets(buf, sizeof(buf), config_file)) {
1017 int i;
Andy Whitcroft480c9e52007-01-08 15:58:38 +00001018 int length;
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01001019 for (i = 0; buf[i] && isspace(buf[i]); i++)
1020 ; /* do nothing */
1021 if (buf[i] == '[') {
1022 /* it's a section */
Paolo Bonzini118f8b22007-03-02 21:53:33 +01001023 if (section_name_match (&buf[i+1], old_name)) {
1024 ret++;
1025 if (new_name == NULL) {
1026 remove = 1;
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01001027 continue;
1028 }
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01001029 store.baselen = strlen(new_name);
Andy Whitcroft480c9e52007-01-08 15:58:38 +00001030 if (!store_write_section(out_fd, new_name)) {
1031 ret = write_error();
1032 goto out;
1033 }
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01001034 continue;
1035 }
Paolo Bonzini118f8b22007-03-02 21:53:33 +01001036 remove = 0;
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01001037 }
Paolo Bonzini118f8b22007-03-02 21:53:33 +01001038 if (remove)
1039 continue;
Andy Whitcroft480c9e52007-01-08 15:58:38 +00001040 length = strlen(buf);
1041 if (write_in_full(out_fd, buf, length) != length) {
1042 ret = write_error();
1043 goto out;
1044 }
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01001045 }
Junio C Hamanofc1905b2006-12-19 21:55:27 -08001046 fclose(config_file);
Geert Bosch01ebb9d2007-04-05 10:20:55 -04001047 unlock_and_out:
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01001048 if (close(out_fd) || commit_lock_file(lock) < 0)
Andy Whitcroft480c9e52007-01-08 15:58:38 +00001049 ret = error("Cannot commit config file!");
Junio C Hamanofc1905b2006-12-19 21:55:27 -08001050 out:
1051 free(config_filename);
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01001052 return ret;
1053}