Commit 9d31a1d8a43396fe66a94b1110e395d134bebf3f

Stefan Sperling 2018-03-11T20:39:31

initial implementation of worktree checkout

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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
diff --git a/include/got_error.h b/include/got_error.h
index 282259e..e8a9594 100644
--- a/include/got_error.h
+++ b/include/got_error.h
@@ -43,6 +43,8 @@
 #define GOT_ERR_WORKTREE_META	27
 #define GOT_ERR_WORKTREE_VERS	28
 #define GOT_ERR_WORKTREE_BUSY	29
+#define GOT_ERR_DIR_OBSTRUCTED	30
+#define GOT_ERR_FILE_OBSTRUCTED	31
 
 static const struct got_error {
 	int code;
diff --git a/include/got_worktree.h b/include/got_worktree.h
index c872b92..3bfc191 100644
--- a/include/got_worktree.h
+++ b/include/got_worktree.h
@@ -23,4 +23,4 @@ void got_worktree_close(struct got_worktree *);
 char *got_worktree_get_repo_path(struct got_worktree *);
 char  *got_worktree_get_head_ref_name(struct got_worktree *);
 const struct got_error *got_worktree_checkout_files(struct got_worktree *,
-    struct got_repository *);
+    struct got_reference *, struct got_repository *);
diff --git a/lib/fileindex.c b/lib/fileindex.c
index 6430af1..85fdbb1 100644
--- a/lib/fileindex.c
+++ b/lib/fileindex.c
@@ -75,3 +75,44 @@ got_fileindex_entry_close(struct got_fileindex_entry *entry)
 	free(entry->path);
 	free(entry);
 }
+
+const struct got_error *
+got_fileindex_entry_add(struct got_fileindex *fileindex,
+    struct got_fileindex_entry *entry)
+{
+	/* TODO keep entries sorted by name */
+	TAILQ_INSERT_TAIL(&fileindex->entries, entry, entry);
+	fileindex->nentries++;
+	return NULL;
+}
+
+struct got_fileindex *
+got_fileindex_open(void)
+{
+	struct got_fileindex *fileindex;
+
+	fileindex = calloc(1, sizeof(*fileindex));
+	if (fileindex)
+		TAILQ_INIT(&fileindex->entries);
+	return fileindex;
+}
+
+void
+got_fileindex_close(struct got_fileindex *fileindex)
+{
+	struct got_fileindex_entry *entry;
+
+	while (!TAILQ_EMPTY(&fileindex->entries)) {
+		entry = TAILQ_FIRST(&fileindex->entries);
+		TAILQ_REMOVE(&fileindex->entries, entry, entry);
+		got_fileindex_entry_close(entry);
+		fileindex->nentries--;
+	}
+	free(fileindex);
+}
+
+const struct got_error *
+got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
+{
+	return NULL;
+}
diff --git a/lib/got_fileindex_lib.h b/lib/got_fileindex_lib.h
index d7cf180..546f7dd 100644
--- a/lib/got_fileindex_lib.h
+++ b/lib/got_fileindex_lib.h
@@ -84,3 +84,8 @@ struct got_fileindex_hdr {
 const struct got_error *got_fileindex_entry_open(struct got_fileindex_entry **,
     const char *, uint8_t *);
 void got_fileindex_entry_close(struct got_fileindex_entry *);
+struct got_fileindex *got_fileindex_open(void);
+void got_fileindex_close(struct got_fileindex *);
+const struct got_error *got_fileindex_write(struct got_fileindex *, FILE *);
+const struct got_error *got_fileindex_entry_add(struct got_fileindex *,
+    struct got_fileindex_entry *);
diff --git a/lib/got_path_lib.h b/lib/got_path_lib.h
index 977e6fa..53b63ef 100644
--- a/lib/got_path_lib.h
+++ b/lib/got_path_lib.h
@@ -37,3 +37,11 @@ char *got_path_normalize(const char *);
 /* Open a new temporary file for writing.
  * The file is not visible in the filesystem. */
 FILE *got_opentemp(void);
+
+/* Open a new temporary file for writing.
+ * The file is visible in the filesystem. */
+const struct got_error *got_opentemp_named(char **, FILE **, const char *);
+
+/* Count the number of path segments separated by '/'. */
+const struct got_error *
+got_path_segment_count(int *count, const char *path);
diff --git a/lib/path.c b/lib/path.c
index 021720b..9f0d700 100644
--- a/lib/path.c
+++ b/lib/path.c
@@ -20,6 +20,8 @@
 #include <stdio.h>
 #include <string.h>
 
+#include "got_error.h"
+
 #include "got_path_lib.h"
 
 int
@@ -61,6 +63,26 @@ got_path_normalize(const char *path)
 	return resolved;
 }
 
