Commit 3ce1b84566a5dc6cbbfcfc87507aa84de4f0c9b9

Stefan Sperling 2019-07-14T18:59:53

initial 'got import' implementation

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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
diff --git a/got/got.1 b/got/got.1
index ac80377..1103aa2 100644
--- a/got/got.1
+++ b/got/got.1
@@ -63,6 +63,58 @@ are as follows:
 .It Cm init Ar path
 Create a new empty repository at the specified
 .Ar path .
+.Pp
+After
+.Cm got init ,
+the
+.Cm got import
+command must be used to populate the empty repository before
+.Cm got checkout
+can be used.
+.Pp
+.It Cm import [ Fl b Ar branch ] [ Fl m Ar message ] [ Fl r Ar repository-path ] [ Fl I Ar pattern ] directory
+Create an initial commit in a repository from the file hierarchy
+within the specified
+.Ar directory .
+The created commit will not have any parent commits, i.e. it will be a
+root commit.
+Also create a new reference which provides a branch name for the newly
+created commit.
+.Pp
+Show the path of each added file to indicate progress.
+.Pp
+The options for
+.Cm got import
+are as follows:
+.Bl -tag -width Ds
+.It Fl b Ar branch
+Create the specified
+.Ar branch
+instead of creating the default branch
+.Dq master .
+Use of this option is required if the
+.Dq master
+branch already exists.
+.It Fl m Ar message
+Use the specified log message when creating the new commit.
+Without the
+.Fl m
+option,
+.Cm got import
+opens a temporary file in an editor where a log message can be written.
+.It Fl r Ar repository-path
+Use the repository at the specified path.
+If not specified, assume the repository is located at or above the current
+working directory.
+.It Fl I Ar pattern
+Ignore files or directories with a name which matches the specified
+.Ar pattern .
+This option may be specified multiple times to build a list of ignore patterns.
+The
+.Ar pattern
+follows the globbing rules documented in
+.Xr glob 7 .
+.El
 .It Cm checkout [ Fl b Ar branch ] [ Fl c Ar commit ] [ Fl p Ar path-prefix ] repository-path [ work-tree-path ]
 Copy files from a repository into a new work tree.
 If the
@@ -639,7 +691,6 @@ The editor spawned by
 .Sh EXIT STATUS
 .Ex -std got
 .Sh EXAMPLES
-.Pp
 Clone an existing Git repository for use with
 .Nm .
 This step currently requires
@@ -648,7 +699,16 @@ This step currently requires
 .Dl $ cd /var/git/
 .Dl $ git clone --bare https://github.com/openbsd/src.git
 .Pp
-Check out a work tree from this Git repository to /usr/src:
+Alternatively, for quick and dirty local testing of
+.Nm
+a new Git repository could be created and populated with files,
+e.g. from a temporary CVS checkout located at
+.Pa /tmp/src :
+.Pp
+.Dl $ got init /var/git/src.git
+.Dl $ got import -r /var/src.git -I CVS -I obj /tmp/src
+.Pp
+Check out a work tree from the Git repository to /usr/src:
 .Pp
 .Dl $ got checkout /var/git/src.git /usr/src
 .Pp
