Commit 036813ee25618d0c6e72887d36846d41422f4d46

Stefan Sperling 2019-05-08T12:17:01

more progress on commits: write trees recursively

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
diff --git a/lib/worktree.c b/lib/worktree.c
index 48514a2..cd9ee78 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -2219,20 +2219,20 @@ free_commitable(struct commitable *ct)
 }
 
 struct collect_commitables_arg {
-	struct got_pathlist_head *paths;
+	struct got_pathlist_head *commitable_paths;
 	struct got_repository *repo;
 	struct got_worktree *worktree;
 };
 
 static const struct got_error *
-collect_commitables(void *arg, unsigned char status, const char *path,
+collect_commitables(void *arg, unsigned char status, const char *relpath,
     struct got_object_id *id)
 {
 	struct collect_commitables_arg *a = arg;
 	const struct got_error *err = NULL;
 	struct commitable *ct = NULL;
 	struct got_pathlist_entry *new = NULL;
-	char *parent_path = NULL;
+	char *parent_path = NULL, *path = NULL;
 
 	if (status == GOT_STATUS_CONFLICT)
 		return got_error(GOT_ERR_COMMIT_CONFLICT);
@@ -2241,8 +2241,12 @@ collect_commitables(void *arg, unsigned char status, const char *path,
 	    status != GOT_STATUS_DELETE)
 		return NULL;
 
-	if (strchr(path, '/') == NULL) {
-		parent_path = strdup("/");
+	if (asprintf(&path, "/%s", relpath) == -1) {
+		err = got_error_from_errno();
+		goto done;
+	}
+	if (strcmp(path, "/") == 0) {
+		parent_path = strdup("");
 		if (parent_path == NULL)
 			return got_error_from_errno();
 	} else {
@@ -2251,18 +2255,20 @@ collect_commitables(void *arg, unsigned char status, const char *path,
 			return err;
 	}
 
-	ct = malloc(sizeof(*ct));
+	ct = calloc(1, sizeof(*ct));
 	if (ct == NULL) {
 		err = got_error_from_errno();
 		goto done;
 	}
 
 	ct->status = status;
-	ct->id = NULL;
-	ct->base_id = got_object_id_dup(id);
-	if (ct->base_id == NULL) {
-		err = got_error_from_errno();
-		goto done;
+	ct->id = NULL; /* will be filled in when blob gets created */
+	if (ct->status != GOT_STATUS_ADD) {
+		ct->base_id = got_object_id_dup(id);
+		if (ct->base_id == NULL) {
+			err = got_error_from_errno();
+			goto done;
+		}
 	}
 	err = got_object_id_by_path(&ct->tree_id, a->repo,
 	    a->worktree->base_commit_id, parent_path);
@@ -2273,129 +2279,298 @@ collect_commitables(void *arg, unsigned char status, const char *path,
 		err = got_error_from_errno();
 		goto done;
 	}
-	err = got_pathlist_insert(&new, a->paths, ct->path, ct);
+	err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
 done:
 	if (ct && (err || new == NULL))
 		free_commitable(ct);
 	free(parent_path);
+	free(path);
 	return err;
 }
 
-struct write_tree_arg {
-	struct got_pathlist_head *commitable_paths;
-	struct got_object_idset *affected_trees;
-	struct got_repository *repo;
-};
+static const struct got_error *write_tree(struct got_object_id **,
+    struct got_tree_object *, const char *, struct got_pathlist_head *,
+    struct got_repository *);
+
+static const struct got_error *
+write_subtree(struct got_object_id **new_subtree_id,
+    struct got_tree_entry *te, const char *parent_path,
+    struct got_pathlist_head *commitable_paths, struct got_repository *repo)
+{
+	const struct got_error *err = NULL;
+	struct got_tree_object *subtree;
+	char *subpath;
+
+	if (asprintf(&subpath, "%s%s%s", parent_path,
+	    parent_path[0] == '\0' ? "" : "/", te->name) == -1)
+		return got_error_from_errno();
+
+	err = got_object_open_as_tree(&subtree, repo, te->id);
+	if (err)
+		return err;
+
+	err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
+	    repo);
+	got_object_tree_close(subtree);
+	free(subpath);
+	return err;
+}
+
+static const struct got_error *
+match_ct_parent_path(int *match, struct commitable *ct, const char *path)
+{
+	const struct got_error *err = NULL;
+	char *ct_parent_path = NULL;
+
+	*match = 0;
+
+	if (strchr(ct->path, '/') == NULL) {
+		ct_parent_path = strdup("/");
+		if (ct_parent_path == NULL)
+			return got_error_from_errno();
+	} else {
+		err = got_path_dirname(&ct_parent_path, ct->path);
+		if (err)
+			return err;
+	}
+
+	*match = (strcmp(path, ct_parent_path) == 0);
+	free(ct_parent_path);
+	return err;
+}
+
+static const struct got_error *
+alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
+    struct got_tree_entry *te, struct commitable *ct)
+{
+	const struct got_error *err = NULL;
+
+	*new_te = NULL;
+
+	err = got_object_tree_entry_dup(new_te, te);
+	if (err)
+		goto done;
+
+	/* XXX TODO: update mode from disk (derive from ct?)! */
+	(*new_te)->mode = GOT_DEFAULT_FILE_MODE;
+
+	free((*new_te)->id);
+	(*new_te)->id = got_object_id_dup(ct->id);
+	if ((*new_te)->id == NULL) {
+		err = got_error_from_errno();
+		goto done;
+	}
+done:
+	if (err && *new_te) {
+		got_object_tree_entry_close(*new_te);
+		*new_te = NULL;
+	}
+	return err;
+}
 
 static const struct got_error *
-write_tree(struct got_object_id *base_tree_id, void *data, void *arg)
+alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
+    struct commitable *ct)
+{
+	const struct got_error *err = NULL;
+	char *ct_name;
+
+	 *new_te = NULL;
+
+	*new_te = calloc(1, sizeof(*new_te));
+	if (*new_te == NULL)
+		return got_error_from_errno();
+
+	ct_name = basename(ct->path);
+	if (ct_name == NULL) {
+		err = got_error_from_errno();
+		goto done;
+	}
+	(*new_te)->name = strdup(ct_name);
+	if ((*new_te)->name == NULL) {
+		err = got_error_from_errno();
+		goto done;
+	}
+
+	/* XXX TODO: update mode from disk (derive from ct?)! */
+	(*new_te)->mode = GOT_DEFAULT_FILE_MODE;
+
+	(*new_te)->id = got_object_id_dup(ct->id);
+	if ((*new_te)->id == NULL) {
+		err = got_error_from_errno();
+		goto done;
+	}
+done:
+	if (err && *new_te) {
+		got_object_tree_entry_close(*new_te);
+		*new_te = NULL;
+	}
+	return err;
+}
+
+static const struct got_error *
+insert_tree_entry(struct got_tree_entry *new_te,
+    struct got_pathlist_head *paths)
+{
+	const struct got_error *err = NULL;
+	struct got_pathlist_entry *new_pe;
+
+	err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
+	if (err)
+		return err;
+	if (new_pe == NULL)
+		return got_error(GOT_ERR_TREE_DUP_ENTRY);
+	return NULL;
+}
+
+static const struct got_error *
+match_deleted_or_modified_ct(struct commitable **ctp,
+    struct got_tree_entry *te, const char *base_tree_path,
+    struct got_pathlist_head *commitable_paths)
+{
+	const struct got_error *err = NULL;
+	struct got_pathlist_entry *pe;
+
+	*ctp = NULL;
+
+	TAILQ_FOREACH(pe, commitable_paths, entry) {
+		struct commitable *ct = pe->data;
+		char *ct_name = NULL;
+		int path_matches;
+
+		if (ct->status != GOT_STATUS_MODIFY &&
+		    ct->status != GOT_STATUS_DELETE)
+			continue;
+
+		if (got_object_id_cmp(ct->base_id, te->id) != 0)
+			continue;
+
+		 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
+		 if (err)
+			return err;
+		if (!path_matches)
+			continue;
+
+		ct_name = basename(pe->path);
+		if (ct_name == NULL)
+			return got_error_from_errno();
+
+		if (strcmp(te->name, ct_name) != 0)
+			continue;
+
+		*ctp = ct;
+		break;
+	}
+
+	return err;
+}
+
+static const struct got_error *
+write_tree(struct got_object_id **new_tree_id,
+    struct got_tree_object *base_tree, const char *path_base_tree,
+    struct got_pathlist_head *commitable_paths,
+    struct got_repository *repo)
 {
 	const struct got_error *err = NULL;
-	struct write_tree_arg *a = arg;
 	const struct got_tree_entries *base_entries = NULL;
-	struct got_pathlist_head new_entries;
+	struct got_pathlist_head paths;
 	struct got_tree_entries new_tree_entries;
-	struct got_tree_object *base_tree = NULL;
 	struct got_tree_entry *te;
 	struct got_pathlist_entry *pe;
-	struct got_object_id *new_tree_id = NULL;
 
-	TAILQ_INIT(&new_entries);
+	TAILQ_INIT(&paths);
 	new_tree_entries.nentries = 0;
 	SIMPLEQ_INIT(&new_tree_entries.head);
 
-	err = got_object_open_as_tree(&base_tree, a->repo, base_tree_id);
-	if (err)
-		return err;
-
-	base_entries = got_object_tree_get_entries(base_tree);
+	/* Insert, and recurse into, newly added entries first. */
+	TAILQ_FOREACH(pe, commitable_paths, entry) {
+		struct commitable *ct = pe->data;
+		char *child_path = NULL;
 
-	SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
-		struct got_tree_entry *new_te = NULL;
-		struct got_pathlist_entry *new_pe = NULL;
-		int ct_found = 0;
-		TAILQ_FOREACH(pe, a->commitable_paths, entry) {
-			struct commitable *ct = NULL;
-			char *ct_name = NULL;
+		if (ct->status != GOT_STATUS_ADD)
+			continue;
 
-			ct = pe->data;
+		 printf("pe->path='%s'\n", pe->path);
+		 printf("path_base_tree='%s'\n", path_base_tree);
+		 if (!got_path_is_child(pe->path, path_base_tree,
+		     strlen(path_base_tree))) {
+			printf("not a child\n");
+			continue;
+		}
 
-			if (got_object_id_cmp(ct->base_id, te->id) != 0)
-				continue; /* not part of this tree */
+		err = got_path_skip_common_ancestor(&child_path, path_base_tree,
+		    pe->path);
+		if (err)
+			goto done;
 
-			ct_name = basename(pe->path);
-			if (ct_name == NULL) {
-				err = got_error_from_errno();
+		if (strchr(child_path, '/') == NULL) {
+			struct got_tree_entry *new_te;
+			err = alloc_added_blob_tree_entry(&new_te, ct);
+			if (err)
 				goto done;
-			}
-			/* Commitable and tree entry must correspond. */
-			if (strcmp(te->name, ct_name) != 0)
-				continue;
+			err = insert_tree_entry(new_te, &paths);
+			if (err)
+				goto done;
+		} else {
+			/* TODO: Create subtree and insert element for it. */
+		}
+	}
 
-			ct_found = 1;
+	/* Handle modified and deleted entries. */
+	base_entries = got_object_tree_get_entries(base_tree);
+	SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
+		struct got_tree_entry *new_te = NULL;
+		struct commitable *ct = NULL;
 
-			if (ct->status == GOT_STATUS_DELETE) {
-				/* Deleted entries disappear. */
-				break;
-			}
+		if (S_ISDIR(te->mode)) {
+			err = got_object_tree_entry_dup(&new_te, te);
+			if (err)
+				goto done;
+			free(new_te->id);
+			err = write_subtree(&new_te->id, te, path_base_tree,
+			    commitable_paths, repo);
+			if (err)
+				goto done;
+			err = insert_tree_entry(new_te, &paths);
+			if (err)
+				goto done;
+			continue;
+		}
 
-			/* Modified entries get updated mode and ID. */
+		err = match_deleted_or_modified_ct(&ct, te, path_base_tree,
+		    commitable_paths);
+		if (ct) {
+			/* NB: Deleted entries get dropped here. */
 			if (ct->status == GOT_STATUS_MODIFY) {
-				err = got_object_tree_entry_dup(&new_te, te);
+				err = alloc_modified_blob_tree_entry(&new_te,
+				    te, ct);
 				if (err)
 					goto done;
-				free(new_te->id);
-			} else if (ct->status == GOT_STATUS_ADD) {
-				/* Added entries get... well, added. */
-				new_te = calloc(1, sizeof(*new_te));
-				if (new_te == NULL) {
-					err = got_error_from_errno();
-					goto done;
-				}
-				new_te->name = strdup(ct_name);
-				if (new_te->name == NULL) {
-					err = got_error_from_errno();
+				err = insert_tree_entry(new_te, &paths);
+				if (err)
 					goto done;
-				}
 			}
-			new_te->mode = GOT_DEFAULT_FILE_MODE; /* XXX */
-			new_te->id = got_object_id_dup(ct->id);
-			if (new_te->id == NULL) {
-				err = got_error_from_errno();
-				goto done;
-			}
-			break;
-		}
-
-		if (new_te == NULL) {
-			if (ct_found) /* GOT_STATUS_DELETE */
-				continue;
+		} else {
+			/* Entry is unchanged; just copy it. */
 			err = got_object_tree_entry_dup(&new_te, te);
 			if (err)
 				goto done;
-		}
-
-		err = got_pathlist_insert(&new_pe, &new_entries,
-		    new_te->name, new_te);
-		if (err)
-			goto done;
-		if (new_pe == NULL) {
-			err = got_error(GOT_ERR_TREE_DUP_ENTRY);
-			goto done;
+			err = insert_tree_entry(new_te, &paths);
+			if (err)
+				goto done;
 		}
 	}
 
-	TAILQ_FOREACH(pe, &new_entries, entry) {
+	/* Write new list of entries; deleted entries have been dropped. */
+	TAILQ_FOREACH(pe, &paths, entry) {
 		struct got_tree_entry *te = pe->data;
 		new_tree_entries.nentries++;
 		SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
 	}
-
-	err = got_object_tree_create(&new_tree_id, &new_tree_entries, a->repo);
+	err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
 done:
-	free(new_tree_id);
 	got_object_tree_entries_close(&new_tree_entries);
-	got_pathlist_free(&new_entries);
+	got_pathlist_free(&paths);
 	if (base_tree)
 		got_object_tree_close(base_tree);
 	return err;
@@ -2408,17 +2583,16 @@ got_worktree_commit(struct got_object_id **new_commit_id,
 {
 	const struct got_error *err = NULL, *unlockerr = NULL;
 	struct collect_commitables_arg cc_arg;
-	struct write_tree_arg wt_arg;
-	struct got_pathlist_head paths;
+	struct got_pathlist_head commitable_paths;
 	struct got_pathlist_entry *pe;
 	char *relpath = NULL;
 	struct got_commit_object *base_commit = NULL;
 	struct got_tree_object *base_tree = NULL;
-	struct got_object_idset *tree_ids = NULL;
+	struct got_object_id *new_tree_id = NULL;
 
 	*new_commit_id = NULL;
 
-	TAILQ_INIT(&paths);
+	TAILQ_INIT(&commitable_paths);
 
 	if (ondisk_path) {
 		err = got_path_skip_common_ancestor(&relpath,
@@ -2427,15 +2601,11 @@ got_worktree_commit(struct got_object_id **new_commit_id,
 			return err;
 	}
 
-	tree_ids = got_object_idset_alloc();
-	if (tree_ids == NULL)
-		return got_error_from_errno();
-
 	err = lock_worktree(worktree, LOCK_EX);
 	if (err)
 		goto done;
 
-	cc_arg.paths = &paths;
+	cc_arg.commitable_paths = &commitable_paths;
 	cc_arg.worktree = worktree;
 	cc_arg.repo = repo;
 	err = got_worktree_status(worktree, relpath ? relpath : "",
@@ -2445,20 +2615,8 @@ got_worktree_commit(struct got_object_id **new_commit_id,
 
 	/* TODO: collect commit message if not specified */
 
-	/* Collect IDs of affected leaf trees. */
-	TAILQ_FOREACH(pe, &paths, entry) {
-		struct commitable *ct = pe->data;
-
-		if (got_object_idset_contains(tree_ids, ct->tree_id))
-			continue;
-
-		err = got_object_idset_add(tree_ids, ct->tree_id, NULL);
-		if (err)
-			goto done;
-	}
-
 	/* Create blobs from added and modified files and record their IDs. */
-	TAILQ_FOREACH(pe, &paths, entry) {
+	TAILQ_FOREACH(pe, &commitable_paths, entry) {
 		struct commitable *ct = pe->data;
 		char *ondisk_path;
 
@@ -2477,38 +2635,34 @@ got_worktree_commit(struct got_object_id **new_commit_id,
 			goto done;
 	}
 
-	/* Write new leaf tree objects. */
-	wt_arg.commitable_paths = &paths;
-	wt_arg.affected_trees = got_object_idset_alloc();
-	if (wt_arg.affected_trees == NULL) {
-		err = got_error_from_errno();
-		goto done;
-	}
-	wt_arg.repo = repo;
-	err = got_object_idset_for_each(tree_ids, write_tree, &wt_arg);
-	if (err)
-		goto done;
-
 	err = got_object_open_as_commit(&base_commit, repo,
 	    worktree->base_commit_id);
 	if (err)
 		goto done;
-	err = got_object_open_as_tree(&base_tree, repo,
-	    base_commit->tree_id);
+	err = got_object_open_as_tree(&base_tree, repo, base_commit->tree_id);
+	if (err)
+		goto done;
+
+	/* Recursively write new tree objects. */
+	err = write_tree(&new_tree_id, base_tree, "/", &commitable_paths, repo);
 	if (err)
 		goto done;
+
+	/* TODO: Write new commit. */
+
 done:
 	unlockerr = lock_worktree(worktree, LOCK_SH);
 	if (unlockerr && err == NULL)
 		err = unlockerr;
-	TAILQ_FOREACH(pe, &paths, entry) {
+	TAILQ_FOREACH(pe, &commitable_paths, entry) {
 		struct commitable *ct = pe->data;
 		free_commitable(ct);
 	}
-	got_object_idset_free(tree_ids);
-	got_pathlist_free(&paths);
+	got_pathlist_free(&commitable_paths);
 	if (base_tree)
 		got_object_tree_close(base_tree);
+	if (base_commit)
+		got_object_commit_close(base_commit);
 	free(relpath);
 	return err;
 }
diff --git a/regress/cmdline/commit.sh b/regress/cmdline/commit.sh
index 0a36cee..f621ba0 100755
--- a/regress/cmdline/commit.sh
+++ b/regress/cmdline/commit.sh
@@ -19,6 +19,8 @@
 function test_commit_basic {
 	local testroot=`test_init commit_basic`
 
+	find $testroot/repo/.git/objects > /tmp/1
+
 	got checkout $testroot/repo $testroot/wt > /dev/null
 	ret="$?"
 	if [ "$ret" != "0" ]; then
@@ -36,6 +38,8 @@ function test_commit_basic {
 
 	(cd $testroot/wt && got commit -m 'test commit_basic' > $testroot/stdout)
 
+	find $testroot/repo/.git/objects > /tmp/2
+
 	local head_rev=`git_show_head $testroot/repo`
 	echo "M alpha" > $testroot/stdout.expected
 	echo "D beta" >> $testroot/stdout.expected