+const struct got_error *
+got_path_segment_count(int *count, const char *path)
+{
+	int n = 0;
+	char *s = strdup(path), *p;
+
+	*count = 0;
+
+	if (s == NULL)
+		return got_error(GOT_ERR_NO_MEM);
+
+	do {
+		p = strsep(&s, "/");
+		if (s && *s != '/')
+			(*count)++;
+	} while (p);
+
+	return NULL;
+}
+
 FILE *
 got_opentemp(void)
 {
@@ -84,3 +106,32 @@ got_opentemp(void)
 
 	return f;
 }
+
+const struct got_error *
+got_opentemp_named(char **path, FILE **outfile, const char *basepath)
+{
+	const struct got_error *err = NULL;
+	int fd, ret;
+
+	if (asprintf(path, "%s-XXXXXX", basepath) == -1) {
+		*path = NULL;
+		return got_error(GOT_ERR_NO_MEM);
+	}
+
+	fd = mkstemp(*path);
+	if (fd == -1) {
+		err = got_error_from_errno();
+		free(*path);
+		*path = NULL;
+		return err;
+	}
+
+	*outfile = fdopen(fd, "w+");
+	if (*outfile == NULL) {
+		err = got_error_from_errno();
+		free(*path);
+		*path = NULL;
+	}
+
+	return err;
+}
diff --git a/lib/worktree.c b/lib/worktree.c
index b7998d0..c8140c7 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -16,6 +16,7 @@
 
 #include <sys/stat.h>
 #include <sys/limits.h>
+#include <sys/queue.h>
 
 #include <string.h>
 #include <stdio.h>
@@ -23,15 +24,27 @@
 #include <fcntl.h>
 #include <errno.h>
 #include <unistd.h>
+#include <sha1.h>
+#include <zlib.h>
+#include <fnmatch.h>
 
 #include "got_error.h"
 #include "got_repository.h"
 #include "got_refs.h"
+#include "got_object.h"
 #include "got_worktree.h"
 
 #include "got_worktree_lib.h"
 #include "got_path_lib.h"
 #include "got_sha1_lib.h"
+#include "got_fileindex_lib.h"
+#include "got_zbuf_lib.h"
+#include "got_delta_lib.h"
+#include "got_object_lib.h"
+
+#ifndef MIN
+#define	MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
+#endif
 
 static const struct got_error *
 create_meta_file(const char *path_got, const char *name, const char *content)
@@ -340,9 +353,313 @@ got_worktree_get_head_ref_name(struct got_worktree *worktree)
 	return strdup(worktree->head_ref);
 }
 
