{"thread":{"id":"20415","subject":"[PATCH v2 1/4] sequencer: add \"reset_almost_hard()\" and related functions","startedAt":"2009-08-05T20:52:57Z","lastAt":"2009-08-05T20:52:57Z","messageCount":1,"participants":["Christian Couder"],"isPatch":true,"patchVersion":2,"patchTotal":4},"messages":[{"id":"119705","messageId":"20090805205301.3673.63923.chriscool@tuxfamily.org","threadId":"20415","inReplyTo":null,"subject":"[PATCH v2 1/4] sequencer: add \"reset_almost_hard()\" and related functions","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2009-08-05T20:52:57Z","receivedAt":"2009-08-05T20:52:57Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"From: Stephan Beyer <s-beyer@gmx.net>\n\nThis patch adds some code that comes as is from the sequencer GSoC\nproject:\n\ngit://repo.or.cz/git/sbeyer.git\n\n(commit e7b8dab0c2a73ade92017a52bb1405ea1534ef20)\n\nIt adds some static variables and the following functions:\n\n\t- parse_and_init_tree_desc()\n\t- reset_index_file()\n\t- reset_almost_hard()\n\t- set_verbosity()\n\nMentored-by: Daniel Barkalow <barkalow@iabervon.org>\nMentored-by: Christian Couder <chriscool@tuxfamily.org>\nSigned-off-by: Stephan Beyer <s-beyer@gmx.net>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin-sequencer--helper.c |   94 +++++++++++++++++++++++++++++++++++++++++++\n 1 files changed, 94 insertions(+), 0 deletions(-)\n\ndiff --git a/builtin-sequencer--helper.c b/builtin-sequencer--helper.c\nindex 1dda525..82a830d 100644\n--- a/builtin-sequencer--helper.c\n+++ b/builtin-sequencer--helper.c\n@@ -2,16 +2,95 @@\n #include \"cache.h\"\n #include \"parse-options.h\"\n #include \"run-command.h\"\n+#include \"refs.h\"\n+#include \"diff.h\"\n+#include \"unpack-trees.h\"\n \n #define SEQ_DIR \"rebase-merge\"\n \n #define PATCH_FILE\tgit_path(SEQ_DIR \"/patch\")\n \n+static char *reflog;\n+\n+static int allow_dirty = 0, verbosity = 1, advice = 1;\n+\n+static unsigned char head_sha1[20];\n+\n static const char * const git_sequencer_helper_usage[] = {\n \t\"git sequencer--helper --make-patch <commit>\",\n \tNULL\n };\n \n+static int parse_and_init_tree_desc(const unsigned char *sha1,\n+\t\t\t\t    struct tree_desc *desc)\n+{\n+\tstruct tree *tree = parse_tree_indirect(sha1);\n+\tif (!tree)\n+\t\treturn 1;\n+\tinit_tree_desc(desc, tree->buffer, tree->size);\n+\treturn 0;\n+}\n+\n+static int reset_index_file(const unsigned char *sha1, int update, int dirty)\n+{\n+\tint nr = 1;\n+\tint newfd;\n+\tstruct tree_desc desc[2];\n+\tstruct unpack_trees_options opts;\n+\tstruct lock_file *lock = xcalloc(1, sizeof(struct lock_file));\n+\n+\tmemset(&opts, 0, sizeof(opts));\n+\topts.head_idx = 1;\n+\topts.src_index = &the_index;\n+\topts.dst_index = &the_index;\n+\topts.reset = 1; /* ignore unmerged entries and overwrite wt files */\n+\topts.merge = 1;\n+\topts.fn = oneway_merge;\n+\tif (verbosity > 2)\n+\t\topts.verbose_update = 1;\n+\tif (update) /* update working tree */\n+\t\topts.update = 1;\n+\n+\tnewfd = hold_locked_index(lock, 1);\n+\n+\tread_cache_unmerged();\n+\n+\tif (dirty) {\n+\t\tif (get_sha1(\"HEAD\", head_sha1))\n+\t\t\treturn error(\"You do not have a valid HEAD.\");\n+\t\tif (parse_and_init_tree_desc(head_sha1, desc))\n+\t\t\treturn error(\"Failed to find tree of HEAD.\");\n+\t\tnr++;\n+\t\topts.fn = twoway_merge;\n+\t}\n+\n+\tif (parse_and_init_tree_desc(sha1, desc + nr - 1))\n+\t\treturn error(\"Failed to find tree of %s.\", sha1_to_hex(sha1));\n+\tif (unpack_trees(nr, desc, &opts))\n+\t\treturn -1;\n+\tif (write_cache(newfd, active_cache, active_nr) ||\n+\t    commit_locked_index(lock))\n+\t\treturn error(\"Could not write new index file.\");\n+\n+\treturn 0;\n+}\n+\n+/*\n+ * Realize reset --hard behavior.\n+ * If allow_dirty is set and there is a dirty working tree,\n+ * then the changes are to be kept.\n+ */\n+static int reset_almost_hard(const unsigned char *sha)\n+{\n+\tint err = allow_dirty ?\n+\t\t(reset_index_file(sha, 1, 1) || reset_index_file(sha, 0, 0)) :\n+\t\treset_index_file(sha, 1, 0);\n+\tif (err)\n+\t\treturn error(\"Could not reset index.\");\n+\n+\treturn update_ref(reflog, \"HEAD\", sha, NULL, 0, MSG_ON_ERR);\n+}\n+\n /* Generate purely informational patch file */\n static void make_patch(struct commit *commit)\n {\n@@ -78,6 +157,21 @@ static struct commit *get_commit(const char *arg)\n \treturn lookup_commit_reference(sha1);\n }\n \n+static int set_verbosity(int verbose)\n+{\n+\tchar tmp[] = \"0\";\n+\tverbosity = verbose;\n+\tif (verbosity <= 0) {\n+\t\tverbosity = 0;\n+\t\tadvice = 0;\n+\t} else if (verbosity > 5)\n+\t\tverbosity = 5;\n+\t/* Git does not run on EBCDIC, so we rely on ASCII: */\n+\ttmp[0] += verbosity;\n+\tsetenv(\"GIT_MERGE_VERBOSITY\", tmp, 1);\n+\treturn 0;\n+}\n+\n int cmd_sequencer__helper(int argc, const char **argv, const char *prefix)\n {\n \tchar *commit = NULL;\n-- \n1.6.4.133.g8a5c8\n"}]}