|
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 |
|
@@ -924,6 +925,73 @@ static int sparse_checkout_reapply(int argc, const char **argv,
|
924 | 925 | return update_working_directory(repo, NULL);
|
925 | 926 | }
|
926 | 927 |
|
| 928 | +static char const * const builtin_sparse_checkout_clean_usage[] = { |
| 929 | + "git sparse-checkout clean [-n|--dry-run]", |
| 930 | + NULL |
| 931 | +}; |
| 932 | + |
| 933 | +static struct sparse_checkout_clean_opts { |
| 934 | + int dry_run; |
| 935 | +} clean_opts; |
| 936 | + |
| 937 | +static const char *msg_remove = N_("Removing %s\n"); |
| 938 | +static const char *msg_would_remove = N_("Would remove %s\n"); |
| 939 | + |
| 940 | +static int sparse_checkout_clean(int argc, const char **argv, |
| 941 | + const char *prefix, |
| 942 | + struct repository *repo) |
| 943 | +{ |
| 944 | + struct strbuf full_path = STRBUF_INIT; |
| 945 | + size_t worktree_len; |
| 946 | + static struct option builtin_sparse_checkout_clean_options[] = { |
| 947 | + OPT_BOOL('n', "dry-run", &clean_opts.dry_run, |
| 948 | + N_("list the directories that would be removed without making filesystem changes")), |
| 949 | + OPT_END(), |
| 950 | + }; |
| 951 | + |
| 952 | + setup_work_tree(); |
| 953 | + if (!repo->settings.sparse_checkout) |
| 954 | + die(_("must be in a sparse-checkout to clean directories")); |
| 955 | + if (!repo->settings.sparse_checkout_cone) |
| 956 | + die(_("must be in a cone-mode sparse-checkout to clean directories")); |
| 957 | + |
| 958 | + argc = parse_options(argc, argv, prefix, |
| 959 | + builtin_sparse_checkout_clean_options, |
| 960 | + builtin_sparse_checkout_clean_usage, 0); |
| 961 | + |
| 962 | + if (repo_read_index(repo) < 0) |
| 963 | + die(_("failed to read index")); |
| 964 | + |
| 965 | + if (convert_to_sparse(repo->index, SPARSE_INDEX_MEMORY_ONLY)) |
| 966 | + die(_("failed to convert index to a sparse index")); |
| 967 | + |
| 968 | + strbuf_addstr(&full_path, repo->worktree); |
| 969 | + strbuf_addch(&full_path, '/'); |
| 970 | + worktree_len = full_path.len; |
| 971 | + |
| 972 | + for (size_t i = 0; i < repo->index->cache_nr; i++) { |
| 973 | + struct cache_entry *ce = repo->index->cache[i]; |
| 974 | + if (!S_ISSPARSEDIR(ce->ce_mode)) |
| 975 | + continue; |
| 976 | + strbuf_setlen(&full_path, worktree_len); |
| 977 | + strbuf_add(&full_path, ce->name, ce->ce_namelen); |
| 978 | + |
| 979 | + if (!is_directory(full_path.buf)) |
| 980 | + continue; |
| 981 | + |
| 982 | + if (!clean_opts.dry_run) { |
| 983 | + printf(msg_remove, ce->name); |
| 984 | + if (remove_dir_recursively(&full_path, 0)) |
| 985 | + warning_errno(_("failed to remove '%s'"), ce->name); |
| 986 | + } else { |
| 987 | + printf(msg_would_remove, ce->name); |
| 988 | + } |
| 989 | + } |
| 990 | + |
| 991 | + strbuf_release(&full_path); |
| 992 | + return 0; |
| 993 | +} |
| 994 | + |
927 | 995 | static char const * const builtin_sparse_checkout_disable_usage[] = {
|
928 | 996 | "git sparse-checkout disable",
|
929 | 997 | NULL
|
@@ -1079,6 +1147,7 @@ int cmd_sparse_checkout(int argc,
|
1079 | 1147 | OPT_SUBCOMMAND("set", &fn, sparse_checkout_set),
|
1080 | 1148 | OPT_SUBCOMMAND("add", &fn, sparse_checkout_add),
|
1081 | 1149 | OPT_SUBCOMMAND("reapply", &fn, sparse_checkout_reapply),
|
| 1150 | + OPT_SUBCOMMAND("clean", &fn, sparse_checkout_clean), |
1082 | 1151 | OPT_SUBCOMMAND("disable", &fn, sparse_checkout_disable),
|
1083 | 1152 | OPT_SUBCOMMAND("check-rules", &fn, sparse_checkout_check_rules),
|
1084 | 1153 | OPT_END(),
|
|
0 commit comments