Commit 003808b38cd09dab8748f93bb44a0df9aca6820c

Vicent Marti 2012-10-31T20:52:16

Merge branch 'config-refresh' into development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
diff --git a/include/git2/config.h b/include/git2/config.h
index 67408f9..e417cb3 100644
--- a/include/git2/config.h
+++ b/include/git2/config.h
@@ -56,6 +56,7 @@ struct git_config_file {
 	int (*set_multivar)(git_config_file *cfg, const char *name, const char *regexp, const char *value);
 	int (*del)(struct git_config_file *, const char *key);
 	int (*foreach)(struct git_config_file *, const char *, int (*fn)(const git_config_entry *, void *), void *data);
+	int (*refresh)(struct git_config_file *);
 	void (*free)(struct git_config_file *);
 };
 
@@ -243,6 +244,19 @@ GIT_EXTERN(int) git_config_open_level(
     unsigned int level);
 
 /**
+ * Reload changed config files
+ *
+ * A config file may be changed on disk out from under the in-memory
+ * config object.  This function causes us to look for files that have
+ * been modified since we last loaded them and refresh the config with
+ * the latest information.
+ *
+ * @param cfg The configuration to refresh
+ * @return 0 or an error code
+ */
+GIT_EXTERN(int) git_config_refresh(git_config *cfg);
+
+/**
  * Free the configuration and its associated memory and files
  *
  * @param cfg the configuration to free
diff --git a/src/attr.c b/src/attr.c
index f5e09cc..b575744 100644
--- a/src/attr.c
+++ b/src/attr.c
@@ -261,32 +261,26 @@ bool git_attr_cache__is_cached(
 
 static int load_attr_file(
 	const char **data,
-	git_attr_file_stat_sig *sig,
+	git_futils_filestamp *stamp,
 	const char *filename)
 {
 	int error;
 	git_buf content = GIT_BUF_INIT;
-	struct stat st;
 
-	if (p_stat(filename, &st) < 0)
-		return GIT_ENOTFOUND;
+	error = git_futils_filestamp_check(stamp, filename);
+	if (error < 0)
+		return error;
 
-	if (sig != NULL &&
-		(git_time_t)st.st_mtime == sig->seconds &&
-		(git_off_t)st.st_size == sig->size &&
-		(unsigned int)st.st_ino == sig->ino)
+	/* if error == 0, then file is up to date. By returning GIT_ENOTFOUND,
+	 * we tell the caller not to reparse this file...
+	 */
+	if (!error)
 		return GIT_ENOTFOUND;
 
-	error = git_futils_readbuffer_updated(&content, filename, NULL, NULL);
+	error = git_futils_readbuffer(&content, filename);
 	if (error < 0)
 		return error;
 
-	if (sig != NULL) {
-		sig->seconds = (git_time_t)st.st_mtime;
-		sig->size    = (git_off_t)st.st_size;
-		sig->ino     = (unsigned int)st.st_ino;
-	}
-
 	*data = git_buf_detach(&content);
 
 	return 0;
