forked from git/git
-
Notifications
You must be signed in to change notification settings - Fork 142
Sparse-checkout: Add subcommand and Windows paths #546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
derrickstolee
wants to merge
4
commits into
gitgitgadget:ds/sparse-checkout-harden
from
derrickstolee:sparse-add
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
75ee62c
sparse-checkout: extract add_patterns_from_input()
derrickstolee 7990916
sparse-checkout: extract pattern update from 'set' subcommand
derrickstolee 663c40a
sparse-checkout: create 'add' subcommand
derrickstolee f770df4
sparse-checkout: work with Windows paths
derrickstolee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ | |
static const char *empty_base = ""; | ||
|
||
static char const * const builtin_sparse_checkout_usage[] = { | ||
N_("git sparse-checkout (init|list|set|disable) <options>"), | ||
N_("git sparse-checkout (init|list|set|add|disable) <options>"), | ||
NULL | ||
}; | ||
|
||
|
@@ -394,6 +394,9 @@ static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl) | |
|
||
strbuf_trim_trailing_dir_sep(line); | ||
|
||
if (strbuf_normalize_path(line)) | ||
die(_("could not normalize path %s"), line->buf); | ||
|
||
if (!line->len) | ||
return; | ||
|
||
|
@@ -404,44 +407,24 @@ static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl) | |
} | ||
|
||
static char const * const builtin_sparse_checkout_set_usage[] = { | ||
N_("git sparse-checkout set (--stdin | <patterns>)"), | ||
N_("git sparse-checkout (set|add) (--stdin | <patterns>)"), | ||
NULL | ||
}; | ||
|
||
static struct sparse_checkout_set_opts { | ||
int use_stdin; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On the Git mailing list, Junio C Hamano wrote (reply to this):
|
||
} set_opts; | ||
|
||
static int sparse_checkout_set(int argc, const char **argv, const char *prefix) | ||
static void add_patterns_from_input(struct pattern_list *pl, | ||
int argc, const char **argv) | ||
{ | ||
int i; | ||
struct pattern_list pl; | ||
int result; | ||
int changed_config = 0; | ||
|
||
static struct option builtin_sparse_checkout_set_options[] = { | ||
OPT_BOOL(0, "stdin", &set_opts.use_stdin, | ||
N_("read patterns from standard in")), | ||
OPT_END(), | ||
}; | ||
|
||
repo_read_index(the_repository); | ||
require_clean_work_tree(the_repository, | ||
N_("set sparse-checkout patterns"), NULL, 1, 0); | ||
|
||
memset(&pl, 0, sizeof(pl)); | ||
|
||
argc = parse_options(argc, argv, prefix, | ||
builtin_sparse_checkout_set_options, | ||
builtin_sparse_checkout_set_usage, | ||
PARSE_OPT_KEEP_UNKNOWN); | ||
|
||
if (core_sparse_checkout_cone) { | ||
struct strbuf line = STRBUF_INIT; | ||
|
||
hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0); | ||
hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0); | ||
pl.use_cone_patterns = 1; | ||
hashmap_init(&pl->recursive_hashmap, pl_hashmap_cmp, NULL, 0); | ||
hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0); | ||
pl->use_cone_patterns = 1; | ||
|
||
if (set_opts.use_stdin) { | ||
struct strbuf unquoted = STRBUF_INIT; | ||
|
@@ -455,15 +438,15 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix) | |
strbuf_swap(&unquoted, &line); | ||
} | ||
|
||
strbuf_to_cone_pattern(&line, &pl); | ||
strbuf_to_cone_pattern(&line, pl); | ||
} | ||
|
||
strbuf_release(&unquoted); | ||
} else { | ||
for (i = 0; i < argc; i++) { | ||
strbuf_setlen(&line, 0); | ||
strbuf_addstr(&line, argv[i]); | ||
strbuf_to_cone_pattern(&line, &pl); | ||
strbuf_to_cone_pattern(&line, pl); | ||
} | ||
} | ||
} else { | ||
|
@@ -473,13 +456,84 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix) | |
while (!strbuf_getline(&line, stdin)) { | ||
size_t len; | ||
char *buf = strbuf_detach(&line, &len); | ||
add_pattern(buf, empty_base, 0, &pl, 0); | ||
add_pattern(buf, empty_base, 0, pl, 0); | ||
} | ||
} else { | ||
for (i = 0; i < argc; i++) | ||
add_pattern(argv[i], empty_base, 0, &pl, 0); | ||
add_pattern(argv[i], empty_base, 0, pl, 0); | ||
} | ||
} | ||
} | ||
|
||
enum modify_type { | ||
REPLACE, | ||
ADD, | ||
}; | ||
|
||
static void add_patterns_cone_mode(int argc, const char **argv, | ||
struct pattern_list *pl) | ||
{ | ||
struct strbuf buffer = STRBUF_INIT; | ||
struct pattern_entry *pe; | ||
struct hashmap_iter iter; | ||
struct pattern_list existing; | ||
char *sparse_filename = get_sparse_checkout_filename(); | ||
|
||
add_patterns_from_input(pl, argc, argv); | ||
|
||
memset(&existing, 0, sizeof(existing)); | ||
existing.use_cone_patterns = core_sparse_checkout_cone; | ||
|
||
if (add_patterns_from_file_to_list(sparse_filename, "", 0, | ||
&existing, NULL)) | ||
die(_("unable to load existing sparse-checkout patterns")); | ||
free(sparse_filename); | ||
|
||
hashmap_for_each_entry(&existing.recursive_hashmap, &iter, pe, ent) { | ||
if (!hashmap_contains_parent(&pl->recursive_hashmap, | ||
pe->pattern, &buffer) || | ||
!hashmap_contains_parent(&pl->parent_hashmap, | ||
pe->pattern, &buffer)) { | ||
strbuf_reset(&buffer); | ||
strbuf_addstr(&buffer, pe->pattern); | ||
insert_recursive_pattern(pl, &buffer); | ||
} | ||
} | ||
|
||
clear_pattern_list(&existing); | ||
strbuf_release(&buffer); | ||
} | ||
|
||
static void add_patterns_literal(int argc, const char **argv, | ||
struct pattern_list *pl) | ||
{ | ||
char *sparse_filename = get_sparse_checkout_filename(); | ||
if (add_patterns_from_file_to_list(sparse_filename, "", 0, | ||
pl, NULL)) | ||
die(_("unable to load existing sparse-checkout patterns")); | ||
free(sparse_filename); | ||
add_patterns_from_input(pl, argc, argv); | ||
} | ||
|
||
static int modify_pattern_list(int argc, const char **argv, enum modify_type m) | ||
{ | ||
int result; | ||
int changed_config = 0; | ||
struct pattern_list pl; | ||
memset(&pl, 0, sizeof(pl)); | ||
|
||
switch (m) { | ||
case ADD: | ||
if (core_sparse_checkout_cone) | ||
add_patterns_cone_mode(argc, argv, &pl); | ||
else | ||
add_patterns_literal(argc, argv, &pl); | ||
break; | ||
|
||
case REPLACE: | ||
add_patterns_from_input(&pl, argc, argv); | ||
break; | ||
} | ||
|
||
if (!core_apply_sparse_checkout) { | ||
set_config(MODE_ALL_PATTERNS); | ||
|
@@ -496,6 +550,27 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix) | |
return result; | ||
} | ||
|
||
static int sparse_checkout_set(int argc, const char **argv, const char *prefix, | ||
enum modify_type m) | ||
{ | ||
static struct option builtin_sparse_checkout_set_options[] = { | ||
OPT_BOOL(0, "stdin", &set_opts.use_stdin, | ||
N_("read patterns from standard in")), | ||
OPT_END(), | ||
}; | ||
|
||
repo_read_index(the_repository); | ||
require_clean_work_tree(the_repository, | ||
N_("set sparse-checkout patterns"), NULL, 1, 0); | ||
|
||
argc = parse_options(argc, argv, prefix, | ||
builtin_sparse_checkout_set_options, | ||
builtin_sparse_checkout_set_usage, | ||
PARSE_OPT_KEEP_UNKNOWN); | ||
|
||
return modify_pattern_list(argc, argv, m); | ||
} | ||
|
||
static int sparse_checkout_disable(int argc, const char **argv) | ||
{ | ||
struct pattern_list pl; | ||
|
@@ -544,7 +619,9 @@ int cmd_sparse_checkout(int argc, const char **argv, const char *prefix) | |
if (!strcmp(argv[0], "init")) | ||
return sparse_checkout_init(argc, argv); | ||
if (!strcmp(argv[0], "set")) | ||
return sparse_checkout_set(argc, argv, prefix); | ||
return sparse_checkout_set(argc, argv, prefix, REPLACE); | ||
if (!strcmp(argv[0], "add")) | ||
return sparse_checkout_set(argc, argv, prefix, ADD); | ||
if (!strcmp(argv[0], "disable")) | ||
return sparse_checkout_disable(argc, argv); | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On the Git mailing list, Junio C Hamano wrote (reply to this):