checkout -f: allow ignoring unmerged paths when checking out of the index

Earlier we made "git checkout $pathspec" to atomically refuse
the operation of $pathspec matched any path with unmerged
stages.  This patch allows:

    $ git checkout -f a b c

to ignore, instead of error out on, such unmerged paths.  The
fix to prevent checkout of an unmerged path from random stages
is still there.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Junio C Hamano 2008-08-30 07:46:55 -07:00
parent 8fdcf31254
commit db9410990e
3 changed files with 59 additions and 24 deletions

View file

@ -9,7 +9,7 @@ SYNOPSIS
--------
[verse]
'git checkout' [-q] [-f] [[--track | --no-track] -b <new_branch> [-l]] [-m] [<branch>]
'git checkout' [<tree-ish>] [--] <paths>...
'git checkout' [-f] [<tree-ish>] [--] <paths>...
DESCRIPTION
-----------
@ -23,14 +23,17 @@ options, which will be passed to `git branch`.
When <paths> are given, this command does *not* switch
branches. It updates the named paths in the working tree from
the index file (i.e. it runs `git checkout-index -f -u`), or
from a named commit. In
this case, the `-f` and `-b` options are meaningless and giving
the index file, or from a named commit. In
this case, the `-b` options is meaningless and giving
either of them results in an error. <tree-ish> argument can be
used to specify a specific tree-ish (i.e. commit, tag or tree)
to update the index for the given paths before updating the
working tree.
The index may contain unmerged entries after a failed merge. By
default, if you try to check out such an entry from the index, the
checkout operation will fail and nothing will be checked out.
Using -f will ignore these unmerged entries.
OPTIONS
-------
@ -38,8 +41,12 @@ OPTIONS
Quiet, suppress feedback messages.
-f::
Proceed even if the index or the working tree differs
from HEAD. This is used to throw away local changes.
When switching branches, proceed even if the index or the
working tree differs from HEAD. This is used to throw away
local changes.
+
When checking out paths from the index, do not fail upon unmerged
entries; instead, unmerged entries are ignored.
-b::
Create a new branch named <new_branch> and start it at

View file

@ -20,6 +20,17 @@ static const char * const checkout_usage[] = {
NULL,
};
struct checkout_opts {
int quiet;
int merge;
int force;
int writeout_error;
const char *new_branch;
int new_branch_log;
enum branch_track track;
};
static int post_checkout_hook(struct commit *old, struct commit *new,
int changed)
{
@ -85,7 +96,8 @@ static int skip_same_name(struct cache_entry *ce, int pos)
}
static int checkout_paths(struct tree *source_tree, const char **pathspec)
static int checkout_paths(struct tree *source_tree, const char **pathspec,
struct checkout_opts *opts)
{
int pos;
struct checkout state;
@ -122,8 +134,12 @@ static int checkout_paths(struct tree *source_tree, const char **pathspec)
if (pathspec_match(pathspec, NULL, ce->name, 0)) {
if (!ce_stage(ce))
continue;
errs = 1;
error("path '%s' is unmerged", ce->name);
if (opts->force) {
warning("path '%s' is unmerged", ce->name);
} else {
errs = 1;
error("path '%s' is unmerged", ce->name);
}
pos = skip_same_name(ce, pos) - 1;
}
}
@ -178,17 +194,6 @@ static void describe_detached_head(char *msg, struct commit *commit)
strbuf_release(&sb);
}
struct checkout_opts {
int quiet;
int merge;
int force;
int writeout_error;
char *new_branch;
int new_branch_log;
enum branch_track track;
};
static int reset_tree(struct tree *tree, struct checkout_opts *o, int worktree)
{
struct unpack_trees_options opts;
@ -554,15 +559,15 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
die("invalid path specification");
/* Checkout paths */
if (opts.new_branch || opts.force || opts.merge) {
if (opts.new_branch || opts.merge) {
if (argc == 1) {
die("git checkout: updating paths is incompatible with switching branches/forcing\nDid you intend to checkout '%s' which can not be resolved as commit?", argv[0]);
die("git checkout: updating paths is incompatible with switching branches.\nDid you intend to checkout '%s' which can not be resolved as commit?", argv[0]);
} else {
die("git checkout: updating paths is incompatible with switching branches/forcing");
die("git checkout: updating paths is incompatible with switching branches.");
}
}
return checkout_paths(source_tree, pathspec);
return checkout_paths(source_tree, pathspec, &opts);
}
if (new.name && !new.commit) {

View file

@ -359,4 +359,27 @@ test_expect_success 'checkout an unmerged path should fail' '
test_cmp sample file
'
test_expect_success 'checkout with an unmerged path can be ignored' '
rm -f .git/index &&
O=$(echo original | git hash-object -w --stdin) &&
A=$(echo ourside | git hash-object -w --stdin) &&
B=$(echo theirside | git hash-object -w --stdin) &&
(
echo "100644 $A 0 fild" &&
echo "100644 $O 1 file" &&
echo "100644 $A 2 file" &&
echo "100644 $B 3 file" &&
echo "100644 $A 0 filf"
) | git update-index --index-info &&
echo "none of the above" >sample &&
echo ourside >expect &&
cat sample >fild &&
cat sample >file &&
cat sample >filf &&
git checkout -f fild file filf &&
test_cmp expect fild &&
test_cmp expect filf &&
test_cmp sample file
'
test_done