Commit 305407e1bb7a097aba7a0304397bf280437b608e

Carlos Martín Nieto 2015-09-13T06:03:12

Merge pull request #3370 from libgit2/cmn/submodule-refactor submodule: refactor to be more explicit in the search

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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
diff --git a/src/submodule.c b/src/submodule.c
index 7f52c36..1d73dc2 100644
--- a/src/submodule.c
+++ b/src/submodule.c
@@ -89,9 +89,11 @@ __KHASH_IMPL(
 
 static int submodule_alloc(git_submodule **out, git_repository *repo, const char *name);
 static git_config_backend *open_gitmodules(git_repository *repo, int gitmod);
+static git_config *gitmodules_snapshot(git_repository *repo);
 static int get_url_base(git_buf *url, git_repository *repo);
 static int lookup_head_remote_key(git_buf *remote_key, git_repository *repo);
-static int submodule_load_from_config(const git_config_entry *, void *);
+static int submodule_load_each(const git_config_entry *entry, void *payload);
+static int submodule_read_config(git_submodule *sm, git_config *cfg);
 static int submodule_load_from_wd_lite(git_submodule *);
 static void submodule_get_index_status(unsigned int *, git_submodule *);
 static void submodule_get_wd_status(unsigned int *, git_submodule *, git_repository *, git_submodule_ignore_t);
@@ -144,6 +146,41 @@ static int find_by_path(const git_config_entry *entry, void *payload)
 	return 0;
 }
 
+/**
+ * Find out the name of a submodule from its path
+ */
+static int name_from_path(git_buf *out, git_config *cfg, const char *path)
+{
+	const char *key = "submodule\\..*\\.path";
+	git_config_iterator *iter;
+	git_config_entry *entry;
+	int error;
+
+	if ((error = git_config_iterator_glob_new(&iter, cfg, key)) < 0)
+		return error;
+
+	while ((error = git_config_next(&entry, iter)) == 0) {
+		const char *fdot, *ldot;
+		/* TODO: this should maybe be strcasecmp on a case-insensitive fs */
+		if (strcmp(path, entry->value) != 0)
+			continue;
+
+		fdot = strchr(entry->name, '.');
+		ldot = strrchr(entry->name, '.');
+
+		git_buf_clear(out);
+		git_buf_put(out, fdot + 1, ldot - fdot - 1);
+		return 0;
+	}
+
+	if (error == GIT_ITEROVER) {
+		giterr_set(GITERR_SUBMODULE, "could not find a submodule name for '%s'", path);
+		error = GIT_ENOTFOUND;
+	}
+
+	return error;
+}
+
 int git_submodule_lookup(
 	git_submodule **out, /* NULL if user only wants to test existence */
 	git_repository *repo,
@@ -280,11 +317,12 @@ done:
 	return 0;
 }
 
