|  | 
| 2 | 2 | #define DISABLE_SIGN_COMPARE_WARNINGS | 
| 3 | 3 | 
 | 
| 4 | 4 | #include "builtin.h" | 
|  | 5 | +#include "abspath.h" | 
| 5 | 6 | #include "config.h" | 
| 6 | 7 | #include "dir.h" | 
| 7 | 8 | #include "environment.h" | 
|  | 
| 23 | 24 | static const char *empty_base = ""; | 
| 24 | 25 | 
 | 
| 25 | 26 | static char const * const builtin_sparse_checkout_usage[] = { | 
| 26 |  | -	N_("git sparse-checkout (init | list | set | add | reapply | disable | check-rules) [<options>]"), | 
|  | 27 | +	N_("git sparse-checkout (init | list | set | add | reapply | disable | check-rules | clean) [<options>]"), | 
| 27 | 28 | 	NULL | 
| 28 | 29 | }; | 
| 29 | 30 | 
 | 
| @@ -933,6 +934,66 @@ static int sparse_checkout_reapply(int argc, const char **argv, | 
| 933 | 934 | 	return update_working_directory(repo, NULL); | 
| 934 | 935 | } | 
| 935 | 936 | 
 | 
|  | 937 | +static char const * const builtin_sparse_checkout_clean_usage[] = { | 
|  | 938 | +	"git sparse-checkout clean [-n|--dry-run]", | 
|  | 939 | +	NULL | 
|  | 940 | +}; | 
|  | 941 | + | 
|  | 942 | +static const char *msg_remove = N_("Removing %s\n"); | 
|  | 943 | + | 
|  | 944 | +static int sparse_checkout_clean(int argc, const char **argv, | 
|  | 945 | +				   const char *prefix, | 
|  | 946 | +				   struct repository *repo) | 
|  | 947 | +{ | 
|  | 948 | +	struct strbuf full_path = STRBUF_INIT; | 
|  | 949 | +	const char *msg = msg_remove; | 
|  | 950 | +	size_t worktree_len; | 
|  | 951 | + | 
|  | 952 | +	struct option builtin_sparse_checkout_clean_options[] = { | 
|  | 953 | +		OPT_END(), | 
|  | 954 | +	}; | 
|  | 955 | + | 
|  | 956 | +	setup_work_tree(); | 
|  | 957 | +	if (!core_apply_sparse_checkout) | 
|  | 958 | +		die(_("must be in a sparse-checkout to clean directories")); | 
|  | 959 | +	if (!core_sparse_checkout_cone) | 
|  | 960 | +		die(_("must be in a cone-mode sparse-checkout to clean directories")); | 
|  | 961 | + | 
|  | 962 | +	argc = parse_options(argc, argv, prefix, | 
|  | 963 | +			     builtin_sparse_checkout_clean_options, | 
|  | 964 | +			     builtin_sparse_checkout_clean_usage, 0); | 
|  | 965 | + | 
|  | 966 | +	if (repo_read_index(repo) < 0) | 
|  | 967 | +		die(_("failed to read index")); | 
|  | 968 | + | 
|  | 969 | +	if (convert_to_sparse(repo->index, SPARSE_INDEX_MEMORY_ONLY) || | 
|  | 970 | +	    repo->index->sparse_index == INDEX_EXPANDED) | 
|  | 971 | +		die(_("failed to convert index to a sparse index; resolve merge conflicts and try again")); | 
|  | 972 | + | 
|  | 973 | +	strbuf_addstr(&full_path, repo->worktree); | 
|  | 974 | +	strbuf_addch(&full_path, '/'); | 
|  | 975 | +	worktree_len = full_path.len; | 
|  | 976 | + | 
|  | 977 | +	for (size_t i = 0; i < repo->index->cache_nr; i++) { | 
|  | 978 | +		struct cache_entry *ce = repo->index->cache[i]; | 
|  | 979 | +		if (!S_ISSPARSEDIR(ce->ce_mode)) | 
|  | 980 | +			continue; | 
|  | 981 | +		strbuf_setlen(&full_path, worktree_len); | 
|  | 982 | +		strbuf_add(&full_path, ce->name, ce->ce_namelen); | 
|  | 983 | + | 
|  | 984 | +		if (!is_directory(full_path.buf)) | 
|  | 985 | +			continue; | 
|  | 986 | + | 
|  | 987 | +		printf(msg, ce->name); | 
|  | 988 | + | 
|  | 989 | +		if (remove_dir_recursively(&full_path, 0)) | 
|  | 990 | +			warning_errno(_("failed to remove '%s'"), ce->name); | 
|  | 991 | +	} | 
|  | 992 | + | 
|  | 993 | +	strbuf_release(&full_path); | 
|  | 994 | +	return 0; | 
|  | 995 | +} | 
|  | 996 | + | 
| 936 | 997 | static char const * const builtin_sparse_checkout_disable_usage[] = { | 
| 937 | 998 | 	"git sparse-checkout disable", | 
| 938 | 999 | 	NULL | 
| @@ -1089,6 +1150,7 @@ int cmd_sparse_checkout(int argc, | 
| 1089 | 1150 | 		OPT_SUBCOMMAND("set", &fn, sparse_checkout_set), | 
| 1090 | 1151 | 		OPT_SUBCOMMAND("add", &fn, sparse_checkout_add), | 
| 1091 | 1152 | 		OPT_SUBCOMMAND("reapply", &fn, sparse_checkout_reapply), | 
|  | 1153 | +		OPT_SUBCOMMAND("clean", &fn, sparse_checkout_clean), | 
| 1092 | 1154 | 		OPT_SUBCOMMAND("disable", &fn, sparse_checkout_disable), | 
| 1093 | 1155 | 		OPT_SUBCOMMAND("check-rules", &fn, sparse_checkout_check_rules), | 
| 1094 | 1156 | 		OPT_END(), | 
|  | 
0 commit comments