revert: actually check for a dirty index
The previous code mistakenly used wt_status_prepare to check whether the index had anything commitable in it; however, that function is just an init function, and will never report a dirty index. The correct way with wt_status_* would be to call wt_status_print with the output pointing to /dev/null or similar. However, that does extra work by both examining the working tree and spewing status information to nowhere. Instead, let's just implement the useful subset of wt_status_print as an "is_index_dirty" function. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
90d0ed96b7
commit
0f2d4476c1
@ -8,6 +8,8 @@
|
|||||||
#include "exec_cmd.h"
|
#include "exec_cmd.h"
|
||||||
#include "utf8.h"
|
#include "utf8.h"
|
||||||
#include "parse-options.h"
|
#include "parse-options.h"
|
||||||
|
#include "diff.h"
|
||||||
|
#include "revision.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This implements the builtins revert and cherry-pick.
|
* This implements the builtins revert and cherry-pick.
|
||||||
@ -245,6 +247,17 @@ static char *help_msg(const unsigned char *sha1)
|
|||||||
return helpbuf;
|
return helpbuf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int index_is_dirty(void)
|
||||||
|
{
|
||||||
|
struct rev_info rev;
|
||||||
|
init_revisions(&rev, NULL);
|
||||||
|
setup_revisions(0, NULL, &rev, "HEAD");
|
||||||
|
DIFF_OPT_SET(&rev.diffopt, QUIET);
|
||||||
|
DIFF_OPT_SET(&rev.diffopt, EXIT_WITH_STATUS);
|
||||||
|
run_diff_index(&rev, 1);
|
||||||
|
return !!DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES);
|
||||||
|
}
|
||||||
|
|
||||||
static int revert_or_cherry_pick(int argc, const char **argv)
|
static int revert_or_cherry_pick(int argc, const char **argv)
|
||||||
{
|
{
|
||||||
unsigned char head[20];
|
unsigned char head[20];
|
||||||
@ -273,12 +286,11 @@ static int revert_or_cherry_pick(int argc, const char **argv)
|
|||||||
if (write_tree(head, 0, NULL))
|
if (write_tree(head, 0, NULL))
|
||||||
die ("Your index file is unmerged.");
|
die ("Your index file is unmerged.");
|
||||||
} else {
|
} else {
|
||||||
struct wt_status s;
|
|
||||||
|
|
||||||
if (get_sha1("HEAD", head))
|
if (get_sha1("HEAD", head))
|
||||||
die ("You do not have a valid HEAD");
|
die ("You do not have a valid HEAD");
|
||||||
wt_status_prepare(&s);
|
if (read_cache() < 0)
|
||||||
if (s.commitable)
|
die("could not read the index");
|
||||||
|
if (index_is_dirty())
|
||||||
die ("Dirty index: cannot %s", me);
|
die ("Dirty index: cannot %s", me);
|
||||||
discard_cache();
|
discard_cache();
|
||||||
}
|
}
|
||||||
|
@ -59,4 +59,13 @@ test_expect_success 'revert after renaming branch' '
|
|||||||
|
|
||||||
'
|
'
|
||||||
|
|
||||||
|
test_expect_success 'revert forbidden on dirty working tree' '
|
||||||
|
|
||||||
|
echo content >extra_file &&
|
||||||
|
git add extra_file &&
|
||||||
|
test_must_fail git revert HEAD 2>errors &&
|
||||||
|
grep "Dirty index" errors
|
||||||
|
|
||||||
|
'
|
||||||
|
|
||||||
test_done
|
test_done
|
||||||
|
Loading…
Reference in New Issue
Block a user