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 <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Pranit Bauva committed
Sep 29, 2017 at 06:49 UTC
fb71a329964da0892cc11cc94b5b379b0803ed92
4 files changed
+56
-24
bisect.c
+42
@@ -433,7 +433,12 @@ static int read_bisect_refs(void)
433
434
static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES")
435
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")
440
static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS")
441
+static GIT_PATH_FUNC(git_path_head_name, "head-name")
442
443
static void read_bisect_paths(struct argv_array *array)
444
{
@@ -1044,3 +1049,40 @@ int estimate_bisect_steps(int all)
1049
1050
return (e < 3 * x) ? n : n - 1;
1051
}
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
+2
@@ -28,4 +28,6 @@ extern int estimate_bisect_steps(int all);
28
29
extern void read_bisect_terms(const char **bad, const char **good);
30
31
+extern int bisect_clean_state(void);
32
+
33
#endif
builtin/bisect--helper.c
+9
-1
@@ -9,6 +9,7 @@ static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS")
9
static const char * const git_bisect_helper_usage[] = {
10
N_("git bisect--helper --next-all [--no-checkout]"),
11
N_("git bisect--helper --write-terms <bad_term> <good_term>"),
12
+ N_("git bisect--helper --bisect-clean-state"),
13
NULL
14
};
15
@@ -83,7 +84,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
84
{
85
enum {
86
NEXT_ALL = 1,
86
- WRITE_TERMS
87
+ WRITE_TERMS,
88
+ BISECT_CLEAN_STATE
89
} cmdmode = 0;
90
int no_checkout = 0;
91
struct option options[] = {
@@ -91,6 +93,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
93
N_("perform 'git bisect next'"), NEXT_ALL),
94
OPT_CMDMODE(0, "write-terms", &cmdmode,
95
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),
98
OPT_BOOL(0, "no-checkout", &no_checkout,
99
N_("update BISECT_HEAD instead of checking out the current commit")),
100
OPT_END()
@@ -109,6 +113,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
113
if (argc != 2)
114
return error(_("--write-terms requires two arguments"));
115
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();
120
default:
121
return error("BUG: unknown subcommand '%d'", cmdmode);
122
}
git-bisect.sh
+3
-23
@@ -186,7 +186,7 @@ bisect_start() {
186
#
187
# Get rid of any old bisect state.
188
#
189
- bisect_clean_state || exit
189
+ git bisect--helper --bisect-clean-state || exit
190
191
#
192
# Change state.
@@ -195,7 +195,7 @@ bisect_start() {
195
# We have to trap this to be able to clean up using
196
# "bisect_clean_state".
197
#
198
- trap 'bisect_clean_state' 0
198
+ trap 'git bisect--helper --bisect-clean-state' 0
199
trap 'exit 255' 1 2 3 15
200
201
#
@@ -430,27 +430,7 @@ bisect_reset() {
430
die "$(eval_gettext "Could not check out original HEAD '\$branch'.
431
Try 'git bisect reset <commit>'.")"
432
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
434
}
435
436
bisect_replay () {