Commit 2ab439478bef81058efc85623de2d92cd8a66c98

Stefan Sperling 2020-03-18T16:16:39

move clone code and documentation up so it appears before 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
diff --git a/got/got.1 b/got/got.1
index 1e1b77c..e81a776 100644
--- a/got/got.1
+++ b/got/got.1
@@ -136,6 +136,63 @@ follows the globbing rules documented in
 .It Cm im
 Short alias for
 .Cm import .
+.It Cm clone Oo Fl q Oc Oo Fl v Oc Ar repository-URL Op Ar target-directory
+Clone a Git repository at the specified
+.Ar repository-URL
+into the specified
+.Ar target-directory .
+If no
+.Ar target-directory
+is specified the directory name will be derived from the name of the
+cloned repository.
+.Cm got clone
+will refuse to run if the
+.Ar target-directory
+already exists.
+.Pp
+The
+.Ar repository-URL
+specifies a protocol scheme, a server hostname, and a path to the repository
+on the server:
+.Lk scheme://hostname/path/to/repository
+.Pp
+The following protocol schemes are supported:
+.Bl -tag -width git+ssh
+.It git
+The Git protocol as implemented by the
+.Xr git-daemon 1
+server.
+This protocol is discouraged since it supports neither authentication nor
+encryption.
+.It git+ssh
+The Git protocol wrapped in an authenticated and encrypted
+.Xr ssh 1
+tunnel.
+With this protocol the hostname may contain an embedded username for
+.Xr ssh 1
+to use:
+.Mt user@hostname
+.It ssh
+Short alias for git+ssh.
+.El
+.Pp
+The options for
+.Cm got clone
+are as follows:
+.Bl -tag -width Ds
+.It Fl q
+Suppress progress reporting output.
+The same option will be passed to
+.Xr ssh 1
+if applicable.
+.It Fl v
+Increase the verbosity of progress reporting output.
+The same option will be passed to
+.Xr ssh 1
+if applicable.
+Multiple -v options increase the verbosity.
+The maximum is 3.
+.El
 .It Cm checkout  Oo Fl E Oc Oo Fl b Ar branch Oc Oo Fl c Ar commit Oc Oo Fl p Ar path-prefix Oc Ar repository-path Op Ar work-tree-path
 Copy files from a repository into a new work tree.
 Show the status of each affected file, using the following status codes:
@@ -200,63 +257,6 @@ will be checked out.
 .It Cm co
 Short alias for
 .Cm checkout .
-.It Cm clone Oo Fl q Oc Oo Fl v Oc Ar repository-URL Op Ar target-directory
-Clone a Git repository at the specified
-.Ar repository-URL
-into the specified
-.Ar target-directory .
-If no
-.Ar target-directory
-is specified the directory name will be derived from the name of the
-cloned repository.
-.Cm got clone
-will refuse to run if the
-.Ar target-directory
-already exists.
-.Pp
-The
-.Ar repository-URL
-specifies a protocol scheme, a server hostname, and a path to the repository
-on the server:
-.Lk scheme://hostname/path/to/repository
-.Pp
-The following protocol schemes are supported:
-.Bl -tag -width git+ssh
-.It git
-The Git protocol as implemented by the
-.Xr git-daemon 1
-server.
-This protocol is discouraged since it supports neither authentication nor
-encryption.
-.It git+ssh
-The Git protocol wrapped in an authenticated and encrypted
-.Xr ssh 1
-tunnel.
-With this protocol the hostname may contain an embedded username for
-.Xr ssh 1
-to use:
-.Mt user@hostname
-.It ssh
-Short alias for git+ssh.
-.El
-.Pp
-The options for
-.Cm got clone
-are as follows:
-.Bl -tag -width Ds
-.It Fl q
-Suppress progress reporting output.
-The same option will be passed to
-.Xr ssh 1
-if applicable.
-.It Fl v
-Increase the verbosity of progress reporting output.
-The same option will be passed to
-.Xr ssh 1
-if applicable.
-Multiple -v options increase the verbosity.
-The maximum is 3.
-.El
 .It Cm update Oo Fl b Ar branch Oc Oo Fl c Ar commit Oc Op Ar path ...
 Update an existing work tree to a different commit.
 Show the status of each affected file, using the following status codes:
diff --git a/got/got.c b/got/got.c
index 799c85e..ac737b0 100644
--- a/got/got.c
+++ b/got/got.c
@@ -85,8 +85,8 @@ 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_clone(void);
+__dead static void	usage_checkout(void);
 __dead static void	usage_update(void);
 __dead static void	usage_log(void);
 __dead static void	usage_diff(void);
