|
| 1 | +#include "builtin.h" |
| 2 | +#include "config.h" |
| 3 | +#include "dir.h" |
| 4 | +#include "parse-options.h" |
| 5 | +#include "pathspec.h" |
| 6 | +#include "repository.h" |
| 7 | +#include "run-command.h" |
| 8 | +#include "strbuf.h" |
| 9 | + |
| 10 | +static char const * const builtin_sparse_checkout_usage[] = { |
| 11 | + N_("git sparse-checkout list"), |
| 12 | + NULL |
| 13 | +}; |
| 14 | + |
| 15 | +static char *get_sparse_checkout_filename(void) |
| 16 | +{ |
| 17 | + return git_pathdup("info/sparse-checkout"); |
| 18 | +} |
| 19 | + |
| 20 | +static void write_patterns_to_file(FILE *fp, struct pattern_list *pl) |
| 21 | +{ |
| 22 | + int i; |
| 23 | + |
| 24 | + for (i = 0; i < pl->nr; i++) { |
| 25 | + struct path_pattern *p = pl->patterns[i]; |
| 26 | + |
| 27 | + if (p->flags & PATTERN_FLAG_NEGATIVE) |
| 28 | + fprintf(fp, "!"); |
| 29 | + |
| 30 | + fprintf(fp, "%s", p->pattern); |
| 31 | + |
| 32 | + if (p->flags & PATTERN_FLAG_MUSTBEDIR) |
| 33 | + fprintf(fp, "/"); |
| 34 | + |
| 35 | + fprintf(fp, "\n"); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +static int sparse_checkout_list(int argc, const char **argv) |
| 40 | +{ |
| 41 | + struct pattern_list pl; |
| 42 | + char *sparse_filename; |
| 43 | + int res; |
| 44 | + |
| 45 | + memset(&pl, 0, sizeof(pl)); |
| 46 | + |
| 47 | + sparse_filename = get_sparse_checkout_filename(); |
| 48 | + res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL); |
| 49 | + free(sparse_filename); |
| 50 | + |
| 51 | + if (res < 0) { |
| 52 | + warning(_("this worktree is not sparse (sparse-checkout file may not exist)")); |
| 53 | + return 0; |
| 54 | + } |
| 55 | + |
| 56 | + write_patterns_to_file(stdout, &pl); |
| 57 | + clear_pattern_list(&pl); |
| 58 | + |
| 59 | + return 0; |
| 60 | +} |
| 61 | + |
| 62 | +int cmd_sparse_checkout(int argc, const char **argv, const char *prefix) |
| 63 | +{ |
| 64 | + static struct option builtin_sparse_checkout_options[] = { |
| 65 | + OPT_END(), |
| 66 | + }; |
| 67 | + |
| 68 | + if (argc == 2 && !strcmp(argv[1], "-h")) |
| 69 | + usage_with_options(builtin_sparse_checkout_usage, |
| 70 | + builtin_sparse_checkout_options); |
| 71 | + |
| 72 | + argc = parse_options(argc, argv, prefix, |
| 73 | + builtin_sparse_checkout_options, |
| 74 | + builtin_sparse_checkout_usage, |
| 75 | + PARSE_OPT_STOP_AT_NON_OPTION); |
| 76 | + |
| 77 | + git_config(git_default_config, NULL); |
| 78 | + |
| 79 | + if (argc > 0) { |
| 80 | + if (!strcmp(argv[0], "list")) |
| 81 | + return sparse_checkout_list(argc, argv); |
| 82 | + } |
| 83 | + |
| 84 | + usage_with_options(builtin_sparse_checkout_usage, |
| 85 | + builtin_sparse_checkout_options); |
| 86 | +} |
0 commit comments