diff --git a/got/got.c b/got/got.c
index 0bfd597..2f7d8b5 100644
--- a/got/got.c
+++ b/got/got.c
@@ -76,6 +76,7 @@ struct got_cmd {
 
 __dead static void	usage(int);
 __dead static void	usage_init(void);
+__dead static void	usage_import(void);
 __dead static void	usage_checkout(void);
 __dead static void	usage_update(void);
 __dead static void	usage_log(void);
@@ -94,6 +95,7 @@ __dead static void	usage_backout(void);
 __dead static void	usage_rebase(void);
 
 static const struct got_error*		cmd_init(int, char *[]);
+static const struct got_error*		cmd_import(int, char *[]);
 static const struct got_error*		cmd_checkout(int, char *[]);
 static const struct got_error*		cmd_update(int, char *[]);
 static const struct got_error*		cmd_log(int, char *[]);
@@ -113,6 +115,7 @@ static const struct got_error*		cmd_rebase(int, char *[]);
 
 static struct got_cmd got_commands[] = {
 	{ "init",	cmd_init,	usage_init,	"" },
+	{ "import",	cmd_import,	usage_import,	"" },
 	{ "checkout",	cmd_checkout,	usage_checkout,	"co" },
 	{ "update",	cmd_update,	usage_update,	"up" },
 	{ "log",	cmd_log,	usage_log,	"" },
@@ -335,6 +338,313 @@ done:
 }
 
 __dead static void
+usage_import(void)
+{
+	fprintf(stderr, "usage: %s import [-b branch] [-m message] "
+	    "[-r repository-path] [-I pattern] path\n", getprogname());
+	exit(1);
+}
+
+int
+spawn_editor(const char *editor, const char *file)
+{
+	pid_t pid;
+	sig_t sighup, sigint, sigquit;
+	int st = -1;
+
+	sighup = signal(SIGHUP, SIG_IGN);
+	sigint = signal(SIGINT, SIG_IGN);
+	sigquit = signal(SIGQUIT, SIG_IGN);
+
+	switch (pid = fork()) {
+	case -1:
+		goto doneediting;
+	case 0:
+		execl(editor, editor, file, (char *)NULL);
+		_exit(127);
+	}
+
+	while (waitpid(pid, &st, 0) == -1)
+		if (errno != EINTR)
+			break;
+
+doneediting:
+	(void)signal(SIGHUP, sighup);
+	(void)signal(SIGINT, sigint);
+	(void)signal(SIGQUIT, sigquit);
+
+	if (!WIFEXITED(st)) {
+		errno = EINTR;
+		return -1;
+	}
+
+	return WEXITSTATUS(st);
+}
+
+static const struct got_error *
+edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
+    const char *initial_content)
+{
+	const struct got_error *err = NULL;
+	char buf[1024];
+	struct stat st, st2;
+	FILE *fp;
+	int content_changed = 0;
+	size_t len;
+
+	*logmsg = NULL;
+
+	if (stat(logmsg_path, &st) == -1)
+		return got_error_from_errno2("stat", logmsg_path);
+
+	if (spawn_editor(editor, logmsg_path) == -1)
+		return got_error_from_errno("failed spawning editor");
+
+	if (stat(logmsg_path, &st2) == -1)
+		return got_error_from_errno("stat");
+
+	if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
+		return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
+		    "no changes made to commit message, aborting");
+
+	*logmsg = malloc(st2.st_size + 1);
+	if (*logmsg == NULL)
+		return got_error_from_errno("malloc");
+	(*logmsg)[0] = '\0';
+	len = 0;
+
+	fp = fopen(logmsg_path, "r");
+	if (fp == NULL) {
+		err = got_error_from_errno("fopen");
+		goto done;
+	}
+	while (fgets(buf, sizeof(buf), fp) != NULL) {
+		if (!content_changed && strcmp(buf, initial_content) != 0)
+			content_changed = 1;
+		if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
+			continue; /* remove comments and leading empty lines */
+		len = strlcat(*logmsg, buf, st2.st_size);
+	}
+	fclose(fp);
+
+	while (len > 0 && (*logmsg)[len - 1] == '\n') {
+		(*logmsg)[len - 1] = '\0';
+		len--;
+	}
+
+	if (len == 0 || !content_changed)
+		err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
+		    "commit message cannot be empty, aborting");
+done:
+	if (err) {
+		free(*logmsg);
+		*logmsg = NULL;
+	}
+	return err;
+}
+
+static const struct got_error *
+collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
+    const char *branch_name)
+{
+	char *initial_content = NULL, *logmsg_path = NULL;
+	const struct got_error *err = NULL;
+	int fd;
+
+	if (asprintf(&initial_content,
+	    "\n# %s to be imported to branch %s\n", path_dir,
+	    branch_name) == -1)
+		return got_error_from_errno("asprintf");
+
+	err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
+	if (err)
+		goto done;
+
+	dprintf(fd, initial_content);
+	close(fd);
+
+	err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
+done:
+	free(initial_content);
+	free(logmsg_path);
+	return err;
+}
+
+static const struct got_error *
+import_progress(void *arg, const char *path)
+{
+	printf("A  %s\n", path);
+	return NULL;
+}
+
+static const struct got_error *
+cmd_import(int argc, char *argv[])
+{
+	const struct got_error *error = NULL;
+	char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
+	char *editor = NULL;
+	const char *got_author = getenv("GOT_AUTHOR");
+	const char *branch_name = "master";
+	char *refname = NULL, *id_str = NULL;
+	struct got_repository *repo = NULL;
+	struct got_reference *branch_ref = NULL, *head_ref = NULL;
+	struct got_object_id *new_commit_id = NULL;
+	int ch;
+	struct got_pathlist_head ignores;
+	struct got_pathlist_entry *pe;
+
+	TAILQ_INIT(&ignores);
+
+	while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
+		switch (ch) {
+		case 'b':
+			branch_name = optarg;
+			break;
+		case 'm':
+			logmsg = strdup(optarg);
+			if (logmsg == NULL) {
+				error = got_error_from_errno("strdup");
+				goto done;
+			}
+			break;
+		case 'r':
+			repo_path = realpath(optarg, NULL);
+			if (repo_path == NULL) {
+				error = got_error_from_errno("realpath");
+				goto done;
+			}
+			break;
+		case 'I':
+			if (optarg[0] == '\0')
+				break;
+			error = got_pathlist_insert(&pe, &ignores, optarg,
+			    NULL);
+			if (error)
+				goto done;
+			break;
+		default:
+			usage_init();
+			/* NOTREACHED */
+		}
+	}
+
+	argc -= optind;
+	argv += optind;
+
+#ifndef PROFILE
+	if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
+	    NULL) == -1)
+		err(1, "pledge");
+#endif
+	if (argc != 1)
+		usage_import();
+
+	if (got_author == NULL) {
+		/* TODO: Look current user up in password database */
+		error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
+		goto done;
+	}
+
+	if (repo_path == NULL) {
+		repo_path = getcwd(NULL, 0);
+		if (repo_path == NULL)
+			return got_error_from_errno("getcwd");
+	}
+	got_path_strip_trailing_slashes(repo_path);
+	error = got_repo_open(&repo, repo_path);
+	if (error)
+		goto done;
+
+	if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
+		error = got_error_from_errno("asprintf");
+		goto done;
+	}
+
+	error = got_ref_open(&branch_ref, repo, refname, 0);
+	if (error) {
+		if (error->code != GOT_ERR_NOT_REF)
+			goto done;
+	} else {
+		error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
+		    "import target branch already exists");
+		goto done;
+	}
+
+	path_dir = realpath(argv[0], NULL);
+	if (path_dir == NULL) {
+		error = got_error_from_errno("realpath");
+		goto done;
+	}
+	got_path_strip_trailing_slashes(path_dir);
+
+	/*
+	 * unveil(2) traverses exec(2); if an editor is used we have
+	 * to apply unveil after the log message has been written.
+	 */
+	if (logmsg == NULL || strlen(logmsg) == 0) {
+		error = get_editor(&editor);
+		if (error)
+			goto done;
+		error = collect_import_msg(&logmsg, editor, path_dir, refname);
+		if (error)
+			goto done;
+	}
+
+	if (unveil(path_dir, "r") != 0)
+		return got_error_from_errno2("unveil", path_dir);
+
+	error = apply_unveil(got_repo_get_path(repo), 0, NULL, 0);
+	if (error)
+		goto done;
+
+	error = got_repo_import(&new_commit_id, path_dir, logmsg,
+	    got_author, &ignores, repo, import_progress, NULL);
+	if (error)
+		goto done;
+
+	error = got_ref_alloc(&branch_ref, refname, new_commit_id);
+	if (error)
+		goto done;
+
+	error = got_ref_write(branch_ref, repo);
+	if (error)
+		goto done;
+
+	error = got_object_id_str(&id_str, new_commit_id);
+	if (error)
+		goto done;
+
+	error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
+	if (error) {
+		if (error->code != GOT_ERR_NOT_REF)
+			goto done;
+
+		error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
+		    branch_ref);
+		if (error)
+			goto done;
+
+		error = got_ref_write(head_ref, repo);
+		if (error)
+			goto done;
+	}
+
+	printf("Created branch %s with commit %s\n",
+	    got_ref_get_name(branch_ref), id_str);
+done:
+	free(repo_path);
+	free(editor);
+	free(refname);
+	free(new_commit_id);
+	free(id_str);
+	if (branch_ref)
+		got_ref_close(branch_ref);
+	if (head_ref)
+		got_ref_close(head_ref);
+	return error;
+}
+
+__dead static void
 usage_checkout(void)
 {
 	fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
@@ -2760,42 +3070,6 @@ usage_commit(void)
 	exit(1);
 }
 