@@ -138,8 +138,8 @@ static const struct got_error*		cmd_cat(int, char *[]);
 static struct got_cmd got_commands[] = {
 	{ "init",	cmd_init,	usage_init,	"in" },
 	{ "import",	cmd_import,	usage_import,	"im" },
-	{ "checkout",	cmd_checkout,	usage_checkout,	"co" },
 	{ "clone",	cmd_clone,	usage_clone,	"cl" },
+	{ "checkout",	cmd_checkout,	usage_checkout,	"co" },
 	{ "update",	cmd_update,	usage_update,	"up" },
 	{ "log",	cmd_log,	usage_log,	"" },
 	{ "diff",	cmd_diff,	usage_diff,	"di" },
@@ -813,163 +813,6 @@ usage_clone(void)
 	exit(1);
 }
 
-__dead static void
-usage_checkout(void)
-{
-	fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
-	    "[-p prefix] repository-path [worktree-path]\n", getprogname());
-	exit(1);
-}
-
-static void
-show_worktree_base_ref_warning(void)
-{
-	fprintf(stderr, "%s: warning: could not create a reference "
-	    "to the work tree's base commit; the commit could be "
-	    "garbage-collected by Git; making the repository "
-	    "writable and running 'got update' will prevent this\n",
-	    getprogname());
-}
-
-struct got_checkout_progress_arg {
-	const char *worktree_path;
-	int had_base_commit_ref_error;
-};
-
-static const struct got_error *
-checkout_progress(void *arg, unsigned char status, const char *path)
-{
-	struct got_checkout_progress_arg *a = arg;
-
-	/* Base commit bump happens silently. */
-	if (status == GOT_STATUS_BUMP_BASE)
-		return NULL;
-
-	if (status == GOT_STATUS_BASE_REF_ERR) {
-		a->had_base_commit_ref_error = 1;
-		return NULL;
-	}
-
-	while (path[0] == '/')
-		path++;
-
-	printf("%c  %s/%s\n", status, a->worktree_path, path);
-	return NULL;
-}
-
-static const struct got_error *
-check_cancelled(void *arg)
-{
-	if (sigint_received || sigpipe_received)
-		return got_error(GOT_ERR_CANCELLED);
-	return NULL;
-}
-
-static const struct got_error *
-check_linear_ancestry(struct got_object_id *commit_id,
-    struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
-    struct got_repository *repo)
-{
-	const struct got_error *err = NULL;
-	struct got_object_id *yca_id;
-
-	err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
-	    commit_id, base_commit_id, repo, check_cancelled, NULL);
-	if (err)
-		return err;
-
-	if (yca_id == NULL)
-		return got_error(GOT_ERR_ANCESTRY);
-
-	/*
-	 * Require a straight line of history between the target commit
-	 * and the work tree's base commit.
-	 *
-	 * Non-linear situations such as this require a rebase:
-	 *
-	 * (commit) D       F (base_commit)
-	 *           \     /
-	 *            C   E
-	 *             \ /
-	 *              B (yca)
-	 *              |
-	 *              A
-	 *
-	 * 'got update' only handles linear cases:
-	 * Update forwards in time:  A (base/yca) - B - C - D (commit)
-	 * Update backwards in time: D (base) - C - B - A (commit/yca)
-	 */
-	if (allow_forwards_in_time_only) {
-	    if (got_object_id_cmp(base_commit_id, yca_id) != 0)
-		return got_error(GOT_ERR_ANCESTRY);
-	} else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
-	    got_object_id_cmp(base_commit_id, yca_id) != 0)
-		return got_error(GOT_ERR_ANCESTRY);
-
-	free(yca_id);
-	return NULL;
-}
-
-static const struct got_error *
-check_same_branch(struct got_object_id *commit_id,
-    struct got_reference *head_ref, struct got_object_id *yca_id,
-    struct got_repository *repo)
-{
-	const struct got_error *err = NULL;
-	struct got_commit_graph *graph = NULL;
-	struct got_object_id *head_commit_id = NULL;
-	int is_same_branch = 0;
-
-	err = got_ref_resolve(&head_commit_id, repo, head_ref);
-	if (err)
-		goto done;
-
-	if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
-		is_same_branch = 1;
-		goto done;
-	}
-	if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
-		is_same_branch = 1;
-		goto done;
-	}
-
-	err = got_commit_graph_open(&graph, "/", 1);
-	if (err)
-		goto done;
-
-	err = got_commit_graph_iter_start(graph, head_commit_id, repo,
-	    check_cancelled, NULL);
-	if (err)
-		goto done;
-
-	for (;;) {
-		struct got_object_id *id;
-		err = got_commit_graph_iter_next(&id, graph, repo,
-		    check_cancelled, NULL);
-		if (err) {
-			if (err->code == GOT_ERR_ITER_COMPLETED)
-				err = NULL;
-			break;
-		}
-
-		if (id) {
-			if (yca_id && got_object_id_cmp(id, yca_id) == 0)
-				break;
-			if (got_object_id_cmp(id, commit_id) == 0) {
-				is_same_branch = 1;
-				break;
-			}
-		}
-	}
-done:
-	if (graph)
-		got_commit_graph_close(graph);
-	free(head_commit_id);
-	if (!err && !is_same_branch)
-		err = got_error(GOT_ERR_ANCESTRY);
-	return err;
-}
-
 struct got_fetch_progress_arg {
 	char last_scaled_size[FMT_SCALED_STRSIZE];
 	int last_p_indexed;
@@ -1312,6 +1155,163 @@ done:
 	return error;
 }
 
