checkout: Fix "initial checkout" detection

Earlier commit 5521883 (checkout: do not lose staged removal, 2008-09-07)
tightened the rule to prevent switching branches from losing local
changes, so that staged removal of paths can be protected, while
attempting to keep a loophole to still allow a special case of switching
out of an un-checked-out state.

However, the loophole was made a bit too tight, and did not allow
switching from one branch (in an un-checked-out state) to check out
another branch.

The change to builtin-checkout.c in this commit loosens it to allow this,
by not insisting the original commit and the new commit to be the same.

It also introduces a new function, is_index_unborn (and an associated
macro, is_cache_unborn), to check if the repository is truly in an
un-checked-out state more reliably, by making sure that $GIT_INDEX_FILE
did not exist when populating the in-core index structure.  A few places
the earlier commit 5521883 added the check for the initial checkout
condition are updated to use this function.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Junio C Hamano 2008-11-12 11:52:35 -08:00
parent 0d641f75d1
commit fa7b3c2f75
4 changed files with 9 additions and 3 deletions

View File

@ -269,8 +269,7 @@ static int merge_working_tree(struct checkout_opts *opts,
} }
/* 2-way merge to the new branch */ /* 2-way merge to the new branch */
topts.initial_checkout = (!active_nr && topts.initial_checkout = is_cache_unborn();
(old->commit == new->commit));
topts.update = 1; topts.update = 1;
topts.merge = 1; topts.merge = 1;
topts.gently = opts->merge; topts.gently = opts->merge;

View File

@ -206,7 +206,7 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
break; break;
case 2: case 2:
opts.fn = twoway_merge; opts.fn = twoway_merge;
opts.initial_checkout = !active_nr; opts.initial_checkout = is_cache_unborn();
break; break;
case 3: case 3:
default: default:

View File

@ -255,6 +255,7 @@ static inline void remove_name_hash(struct cache_entry *ce)
#define read_cache() read_index(&the_index) #define read_cache() read_index(&the_index)
#define read_cache_from(path) read_index_from(&the_index, (path)) #define read_cache_from(path) read_index_from(&the_index, (path))
#define is_cache_unborn() is_index_unborn(&the_index)
#define read_cache_unmerged() read_index_unmerged(&the_index) #define read_cache_unmerged() read_index_unmerged(&the_index)
#define write_cache(newfd, cache, entries) write_index(&the_index, (newfd)) #define write_cache(newfd, cache, entries) write_index(&the_index, (newfd))
#define discard_cache() discard_index(&the_index) #define discard_cache() discard_index(&the_index)
@ -360,6 +361,7 @@ extern int init_db(const char *template_dir, unsigned int flags);
/* Initialize and use the cache information */ /* Initialize and use the cache information */
extern int read_index(struct index_state *); extern int read_index(struct index_state *);
extern int read_index_from(struct index_state *, const char *path); extern int read_index_from(struct index_state *, const char *path);
extern int is_index_unborn(struct index_state *);
extern int read_index_unmerged(struct index_state *); extern int read_index_unmerged(struct index_state *);
extern int write_index(const struct index_state *, int newfd); extern int write_index(const struct index_state *, int newfd);
extern int discard_index(struct index_state *); extern int discard_index(struct index_state *);

View File

@ -1239,6 +1239,11 @@ unmap:
die("index file corrupt"); die("index file corrupt");
} }
int is_index_unborn(struct index_state *istate)
{
return (!istate->cache_nr && !istate->alloc && !istate->timestamp);
}
int discard_index(struct index_state *istate) int discard_index(struct index_state *istate)
{ {
istate->cache_nr = 0; istate->cache_nr = 0;