-int
-spawn_editor(const char *editor, const char *file)
-{
-	pid_t pid;
-	sig_t sighup, sigint, sigquit;
-	int st = -1;
-
-	sighup = signal(SIGHUP, SIG_IGN);
-	sigint = signal(SIGINT, SIG_IGN);
-	sigquit = signal(SIGQUIT, SIG_IGN);
-
-	switch (pid = fork()) {
-	case -1:
-		goto doneediting;
-	case 0:
-		execl(editor, editor, file, (char *)NULL);
-		_exit(127);
-	}
-
-	while (waitpid(pid, &st, 0) == -1)
-		if (errno != EINTR)
-			break;
-
-doneediting:
-	(void)signal(SIGHUP, sighup);
-	(void)signal(SIGINT, sigint);
-	(void)signal(SIGQUIT, sigquit);
-
-	if (!WIFEXITED(st)) {
-		errno = EINTR;
-		return -1;
-	}
-
-	return WEXITSTATUS(st);
-}
-
 struct collect_commit_logmsg_arg {
 	const char *cmdline_log;
 	const char *editor;
@@ -2815,11 +3089,8 @@ collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
 	const struct got_error *err = NULL;
 	char *template = NULL;
 	struct collect_commit_logmsg_arg *a = arg;
-	char buf[1024];
-	struct stat st, st2;
-	FILE *fp;
+	int fd;
 	size_t len;
-	int fd, content_changed = 0;
 
 	/* if a message was specified on the command line, just use it */
 	if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
@@ -2853,72 +3124,21 @@ collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
 	}
 	close(fd);
 
