{"thread":{"id":"42535","subject":"[PATCH v5 1/2] builtin/apply: add 'lock_file' pointer into 'struct apply_state'","startedAt":"2016-06-06T09:56:57Z","lastAt":"2016-06-06T09:56:58Z","messageCount":2,"participants":["Christian Couder"],"isPatch":true,"patchVersion":5,"patchTotal":2},"messages":[{"id":"288411","messageId":"20160606095658.1815-1-chriscool@tuxfamily.org","threadId":"42535","inReplyTo":null,"subject":"[PATCH v5 1/2] builtin/apply: add 'lock_file' pointer into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-06-06T09:56:57Z","receivedAt":"2016-06-06T09:56:57Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"From: Christian Couder <christian.couder@gmail.com>\n\nWe cannot have a 'struct lock_file' allocated on the stack, as lockfile.c\nkeeps a linked list of all created lock_file structures.\n\nAlso 'struct apply_state' users might later want the same 'struct lock_file'\ninstance to be reused by different series of calls to the apply api.\n\nSo let's add a 'struct lock_file *lock_file' pointer into 'struct apply_state'\nand have the user of 'struct apply_state' allocate memory for the actual\n'struct lock_file' instance.\n\nLet's also add an argument to init_apply_state(), so that the caller can\neasily supply a pointer to the allocated instance.\n\nHelped-by: Eric Sunshine <sunshine@sunshineco.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n\nThis is to replace:\n\n\"[PATCH v3 48/49] builtin/apply: move 'lock_file' global into 'struct apply_state'\"\n\nfrom the \"libify apply and use lib in am, part 1\" patch series, and\n\n\"[PATCH v4 1/2] builtin/apply: add 'lock_file' pointer into 'struct apply_state'\"\n\nthat was sent previously to replace v3 48/49 above.\n\nSee: http://thread.gmane.org/gmane.comp.version-control.git/296350/\n\nThe only change compared to v4 1/2 is that the comment above 'struct\nlock_file *lock_file' in 'struct apply_state' has been replaced with a\nmuch shorter one.\n\nThis 2 patch long patch series on top of the other unchanged commits\nfrom v3 is available here:\n\nhttps://github.com/chriscool/git/commits/libify-apply64\n\n builtin/apply.c | 16 ++++++++++++----\n 1 file changed, 12 insertions(+), 4 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 5027f1b..bbe0df1 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -52,6 +52,9 @@ struct apply_state {\n \tconst char *prefix;\n \tint prefix_length;\n \n+\t/* These are lock_file related */\n+\tstruct lock_file *lock_file;\n+\n \t/* These control what gets looked at and modified */\n \tint apply; /* this is not a dry-run */\n \tint cached; /* apply to the index only */\n@@ -4547,7 +4550,7 @@ static int apply_patch(struct apply_state *state,\n \n \tstate->update_index = state->check_index && state->apply;\n \tif (state->update_index && newfd < 0)\n-\t\tnewfd = hold_locked_index(&lock_file, 1);\n+\t\tnewfd = hold_locked_index(state->lock_file, 1);\n \n \tif (state->check_index) {\n \t\tif (read_cache() < 0)\n@@ -4648,11 +4651,14 @@ static int option_parse_directory(const struct option *opt,\n \treturn 0;\n }\n \n-static void init_apply_state(struct apply_state *state, const char *prefix)\n+static void init_apply_state(struct apply_state *state,\n+\t\t\t     const char *prefix,\n+\t\t\t     struct lock_file *lock_file)\n {\n \tmemset(state, 0, sizeof(*state));\n \tstate->prefix = prefix;\n \tstate->prefix_length = state->prefix ? strlen(state->prefix) : 0;\n+\tstate->lock_file = lock_file;\n \tstate->apply = 1;\n \tstate->line_termination = '\\n';\n \tstate->p_value = 1;\n@@ -4705,6 +4711,8 @@ static void check_apply_state(struct apply_state *state, int force_apply)\n \t}\n \tif (state->check_index)\n \t\tstate->unsafe_paths = 0;\n+\tif (!state->lock_file)\n+\t\tdie(\"BUG: state->lock_file should not be NULL\");\n }\n \n static int apply_all_patches(struct apply_state *state,\n@@ -4769,7 +4777,7 @@ static int apply_all_patches(struct apply_state *state,\n \t}\n \n \tif (state->update_index) {\n-\t\tif (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))\n+\t\tif (write_locked_index(&the_index, state->lock_file, COMMIT_LOCK))\n \t\t\tdie(_(\"Unable to write new index file\"));\n \t}\n \n@@ -4852,7 +4860,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\tOPT_END()\n \t};\n \n-\tinit_apply_state(&state, prefix);\n+\tinit_apply_state(&state, prefix, &lock_file);\n \n \targc = parse_options(argc, argv, state.prefix, builtin_apply_options,\n \t\t\tapply_usage, 0);\n-- \n2.8.2.445.g4623162\n"},{"id":"288412","messageId":"20160606095658.1815-2-chriscool@tuxfamily.org","threadId":"42535","inReplyTo":"20160606095658.1815-1-chriscool@tuxfamily.org","subject":"[PATCH v5 2/2] builtin/apply: move 'newfd' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-06-06T09:56:58Z","receivedAt":"2016-06-06T09:56:58Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"From: Christian Couder <christian.couder@gmail.com>\n\nTo libify the apply functionality the 'newfd' variable should\nnot be static and global to the file. Let's move it into\n'struct apply_state'.\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n\nThis is to replace:\n\n\"[PATCH v3 49/49] builtin/apply: move 'newfd' global into 'struct apply_state'\"\n\nfrom the \"libify apply and use lib in am, part 1\" patch series, and\n\n\"[PATCH v4 2/2] builtin/apply: move 'newfd' global into 'struct apply_state'\"\n\nthat was sent previously to replace v3 49/49 above.\n\nThere is no change compared to both previous version except that this\npatch should apply cleanly on top of PATCH v5 1/2.\n\n builtin/apply.c | 9 +++++----\n 1 file changed, 5 insertions(+), 4 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex bbe0df1..b3eb704 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -54,6 +54,7 @@ struct apply_state {\n \n \t/* These are lock_file related */\n \tstruct lock_file *lock_file;\n+\tint newfd;\n \n \t/* These control what gets looked at and modified */\n \tint apply; /* this is not a dry-run */\n@@ -117,8 +118,6 @@ struct apply_state {\n \tint applied_after_fixing_ws;\n };\n \n-static int newfd = -1;\n-\n static const char * const apply_usage[] = {\n \tN_(\"git apply [<options>] [<patch>...]\"),\n \tNULL\n@@ -4549,8 +4548,8 @@ static int apply_patch(struct apply_state *state,\n \t\tstate->apply = 0;\n \n \tstate->update_index = state->check_index && state->apply;\n-\tif (state->update_index && newfd < 0)\n-\t\tnewfd = hold_locked_index(state->lock_file, 1);\n+\tif (state->update_index && state->newfd < 0)\n+\t\tstate->newfd = hold_locked_index(state->lock_file, 1);\n \n \tif (state->check_index) {\n \t\tif (read_cache() < 0)\n@@ -4659,6 +4658,7 @@ static void init_apply_state(struct apply_state *state,\n \tstate->prefix = prefix;\n \tstate->prefix_length = state->prefix ? strlen(state->prefix) : 0;\n \tstate->lock_file = lock_file;\n+\tstate->newfd = -1;\n \tstate->apply = 1;\n \tstate->line_termination = '\\n';\n \tstate->p_value = 1;\n@@ -4779,6 +4779,7 @@ static int apply_all_patches(struct apply_state *state,\n \tif (state->update_index) {\n \t\tif (write_locked_index(&the_index, state->lock_file, COMMIT_LOCK))\n \t\t\tdie(_(\"Unable to write new index file\"));\n+\t\tstate->newfd = -1;\n \t}\n \n \treturn !!errs;\n-- \n2.8.2.445.g4623162\n"}]}