+static const struct got_error *
+lock_worktree(struct got_worktree *worktree, int operation)
+{
+	if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
+		return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
+		    : got_error_from_errno());
+	return NULL;
+}
+
+static const char *
+apply_path_prefix(struct got_worktree *worktree, const char *path)
+{
+	const char *p = path;
+	p += strlen(worktree->path_prefix);
+	if (*p == '/')
+		p++;
+	return p;
+}
+
+static const struct got_error *
+add_file_on_disk(struct got_worktree *worktree, struct got_fileindex *fileindex,
+   const char *path, struct got_blob_object *blob, struct got_repository *repo)
+{
+	const struct got_error *err = NULL;
+	char *abspath;
+	int fd;
+	size_t len, hdrlen;
+	struct got_fileindex_entry *entry;
+
+	if (asprintf(&abspath, "%s/%s", worktree->root_path,
+	    apply_path_prefix(worktree, path)) == -1)
+		return got_error(GOT_ERR_NO_MEM);
+
+	fd = open(abspath, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
+	    GOT_DEFAULT_FILE_MODE);
+	if (fd == -1) {
+		err = got_error_from_errno();
+		if (errno == EEXIST) {
+			struct stat sb;
+			if (lstat(abspath, &sb) == -1) {
+				err = got_error_from_errno();
+			} else if (!S_ISREG(sb.st_mode)) {
+				/* TODO file is obstructed; do something */
+				err = got_error(GOT_ERR_FILE_OBSTRUCTED);
+			}
+		}
+		return err;
+	}
+
+	hdrlen = got_object_blob_get_hdrlen(blob);
+	do {
+		const uint8_t *buf = got_object_blob_get_read_buf(blob);
+		err = got_object_blob_read_block(&len, blob);
+		if (err)
+			break;
+		if (len > 0) {
+			/* Skip blob object header first time around. */
+			ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
+			hdrlen = 0;
+			if (outlen == -1) {
+				err = got_error_from_errno();
+				break;
+			} else if (outlen != len) {
+				err = got_error(GOT_ERR_IO);
+				break;
+			}
+		}
+	} while (len != 0);
+
+	fsync(fd);
+
+	err = got_fileindex_entry_open(&entry, abspath, blob->id.sha1);
+	if (err)
+		goto done;
+
+	err = got_fileindex_entry_add(fileindex, entry);
+	if (err)
+		goto done;
+done:
+	close(fd);
+	free(abspath);
+	return err;
+}
+
+static const struct got_error *
+add_dir_on_disk(struct got_worktree *worktree, const char *path)
+{
+	const struct got_error *err = NULL;
+	char *abspath;
+	size_t len;
+
+	if (asprintf(&abspath, "%s/%s", worktree->root_path,
+	    apply_path_prefix(worktree, path)) == -1)
+		return got_error(GOT_ERR_NO_MEM);
+
+	/* XXX queue work rather than editing disk directly? */
+	if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
+		struct stat sb;
+
+		if (errno != EEXIST) {
+			err = got_error_from_errno();
+			goto done;
+		}
+
+		if (lstat(abspath, &sb) == -1) {
+			err = got_error_from_errno();
+			goto done;
+		}
+
+		if (!S_ISDIR(sb.st_mode)) {
+			/* TODO directory is obstructed; do something */
+			return got_error(GOT_ERR_FILE_OBSTRUCTED);
+		}
+	}
+
+done:
+	free(abspath);
+	return err;
+}
+
+static const struct got_error *
+tree_checkout(struct got_worktree *, struct got_fileindex *,
+    struct got_tree_object *, const char *, struct got_repository *);
+
+static const struct got_error *
+tree_checkout_entry(struct got_worktree *worktree,
+    struct got_fileindex *fileindex, struct got_tree_entry *te,
+    const char *parent, struct got_repository *repo)
+{
+	const struct got_error *err = NULL;
+	struct got_object *obj = NULL;
+	struct got_blob_object *blob = NULL;
+	struct got_tree_object *tree = NULL;
+	char *path = NULL;
+	size_t len;
+
+	if (parent[0] == '/' && parent[1] == '\0')
+		parent = "";
+	if (asprintf(&path, "%s/%s", parent, te->name) == -1)
+		return got_error(GOT_ERR_NO_MEM);
+
+	/* Skip this entry if it is outside of our path prefix. */
+	len = MIN(strlen(worktree->path_prefix), strlen(path));
+	if (strncmp(path, worktree->path_prefix, len) != 0) {
+		free(path);
+		return NULL;
+	}
+
+	err = got_object_open(&obj, repo, te->id);
+	if (err)
+		goto done;
+
+	switch (got_object_get_type(obj)) {
+	case GOT_OBJ_TYPE_BLOB:
+		if (strlen(worktree->path_prefix) >= strlen(path))
+			break;
+		err = got_object_blob_open(&blob, repo, obj, 8192);
+		if (err)
+			goto done;
+		err = add_file_on_disk(worktree, fileindex, path, blob, repo);
+		break;
+	case GOT_OBJ_TYPE_TREE:
+		err = got_object_tree_open(&tree, repo, obj);
+		if (err)
+			goto done;
+		if (strlen(worktree->path_prefix) < strlen(path)) {
+			err = add_dir_on_disk(worktree, path);
+			if (err)
+				break;
+		}
+		err = tree_checkout(worktree, fileindex, tree, path, repo);
+		break;
+	default:
+		break;
+	}
+
+done:
+	if (blob)
+		got_object_blob_close(blob);
+	if (tree)
+		got_object_tree_close(tree);
+	if (obj)
+		got_object_close(obj);
+	free(path);
+	return err;
+}
+
+static const struct got_error *
+tree_checkout(struct got_worktree *worktree,
+    struct got_fileindex *fileindex, struct got_tree_object *tree,
+    const char *path, struct got_repository *repo)
+{
+	const struct got_error *err = NULL;
+	struct got_tree_entry *te;
+	size_t len;
+
+	/* Skip this tree if it is outside of our path prefix. */
+	len = MIN(strlen(worktree->path_prefix), strlen(path));
+	if (strncmp(path, worktree->path_prefix, len) != 0)
+		return NULL;
+
+	SIMPLEQ_FOREACH(te, &tree->entries, entry) {
+		err = tree_checkout_entry(worktree, fileindex, te, path, repo);
+		if (err)
+			break;
+	}
+
+	return err;
+}
+
 const struct got_error *
 got_worktree_checkout_files(struct got_worktree *worktree,
-    struct got_repository *repo)
+    struct got_reference *head_ref, struct got_repository *repo)
 {
-	return NULL;
+	const struct got_error *err = NULL, *unlockerr;
+	struct got_object_id *commit_id = NULL;
+	struct got_object *obj = NULL;
+	struct got_commit_object *commit = NULL;
+	struct got_tree_object *tree = NULL;
+	char *fileindex_path = NULL, *new_fileindex_path = NULL;
+	struct got_fileindex *fileindex = NULL;
+	FILE *findex = NULL;
+
+	err = lock_worktree(worktree, LOCK_EX);
+	if (err)
+		return err;
+
+	fileindex = got_fileindex_open();
+	if (fileindex == NULL) {
+		err = got_error(GOT_ERR_NO_MEM);
+		goto done;
+	}
+
+	err = got_opentemp_named(&new_fileindex_path, &findex, fileindex_path);
+	if (err)
+		goto done;
+
+
+	if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
+	    GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
+		err = got_error(GOT_ERR_NO_MEM);
+		fileindex_path = NULL;
+		goto done;
+	}
+
+	err = got_ref_resolve(&commit_id, repo, head_ref);
+	if (err)
+		goto done;
+
+	err = got_object_open(&obj, repo, commit_id);
+	if (err)
+		goto done;
+
+	if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
+		err = got_error(GOT_ERR_OBJ_TYPE);
+		goto done;
+	}
+
+	err = got_object_commit_open(&commit, repo, obj);
+	if (err)
+		goto done;
+
+	got_object_close(obj);
+	err = got_object_open(&obj, repo, commit->tree_id);
+	if (err)
+		goto done;
+
+	if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
+		err = got_error(GOT_ERR_OBJ_TYPE);
+		goto done;
+	}
+
+	err = got_object_tree_open(&tree, repo, obj);
+	if (err)
+		goto done;
+
+	err = tree_checkout(worktree, fileindex, tree, "/", repo);
+	if (err)
+		goto done;
+
+	err = got_fileindex_write(fileindex, findex);
+	if (err)
+		goto done;
+
+	if (rename(new_fileindex_path, fileindex_path) != 0) {
+		err = got_error_from_errno();
+		goto done;
+	}
+
+	free(new_fileindex_path);
+	new_fileindex_path = NULL;
+
+done:
+	if (commit)
+		got_object_commit_close(commit);
+	if (obj)
+		got_object_close(obj);
+	free(commit_id);
+	if (new_fileindex_path)
+		unlink(new_fileindex_path);
+	if (findex)
+		fclose(findex);
+	free(new_fileindex_path);
+	free(fileindex_path);
+	got_fileindex_close(fileindex);
+	unlockerr = lock_worktree(worktree, LOCK_SH);
+	if (unlockerr && err == NULL)
+		err = unlockerr;
+	return err;
 }