-	if (stat(a->logmsg_path, &st) == -1) {
-		err = got_error_from_errno2("stat", a->logmsg_path);
-		goto done;
-	}
-
-	if (spawn_editor(a->editor, a->logmsg_path) == -1) {
-		err = got_error_from_errno("failed spawning editor");
-		goto done;
-	}
-
-	if (stat(a->logmsg_path, &st2) == -1) {
-		err = got_error_from_errno("stat");
-		goto done;
-	}
-
-	if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
-		unlink(a->logmsg_path);
-		free(a->logmsg_path);
-		a->logmsg_path = NULL;
-		err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
-		    "no changes made to commit message, aborting");
-		goto done;
-	}
-
-	*logmsg = malloc(st2.st_size + 1);
-	if (*logmsg == NULL) {
-		err = got_error_from_errno("malloc");
-		goto done;
-	}
-	(*logmsg)[0] = '\0';
-	len = 0;
-
-	fp = fopen(a->logmsg_path, "r");
-	if (fp == NULL) {
-		err = got_error_from_errno("fopen");
-		goto done;
-	}
-	while (fgets(buf, sizeof(buf), fp) != NULL) {
-		if (!content_changed && strcmp(buf, initial_content) != 0)
-			content_changed = 1;
-		if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
-			continue; /* remove comments and leading empty lines */
-		len = strlcat(*logmsg, buf, st2.st_size);
-	}
-	fclose(fp);
-
-	while (len > 0 && (*logmsg)[len - 1] == '\n') {
-		(*logmsg)[len - 1] = '\0';
-		len--;
-	}
-
-	if (len == 0 || !content_changed) {
-		unlink(a->logmsg_path);
-		free(a->logmsg_path);
-		a->logmsg_path = NULL;
-		err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
-		    "commit message cannot be empty, aborting");
-		goto done;
-	}
+	err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
 done:
+	unlink(a->logmsg_path);
+	free(a->logmsg_path);
 	free(initial_content);
 	free(template);
 
 	/* Editor is done; we can now apply unveil(2) */
-	if (err == NULL)
+	if (err == NULL) {
 		err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
+		if (err) {
+			free(*logmsg);
+			*logmsg = NULL;
+		}
+	}
 	return err;
 }
 
diff --git a/include/got_repository.h b/include/got_repository.h
index d4ac915..6b8435a 100644
--- a/include/got_repository.h
+++ b/include/got_repository.h
@@ -15,6 +15,7 @@
  */
 
 struct got_repository;
+struct got_pathlist_head;
 
 /* Open and close repositories. */
 const struct got_error *got_repo_open(struct got_repository**, const char *);
@@ -61,3 +62,14 @@ const struct got_error *got_repo_init(const char *);
 /* Attempt to find a unique object ID for a given ID string prefix. */
 const struct got_error *got_repo_match_object_id_prefix(struct got_object_id **,
     const char *, int, struct got_repository *);
