59
60
struct clone_opts {
61
int wants_head;
62
+ int detach;
63
};
64
#define CLONE_OPTS_INIT { \
65
.wants_head = 1 /* default enabled */ \
566
}
567
}
568
568
-static void update_head(const struct ref *our, const struct ref *remote,
569
+static void update_head(struct clone_opts *opts, const struct ref *our, const struct ref *remote,
570
const char *unborn, const char *msg)
571
{
572
const char *head;
572
- if (our && skip_prefix(our->name, "refs/heads/", &head)) {
573
+ if (our && !opts->detach && skip_prefix(our->name, "refs/heads/", &head)) {
574
/* Local default branch link */
575
if (refs_update_symref(get_main_ref_store(the_repository), "HEAD", our->name, NULL) < 0)
576
die(_("unable to update HEAD"));
581
install_branch_config(0, head, remote_name, our->name);
582
}
583
} else if (our) {
583
- struct commit *c = lookup_commit_reference(the_repository,
584
- &our->old_oid);
584
+ struct commit *c = lookup_commit_or_die(&our->old_oid,
585
+ our->name);
586
+
587
/* --branch specifies a non-branch (i.e. tags), detach HEAD */
588
refs_update_ref(get_main_ref_store(the_repository), msg,
589
"HEAD", &c->object.oid, NULL, REF_NO_DEREF,
902
int option_filter_submodules = -1; /* unspecified */
903
struct string_list server_options = STRING_LIST_INIT_NODUP;
904
const char *bundle_uri = NULL;
905
+ char *option_rev = NULL;
906
907
struct clone_opts opts = CLONE_OPTS_INIT;
908
946
N_("use <name> instead of 'origin' to track upstream")),
947
OPT_STRING('b', "branch", &option_branch, N_("branch"),
948
N_("checkout <branch> instead of the remote's HEAD")),
949
+ OPT_STRING(0, "revision", &option_rev, N_("rev"),
950
+ N_("clone single revision <rev> and check out")),
951
OPT_STRING('u', "upload-pack", &option_upload_pack, N_("path"),
952
N_("path to git-upload-pack on the remote")),
953
OPT_STRING(0, "depth", &option_depth, N_("depth"),
1284
strbuf_addstr(&branch_top, src_ref_prefix);
1285
1286
git_config_set("core.bare", "true");
1282
- } else {
1287
+ } else if (!option_rev) {
1288
strbuf_addf(&branch_top, "refs/remotes/%s/", remote_name);
1289
}
1290
1303
1304
remote = remote_get_early(remote_name);
1305
1301
- refspec_appendf(&remote->fetch, "+%s*:%s*", src_ref_prefix,
1302
- branch_top.buf);
1306
+ if (!option_rev)
1307
+ refspec_appendf(&remote->fetch, "+%s*:%s*", src_ref_prefix,
1308
+ branch_top.buf);
1309
1310
path = get_repo_path(remote->url.v[0], &is_bundle);
1311
is_local = option_local != 0 && path && !is_bundle;
1348
1349
transport_set_option(transport, TRANS_OPT_KEEP, "yes");
1350
1351
+ die_for_incompatible_opt2(!!option_rev, "--revision",
1352
+ !!option_branch, "--branch");
1353
+ die_for_incompatible_opt2(!!option_rev, "--revision",
1354
+ option_mirror, "--mirror");
1355
+
1356
if (reject_shallow)
1357
transport_set_option(transport, TRANS_OPT_REJECT_SHALLOW, "1");
1358
if (option_depth)
1389
if (transport->smart_options && !deepen && !filter_options.choice)
1390
transport->smart_options->check_self_contained_and_connected = 1;
1391
1381
- strvec_push(&transport_ls_refs_options.ref_prefixes, "HEAD");
1392
+ if (option_rev) {
1393
+ option_tags = 0;
1394
+ option_single_branch = 0;
1395
+ opts.wants_head = 0;
1396
+ opts.detach = 1;
1397
+
1398
+ refspec_append(&remote->fetch, option_rev);
1399
+ }
1400
1401
if (option_tags || option_branch)
1402
/*
1411
expand_ref_prefix(&transport_ls_refs_options.ref_prefixes,
1412
option_branch);
1413
1414
+ /*
1415
+ * As part of transport_get_remote_refs() the server tells us the hash
1416
+ * algorithm, which we require to initialize the repo. But calling that
1417
+ * function without any ref prefix, will cause the server to announce
1418
+ * all known refs. If the argument passed to --revision was a hex oid,
1419
+ * ref_prefixes will be empty so we fall back to asking about HEAD to
1420
+ * reduce traffic from the server.
1421
+ */
1422
+ if (opts.wants_head || transport_ls_refs_options.ref_prefixes.nr == 0)
1423
+ strvec_push(&transport_ls_refs_options.ref_prefixes, "HEAD");
1424
+
1425
refs = transport_get_remote_refs(transport, &transport_ls_refs_options);
1426
1427
/*
1530
if (!our_head_points_at)
1531
die(_("Remote branch %s not found in upstream %s"),
1532
option_branch, remote_name);
1533
+ } else if (option_rev) {
1534
+ our_head_points_at = mapped_refs;
1535
+ if (!our_head_points_at)
1536
+ die(_("Remote revision %s not found in upstream %s"),
1537
+ option_rev, remote_name);
1538
} else if (remote_head_points_at) {
1539
our_head_points_at = remote_head_points_at;
1540
} else if (remote_head) {
1573
free(to_free);
1574
}
1575
1542
- write_refspec_config(src_ref_prefix, our_head_points_at,
1543
- remote_head_points_at, &branch_top);
1576
+ if (!option_rev)
1577
+ write_refspec_config(src_ref_prefix, our_head_points_at,
1578
+ remote_head_points_at, &branch_top);
1579
1580
if (filter_options.choice)
1581
partial_clone_register(remote_name, &filter_options);
1591
branch_top.buf, reflog_msg.buf, transport,
1592
!is_local);
1593
1559
- update_head(our_head_points_at, remote_head, unborn_head, reflog_msg.buf);
1594
+ update_head(&opts, our_head_points_at, remote_head, unborn_head, reflog_msg.buf);
1595
1596
/*
1597
* We want to show progress for recursive submodule clones iff