+__dead static void
+usage_checkout(void)
+{
+	fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
+	    "[-p prefix] repository-path [worktree-path]\n", getprogname());
+	exit(1);
+}
+
+static void
+show_worktree_base_ref_warning(void)
+{
+	fprintf(stderr, "%s: warning: could not create a reference "
+	    "to the work tree's base commit; the commit could be "
+	    "garbage-collected by Git; making the repository "
+	    "writable and running 'got update' will prevent this\n",
+	    getprogname());
+}
+
+struct got_checkout_progress_arg {
+	const char *worktree_path;
+	int had_base_commit_ref_error;
+};
+
+static const struct got_error *
+checkout_progress(void *arg, unsigned char status, const char *path)
+{
+	struct got_checkout_progress_arg *a = arg;
+
+	/* Base commit bump happens silently. */
+	if (status == GOT_STATUS_BUMP_BASE)
+		return NULL;
+
+	if (status == GOT_STATUS_BASE_REF_ERR) {
+		a->had_base_commit_ref_error = 1;
+		return NULL;
+	}
+
+	while (path[0] == '/')
+		path++;
+
+	printf("%c  %s/%s\n", status, a->worktree_path, path);
+	return NULL;
+}
+
+static const struct got_error *
+check_cancelled(void *arg)
+{
+	if (sigint_received || sigpipe_received)
+		return got_error(GOT_ERR_CANCELLED);
+	return NULL;
+}
+
+static const struct got_error *
+check_linear_ancestry(struct got_object_id *commit_id,
+    struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
+    struct got_repository *repo)
+{
+	const struct got_error *err = NULL;
+	struct got_object_id *yca_id;
+
+	err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
+	    commit_id, base_commit_id, repo, check_cancelled, NULL);
+	if (err)
+		return err;
+
+	if (yca_id == NULL)
+		return got_error(GOT_ERR_ANCESTRY);
+
+	/*
+	 * Require a straight line of history between the target commit
+	 * and the work tree's base commit.
+	 *
+	 * Non-linear situations such as this require a rebase:
+	 *
+	 * (commit) D       F (base_commit)
+	 *           \     /
+	 *            C   E
+	 *             \ /
+	 *              B (yca)
+	 *              |
+	 *              A
+	 *
+	 * 'got update' only handles linear cases:
+	 * Update forwards in time:  A (base/yca) - B - C - D (commit)
+	 * Update backwards in time: D (base) - C - B - A (commit/yca)
+	 */
+	if (allow_forwards_in_time_only) {
+	    if (got_object_id_cmp(base_commit_id, yca_id) != 0)
+		return got_error(GOT_ERR_ANCESTRY);
+	} else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
+	    got_object_id_cmp(base_commit_id, yca_id) != 0)
+		return got_error(GOT_ERR_ANCESTRY);
+
+	free(yca_id);
+	return NULL;
+}
+
+static const struct got_error *
+check_same_branch(struct got_object_id *commit_id,
+    struct got_reference *head_ref, struct got_object_id *yca_id,
+    struct got_repository *repo)
+{
+	const struct got_error *err = NULL;
+	struct got_commit_graph *graph = NULL;
+	struct got_object_id *head_commit_id = NULL;
+	int is_same_branch = 0;
+
+	err = got_ref_resolve(&head_commit_id, repo, head_ref);
+	if (err)
+		goto done;
+
+	if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
+		is_same_branch = 1;
+		goto done;
+	}
+	if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
+		is_same_branch = 1;
+		goto done;
+	}
+
+	err = got_commit_graph_open(&graph, "/", 1);
+	if (err)
+		goto done;
+
+	err = got_commit_graph_iter_start(graph, head_commit_id, repo,
+	    check_cancelled, NULL);
+	if (err)
+		goto done;
+
+	for (;;) {
+		struct got_object_id *id;
+		err = got_commit_graph_iter_next(&id, graph, repo,
+		    check_cancelled, NULL);
+		if (err) {
+			if (err->code == GOT_ERR_ITER_COMPLETED)
+				err = NULL;
+			break;
+		}
+
+		if (id) {
+			if (yca_id && got_object_id_cmp(id, yca_id) == 0)
+				break;
+			if (got_object_id_cmp(id, commit_id) == 0) {
+				is_same_branch = 1;
+				break;
+			}
+		}
+	}
+done:
+	if (graph)
+		got_commit_graph_close(graph);
+	free(head_commit_id);
+	if (!err && !is_same_branch)
+		err = got_error(GOT_ERR_ANCESTRY);
+	return err;
+}
+
 static const struct got_error *
 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
 {