+
+/* A callback function which is invoked when a path is imported. */
+typedef const struct got_error *(*got_repo_import_cb)(void *, const char *);
+
+/*
+ * Import an unversioned directory tree into the repository.
+ * Creates a root commit, i.e. a commit with zero parents.
+ */
+const struct got_error *got_repo_import(struct got_object_id **, const char *,
+    const char *, const char *, struct got_pathlist_head *,
+    struct got_repository *, got_repo_import_cb, void *);
diff --git a/lib/object_create.c b/lib/object_create.c
index feb2dca..867b2d2 100644
--- a/lib/object_create.c
+++ b/lib/object_create.c
@@ -392,28 +392,30 @@ got_object_commit_create(struct got_object_id **id,
 		goto done;
 	}
 
-	SIMPLEQ_FOREACH(qid, parent_ids, entry) {
-		char *parent_str = NULL;
-
-		free(id_str);
-
-		err = got_object_id_str(&id_str, qid->id);
-		if (err)
-			goto done;
-		if (asprintf(&parent_str, "%s%s\n", GOT_COMMIT_LABEL_PARENT,
-		    id_str) == -1) {
-			err = got_error_from_errno("asprintf");
-			goto done;
-		}
-		len = strlen(parent_str);
-		SHA1Update(&sha1_ctx, parent_str, len);
-		n = fwrite(parent_str, 1, len, commitfile);
-		if (n != len) {
-			err = got_ferror(commitfile, GOT_ERR_IO);
+	if (parent_ids) {
+		SIMPLEQ_FOREACH(qid, parent_ids, entry) {
+			char *parent_str = NULL;
+
+			free(id_str);
+
+			err = got_object_id_str(&id_str, qid->id);
+			if (err)
+				goto done;
+			if (asprintf(&parent_str, "%s%s\n",
+			    GOT_COMMIT_LABEL_PARENT, id_str) == -1) {
+				err = got_error_from_errno("asprintf");
+				goto done;
+			}
+			len = strlen(parent_str);
+			SHA1Update(&sha1_ctx, parent_str, len);
+			n = fwrite(parent_str, 1, len, commitfile);
+			if (n != len) {
+				err = got_ferror(commitfile, GOT_ERR_IO);
+				free(parent_str);
+				goto done;
+			}
 			free(parent_str);
-			goto done;
 		}
-		free(parent_str);
 	}
 
 	len = strlen(author_str);
diff --git a/lib/repository.c b/lib/repository.c
index c8076c6..39c436e 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -23,6 +23,7 @@
 
 #include <ctype.h>
 #include <fcntl.h>
+#include <fnmatch.h>
 #include <limits.h>
 #include <dirent.h>
 #include <stdlib.h>
@@ -46,6 +47,8 @@
 #include "got_lib_delta.h"
 #include "got_lib_inflate.h"
 #include "got_lib_object.h"
+#include "got_lib_object_parse.h"
+#include "got_lib_object_create.h"
 #include "got_lib_pack.h"
 #include "got_lib_privsep.h"
 #include "got_lib_worktree.h"
@@ -1109,3 +1112,223 @@ done:
 
 	return err;
 }
+
+static const struct got_error *
+alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
+    const char *name, mode_t mode, struct got_object_id *blob_id)
+{
+	const struct got_error *err = NULL;
+
+	 *new_te = NULL;
+
+	*new_te = calloc(1, sizeof(**new_te));
+	if (*new_te == NULL)
+		return got_error_from_errno("calloc");
+
+	(*new_te)->name = strdup(name);
+	if ((*new_te)->name == NULL) {
+		err = got_error_from_errno("strdup");
+		goto done;
+	}
+
+	(*new_te)->mode = S_IFREG | (mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
+	(*new_te)->id = blob_id;
+done:
+	if (err && *new_te) {
+		got_object_tree_entry_close(*new_te);
+		*new_te = NULL;
+	}
+	return err;
+}
+
+static const struct got_error *
+import_file(struct got_tree_entry **new_te, struct dirent *de,
+    const char *path, struct got_repository *repo)
+{
+	const struct got_error *err;
+	struct got_object_id *blob_id = NULL;
+	char *filepath;
+	struct stat sb;
+
+	if (asprintf(&filepath, "%s%s%s", path,
+	    path[0] == '\0' ? "" : "/", de->d_name) == -1)
+		return got_error_from_errno("asprintf");
+
+	if (lstat(filepath, &sb) != 0) {
+		err = got_error_from_errno2("lstat", path);
+		goto done;
+	}
+
+	err = got_object_blob_create(&blob_id, filepath, repo);
+	if (err)
+		goto done;
+
+	err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
+	    blob_id);
+done:
+	free(filepath);
+	if (err)
+		free(blob_id);
+	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 *write_tree(struct got_object_id **,
+    const char *, struct got_pathlist_head *, struct got_repository *,
+    got_repo_import_cb progress_cb, void *progress_arg);
+
+static const struct got_error *
+import_subdir(struct got_tree_entry **new_te, struct dirent *de,
+    const char *path, struct got_pathlist_head *ignores,
+    struct got_repository *repo,
+    got_repo_import_cb progress_cb, void *progress_arg)
+{
+	const struct got_error *err;
+	char *subdirpath;
+
+	if (asprintf(&subdirpath, "%s%s%s", path,
+	    path[0] == '\0' ? "" : "/", de->d_name) == -1)
+		return got_error_from_errno("asprintf");
+
+	(*new_te) = calloc(1, sizeof(**new_te));
+	(*new_te)->mode = S_IFDIR;
+	(*new_te)->name = strdup(de->d_name);
+	if ((*new_te)->name == NULL) {
+		err = got_error_from_errno("strdup");
+		goto done;
+	}
+
+	err = write_tree(&(*new_te)->id, subdirpath, ignores,  repo,
+	    progress_cb, progress_arg);
+done:
+	free(subdirpath);
+	if (err) {
+		got_object_tree_entry_close(*new_te);
+		*new_te = NULL;
+	}
+	return err;
+}
+
+static const struct got_error *
+write_tree(struct got_object_id **new_tree_id, const char *path_dir,
+    struct got_pathlist_head *ignores, struct got_repository *repo,
+    got_repo_import_cb progress_cb, void *progress_arg)
+{
+	const struct got_error *err = NULL;
+	DIR *dir;
+	struct dirent *de;
+	struct got_tree_entries new_tree_entries;
+	struct got_tree_entry *new_te = NULL;
+	struct got_pathlist_head paths;
+	struct got_pathlist_entry *pe;
+	char *name;
+
+	*new_tree_id = NULL;
+
+	TAILQ_INIT(&paths);
+	new_tree_entries.nentries = 0;
+	SIMPLEQ_INIT(&new_tree_entries.head);
+
+	dir = opendir(path_dir);
+	if (dir == NULL) {
+		err = got_error_from_errno2("opendir", path_dir);
+		goto done;
+	}
+
+	while ((de = readdir(dir)) != NULL) {
+		int ignore = 0;
+
+		if (strcmp(de->d_name, ".") == 0 ||
+		    strcmp(de->d_name, "..") == 0)
+			continue;
+
+		TAILQ_FOREACH(pe, ignores, entry) {
+			if (fnmatch(pe->path, de->d_name, 0) == 0) {
+				ignore = 1;
+				break;
+			}
+		}
+		if (ignore)
+			continue;
+		if (de->d_type == DT_DIR) {
+			err = import_subdir(&new_te, de, path_dir,
+			    ignores, repo, progress_cb, progress_arg);
+			if (err)
+				goto done;
+		} else if (de->d_type == DT_REG) {
+			err = import_file(&new_te, de, path_dir, repo);
+			if (err)
+				goto done;
+		} else
+			continue;
+
+		err = insert_tree_entry(new_te, &paths);
+		if (err)
+			goto done;
+	}
+
+	TAILQ_FOREACH(pe, &paths, entry) {
+		struct got_tree_entry *te = pe->data;
+		char *path;
+		new_tree_entries.nentries++;
+		SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
+		if (!S_ISREG(te->mode))
+			continue;
+		if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
+			err = got_error_from_errno("asprintf");
+			goto done;
+		}
+		err = (*progress_cb)(progress_arg, path);
+		free(path);
+		if (err)
+			goto done;
+	}
+
+	name = basename(path_dir);
+	if (name == NULL) {
+		err = got_error_from_errno("basename");
+		goto done;
+	}
+
+	err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
+done:
+	if (dir)
+		closedir(dir);
+	got_object_tree_entries_close(&new_tree_entries);
+	got_pathlist_free(&paths);
+	return err;
+}
+
+const struct got_error *
+got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
+    const char *logmsg, const char *author, struct got_pathlist_head *ignores,
+    struct got_repository *repo, got_repo_import_cb progress_cb,
+    void *progress_arg)
+{
+	const struct got_error *err;
+	struct got_object_id *new_tree_id;
+
+	err = write_tree(&new_tree_id, path_dir, ignores, repo,
+	    progress_cb, progress_arg);
+	if (err)
+		return err;
+
+	err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
+	    author, time(NULL), author, time(NULL), logmsg, repo);
+	free(new_tree_id);
+	return err;
+}
diff --git a/regress/cmdline/Makefile b/regress/cmdline/Makefile
index cc53c40..682c9e1 100644
--- a/regress/cmdline/Makefile
+++ b/regress/cmdline/Makefile
@@ -1,5 +1,5 @@
 REGRESS_TARGETS=checkout update status log add rm diff commit \