diff --git a/regress/worktree/worktree_test.c b/regress/worktree/worktree_test.c
index 1bde89a..ecc33b7 100644
--- a/regress/worktree/worktree_test.c
+++ b/regress/worktree/worktree_test.c
@@ -304,6 +304,74 @@ done:
 	return (ok == 7);
 }
 
+static int
+worktree_checkout(const char *repo_path)
+{
+	const struct got_error *err;
+	struct got_repository *repo = NULL;
+	struct got_reference *head_ref = NULL;
+	struct got_worktree *worktree = NULL;
+	char *makefile_path = NULL, *cfile_path = NULL;
+	char worktree_path[PATH_MAX];
+	int ok = 0;
+	struct stat sb;
+
+	err = got_repo_open(&repo, repo_path);
+	if (err != NULL || repo == NULL)
+		goto done;
+	err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
+	if (err != NULL || head_ref == NULL)
+		goto done;
+
+	strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
+	if (mkdtemp(worktree_path) == NULL)
+		goto done;
+
+	err = got_worktree_init(worktree_path, head_ref, "/regress/worktree",
+	    repo);
+	if (err != NULL)
+		goto done;
+
+	err = got_worktree_open(&worktree, worktree_path);
+	if (err != NULL)
+		goto done;
+
+	err = got_worktree_checkout_files(worktree, head_ref, repo);
+	if (err != NULL)
+		goto done;
+
+	test_printf("checked out %s\n", worktree_path);
+
+	/* The work tree should contain a Makefile and worktree_test.c. */
+	if (asprintf(&makefile_path, "%s/Makefile", worktree_path) == -1)
+		goto done;
+	if (stat(makefile_path, &sb) != 0)
+		goto done;
+	else
+		unlink(makefile_path);
+	if (asprintf(&cfile_path, "%s/worktree_test.c", worktree_path) == -1)
+		goto done;
+	if (stat(cfile_path, &sb) != 0)
+		goto done;
+	else
+		unlink(cfile_path);
+
+	if (!remove_worktree(worktree_path))
+		goto done;
+
+	ok = 1;
+done:
+	if (worktree)
+		got_worktree_close(worktree);
+	if (head_ref)
+		got_ref_close(head_ref);
+	if (repo)
+		got_repo_close(repo);
+	free(makefile_path);
+	free(cfile_path);
+	return ok;
+}
+
 #define RUN_TEST(expr, name) \
 	{ test_ok = (expr);  \
 	printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
@@ -348,6 +416,7 @@ main(int argc, char *argv[])
 
 	RUN_TEST(worktree_init(repo_path), "init");
 	RUN_TEST(worktree_init_exists(repo_path), "init exists");
+	RUN_TEST(worktree_checkout(repo_path), "checkout");
 
 	return failure ? 1 : 0;
 }