]> git.ipfire.org Git - thirdparty/git.git/blobdiff - builtin/clone.c
Sync with 2.31.5
[thirdparty/git.git] / builtin / clone.c
index 8c05d58f346deea6acc30919b84871997d931e18..cdf9208d4543bfda0b41cdd77c7b52cbe9950d4a 100644 (file)
@@ -50,6 +50,8 @@ static int option_no_checkout, option_bare, option_mirror, option_single_branch
 static int option_local = -1, option_no_hardlinks, option_shared;
 static int option_no_tags;
 static int option_shallow_submodules;
+static int option_reject_shallow = -1;    /* unspecified */
+static int config_reject_shallow = -1;    /* unspecified */
 static int deepen;
 static char *option_template, *option_depth, *option_since;
 static char *option_origin = NULL;
@@ -90,6 +92,8 @@ static struct option builtin_clone_options[] = {
        OPT__VERBOSITY(&option_verbosity),
        OPT_BOOL(0, "progress", &option_progress,
                 N_("force progress reporting")),
+       OPT_BOOL(0, "reject-shallow", &option_reject_shallow,
+                N_("don't clone shallow repository")),
        OPT_BOOL('n', "no-checkout", &option_no_checkout,
                 N_("don't create a checkout")),
        OPT_BOOL(0, "bare", &option_bare, N_("create a bare repository")),
@@ -818,7 +822,7 @@ static int checkout(int submodule_progress)
        if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
                die(_("unable to write new index file"));
 
-       err |= run_hook_le(NULL, "post-checkout", oid_to_hex(&null_oid),
+       err |= run_hook_le(NULL, "post-checkout", oid_to_hex(null_oid()),
                           oid_to_hex(&oid), "1", NULL);
 
        if (!err && (option_recurse_submodules.nr > 0)) {
@@ -860,6 +864,9 @@ static int git_clone_config(const char *k, const char *v, void *cb)
                free(remote_name);
                remote_name = xstrdup(v);
        }
+       if (!strcmp(k, "clone.rejectshallow"))
+               config_reject_shallow = git_config_bool(k, v);
+
        return git_default_config(k, v, cb);
 }
 
@@ -965,11 +972,12 @@ static int path_exists(const char *path)
 int cmd_clone(int argc, const char **argv, const char *prefix)
 {
        int is_bundle = 0, is_local;
+       int reject_shallow = 0;
        const char *repo_name, *repo, *work_tree, *git_dir;
-       char *path, *dir, *display_repo = NULL;
+       char *path = NULL, *dir, *display_repo = NULL;
        int dest_exists, real_dest_exists = 0;
        const struct ref *refs, *remote_head;
-       const struct ref *remote_head_points_at;
+       struct ref *remote_head_points_at = NULL;
        const struct ref *our_head_points_at;
        struct ref *mapped_refs;
        const struct ref *ref;
@@ -1019,9 +1027,10 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
        repo_name = argv[0];
 
        path = get_repo_path(repo_name, &is_bundle);
-       if (path)
+       if (path) {
+               FREE_AND_NULL(path);
                repo = absolute_pathdup(repo_name);
-       else if (strchr(repo_name, ':')) {
+       else if (strchr(repo_name, ':')) {
                repo = repo_name;
                display_repo = transport_anonymize_url(repo);
        } else
@@ -1158,6 +1167,15 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
         */
        git_config(git_clone_config, NULL);
 
+       /*
+        * If option_reject_shallow is specified from CLI option,
+        * ignore config_reject_shallow from git_clone_config.
+        */
+       if (config_reject_shallow != -1)
+               reject_shallow = config_reject_shallow;
+       if (option_reject_shallow != -1)
+               reject_shallow = option_reject_shallow;
+
        /*
         * apply the remote name provided by --origin only after this second
         * call to git_config, to ensure it overrides all config-based values.
@@ -1218,6 +1236,8 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
                if (filter_options.choice)
                        warning(_("--filter is ignored in local clones; use file:// instead."));
                if (!access(mkpath("%s/shallow", path), F_OK)) {
+                       if (reject_shallow)
+                               die(_("source repository is shallow, reject to clone."));
                        if (option_local > 0)
                                warning(_("source repository is shallow, ignoring --local"));
                        is_local = 0;
@@ -1229,6 +1249,8 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 
        transport_set_option(transport, TRANS_OPT_KEEP, "yes");
 
+       if (reject_shallow)
+               transport_set_option(transport, TRANS_OPT_REJECT_SHALLOW, "1");
        if (option_depth)
                transport_set_option(transport, TRANS_OPT_DEPTH,
                                     option_depth);
@@ -1395,6 +1417,11 @@ cleanup:
        strbuf_release(&reflog_msg);
        strbuf_release(&branch_top);
        strbuf_release(&key);
+       free_refs(mapped_refs);
+       free_refs(remote_head_points_at);
+       free(dir);
+       free(path);
+       UNLEAK(repo);
        junk_mode = JUNK_LEAVE_ALL;
 
        strvec_clear(&transport_ls_refs_options.ref_prefixes);