-	cherrypick backout rebase
+	cherrypick backout rebase import
 NOOBJ=Yes
 
 checkout:
@@ -35,4 +35,7 @@ backout:
 rebase:
 	./rebase.sh
 
+import:
+	./import.sh
+
 .include <bsd.regress.mk>
diff --git a/regress/cmdline/common.sh b/regress/cmdline/common.sh
index 793da6f..4b6b064 100644
--- a/regress/cmdline/common.sh
+++ b/regress/cmdline/common.sh
@@ -88,7 +88,15 @@ function make_test_tree
 	echo delta > $repo/gamma/delta
 	mkdir $repo/epsilon
 	echo zeta > $repo/epsilon/zeta
-	(cd $repo && git add .)
+}
+
+function get_blob_id
+{
+	repo="$1"
+	tree_path="$2"
+	filename="$3"
+
+	got tree -r $repo -i $tree_path | grep ${filename}$ | cut -d' ' -f 1
 }
 
 function test_init
@@ -104,6 +112,7 @@ function test_init
 	git_init $testroot/repo
 	if [ -z "$no_tree" ]; then
 		make_test_tree $testroot/repo
+		(cd $repo && git add .)
 		git_commit $testroot/repo -m "adding the test tree"
 	fi
 	echo "$testroot"
