From mboxrd@z Thu Jan 1 00:00:00 1970 From: Pranit Bauva Subject: [PATCH v2 4/6] bisect--helper: `bisect_reset` shell function in C Date: Wed, 15 Jun 2016 19:30:24 +0530 Message-ID: <20160615140026.10519-5-pranit.bauva@gmail.com> References: <20160607205454.22576-1-pranit.bauva@gmail.com> <20160615140026.10519-1-pranit.bauva@gmail.com> Cc: Pranit Bauva , christian.couder@gmail.com, chriscool@tuxfamily.org, larsxschneider@gmail.com, sunshine@sunshineco.com, tboegi@web.de To: git@vger.kernel.org X-From: git-owner@vger.kernel.org Wed Jun 15 16:02:21 2016 Return-path: Envelope-to: gcvg-git-2@plane.gmane.org Received: from vger.kernel.org ([209.132.180.67]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1bDBOb-00076p-NE for gcvg-git-2@plane.gmane.org; Wed, 15 Jun 2016 16:02:18 +0200 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1161091AbcFOOCI (ORCPT ); Wed, 15 Jun 2016 10:02:08 -0400 Received: from mail-pa0-f65.google.com ([209.85.220.65]:36303 "EHLO mail-pa0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1161083AbcFOOCF (ORCPT ); Wed, 15 Jun 2016 10:02:05 -0400 Received: by mail-pa0-f65.google.com with SMTP id fg1so1639354pad.3 for ; Wed, 15 Jun 2016 07:02:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=6DorQWlJMMPhm1pJ9+Fz1UKHmBLnPYXuOd5+EQ5eVUA=; b=GrpO1Dfps7l/Bcac3EHg4DtKdR7Ot+xB4HalkA4jTlr4LRRYW1qySBUaUCYK8b8oOE va53lWygU9Cvm0QsnMjllTZX2Q157mHwIQrrbIsH/6u8oGz7dcD/5M3P0mHSmCfXmFEN NvLRfZwzeEGbCFy3Ct0mRIjVqPhPcz2O87UseURm1+j0rbxVqzy9kfrVxQpZ87ud9nH2 2LuoKXsjtO6B6HcOf/v4rnUlaf46Fdrs43WXXWNkM+XihsQR4r8MFCNASCI0QC/yvdOZ OugYQQXUEcvvADPxahm0uvp+I23dObNcLNX+514yzvXPoRJnQMwoBnJNiHQbI6V1ibvr /IYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=6DorQWlJMMPhm1pJ9+Fz1UKHmBLnPYXuOd5+EQ5eVUA=; b=HGaS2kRKizSeAZ1b/8UczGcpbixaFRhBUW3l72zs/hg10apabjL+PxbBh3UhxK7c6w i7I/AwmaKFkE4YxBz51oo4GaHccIarGVlxoDG3EMqXf7RuAV9RYNphhNu9ZCAvdj4TEb LbSvFiAsNV2c8ZM0bDK8Ztuvp8kFpe3yehi2lUtAFVzs/PwdFDSQJCxU0A/+hSGmiDMG wMz/PEO2GaDCMqHgCyx6yyrrRXIsfUNxu5JDw45LZaM+OKgqd0DlY6o2vIi3AsYwRiuk IDUwK4mewPRmUIY0fEeH1ohpAj32u8y2sguaXN4QBMTwMvOpLWDmuiyWYBcNcqEqdcZN fsbw== X-Gm-Message-State: ALyK8tLCdh9FxgYWDkiRAhO9exq2hmF6gicbn3UVeEBwAXhIg+nKeMIX2B2MjytQ7LuAfA== X-Received: by 10.66.135.40 with SMTP id pp8mr3966395pab.113.1465999324454; Wed, 15 Jun 2016 07:02:04 -0700 (PDT) Received: from localhost.localdomain ([27.106.54.188]) by smtp.gmail.com with ESMTPSA id i5sm37514156pfk.14.2016.06.15.07.01.58 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 15 Jun 2016 07:02:03 -0700 (PDT) X-Mailer: git-send-email 2.9.0 In-Reply-To: <20160615140026.10519-1-pranit.bauva@gmail.com> Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Archived-At: Reimplement `bisect_reset` shell function in C and add a `--bisect-reset` subcommand to `git bisect--helper` to call it from git-bisect.sh . Using `bisect_reset` subcommand is a temporary measure to port shell functions to C so as to use the existing test suite. As more functions are ported, this subcommand would be retired and will be called by some other method. Note: --bisect-clean-state subcommand has not been retired as there are still a function namely `bisect_start()` which still uses this subcommand. Mentored-by: Lars Schneider Mentored-by: Christian Couder Signed-off-by: Pranit Bauva --- builtin/bisect--helper.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++- git-bisect.sh | 28 ++-------------------------- 2 files changed, 49 insertions(+), 27 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 3e4a458..14043a8 100644 --- a/builtin/bisect--helper.c +++ b/builtin/bisect--helper.c @@ -4,6 +4,8 @@ #include "bisect.h" #include "refs.h" #include "dir.h" +#include "argv-array.h" +#include "run-command.h" static GIT_PATH_FUNC(git_path_bisect_write_terms, "BISECT_TERMS") static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV") @@ -13,11 +15,13 @@ static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES") static GIT_PATH_FUNC(git_path_bisect_run, "BISECT_RUN") static GIT_PATH_FUNC(git_path_head_name, "head-name") static GIT_PATH_FUNC(git_path_bisect_start, "BISECT_START") +static GIT_PATH_FUNC(git_path_bisect_head, "BISECT_HEAD") static const char * const git_bisect_helper_usage[] = { N_("git bisect--helper --next-all [--no-checkout]"), N_("git bisect--helper --write-terms "), N_("git bisect--helper --bisect-clean-state"), + N_("git bisect--helper --bisect-reset []"), NULL }; @@ -123,12 +127,48 @@ static int bisect_clean_state(void) return result; } +static int bisect_reset(const char *commit) +{ + struct strbuf branch = STRBUF_INIT; + + if (!commit) { + if (strbuf_read_file(&branch, git_path_bisect_start(), 0) < 1) { + printf("We are not bisecting.\n"); + return 0; + } + strbuf_rtrim(&branch); + + } else { + struct object_id oid; + if (get_oid(commit, &oid)) + return error(_("'%s' is not a valid commit"), commit); + strbuf_addf(&branch, "%s", commit); + } + + if (!file_exists(git_path_bisect_head())) { + struct argv_array argv = ARGV_ARRAY_INIT; + argv_array_pushl(&argv, "checkout", branch.buf, "--", NULL); + if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) { + error(_("Could not check out original HEAD '%s'. Try" + "'git bisect reset '."), branch.buf); + strbuf_release(&branch); + argv_array_clear(&argv); + return -1; + } + argv_array_clear(&argv); + } + + strbuf_release(&branch); + return bisect_clean_state(); +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { NEXT_ALL = 1, WRITE_TERMS, - BISECT_CLEAN_STATE + BISECT_CLEAN_STATE, + BISECT_RESET } cmdmode = 0; int no_checkout = 0; struct option options[] = { @@ -138,6 +178,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) N_("write the terms to .git/BISECT_TERMS"), WRITE_TERMS), OPT_CMDMODE(0, "bisect-clean-state", &cmdmode, N_("cleanup the bisection state"), BISECT_CLEAN_STATE), + OPT_CMDMODE(0, "bisect-reset", &cmdmode, + N_("reset the bisection state"), BISECT_RESET), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -160,6 +202,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) if (argc != 0) die(_("--bisect-clean-state requires no arguments")); return bisect_clean_state(); + case BISECT_RESET: + if (argc > 1) + die(_("--bisect-reset requires either zero or one arguments")); + return bisect_reset(argc ? argv[0] : NULL); default: die("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index bbc57d2..18580b7 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -409,35 +409,11 @@ bisect_visualize() { eval '"$@"' --bisect -- $(cat "$GIT_DIR/BISECT_NAMES") } -bisect_reset() { - test -s "$GIT_DIR/BISECT_START" || { - gettextln "We are not bisecting." - return - } - case "$#" in - 0) branch=$(cat "$GIT_DIR/BISECT_START") ;; - 1) git rev-parse --quiet --verify "$1^{commit}" >/dev/null || { - invalid="$1" - die "$(eval_gettext "'\$invalid' is not a valid commit")" - } - branch="$1" ;; - *) - usage ;; - esac - - if ! test -f "$GIT_DIR/BISECT_HEAD" && ! git checkout "$branch" -- - then - die "$(eval_gettext "Could not check out original HEAD '\$branch'. -Try 'git bisect reset '.")" - fi - git bisect--helper --bisect-clean-state || exit -} - bisect_replay () { file="$1" test "$#" -eq 1 || die "$(gettext "No logfile given")" test -r "$file" || die "$(eval_gettext "cannot read \$file for replaying")" - bisect_reset + git bisect--helper --bisect-reset || exit while read git bisect command rev do test "$git $bisect" = "git bisect" || test "$git" = "git-bisect" || continue @@ -627,7 +603,7 @@ case "$#" in visualize|view) bisect_visualize "$@" ;; reset) - bisect_reset "$@" ;; + git bisect--helper --bisect-reset "$@" ;; replay) bisect_replay "$@" ;; log) -- 2.9.0