Skip to content

Commit

Permalink
Merge branch 'll/clone-reject-shallow'
Browse files Browse the repository at this point in the history
"git clone --reject-shallow" option fails the clone as soon as we
notice that we are cloning from a shallow repository.

* ll/clone-reject-shallow:
  builtin/clone.c: add --reject-shallow option
  • Loading branch information
gitster committed Apr 8, 2021
2 parents e6b971f + 4fe788b commit 22eee7f
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 6 deletions.
4 changes: 4 additions & 0 deletions Documentation/config/clone.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ clone.defaultRemoteName::
The name of the remote to create when cloning a repository. Defaults to
`origin`, and can be overridden by passing the `--origin` command-line
option to linkgit:git-clone[1].

clone.rejectShallow::
Reject to clone a repository if it is a shallow one, can be overridden by
passing option `--reject-shallow` in command line. See linkgit:git-clone[1]
7 changes: 6 additions & 1 deletion Documentation/git-clone.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ SYNOPSIS
[--dissociate] [--separate-git-dir <git dir>]
[--depth <depth>] [--[no-]single-branch] [--no-tags]
[--recurse-submodules[=<pathspec>]] [--[no-]shallow-submodules]
[--[no-]remote-submodules] [--jobs <n>] [--sparse]
[--[no-]remote-submodules] [--jobs <n>] [--sparse] [--[no-]reject-shallow]
[--filter=<filter>] [--] <repository>
[<directory>]

Expand Down Expand Up @@ -149,6 +149,11 @@ objects from the source repository into a pack in the cloned repository.
--no-checkout::
No checkout of HEAD is performed after the clone is complete.

--[no-]reject-shallow::
Fail if the source repository is a shallow repository.
The 'clone.rejectShallow' configuration variable can be used to
specify the default.

--bare::
Make a 'bare' Git repository. That is, instead of
creating `<directory>` and placing the administrative
Expand Down
21 changes: 21 additions & 0 deletions builtin/clone.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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")),
Expand Down Expand Up @@ -858,6 +862,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);
}