diff --git a/regress/cmdline/import.sh b/regress/cmdline/import.sh
new file mode 100755
index 0000000..bf8a068
--- /dev/null
+++ b/regress/cmdline/import.sh
@@ -0,0 +1,201 @@
+#!/bin/sh
+#
+# Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
+#
+# Permission to use, copy, modify, and distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+. ./common.sh
+
+function test_import_basic {
+	local testroot=`mktemp -p /tmp -d got-test-$testname-XXXXXXXX`
+
+	got init $testroot/repo
+
+	mkdir $testroot/tree
+	make_test_tree $testroot/tree
+
+	got import -m 'init' -r $testroot/repo $testroot/tree \
+		> $testroot/stdout
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	local head_commit=`git_show_head $testroot/repo`
+	echo "A  $testroot/tree/gamma/delta" > $testroot/stdout.expected
+	echo "A  $testroot/tree/epsilon/zeta" >> $testroot/stdout.expected
+	echo "A  $testroot/tree/alpha" >> $testroot/stdout.expected
+	echo "A  $testroot/tree/beta" >> $testroot/stdout.expected
+	echo "Created branch refs/heads/master with commit $head_commit" \
+		>> $testroot/stdout.expected
+
+	cmp -s $testroot/stdout.expected $testroot/stdout
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	(cd $testroot/repo && got log -p | grep -v ^date: > $testroot/stdout)
+
+	id_alpha=`get_blob_id $testroot/repo "" alpha`
+	id_beta=`get_blob_id $testroot/repo "" beta`
+	id_zeta=`get_blob_id $testroot/repo epsilon zeta`
+	id_delta=`get_blob_id $testroot/repo gamma delta`
+
+	echo "-----------------------------------------------" \
+		> $testroot/stdout.expected
+	echo "commit $head_commit (master)" >> $testroot/stdout.expected
+	echo "from: $GOT_AUTHOR" >> $testroot/stdout.expected
+	echo " " >> $testroot/stdout.expected
+	echo " init" >> $testroot/stdout.expected
+	echo " " >> $testroot/stdout.expected
+	echo "diff /dev/null $head_commit" >> $testroot/stdout.expected
+	echo "blob - /dev/null" >> $testroot/stdout.expected
+	echo "blob + $id_alpha" >> $testroot/stdout.expected
+	echo "--- /dev/null" >> $testroot/stdout.expected
+	echo "+++ alpha" >> $testroot/stdout.expected
+	echo "@@ -0,0 +1 @@" >> $testroot/stdout.expected
+	echo "+alpha" >> $testroot/stdout.expected
+	echo "blob - /dev/null" >> $testroot/stdout.expected
+	echo "blob + $id_beta" >> $testroot/stdout.expected
+	echo "--- /dev/null" >> $testroot/stdout.expected
+	echo "+++ beta" >> $testroot/stdout.expected
+	echo "@@ -0,0 +1 @@" >> $testroot/stdout.expected
+	echo "+beta" >> $testroot/stdout.expected
+	echo "blob - /dev/null" >> $testroot/stdout.expected
+	echo "blob + $id_zeta" >> $testroot/stdout.expected
+	echo "--- /dev/null" >> $testroot/stdout.expected
+	echo "+++ epsilon/zeta" >> $testroot/stdout.expected
+	echo "@@ -0,0 +1 @@" >> $testroot/stdout.expected
+	echo "+zeta" >> $testroot/stdout.expected
+	echo "blob - /dev/null" >> $testroot/stdout.expected
+	echo "blob + $id_delta" >> $testroot/stdout.expected
+	echo "--- /dev/null" >> $testroot/stdout.expected
+	echo "+++ gamma/delta" >> $testroot/stdout.expected
+	echo "@@ -0,0 +1 @@" >> $testroot/stdout.expected
+	echo "+delta" >> $testroot/stdout.expected
+	echo "" >> $testroot/stdout.expected
+
+	cmp -s $testroot/stdout.expected $testroot/stdout
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo "A  $testroot/wt/alpha" > $testroot/stdout.expected
+	echo "A  $testroot/wt/beta" >> $testroot/stdout.expected
+	echo "A  $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected
+	echo "A  $testroot/wt/gamma/delta" >> $testroot/stdout.expected
+	echo "Now shut up and hack" >> $testroot/stdout.expected
+
+	got checkout $testroot/repo $testroot/wt > $testroot/stdout
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	cmp -s $testroot/stdout.expected $testroot/stdout
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo "alpha" > $testroot/content.expected
+	echo "beta" >> $testroot/content.expected
+	echo "zeta" >> $testroot/content.expected
+	echo "delta" >> $testroot/content.expected
+	cat $testroot/wt/alpha $testroot/wt/beta $testroot/wt/epsilon/zeta \
+	    $testroot/wt/gamma/delta > $testroot/content
+
+	cmp -s $testroot/content.expected $testroot/content
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/content.expected $testroot/content
+	fi
+	test_done "$testroot" "$ret"
+}
+
+function test_import_requires_new_branch {
+	local testroot=`test_init import_requires_new_branch`
+
+	mkdir $testroot/tree
+	make_test_tree $testroot/tree
+
+	got import -m 'init' -r $testroot/repo $testroot/tree \
+		> $testroot/stdout 2> $testroot/stderr
+	ret="$?"
+	if [ "$ret" == "0" ]; then
+		echo "import command should have failed but did not"
+		test_done "$testroot" "1"
+		return 1
+	fi
+
+	echo "got: import target branch already exists" \
+		> $testroot/stderr.expected
+	cmp -s $testroot/stderr.expected $testroot/stderr
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stderr.expected $testroot/stderr
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	got import -b newbranch -m 'init' -r $testroot/repo $testroot/tree  \
+		> $testroot/stdout
+	ret="$?"
+	test_done "$testroot" "$ret"
+
+}
+
+function test_import_ignores {
+	local testroot=`mktemp -p /tmp -d got-test-$testname-XXXXXXXX`
+
+	got init $testroot/repo
+
+	mkdir $testroot/tree
+	make_test_tree $testroot/tree
+
+	got import -I alpha -I '*lta*' -I '*silon' \
+		-m 'init' -r $testroot/repo $testroot/tree > $testroot/stdout
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	local head_commit=`git_show_head $testroot/repo`
+	echo "A  $testroot/tree/beta" >> $testroot/stdout.expected
+	echo "Created branch refs/heads/master with commit $head_commit" \
+		>> $testroot/stdout.expected
+
+	cmp -s $testroot/stdout.expected $testroot/stdout
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+	fi
+	test_done "$testroot" "$ret"
+
+
+}
+
+run_test test_import_basic
+run_test test_import_requires_new_branch
+run_test test_import_ignores