-static int submodules_from_index(git_strmap *map, git_index *idx)
+static int submodules_from_index(git_strmap *map, git_index *idx, git_config *cfg)
 {
        int error;
        git_iterator *i;
        const git_index_entry *entry;
+       git_buf name = GIT_BUF_INIT;
 
        if ((error = git_iterator_for_index(&i, idx, NULL)) < 0)
                return error;
@@ -293,6 +331,11 @@ static int submodules_from_index(git_strmap *map, git_index *idx)
                khiter_t pos = git_strmap_lookup_index(map, entry->path);
                git_submodule *sm;
 
+	       git_buf_clear(&name);
+	       if (!name_from_path(&name, cfg, entry->path)) {
+		       git_strmap_lookup_index(map, name.ptr);
+	       }
+
                if (git_strmap_valid_index(map, pos)) {
                        sm = git_strmap_value_at(map, pos);
 
@@ -301,7 +344,7 @@ static int submodules_from_index(git_strmap *map, git_index *idx)
                        else
                                sm->flags |= GIT_SUBMODULE_STATUS__INDEX_NOT_SUBMODULE;
                } else if (S_ISGITLINK(entry->mode)) {
-                       if (!submodule_get_or_create(&sm, git_index_owner(idx), map, entry->path)) {
+                       if (!submodule_get_or_create(&sm, git_index_owner(idx), map, name.ptr ? name.ptr : entry->path)) {
                                submodule_update_from_index_entry(sm, entry);
                                git_submodule_free(sm);
                        }
@@ -311,16 +354,18 @@ static int submodules_from_index(git_strmap *map, git_index *idx)
        if (error == GIT_ITEROVER)
                error = 0;
 
+       git_buf_free(&name);
        git_iterator_free(i);
 
        return error;
 }
 
-static int submodules_from_head(git_strmap *map, git_tree *head)
+static int submodules_from_head(git_strmap *map, git_tree *head, git_config *cfg)
 {
        int error;
        git_iterator *i;
        const git_index_entry *entry;
+       git_buf name = GIT_BUF_INIT;
 
        if ((error = git_iterator_for_tree(&i, head, NULL)) < 0)
                return error;
@@ -329,6 +374,11 @@ static int submodules_from_head(git_strmap *map, git_tree *head)
                khiter_t pos = git_strmap_lookup_index(map, entry->path);
                git_submodule *sm;
 
+	       git_buf_clear(&name);
+	       if (!name_from_path(&name, cfg, entry->path)) {
+		       git_strmap_lookup_index(map, name.ptr);
+	       }
+
                if (git_strmap_valid_index(map, pos)) {
                        sm = git_strmap_value_at(map, pos);
 
@@ -337,7 +387,7 @@ static int submodules_from_head(git_strmap *map, git_tree *head)
                        else
                                sm->flags |= GIT_SUBMODULE_STATUS__HEAD_NOT_SUBMODULE;
                } else if (S_ISGITLINK(entry->mode)) {
-                       if (!submodule_get_or_create(&sm, git_tree_owner(head), map, entry->path)) {
+                       if (!submodule_get_or_create(&sm, git_tree_owner(head), map, name.ptr ? name.ptr : entry->path)) {
                                submodule_update_from_head_data(
                                        sm, entry->mode, &entry->id);
                                git_submodule_free(sm);
@@ -348,6 +398,7 @@ static int submodules_from_head(git_strmap *map, git_tree *head)
        if (error == GIT_ITEROVER)
                error = 0;
 
+       git_buf_free(&name);
        git_iterator_free(i);
 
        return error;
@@ -355,8 +406,7 @@ static int submodules_from_head(git_strmap *map, git_tree *head)
 
 /* If have_sm is true, sm is populated, otherwise map an repo are. */
 typedef struct {
-	int have_sm;
-	git_submodule *sm;
+	git_config *mods;
 	git_strmap *map;
 	git_repository *repo;
 } lfc_data;
@@ -369,7 +419,7 @@ static int all_submodules(git_repository *repo, git_strmap *map)
 	const char *wd = NULL;
 	git_buf path = GIT_BUF_INIT;
 	git_submodule *sm;
-	git_config_backend *mods = NULL;
+	git_config *mods = NULL;
 	uint32_t mask;
 
 	assert(repo && map);
@@ -400,24 +450,28 @@ static int all_submodules(git_repository *repo, git_strmap *map)
 			GIT_SUBMODULE_STATUS__WD_FLAGS |
 			GIT_SUBMODULE_STATUS__WD_OID_VALID;
 
+	/* add submodule information from .gitmodules */
+	if (wd) {
+		lfc_data data = { 0 };
+		data.map = map;
+		data.repo = repo;
+
+		if ((mods = gitmodules_snapshot(repo)) == NULL)
+			goto cleanup;
+
+		data.mods = mods;
+		if ((error = git_config_foreach(
+			    mods, submodule_load_each, &data)) < 0)
+			goto cleanup;
+	}
 	/* add back submodule information from index */
 	if (idx) {
-		if ((error = submodules_from_index(map, idx)) < 0)
+		if ((error = submodules_from_index(map, idx, mods)) < 0)
 			goto cleanup;
 	}
 	/* add submodule information from HEAD */
 	if (head) {
-		if ((error = submodules_from_head(map, head)) < 0)
-			goto cleanup;
-	}
-	/* add submodule information from .gitmodules */
-	if (wd) {
-		lfc_data data = { 0 };
-		data.map = map;
-		data.repo = repo;
-		if ((mods = open_gitmodules(repo, false)) != NULL &&
-		    (error = git_config_file_foreach(
-			    mods, submodule_load_from_config, &data)) < 0)
+		if ((error = submodules_from_head(map, head, mods)) < 0)
 			goto cleanup;
 	}
 	/* shallow scan submodules in work tree as needed */
@@ -428,7 +482,7 @@ static int all_submodules(git_repository *repo, git_strmap *map)
 	}
 
 cleanup:
-	git_config_file_free(mods);
+	git_config_free(mods);
 	/* TODO: if we got an error, mark submodule config as invalid? */
 	git_index_free(idx);
 	git_tree_free(head);
@@ -1370,8 +1424,7 @@ static int submodule_update_head(git_submodule *submodule)
 int git_submodule_reload(git_submodule *sm, int force)
 {
 	int error = 0;
-	git_config_backend *mods;
-	lfc_data data = { 0 };
+	git_config *mods;
 
 	GIT_UNUSED(force);
 
@@ -1390,28 +1443,14 @@ int git_submodule_reload(git_submodule *sm, int force)
 		return error;
 
 	/* refresh config data */
-	mods = open_gitmodules(sm->repo, GITMODULES_EXISTING);
+	mods = gitmodules_snapshot(sm->repo);
 	if (mods != NULL) {
-		git_buf path = GIT_BUF_INIT;
-
-		git_buf_sets(&path, "submodule\\.");
-		git_buf_text_puts_escape_regex(&path, sm->name);
-		git_buf_puts(&path, "\\..*");
-
-		if (git_buf_oom(&path)) {
-			error = -1;
-		} else {
-			data.have_sm = 1;
-			data.sm = sm;
-			error = git_config_file_foreach_match(
-				mods, path.ptr, submodule_load_from_config, &data);
-		}
+		error = submodule_read_config(sm, mods);
+		git_config_free(mods);
 
-		git_buf_free(&path);
-		git_config_file_free(mods);
-
-		if (error < 0)
+		if (error < 0) {
 			return error;
+		}
 	}
 
 	/* refresh wd data */
@@ -1627,134 +1666,141 @@ int git_submodule_parse_recurse(git_submodule_recurse_t *out, const char *value)
 	return 0;
 }
 
-static int submodule_load_from_config(
-	const git_config_entry *entry, void *payload)
+static int get_value(const char **out, git_config *cfg, git_buf *buf, const char *name, const char *field)
 {
-	const char *namestart, *property;
-	const char *key = entry->name, *value = entry->value, *path;
-	char *alternate = NULL, *replaced = NULL;
-	git_buf name = GIT_BUF_INIT;
-	lfc_data *data = payload;
-	git_submodule *sm;
-	int error = 0;
-
-	if (git__prefixcmp(key, "submodule.") != 0)
-		return 0;
-
-	namestart = key + strlen("submodule.");
-	property  = strrchr(namestart, '.');
-
-	if (!property || (property == namestart))
-		return 0;
-
-	property++;
-	path = !strcasecmp(property, "path") ? value : NULL;
+	int error;
 
-	if ((error = git_buf_set(&name, namestart, property - namestart -1)) < 0)
-		goto done;
+	git_buf_clear(buf);
 
-	if (data->have_sm) {
-		sm = data->sm;
-	} else {
-		khiter_t pos;
-		git_strmap *map = data->map;
-		pos = git_strmap_lookup_index(map, path ? path : name.ptr);
-		if (git_strmap_valid_index(map, pos)) {
-			sm = git_strmap_value_at(map, pos);
-		} else {
-			if ((error = submodule_alloc(&sm, data->repo, name.ptr)) < 0)
-				goto done;
+	if ((error = git_buf_printf(buf, "submodule.%s.%s", name, field)) < 0 ||
+	    (error = git_config_get_string(out, cfg, buf->ptr)) < 0)
+		return error;
 
-			git_strmap_insert(map, sm->name, sm, error);
-			assert(error != 0);
-			if (error < 0)
-				goto done;
-			error = 0;
-		}
-	}
+	return error;
+}
 
-	sm->flags |= GIT_SUBMODULE_STATUS_IN_CONFIG;
+static int submodule_read_config(git_submodule *sm, git_config *cfg)
+{
+	git_buf key = GIT_BUF_INIT;
+	const char *value;
+	int error, in_config = 0;
 
-	/* Only from config might we get differing names & paths.  If so, then
-	 * update the submodule and insert under the alternative key.
+	/*
+	 * TODO: Look up path in index and if it is present but not a GITLINK
+	 * then this should be deleted (at least to match git's behavior)
 	 */
 
-	/* TODO: if case insensitive filesystem, then the following strcmps
+	if ((error = get_value(&value, cfg, &key, sm->name, "path")) == 0) {
+		in_config = 1;
+	/*
+	 * TODO: if case insensitive filesystem, then the following strcmp
 	 * should be strcasecmp
 	 */
-
-	if (strcmp(sm->name, name.ptr) != 0) { /* name changed */
-		if (sm->path && !strcmp(sm->path, name.ptr)) { /* already set as path */
-			replaced = sm->name;
-			sm->name = sm->path;
-		} else {
-			if (sm->name != sm->path)
-				replaced = sm->name;
-			alternate = sm->name = git_buf_detach(&name);
-		}
-	}
-	else if (path && strcmp(path, sm->path) != 0) { /* path changed */
-		if (!strcmp(sm->name, value)) { /* already set as name */
-			replaced = sm->path;
-			sm->path = sm->name;
-		} else {
-			if (sm->path != sm->name)
-				replaced = sm->path;
-			if ((alternate = git__strdup(value)) == NULL) {
-				error = -1;
-				goto done;
-			}
-			sm->path = alternate;
+		if (strcmp(sm->name, value) != 0) {
+			sm->path = git__strdup(value);
+			GITERR_CHECK_ALLOC(sm->path);
 		}
+	} else if (error != GIT_ENOTFOUND) {
+		return error;
 	}
 
-	/* Deregister under name being replaced */
-	if (replaced) {
-		git__free(replaced);
+	if ((error = get_value(&value, cfg, &key, sm->name, "url")) == 0) {
+		in_config = 1;
+		sm->url = git__strdup(value);
+		GITERR_CHECK_ALLOC(sm->url);
+	} else if (error != GIT_ENOTFOUND) {
+		return error;
 	}
 
-	/* TODO: Look up path in index and if it is present but not a GITLINK
-	 * then this should be deleted (at least to match git's behavior)
-	 */
-
-	if (path)
-		goto done;
-
-	/* copy other properties into submodule entry */
-	if (strcasecmp(property, "url") == 0) {
-		git__free(sm->url);
-		sm->url = NULL;
-
-		if (value != NULL && (sm->url = git__strdup(value)) == NULL) {
-			error = -1;
-			goto done;
-		}
+	if ((error = get_value(&value, cfg, &key, sm->name, "branch")) == 0) {
+		in_config = 1;
+		sm->branch = git__strdup(value);
+		GITERR_CHECK_ALLOC(sm->branch);
+	} else if (error != GIT_ENOTFOUND) {
+		return error;
 	}
-	else if (strcasecmp(property, "branch") == 0) {
-		git__free(sm->branch);
-		sm->branch = NULL;
 
-		if (value != NULL && (sm->branch = git__strdup(value)) == NULL) {
-			error = -1;
-			goto done;
-		}
-	}
-	else if (strcasecmp(property, "update") == 0) {
+	if ((error = get_value(&value, cfg, &key, sm->name, "update")) == 0) {
+		in_config = 1;
 		if ((error = git_submodule_parse_update(&sm->update, value)) < 0)
-			goto done;
+			return error;
 		sm->update_default = sm->update;
+	} else if (error != GIT_ENOTFOUND) {
+		return error;
 	}
-	else if (strcasecmp(property, "fetchRecurseSubmodules") == 0) {
+
+	if ((error = get_value(&value, cfg, &key, sm->name, "fetchRecurseSubmodules")) == 0) {
+		in_config = 1;
 		if ((error = git_submodule_parse_recurse(&sm->fetch_recurse, value)) < 0)
-			goto done;
+			return error;
 		sm->fetch_recurse_default = sm->fetch_recurse;
+	} else if (error != GIT_ENOTFOUND) {
+		return error;
 	}
-	else if (strcasecmp(property, "ignore") == 0) {
+
+	if ((error = get_value(&value, cfg, &key, sm->name, "ignore")) == 0) {
+		in_config = 1;
 		if ((error = git_submodule_parse_ignore(&sm->ignore, value)) < 0)
-			goto done;
+			return error;
 		sm->ignore_default = sm->ignore;
+	} else if (error != GIT_ENOTFOUND) {
+		return error;
 	}
-	/* ignore other unknown submodule properties */
+
+	if (in_config)
+		sm->flags |= GIT_SUBMODULE_STATUS_IN_CONFIG;
+
+	return 0;
+}
+
+static int submodule_load_each(const git_config_entry *entry, void *payload)
+{
+	lfc_data *data = payload;
+	const char *namestart, *property;
+	git_strmap_iter pos;
+	git_strmap *map = data->map;
+	git_buf name = GIT_BUF_INIT;
+	git_submodule *sm;
+	int error;
+
+	if (git__prefixcmp(entry->name, "submodule.") != 0)
+		return 0;
+
+	namestart = entry->name + strlen("submodule.");
+	property  = strrchr(namestart, '.');
+
+	if (!property || (property == namestart))
+		return 0;
+
+	property++;
+
+	if ((error = git_buf_set(&name, namestart, property - namestart -1)) < 0)
+		return error;
+
+	/*
+	 * Now that we have the submodule's name, we can use that to
+	 * figure out whether it's in the map. If it's not, we create
+	 * a new submodule, load the config and insert it. If it's
+	 * already inserted, we've already loaded it, so we skip.
+	 */
+	pos = git_strmap_lookup_index(map, name.ptr);
+	if (git_strmap_valid_index(map, pos))
+		return 0;
+
+	if ((error = submodule_alloc(&sm, data->repo, name.ptr)) < 0)
+		goto done;
+
+	if ((error = submodule_read_config(sm, data->mods)) < 0) {
+		git_submodule_free(sm);
+		goto done;
+	}
+
+	git_strmap_insert(map, sm->name, sm, error);
+	assert(error != 0);
+	if (error < 0)
+		goto done;
+
+	error = 0;
 
 done:
 	git_buf_free(&name);
@@ -1778,6 +1824,35 @@ static int submodule_load_from_wd_lite(git_submodule *sm)
 	return 0;
 }
 
+/**
+ * Returns a snapshot of $WORK_TREE/.gitmodules.
+ *
+ * We ignore any errors and just pretend the file isn't there.
+ */
+static git_config *gitmodules_snapshot(git_repository *repo)
+{
+	const char *workdir = git_repository_workdir(repo);
+	git_config *mods = NULL, *snap = NULL;
+	git_buf path = GIT_BUF_INIT;
+
+	if (workdir != NULL) {
+		if (git_buf_joinpath(&path, workdir, GIT_MODULES_FILE) != 0)
+			return NULL;
+
+		if (git_config_open_ondisk(&mods, path.ptr) < 0)
+			mods = NULL;
+	}
+
+	git_buf_free(&path);
+
+	if (mods) {
+		git_config_snapshot(&snap, mods);
+		git_config_free(mods);
+	}
+
+	return snap;
+}
+
 static git_config_backend *open_gitmodules(
 	git_repository *repo,
 	int okay_to_create)
diff --git a/tests/submodule/lookup.c b/tests/submodule/lookup.c
index ecea694..5f16148 100644
--- a/tests/submodule/lookup.c
+++ b/tests/submodule/lookup.c
@@ -333,3 +333,58 @@ void test_submodule_lookup__prefix_name(void)
 
 	git_submodule_free(sm);
 }
+
+void test_submodule_lookup__renamed(void)
+{
+	const char *newpath = "sm_actually_changed";
+	git_index *idx;
+	sm_lookup_data data;
+
+	cl_git_pass(git_repository_index__weakptr(&idx, g_repo));
+
+	/* We're replicating 'git mv sm_unchanged sm_actually_changed' in this test */
+
+	cl_git_pass(p_rename("submod2/sm_unchanged", "submod2/sm_actually_changed"));
+
+	/* Change the path in .gitmodules and stage it*/
+	{
+		git_config *cfg;
+
+		cl_git_pass(git_config_open_ondisk(&cfg, "submod2/.gitmodules"));
+		cl_git_pass(git_config_set_string(cfg, "submodule.sm_unchanged.path", newpath));
+		git_config_free(cfg);
+
+		cl_git_pass(git_index_add_bypath(idx, ".gitmodules"));
+	}
+
+	/* Change the worktree info in the the submodule's config */
+	{
+		git_config *cfg;
+
+		cl_git_pass(git_config_open_ondisk(&cfg, "submod2/.git/modules/sm_unchanged/config"));
+		cl_git_pass(git_config_set_string(cfg, "core.worktree", "../../../sm_actually_changed"));
+		git_config_free(cfg);
+	}
+
+	/* Rename the entry in the index */
+	{
+		const git_index_entry *e;
+		git_index_entry entry = { 0 };
+
+		e = git_index_get_bypath(idx, "sm_unchanged", 0);
+		cl_assert(e);
+		cl_assert_equal_i(GIT_FILEMODE_COMMIT, e->mode);
+
+		entry.path = newpath;
+		entry.mode = GIT_FILEMODE_COMMIT;
+		git_oid_cpy(&entry.id, &e->id);
+
+		cl_git_pass(git_index_remove(idx, "sm_unchanged", 0));
+		cl_git_pass(git_index_add(idx, &entry));
+		cl_git_pass(git_index_write(idx));
+	}
+
+	memset(&data, 0, sizeof(data));
+	cl_git_pass(git_submodule_foreach(g_repo, sm_lookup_cb, &data));
+	cl_assert_equal_i(8, data.count);
+}