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