Skip to content

Commit fb71a32

Browse files
pranitbauva1997gitster
authored andcommitted
bisect--helper: bisect_clean_state shell function in C
Reimplement `bisect_clean_state` shell function in C and add a `bisect-clean-state` subcommand to `git bisect--helper` to call it from git-bisect.sh . Using `--bisect-clean-state` subcommand is a measure to port shell function to C so as to use the existing test suite. As more functions are ported, this subcommand will be retired but its implementation will be called by bisect_reset() and bisect_start(). Also introduce a function `mark_for_removal` to store the refs which need to be removed while iterating through the refs. Mentored-by: Lars Schneider <[email protected]> Mentored-by: Christian Couder <[email protected]> Signed-off-by: Pranit Bauva <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ecb3f37 commit fb71a32

File tree

4 files changed

+56
-24
lines changed

4 files changed

+56
-24
lines changed

bisect.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,12 @@ static int read_bisect_refs(void)
433433

434434
static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES")
435435
static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV")
436+
static GIT_PATH_FUNC(git_path_bisect_ancestors_ok, "BISECT_ANCESTORS_OK")
437+
static GIT_PATH_FUNC(git_path_bisect_run, "BISECT_RUN")
438+
static GIT_PATH_FUNC(git_path_bisect_start, "BISECT_START")
439+
static GIT_PATH_FUNC(git_path_bisect_log, "BISECT_LOG")
436440
static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS")
441+
static GIT_PATH_FUNC(git_path_head_name, "head-name")
437442

438443
static void read_bisect_paths(struct argv_array *array)
439444
{
@@ -1044,3 +1049,40 @@ int estimate_bisect_steps(int all)
10441049

10451050
return (e < 3 * x) ? n : n - 1;
10461051
}
1052+
1053+
static int mark_for_removal(const char *refname, const struct object_id *oid,
1054+
int flag, void *cb_data)
1055+
{
1056+
struct string_list *refs = cb_data;
1057+
char *ref = xstrfmt("refs/bisect%s", refname);
1058+
string_list_append(refs, ref);
1059+
return 0;
1060+
}
1061+
1062+
int bisect_clean_state(void)
1063+
{
1064+
int result = 0;
1065+
1066+
/* There may be some refs packed during bisection */
1067+
struct string_list refs_for_removal = STRING_LIST_INIT_NODUP;
1068+
for_each_ref_in("refs/bisect", mark_for_removal, (void *) &refs_for_removal);
1069+
string_list_append(&refs_for_removal, xstrdup("BISECT_HEAD"));
1070+
result = delete_refs("bisect: remove", &refs_for_removal, REF_NODEREF);
1071+
refs_for_removal.strdup_strings = 1;
1072+
string_list_clear(&refs_for_removal, 0);
1073+
unlink_or_warn(git_path_bisect_expected_rev());
1074+
unlink_or_warn(git_path_bisect_ancestors_ok());
1075+
unlink_or_warn(git_path_bisect_log());
1076+
unlink_or_warn(git_path_bisect_names());
1077+
unlink_or_warn(git_path_bisect_run());
1078+
unlink_or_warn(git_path_bisect_terms());
1079+
/* Cleanup head-name if it got left by an old version of git-bisect */
1080+
unlink_or_warn(git_path_head_name());
1081+
/*
1082+
* Cleanup BISECT_START last to support the --no-checkout option
1083+
* introduced in the commit 4796e823a.
1084+
*/
1085+
unlink_or_warn(git_path_bisect_start());
1086+
1087+
return result;
1088+
}

bisect.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,6 @@ extern int estimate_bisect_steps(int all);
2828

2929
extern void read_bisect_terms(const char **bad, const char **good);
3030

31+
extern int bisect_clean_state(void);
32+
3133
#endif

builtin/bisect--helper.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS")
99
static const char * const git_bisect_helper_usage[] = {
1010
N_("git bisect--helper --next-all [--no-checkout]"),
1111
N_("git bisect--helper --write-terms <bad_term> <good_term>"),
12+
N_("git bisect--helper --bisect-clean-state"),
1213
NULL
1314
};
1415

@@ -83,14 +84,17 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
8384
{
8485
enum {
8586
NEXT_ALL = 1,
86-
WRITE_TERMS
87+
WRITE_TERMS,
88+
BISECT_CLEAN_STATE
8789
} cmdmode = 0;
8890
int no_checkout = 0;
8991
struct option options[] = {
9092
OPT_CMDMODE(0, "next-all", &cmdmode,
9193
N_("perform 'git bisect next'"), NEXT_ALL),
9294
OPT_CMDMODE(0, "write-terms", &cmdmode,
9395
N_("write the terms to .git/BISECT_TERMS"), WRITE_TERMS),
96+
OPT_CMDMODE(0, "bisect-clean-state", &cmdmode,
97+
N_("cleanup the bisection state"), BISECT_CLEAN_STATE),
9498
OPT_BOOL(0, "no-checkout", &no_checkout,
9599
N_("update BISECT_HEAD instead of checking out the current commit")),
96100
OPT_END()
@@ -109,6 +113,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
109113
if (argc != 2)
110114
return error(_("--write-terms requires two arguments"));
111115
return write_terms(argv[0], argv[1]);
116+
case BISECT_CLEAN_STATE:
117+
if (argc != 0)
118+
return error(_("--bisect-clean-state requires no arguments"));
119+
return bisect_clean_state();
112120
default:
113121
return error("BUG: unknown subcommand '%d'", cmdmode);
114122
}

git-bisect.sh

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ bisect_start() {
186186
#
187187
# Get rid of any old bisect state.
188188
#
189-
bisect_clean_state || exit
189+
git bisect--helper --bisect-clean-state || exit
190190

191191
#
192192
# Change state.
@@ -195,7 +195,7 @@ bisect_start() {
195195
# We have to trap this to be able to clean up using
196196
# "bisect_clean_state".
197197
#
198-
trap 'bisect_clean_state' 0
198+
trap 'git bisect--helper --bisect-clean-state' 0
199199
trap 'exit 255' 1 2 3 15
200200

201201
#
@@ -430,27 +430,7 @@ bisect_reset() {
430430
die "$(eval_gettext "Could not check out original HEAD '\$branch'.
431431
Try 'git bisect reset <commit>'.")"
432432
fi
433-
bisect_clean_state
434-
}
435-
436-
bisect_clean_state() {
437-
# There may be some refs packed during bisection.
438-
git for-each-ref --format='%(refname) %(objectname)' refs/bisect/\* |
439-
while read ref hash
440-
do
441-
git update-ref -d $ref $hash || exit
442-
done
443-
rm -f "$GIT_DIR/BISECT_EXPECTED_REV" &&
444-
rm -f "$GIT_DIR/BISECT_ANCESTORS_OK" &&
445-
rm -f "$GIT_DIR/BISECT_LOG" &&
446-
rm -f "$GIT_DIR/BISECT_NAMES" &&
447-
rm -f "$GIT_DIR/BISECT_RUN" &&
448-
rm -f "$GIT_DIR/BISECT_TERMS" &&
449-
# Cleanup head-name if it got left by an old version of git-bisect
450-
rm -f "$GIT_DIR/head-name" &&
451-
git update-ref -d --no-deref BISECT_HEAD &&
452-
# clean up BISECT_START last
453-
rm -f "$GIT_DIR/BISECT_START"
433+
git bisect--helper --bisect-clean-state || exit
454434
}
455435

456436
bisect_replay () {

0 commit comments

Comments
 (0)