{"thread":{"id":"16221","subject":"[PATCH 1/7 v3] bisect: add \"git bisect replace\" subcommand","startedAt":"2008-11-08T07:48:34Z","lastAt":"2008-11-08T07:48:34Z","messageCount":1,"participants":["Christian Couder"],"isPatch":true,"patchVersion":3,"patchTotal":7},"messages":[{"id":"95197","messageId":"20081108084834.833ddefe.chriscool@tuxfamily.org","threadId":"16221","inReplyTo":null,"subject":"[PATCH 1/7 v3] bisect: add \"git bisect replace\" subcommand","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2008-11-08T07:48:34Z","receivedAt":"2008-11-08T07:48:34Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"This subcommand should be used when you have a branch or a part of a\nbranch that isn't easily bisectable because of a bug that has been\nfixed latter.\n\nWe suppose that a bug as been introduced at some point, say A, and\nthat it has been fixed latter at another point, say B, but that\nbetween these points the code is not easily testable because of the\nbug, so it's not easy to bisect between these points.\n\nIn this case you can create a branch starting at the parent of A, say\nO, that has a fixed history. In this fixed history for example, there\ncould be first a commit C that is the result of squashing A and B\ntogether and then all the commits between A and B that have been\ncherry picked.\n\nFor example, let's say the commits between A and B are X1, X2, ... Xn\nand they have been cherry picked after C as Y1, Y2, ... Yn:\n\n        C--Y1--Y2--...--Yn\n      /\n...--O--A--X1--X2--...--Xn--B--...\n\nBy design, the last cherry picked commit (Yn) should point to the same\ntree as commit B.\n\nSo in this case you can say:\n\n$ git bisect replace B Yn\n\nand a branch will be created that points to commit Yn and that has a\nspecial name like: \"bisect-replace-B\"\n\nWhen bisecting, the branch names will be scanned and each branch named\n\"bisect-replace-X\" and pointing to commit Y will be grafted so that\nX will only have Y as parent.\n\nIn the example above, that means that instead of the above graph, the\nfollowing graph will be bisected:\n\n        C--Y1--Y2--...--Yn\n      /                   \\\n...--O                     B--...\n\nThis means that the bisections on this branch will be much easier\nbecause the bug introduced by commit A and fixed by commit B will not\nannoy you anymore.\n\nAs the branches created by \"git bisect replace\" can be shared between\ndevelopers, this feature might be especially usefull on big projects\nwhere many people often bisect the same code base.\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin-rev-list.c |   71 +++++++++++++++++++++++++++++++++++++++++++++++++++-\n git-bisect.sh      |   37 +++++++++++++++++++++++++-\n 2 files changed, 105 insertions(+), 3 deletions(-)\n\n\tChanges since the previous series are the following:\n\n\t- don't create a tag anymore and use the \"bisect-replace-*\"\n\tbranch to replace to detect grafts that should be created,\n\t- tried to move \"replace\" subcommand before \"run\" subcommand,\n\t- added patches from 4 to 7\n\n\tPerhaps a few more tests are needed for latter patches\n\tespecially patch 7.\n\n\tI wonder about the following:\n\n\t- perhaps \"git bisect replace\" should have a \"NAME\" argument\n\tso that the branch names could be like \"bisect-rename-X-NAME\"\n\twhich could be more descriptive,\n\t- maybe a way to check that the replace branches all point to\n\texisting commits would be useful,\n\t- perhaps bisect log/replay could be improved to handle\n\treplace branches\n\ndiff --git a/builtin-rev-list.c b/builtin-rev-list.c\nindex 06cdeb7..c0717f6 100644\n--- a/builtin-rev-list.c\n+++ b/builtin-rev-list.c\n@@ -574,6 +574,72 @@ static struct commit_list *find_bisection(struct commit_list *list,\n \treturn best;\n }\n \n+static void replace_parents(struct commit *commit,\n+\t\t\t    const char *refname,\n+\t\t\t    const unsigned char *sha1)\n+{\n+\tstruct commit *new_parent = lookup_commit(sha1);\n+\tif (!new_parent) {\n+\t\twarning(\"branch '%s' points to unknown commit '%s'\",\n+\t\t\trefname, sha1_to_hex(sha1));\n+\t\treturn;\n+\t}\n+\n+\tfree_commit_list(commit->parents);\n+\tcommit->parents = NULL;\n+\tcommit_list_insert(new_parent, &commit->parents);\n+}\n+\n+static int bisect_replace(const char *refname, const unsigned char *sha1,\n+\t\t\t  int flag, void *cb_data)\n+{\n+\tunsigned char child[20];\n+\tstruct object *obj;\n+\tstruct commit_graft *graft;\n+\n+\tif (prefixcmp(refname, \"bisect-replace-\"))\n+\t\treturn 0;\n+\n+\tif (get_sha1_hex(refname + 15, child)) {\n+\t\twarning(\"bad sha1 in branch name '%s'\", refname);\n+\t\treturn 0;\n+\t}\n+\n+\t/* Check if child commit exist and is already parsed */\n+\n+\tobj = lookup_object(child);\n+\tif (obj) {\n+\t\tstruct commit *commit;\n+\t\tif (obj->type != OBJ_COMMIT) {\n+\t\t\twarning(\"branch name '%s' refers to non commit '%s'\",\n+\t\t\t\trefname, refname + 15);\n+\t\t\treturn 0;\n+\t\t}\n+\t\tcommit = (struct commit *) obj;\n+\t\tif (commit->object.parsed) {\n+\t\t\treplace_parents(commit, refname, sha1);\n+\t\t\treturn 0;\n+\t\t}\n+\t}\n+\n+\t/* Create a graft to replace child commit parents */\n+\n+\tgraft = xmalloc(sizeof(*graft) + 20);\n+\n+\thashcpy(graft->sha1, child);\n+\tgraft->nr_parent = 1;\n+\thashcpy(graft->parent[0], sha1);\n+\n+\tregister_commit_graft(graft, 1);\n+\n+\treturn 0;\n+}\n+\n+static void bisect_replace_all(void)\n+{\n+\tfor_each_branch_ref(bisect_replace, NULL);\n+}\n+\n int cmd_rev_list(int argc, const char **argv, const char *prefix)\n {\n \tstruct commit_list *list;\n@@ -646,8 +712,11 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)\n \n \tsave_commit_buffer = revs.verbose_header ||\n \t\trevs.grep_filter.pattern_list;\n-\tif (bisect_list)\n+\n+\tif (bisect_list) {\n+\t\tbisect_replace_all();\n \t\trevs.limited = 1;\n+\t}\n \n \tif (prepare_revision_walk(&revs))\n \t\tdie(\"revision walk setup failed\");\ndiff --git a/git-bisect.sh b/git-bisect.sh\nindex 79de701..1daa81c 100755\n--- a/git-bisect.sh\n+++ b/git-bisect.sh\n@@ -1,6 +1,6 @@\n #!/bin/sh\n \n-USAGE='[help|start|bad|good|skip|next|reset|visualize|replay|log|run]'\n+USAGE='[help|start|bad|good|skip|next|reset|visualize|replay|log|replace|run]'\n LONG_USAGE='git bisect help\n         print this long help message.\n git bisect start [<bad> [<good>...]] [--] [<pathspec>...]\n@@ -21,6 +21,8 @@ git bisect replay <logfile>\n         replay bisection log.\n git bisect log\n         show bisect log.\n+git bisect replace <rev> [<rev>]\n+        use another branch for bisection.\n git bisect run <cmd>...\n         use <cmd>... to automatically bisect.\n \n@@ -566,6 +568,36 @@ bisect_replay () {\n \tbisect_auto_next\n }\n \n+bisect_replace() {\n+    test \"$#\" -ge 1 -a \"$#\" -le 2 ||\n+        die \"'git bisect replace' accept one or two arguments\"\n+\n+    source=\"$1\"\n+    target=\"${2:-HEAD}\"\n+\n+    # Check arguments\n+    src_commit=$(git rev-parse --verify \"$source^{commit}\") ||\n+        die \"Bad rev input: $source\"\n+    tgt_commit=$(git rev-parse --verify \"$target^{commit}\") ||\n+        die \"Bad rev input: $target\"\n+\n+    test \"$src_commit\" != \"tgt_commit\" ||\n+        die \"source and target should be different commits\"\n+\n+    # Check that trees from source and target are identical\n+    src_tree=$(git rev-parse --verify \"$src_commit^{tree}\") ||\n+        die \"Could not get tree for source: $source\"\n+    tgt_tree=$(git rev-parse --verify \"$tgt_commit^{tree}\") ||\n+        die \"Could not get tree for target: $target\"\n+\n+    test \"$src_tree\" = \"$tgt_tree\" ||\n+        die \"source and target should point to the same tree\"\n+\n+    # Create branch for the target commit\n+    tgt_branch=\"bisect-replace-$src_commit\"\n+    git branch \"$tgt_branch\" \"$tgt_commit\"\n+}\n+\n bisect_run () {\n     bisect_next_check fail\n \n@@ -618,7 +650,6 @@ bisect_run () {\n     done\n }\n \n-\n case \"$#\" in\n 0)\n     usage ;;\n@@ -645,6 +676,8 @@ case \"$#\" in\n \tcat \"$GIT_DIR/BISECT_LOG\" ;;\n     run)\n         bisect_run \"$@\" ;;\n+    replace)\n+        bisect_replace \"$@\" ;;\n     *)\n         usage ;;\n     esac\n-- \n1.6.0.3.619.g9a6a\n"}]}