]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/apply.c
apply: move libified code from builtin/apply.c to apply.{c,h}
[thirdparty/git.git] / builtin / apply.c
1 #include "cache.h"
2 #include "builtin.h"
3 #include "parse-options.h"
4 #include "lockfile.h"
5 #include "apply.h"
6
7 static const char * const apply_usage[] = {
8 N_("git apply [<options>] [<patch>...]"),
9 NULL
10 };
11
12 static struct lock_file lock_file;
13
14 int cmd_apply(int argc, const char **argv, const char *prefix)
15 {
16 int force_apply = 0;
17 int options = 0;
18 int ret;
19 struct apply_state state;
20
21 struct option builtin_apply_options[] = {
22 { OPTION_CALLBACK, 0, "exclude", &state, N_("path"),
23 N_("don't apply changes matching the given path"),
24 0, apply_option_parse_exclude },
25 { OPTION_CALLBACK, 0, "include", &state, N_("path"),
26 N_("apply changes matching the given path"),
27 0, apply_option_parse_include },
28 { OPTION_CALLBACK, 'p', NULL, &state, N_("num"),
29 N_("remove <num> leading slashes from traditional diff paths"),
30 0, apply_option_parse_p },
31 OPT_BOOL(0, "no-add", &state.no_add,
32 N_("ignore additions made by the patch")),
33 OPT_BOOL(0, "stat", &state.diffstat,
34 N_("instead of applying the patch, output diffstat for the input")),
35 OPT_NOOP_NOARG(0, "allow-binary-replacement"),
36 OPT_NOOP_NOARG(0, "binary"),
37 OPT_BOOL(0, "numstat", &state.numstat,
38 N_("show number of added and deleted lines in decimal notation")),
39 OPT_BOOL(0, "summary", &state.summary,
40 N_("instead of applying the patch, output a summary for the input")),
41 OPT_BOOL(0, "check", &state.check,
42 N_("instead of applying the patch, see if the patch is applicable")),
43 OPT_BOOL(0, "index", &state.check_index,
44 N_("make sure the patch is applicable to the current index")),
45 OPT_BOOL(0, "cached", &state.cached,
46 N_("apply a patch without touching the working tree")),
47 OPT_BOOL(0, "unsafe-paths", &state.unsafe_paths,
48 N_("accept a patch that touches outside the working area")),
49 OPT_BOOL(0, "apply", &force_apply,
50 N_("also apply the patch (use with --stat/--summary/--check)")),
51 OPT_BOOL('3', "3way", &state.threeway,
52 N_( "attempt three-way merge if a patch does not apply")),
53 OPT_FILENAME(0, "build-fake-ancestor", &state.fake_ancestor,
54 N_("build a temporary index based on embedded index information")),
55 /* Think twice before adding "--nul" synonym to this */
56 OPT_SET_INT('z', NULL, &state.line_termination,
57 N_("paths are separated with NUL character"), '\0'),
58 OPT_INTEGER('C', NULL, &state.p_context,
59 N_("ensure at least <n> lines of context match")),
60 { OPTION_CALLBACK, 0, "whitespace", &state, N_("action"),
61 N_("detect new or modified lines that have whitespace errors"),
62 0, apply_option_parse_whitespace },
63 { OPTION_CALLBACK, 0, "ignore-space-change", &state, NULL,
64 N_("ignore changes in whitespace when finding context"),
65 PARSE_OPT_NOARG, apply_option_parse_space_change },
66 { OPTION_CALLBACK, 0, "ignore-whitespace", &state, NULL,
67 N_("ignore changes in whitespace when finding context"),
68 PARSE_OPT_NOARG, apply_option_parse_space_change },
69 OPT_BOOL('R', "reverse", &state.apply_in_reverse,
70 N_("apply the patch in reverse")),
71 OPT_BOOL(0, "unidiff-zero", &state.unidiff_zero,
72 N_("don't expect at least one line of context")),
73 OPT_BOOL(0, "reject", &state.apply_with_reject,
74 N_("leave the rejected hunks in corresponding *.rej files")),
75 OPT_BOOL(0, "allow-overlap", &state.allow_overlap,
76 N_("allow overlapping hunks")),
77 OPT__VERBOSE(&state.apply_verbosely, N_("be verbose")),
78 OPT_BIT(0, "inaccurate-eof", &options,
79 N_("tolerate incorrectly detected missing new-line at the end of file"),
80 APPLY_OPT_INACCURATE_EOF),
81 OPT_BIT(0, "recount", &options,
82 N_("do not trust the line counts in the hunk headers"),
83 APPLY_OPT_RECOUNT),
84 { OPTION_CALLBACK, 0, "directory", &state, N_("root"),
85 N_("prepend <root> to all filenames"),
86 0, apply_option_parse_directory },
87 OPT_END()
88 };
89
90 if (init_apply_state(&state, prefix, &lock_file))
91 exit(128);
92
93 argc = parse_options(argc, argv, state.prefix, builtin_apply_options,
94 apply_usage, 0);
95
96 if (check_apply_state(&state, force_apply))
97 exit(128);
98
99 ret = apply_all_patches(&state, argc, argv, options);
100
101 clear_apply_state(&state);
102
103 return ret;
104 }