@@ -386,7 +380,7 @@ int git_attr_cache__push_file(
 	git_attr_cache *cache = git_repository_attr_cache(repo);
 	git_attr_file *file = NULL;
 	git_blob *blob = NULL;
-	git_attr_file_stat_sig st;
+	git_futils_filestamp stamp;
 
 	assert(filename && stack);
 
@@ -408,12 +402,10 @@ int git_attr_cache__push_file(
 	/* if not in cache, load data, parse, and cache */
 
 	if (source == GIT_ATTR_FILE_FROM_FILE) {
-		if (file)
-			memcpy(&st, &file->cache_data.st, sizeof(st));
-		else
-			memset(&st, 0, sizeof(st));
+		git_futils_filestamp_set(
+			&stamp, file ? &file->cache_data.stamp : NULL);
 
-		error = load_attr_file(&content, &st, filename);
+		error = load_attr_file(&content, &stamp, filename);
 	} else {
 		error = load_attr_blob_from_index(&content, &blob,
 			repo, file ? &file->cache_data.oid : NULL, relfile);
@@ -448,7 +440,7 @@ int git_attr_cache__push_file(
 	if (blob)
 		git_oid_cpy(&file->cache_data.oid, git_object_id((git_object *)blob));
 	else
-		memcpy(&file->cache_data.st, &st, sizeof(st));
+		git_futils_filestamp_set(&file->cache_data.stamp, &stamp);
 
 finish:
 	/* push file onto vector if we found one*/
diff --git a/src/attr_file.h b/src/attr_file.h
index 877daf3..3ea13d2 100644
--- a/src/attr_file.h
+++ b/src/attr_file.h
@@ -11,6 +11,7 @@
 #include "vector.h"
 #include "pool.h"
 #include "buffer.h"
+#include "fileops.h"
 
 #define GIT_ATTR_FILE			".gitattributes"
 #define GIT_ATTR_FILE_INREPO	"info/attributes"
@@ -54,19 +55,13 @@ typedef struct {
 } git_attr_assignment;
 
 typedef struct {
-	git_time_t seconds;
-	git_off_t  size;
-	unsigned int ino;
-} git_attr_file_stat_sig;
-
-typedef struct {
 	char *key;				/* cache "source#path" this was loaded from */
 	git_vector rules;		/* vector of <rule*> or <fnmatch*> */
 	git_pool *pool;
 	bool pool_is_allocated;
 	union {
 		git_oid oid;
-		git_attr_file_stat_sig st;
+		git_futils_filestamp stamp;
 	} cache_data;
 } git_attr_file;
 
diff --git a/src/config.c b/src/config.c
index 937510d..033bde4 100644
--- a/src/config.c
+++ b/src/config.c
@@ -267,6 +267,20 @@ int git_config_add_file(
 	return 0;
 }
 
+int git_config_refresh(git_config *cfg)
+{
+	int error = 0;
+	unsigned int i;
+
+	for (i = 0; i < cfg->files.length && !error; ++i) {
+		file_internal *internal = git_vector_get(&cfg->files, i);
+		git_config_file *file = internal->file;
+		error = file->refresh(file);
+	}
+
+	return error;
+}
+
 /*
  * Loop over all the variables
  */
diff --git a/src/config_file.c b/src/config_file.c
index 92fe132..1eae8b9 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -75,7 +75,11 @@ typedef struct {
 		int eof;
 	} reader;
 
-	char *file_path;
+	char  *file_path;
+	time_t file_mtime;
+	size_t file_size;
+
+	unsigned int level;
 } diskfile_backend;
 
 static int config_parse(diskfile_backend *cfg_file, unsigned int level);
@@ -150,25 +154,53 @@ static int config_open(git_config_file *cfg, unsigned int level)
 	int res;
 	diskfile_backend *b = (diskfile_backend *)cfg;
 
+	b->level = level;
+
 	b->values = git_strmap_alloc();
 	GITERR_CHECK_ALLOC(b->values);
 
 	git_buf_init(&b->reader.buffer, 0);
-	res = git_futils_readbuffer(&b->reader.buffer, b->file_path);
+	res = git_futils_readbuffer_updated(
+		&b->reader.buffer, b->file_path, &b->file_mtime, &b->file_size, NULL);
 
 	/* It's fine if the file doesn't exist */
 	if (res == GIT_ENOTFOUND)
 		return 0;
 
-	if (res < 0 || config_parse(b, level) <  0) {
+	if (res < 0 || (res = config_parse(b, level)) < 0) {
 		free_vars(b->values);
 		b->values = NULL;
-		git_buf_free(&b->reader.buffer);
-		return -1;
 	}
 
 	git_buf_free(&b->reader.buffer);
-	return 0;
+	return res;
+}
+
+static int config_refresh(git_config_file *cfg)
+{
+	int res, updated = 0;
+	diskfile_backend *b = (diskfile_backend *)cfg;
+	git_strmap *old_values;
+
+	res = git_futils_readbuffer_updated(
+		&b->reader.buffer, b->file_path, &b->file_mtime, &b->file_size, &updated);
+	if (res < 0 || !updated)
+		return (res == GIT_ENOTFOUND) ? 0 : res;
+
+	/* need to reload - store old values and prep for reload */
+	old_values = b->values;
+	b->values = git_strmap_alloc();
+	GITERR_CHECK_ALLOC(b->values);
+
+	if ((res = config_parse(b, b->level)) < 0) {
+		free_vars(b->values);
+		b->values = old_values;
+	} else {
+		free_vars(old_values);
+	}
+
+	git_buf_free(&b->reader.buffer);
+	return res;
 }
 
 static void backend_free(git_config_file *_backend)
@@ -527,6 +559,7 @@ int git_config_file__ondisk(git_config_file **out, const char *path)
 	backend->parent.set_multivar = config_set_multivar;
 	backend->parent.del = config_delete;
 	backend->parent.foreach = file_foreach;
+	backend->parent.refresh = config_refresh;
 	backend->parent.free = backend_free;
 
 	*out = (git_config_file *)backend;
@@ -1197,8 +1230,12 @@ static int config_write(diskfile_backend *cfg, const char *key, const regex_t *p
 	git__free(section);
 	git__free(current_section);
 
+	/* refresh stats - if this errors, then commit will error too */
+	(void)git_filebuf_stats(&cfg->file_mtime, &cfg->file_size, &file);
+
 	result = git_filebuf_commit(&file, GIT_CONFIG_FILE_MODE);
 	git_buf_free(&cfg->reader.buffer);
+
 	return result;
 
 rewrite_fail:
diff --git a/src/filebuf.c b/src/filebuf.c
index b9b908c..5e5db0d 100644
--- a/src/filebuf.c
+++ b/src/filebuf.c
@@ -466,3 +466,26 @@ int git_filebuf_printf(git_filebuf *file, const char *format, ...)
 	return res;
 }
 
+int git_filebuf_stats(time_t *mtime, size_t *size, git_filebuf *file)
+{
+	int res;
+	struct stat st;
+
+	if (file->fd_is_open)
+		res = p_fstat(file->fd, &st);
+	else
+		res = p_stat(file->path_original, &st);
+
+	if (res < 0) {
+		giterr_set(GITERR_OS, "Could not get stat info for '%s'",
+			file->path_original);
+		return res;
+	}
+
+	if (mtime)
+		*mtime = st.st_mtime;
+	if (size)
+		*size = (size_t)st.st_size;
+
+	return 0;
+}
diff --git a/src/filebuf.h b/src/filebuf.h
index 3778831..a74bb0c 100644
--- a/src/filebuf.h
+++ b/src/filebuf.h
@@ -82,5 +82,6 @@ int git_filebuf_commit_at(git_filebuf *lock, const char *path, mode_t mode);
 void git_filebuf_cleanup(git_filebuf *lock);
 int git_filebuf_hash(git_oid *oid, git_filebuf *file);
 int git_filebuf_flush(git_filebuf *file);
+int git_filebuf_stats(time_t *mtime, size_t *size, git_filebuf *file);
 
 #endif
diff --git a/src/fileops.c b/src/fileops.c
index 6342b16..2aceb11 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -142,10 +142,11 @@ int git_futils_readbuffer_fd(git_buf *buf, git_file fd, size_t len)
 }
 
 int git_futils_readbuffer_updated(
-	git_buf *buf, const char *path, time_t *mtime, int *updated)
+	git_buf *buf, const char *path, time_t *mtime, size_t *size, int *updated)
 {
 	git_file fd;
 	struct stat st;
+	bool changed = false;
 
 	assert(buf && path && *path);
 
@@ -162,16 +163,25 @@ int git_futils_readbuffer_updated(
 	}
 
 	/*
-	 * If we were given a time, we only want to read the file if it
-	 * has been modified.
+	 * If we were given a time and/or a size, we only want to read the file
+	 * if it has been modified.
 	 */
-	if (mtime != NULL && *mtime >= st.st_mtime) {
+	if (size && *size != (size_t)st.st_size)
+		changed = true;
+	if (mtime && *mtime != st.st_mtime)
+		changed = true;
+	if (!size && !mtime)
+		changed = true;
+
+	if (!changed) {
 		p_close(fd);
 		return 0;
 	}
 
 	if (mtime != NULL)
 		*mtime = st.st_mtime;
+	if (size != NULL)
+		*size = (size_t)st.st_size;
 
 	if (git_futils_readbuffer_fd(buf, fd, (size_t)st.st_size) < 0) {
 		p_close(fd);
@@ -188,7 +198,7 @@ int git_futils_readbuffer_updated(
 
 int git_futils_readbuffer(git_buf *buf, const char *path)
 {
-	return git_futils_readbuffer_updated(buf, path, NULL, NULL);
+	return git_futils_readbuffer_updated(buf, path, NULL, NULL, NULL);
 }
 
 int git_futils_mv_withpath(const char *from, const char *to, const mode_t dirmode)
@@ -660,3 +670,38 @@ int git_futils_cp_r(
 
 	return error;
 }
+
+int git_futils_filestamp_check(
+	git_futils_filestamp *stamp, const char *path)
+{
+	struct stat st;
+
+	/* if the stamp is NULL, then always reload */
+	if (stamp == NULL)
+		return 1;
+
+	if (p_stat(path, &st) < 0)
+		return GIT_ENOTFOUND;
+
+	if (stamp->mtime == (git_time_t)st.st_mtime &&
+		stamp->size  == (git_off_t)st.st_size   &&
+		stamp->ino   == (unsigned int)st.st_ino)
+		return 0;
+
+	stamp->mtime = (git_time_t)st.st_mtime;
+	stamp->size  = (git_off_t)st.st_size;
+	stamp->ino   = (unsigned int)st.st_ino;
+
+	return 1;
+}
+
+void git_futils_filestamp_set(
+	git_futils_filestamp *target, const git_futils_filestamp *source)
+{
+	assert(target);
+
+	if (source)
+		memcpy(target, source, sizeof(*target));
+	else
+		memset(target, 0, sizeof(*target));
+}
diff --git a/src/fileops.h b/src/fileops.h
index 19f7ffd..25e62c5 100644
--- a/src/fileops.h
+++ b/src/fileops.h
@@ -18,7 +18,8 @@
  * Read whole files into an in-memory buffer for processing
  */
 extern int git_futils_readbuffer(git_buf *obj, const char *path);
-extern int git_futils_readbuffer_updated(git_buf *obj, const char *path, time_t *mtime, int *updated);
+extern int git_futils_readbuffer_updated(
+	git_buf *obj, const char *path, time_t *mtime, size_t *size, int *updated);
 extern int git_futils_readbuffer_fd(git_buf *obj, git_file fd, size_t len);
 
 /**
@@ -266,4 +267,44 @@ extern int git_futils_find_system_file(git_buf *path, const char *filename);
  */
 extern int git_futils_fake_symlink(const char *new, const char *old);
 
+/**
+ * A file stamp represents a snapshot of information about a file that can
+ * be used to test if the file changes.  This portable implementation is
+ * based on stat data about that file, but it is possible that OS specific
+ * versions could be implemented in the future.
+ */
+typedef struct {
+	git_time_t mtime;
+	git_off_t  size;
+	unsigned int ino;
+} git_futils_filestamp;
+
+/**
+ * Compare stat information for file with reference info.
+ *
+ * This function updates the file stamp to current data for the given path
+ * and returns 0 if the file is up-to-date relative to the prior setting or
+ * 1 if the file has been changed.  (This also may return GIT_ENOTFOUND if
+ * the file doesn't exist.)
+ *
+ * @param stamp File stamp to be checked
+ * @param path Path to stat and check if changed
+ * @return 0 if up-to-date, 1 if out-of-date, <0 on error
+ */
+extern int git_futils_filestamp_check(
+	git_futils_filestamp *stamp, const char *path);
+
+/**
+ * Set or reset file stamp data
+ *
+ * This writes the target file stamp.  If the source is NULL, this will set
+ * the target stamp to values that will definitely be out of date.  If the
+ * source is not NULL, this copies the source values to the target.
+ *
+ * @param tgt File stamp to write to
+ * @param src File stamp to copy from or NULL to clear the target
+ */
+extern void git_futils_filestamp_set(
+	git_futils_filestamp *tgt, const git_futils_filestamp *src);
+
 #endif /* INCLUDE_fileops_h__ */
diff --git a/src/index.c b/src/index.c
index 44dd934..35cf5dd 100644
--- a/src/index.c
+++ b/src/index.c
@@ -405,7 +405,7 @@ int git_index_read(git_index *index)
 	/* We don't want to update the mtime if we fail to parse the index */
 	mtime = index->last_modified;
 	error = git_futils_readbuffer_updated(
-		&buffer, index->index_file_path, &mtime, &updated);
+		&buffer, index->index_file_path, &mtime, NULL, &updated);
 	if (error < 0)
 		return error;
 
diff --git a/src/refs.c b/src/refs.c
index 833f2fc..779d608 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -123,7 +123,8 @@ static int reference_read(
 	if (git_buf_joinpath(&path, repo_path, ref_name) < 0)
 		return -1;
 
-	result = git_futils_readbuffer_updated(file_content, path.ptr, mtime, updated);
+	result = git_futils_readbuffer_updated(
+		file_content, path.ptr, mtime, NULL, updated);
 	git_buf_free(&path);
 
 	return result;
diff --git a/tests-clar/config/refresh.c b/tests-clar/config/refresh.c
new file mode 100644
index 0000000..99d677f
--- /dev/null
+++ b/tests-clar/config/refresh.c
@@ -0,0 +1,67 @@
+#include "clar_libgit2.h"
+
+#define TEST_FILE "config.refresh"
+
+void test_config_refresh__initialize(void)
+{
+}
+
+void test_config_refresh__cleanup(void)
+{
+	cl_fixture_cleanup(TEST_FILE);
+}
+
+void test_config_refresh__update_value(void)
+{
+	git_config *cfg;
+	int32_t v;
+
+	cl_git_mkfile(TEST_FILE, "[section]\n\tvalue = 1\n\n");
+
+	/* By freeing the config, we make sure we flush the values  */
+	cl_git_pass(git_config_open_ondisk(&cfg, TEST_FILE));
+
+	cl_git_pass(git_config_get_int32(&v, cfg, "section.value"));
+	cl_assert_equal_i(1, v);
+
+	cl_git_rewritefile(TEST_FILE, "[section]\n\tvalue = 10\n\n");
+
+	cl_git_pass(git_config_get_int32(&v, cfg, "section.value"));
+	cl_assert_equal_i(1, v);
+
+	cl_git_pass(git_config_refresh(cfg));
+
+	cl_git_pass(git_config_get_int32(&v, cfg, "section.value"));
+	cl_assert_equal_i(10, v);
+
+	git_config_free(cfg);
+}
+
+void test_config_refresh__delete_value(void)
+{
+	git_config *cfg;
+	int32_t v;
+
+	cl_git_mkfile(TEST_FILE, "[section]\n\tvalue = 1\n\n");
+
+	/* By freeing the config, we make sure we flush the values  */
+	cl_git_pass(git_config_open_ondisk(&cfg, TEST_FILE));
+
+	cl_git_pass(git_config_get_int32(&v, cfg, "section.value"));
+	cl_assert_equal_i(1, v);
+	cl_git_fail(git_config_get_int32(&v, cfg, "section.newval"));
+
+	cl_git_rewritefile(TEST_FILE, "[section]\n\tnewval = 10\n\n");
+
+	cl_git_pass(git_config_get_int32(&v, cfg, "section.value"));
+	cl_assert_equal_i(1, v);
+	cl_git_fail(git_config_get_int32(&v, cfg, "section.newval"));
+
+	cl_git_pass(git_config_refresh(cfg));
+
+	cl_git_fail(git_config_get_int32(&v, cfg, "section.value"));
+	cl_git_pass(git_config_get_int32(&v, cfg, "section.newval"));
+	cl_assert_equal_i(10, v);
+
+	git_config_free(cfg);
+}