const char *prefix;
int prefix_length;
+ /* These are lock_file related */
+ struct lock_file *lock_file;
+ int newfd;
+
/* These control what gets looked at and modified */
int apply; /* this is not a dry-run */
int cached; /* apply to the index only */
int applied_after_fixing_ws;
};
-static int newfd = -1;
-
static const char * const apply_usage[] = {
N_("git apply [<options>] [<patch>...]"),
NULL
#define TERM_SPACE 1
#define TERM_TAB 2
-static int name_terminate(const char *name, int namelen, int c, int terminate)
+static int name_terminate(int c, int terminate)
{
if (c == ' ' && !(terminate & TERM_SPACE))
return 0;
if (!end && isspace(c)) {
if (c == '\n')
break;
- if (name_terminate(start, line-start, c, terminate))
+ if (name_terminate(c, terminate))
break;
}
line++;
state->apply = 0;
state->update_index = state->check_index && state->apply;
- if (state->update_index && newfd < 0)
- newfd = hold_locked_index(&lock_file, 1);
+ if (state->update_index && state->newfd < 0)
+ state->newfd = hold_locked_index(state->lock_file, 1);
if (state->check_index) {
if (read_cache() < 0)
return 0;
}
-static void init_apply_state(struct apply_state *state, const char *prefix)
+static void init_apply_state(struct apply_state *state,
+ const char *prefix,
+ struct lock_file *lock_file)
{
memset(state, 0, sizeof(*state));
state->prefix = prefix;
state->prefix_length = state->prefix ? strlen(state->prefix) : 0;
+ state->lock_file = lock_file;
+ state->newfd = -1;
state->apply = 1;
state->line_termination = '\n';
state->p_value = 1;
state->ws_error_action = warn_on_ws_error;
state->ws_ignore_action = ignore_ws_none;
state->linenr = 1;
+ string_list_init(&state->fn_table, 0);
+ string_list_init(&state->limit_by_name, 0);
+ string_list_init(&state->symlink_changes, 0);
strbuf_init(&state->root, 0);
git_apply_config();
/* &state->fn_table is cleared at the end of apply_patch() */
}
-int cmd_apply(int argc, const char **argv, const char *prefix)
+static void check_apply_state(struct apply_state *state, int force_apply)
+{
+ int is_not_gitdir = !startup_info->have_repository;
+
+ if (state->apply_with_reject && state->threeway)
+ die("--reject and --3way cannot be used together.");
+ if (state->cached && state->threeway)
+ die("--cached and --3way cannot be used together.");
+ if (state->threeway) {
+ if (is_not_gitdir)
+ die(_("--3way outside a repository"));
+ state->check_index = 1;
+ }
+ if (state->apply_with_reject)
+ state->apply = state->apply_verbosely = 1;
+ if (!force_apply && (state->diffstat || state->numstat || state->summary || state->check || state->fake_ancestor))
+ state->apply = 0;
+ if (state->check_index && is_not_gitdir)
+ die(_("--index outside a repository"));
+ if (state->cached) {
+ if (is_not_gitdir)
+ die(_("--cached outside a repository"));
+ state->check_index = 1;
+ }
+ if (state->check_index)
+ state->unsafe_paths = 0;
+ if (!state->lock_file)
+ die("BUG: state->lock_file should not be NULL");
+}
+
+static int apply_all_patches(struct apply_state *state,
+ int argc,
+ const char **argv,
+ int options)
{
int i;
int errs = 0;
- int is_not_gitdir = !startup_info->have_repository;
+ int read_stdin = 1;
+
+ for (i = 0; i < argc; i++) {
+ const char *arg = argv[i];
+ int fd;
+
+ if (!strcmp(arg, "-")) {
+ errs |= apply_patch(state, 0, "<stdin>", options);
+ read_stdin = 0;
+ continue;
+ } else if (0 < state->prefix_length)
+ arg = prefix_filename(state->prefix,
+ state->prefix_length,
+ arg);
+
+ fd = open(arg, O_RDONLY);
+ if (fd < 0)
+ die_errno(_("can't open patch '%s'"), arg);
+ read_stdin = 0;
+ set_default_whitespace_mode(state);
+ errs |= apply_patch(state, fd, arg, options);
+ close(fd);
+ }
+ set_default_whitespace_mode(state);
+ if (read_stdin)
+ errs |= apply_patch(state, 0, "<stdin>", options);
+
+ if (state->whitespace_error) {
+ if (state->squelch_whitespace_errors &&
+ state->squelch_whitespace_errors < state->whitespace_error) {
+ int squelched =
+ state->whitespace_error - state->squelch_whitespace_errors;
+ warning(Q_("squelched %d whitespace error",
+ "squelched %d whitespace errors",
+ squelched),
+ squelched);
+ }
+ if (state->ws_error_action == die_on_ws_error)
+ die(Q_("%d line adds whitespace errors.",
+ "%d lines add whitespace errors.",
+ state->whitespace_error),
+ state->whitespace_error);
+ if (state->applied_after_fixing_ws && state->apply)
+ warning("%d line%s applied after"
+ " fixing whitespace errors.",
+ state->applied_after_fixing_ws,
+ state->applied_after_fixing_ws == 1 ? "" : "s");
+ else if (state->whitespace_error)
+ warning(Q_("%d line adds whitespace errors.",
+ "%d lines add whitespace errors.",
+ state->whitespace_error),
+ state->whitespace_error);
+ }
+
+ if (state->update_index) {
+ if (write_locked_index(&the_index, state->lock_file, COMMIT_LOCK))
+ die(_("Unable to write new index file"));
+ state->newfd = -1;
+ }
+
+ return !!errs;
+}
+
+int cmd_apply(int argc, const char **argv, const char *prefix)
+{
int force_apply = 0;
int options = 0;
- int read_stdin = 1;
+ int ret;
struct apply_state state;
struct option builtin_apply_options[] = {
OPT_END()
};
- init_apply_state(&state, prefix);
+ init_apply_state(&state, prefix, &lock_file);
argc = parse_options(argc, argv, state.prefix, builtin_apply_options,
apply_usage, 0);
- if (state.apply_with_reject && state.threeway)
- die("--reject and --3way cannot be used together.");
- if (state.cached && state.threeway)
- die("--cached and --3way cannot be used together.");
- if (state.threeway) {
- if (is_not_gitdir)
- die(_("--3way outside a repository"));
- state.check_index = 1;
- }
- if (state.apply_with_reject)
- state.apply = state.apply_verbosely = 1;
- if (!force_apply && (state.diffstat || state.numstat || state.summary || state.check || state.fake_ancestor))
- state.apply = 0;
- if (state.check_index && is_not_gitdir)
- die(_("--index outside a repository"));
- if (state.cached) {
- if (is_not_gitdir)
- die(_("--cached outside a repository"));
- state.check_index = 1;
- }
- if (state.check_index)
- state.unsafe_paths = 0;
+ check_apply_state(&state, force_apply);
- for (i = 0; i < argc; i++) {
- const char *arg = argv[i];
- int fd;
-
- if (!strcmp(arg, "-")) {
- errs |= apply_patch(&state, 0, "<stdin>", options);
- read_stdin = 0;
- continue;
- } else if (0 < state.prefix_length)
- arg = prefix_filename(state.prefix,
- state.prefix_length,
- arg);
-
- fd = open(arg, O_RDONLY);
- if (fd < 0)
- die_errno(_("can't open patch '%s'"), arg);
- read_stdin = 0;
- set_default_whitespace_mode(&state);
- errs |= apply_patch(&state, fd, arg, options);
- close(fd);
- }
- set_default_whitespace_mode(&state);
- if (read_stdin)
- errs |= apply_patch(&state, 0, "<stdin>", options);
- if (state.whitespace_error) {
- if (state.squelch_whitespace_errors &&
- state.squelch_whitespace_errors < state.whitespace_error) {
- int squelched =
- state.whitespace_error - state.squelch_whitespace_errors;
- warning(Q_("squelched %d whitespace error",
- "squelched %d whitespace errors",
- squelched),
- squelched);
- }
- if (state.ws_error_action == die_on_ws_error)
- die(Q_("%d line adds whitespace errors.",
- "%d lines add whitespace errors.",
- state.whitespace_error),
- state.whitespace_error);
- if (state.applied_after_fixing_ws && state.apply)
- warning("%d line%s applied after"
- " fixing whitespace errors.",
- state.applied_after_fixing_ws,
- state.applied_after_fixing_ws == 1 ? "" : "s");
- else if (state.whitespace_error)
- warning(Q_("%d line adds whitespace errors.",
- "%d lines add whitespace errors.",
- state.whitespace_error),
- state.whitespace_error);
- }
-
- if (state.update_index) {
- if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
- die(_("Unable to write new index file"));
- }
+ ret = apply_all_patches(&state, argc, argv, options);
clear_apply_state(&state);
- return !!errs;
+ return ret;
}