Expand Down Expand Up @@ -963,6 +970,7 @@ 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 = NULL, *dir, *display_repo = NULL;
int dest_exists, real_dest_exists = 0;
Expand Down Expand Up @@ -1157,6 +1165,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.
Expand Down Expand Up @@ -1217,6 +1234,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;
Expand All @@ -1228,6 +1247,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);
Expand Down
12 changes: 8 additions & 4 deletions fetch-pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -1113,9 +1113,11 @@ static struct ref *do_fetch_pack(struct fetch_pack_args *args,
if (args->deepen)
setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
NULL);
else if (si->nr_ours || si->nr_theirs)
else if (si->nr_ours || si->nr_theirs) {
if (args->reject_shallow_remote)
die(_("source repository is shallow, reject to clone."));
alternate_shallow_file = setup_temporary_shallow(si->shallow);
else
} else
alternate_shallow_file = NULL;
if (get_pack(args, fd, pack_lockfiles, NULL, sought, nr_sought,
&fsck_options.gitmodules_found))
Expand Down Expand Up @@ -1483,10 +1485,12 @@ static void receive_shallow_info(struct fetch_pack_args *args,
* rejected (unless --update-shallow is set); do the same.
*/
prepare_shallow_info(si, shallows);
if (si->nr_ours || si->nr_theirs)
if (si->nr_ours || si->nr_theirs) {
if (args->reject_shallow_remote)
die(_("source repository is shallow, reject to clone."));
alternate_shallow_file =
setup_temporary_shallow(si->shallow);
else
} else
alternate_shallow_file = NULL;
} else {
alternate_shallow_file = NULL;
Expand Down
1 change: 1 addition & 0 deletions fetch-pack.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ struct fetch_pack_args {
unsigned self_contained_and_connected:1;
unsigned cloning:1;
unsigned update_shallow:1;
unsigned reject_shallow_remote:1;
unsigned deepen:1;

/*
Expand Down
9 changes: 9 additions & 0 deletions t/t5601-clone.sh
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,15 @@ test_expect_success 'partial clone using HTTP' '
partial_clone "$HTTPD_DOCUMENT_ROOT_PATH/server" "$HTTPD_URL/smart/server"
'

test_expect_success 'reject cloning shallow repository using HTTP' '
test_when_finished "rm -rf repo" &&
git clone --bare --no-local --depth=1 src "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
test_must_fail git clone --reject-shallow $HTTPD_URL/smart/repo.git repo 2>err &&
test_i18ngrep -e "source repository is shallow, reject to clone." err &&
git clone --no-reject-shallow $HTTPD_URL/smart/repo.git repo
'

# DO NOT add non-httpd-specific tests here, because the last part of this
# test script is only executed when httpd is available and enabled.

Expand Down
27 changes: 26 additions & 1 deletion t/t5606-clone-options.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ test_expect_success 'setup' '
mkdir parent &&
(cd parent && git init &&
echo one >file && git add file &&
git commit -m one)
git commit -m one) &&
git clone --depth=1 --no-local parent shallow-repo
'

Expand Down Expand Up @@ -45,6 +46,30 @@ test_expect_success 'disallows --bare with --separate-git-dir' '
'

test_expect_success 'reject cloning shallow repository' '
test_when_finished "rm -rf repo" &&
test_must_fail git clone --reject-shallow shallow-repo out 2>err &&
test_i18ngrep -e "source repository is shallow, reject to clone." err &&
git clone --no-reject-shallow shallow-repo repo
'

test_expect_success 'reject cloning non-local shallow repository' '
test_when_finished "rm -rf repo" &&
test_must_fail git clone --reject-shallow --no-local shallow-repo out 2>err &&
test_i18ngrep -e "source repository is shallow, reject to clone." err &&
git clone --no-reject-shallow --no-local shallow-repo repo
'

test_expect_success 'succeed cloning normal repository' '
test_when_finished "rm -rf chilad1 child2 child3 child4 " &&
git clone --reject-shallow parent child1 &&
git clone --reject-shallow --no-local parent child2 &&
git clone --no-reject-shallow parent child3 &&
git clone --no-reject-shallow --no-local parent child4
'

test_expect_success 'uses "origin" for default remote name' '
git clone parent clone-default-origin &&
Expand Down
25 changes: 25 additions & 0 deletions t/t5611-clone-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,31 @@ test_expect_success 'clone -c remote.<remote>.fetch=<refspec> --origin=<name>' '
test_cmp expect actual
'

test_expect_success 'set up shallow repository' '
git clone --depth=1 --no-local . shallow-repo
'

test_expect_success 'clone.rejectshallow=true should reject cloning shallow repo' '
test_when_finished "rm -rf out" &&
test_must_fail git -c clone.rejectshallow=true clone --no-local shallow-repo out 2>err &&
test_i18ngrep -e "source repository is shallow, reject to clone." err &&
git -c clone.rejectshallow=false clone --no-local shallow-repo out
'

test_expect_success 'option --[no-]reject-shallow override clone.rejectshallow config' '
test_when_finished "rm -rf out" &&
test_must_fail git -c clone.rejectshallow=false clone --reject-shallow --no-local shallow-repo out 2>err &&
test_i18ngrep -e "source repository is shallow, reject to clone." err &&
git -c clone.rejectshallow=true clone --no-reject-shallow --no-local shallow-repo out
'

test_expect_success 'clone.rejectshallow=true should succeed cloning normal repo' '
test_when_finished "rm -rf out" &&
git -c clone.rejectshallow=true clone --no-local . out
'

test_expect_success MINGW 'clone -c core.hideDotFiles' '
test_commit attributes .gitattributes "" &&
rm -rf child &&
Expand Down
4 changes: 4 additions & 0 deletions transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ static int set_git_option(struct git_transport_options *opts,
list_objects_filter_die_if_populated(&opts->filter_options);
parse_list_objects_filter(&opts->filter_options, value);
return 0;
} else if (!strcmp(name, TRANS_OPT_REJECT_SHALLOW)) {
opts->reject_shallow = !!value;
return 0;
}
return 1;
}
Expand Down Expand Up @@ -370,6 +373,7 @@ static int fetch_refs_via_pack(struct transport *transport,
args.stateless_rpc = transport->stateless_rpc;
args.server_options = transport->server_options;
args.negotiation_tips = data->options.negotiation_tips;
args.reject_shallow_remote = transport->smart_options->reject_shallow;

if (!data->got_remote_heads) {
int i;
Expand Down
4 changes: 4 additions & 0 deletions transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ struct git_transport_options {
unsigned check_self_contained_and_connected : 1;
unsigned self_contained_and_connected : 1;
unsigned update_shallow : 1;
unsigned reject_shallow : 1;
unsigned deepen_relative : 1;

/* see documentation of corresponding flag in fetch-pack.h */
Expand Down Expand Up @@ -194,6 +195,9 @@ void transport_check_allowed(const char *type);
/* Aggressively fetch annotated tags if possible */
#define TRANS_OPT_FOLLOWTAGS "followtags"

/* Reject shallow repo transport */
#define TRANS_OPT_REJECT_SHALLOW "rejectshallow"

/* Accept refs that may update .git/shallow without --depth */
#define TRANS_OPT_UPDATE_SHALLOW "updateshallow"

Expand Down

0 comments on commit 22eee7f

Please sign in to comment.