Skip to content

Commit fa7b3c2

Browse files
committed
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 <[email protected]>
1 parent 0d641f7 commit fa7b3c2

File tree

4 files changed

+9
-3
lines changed

4 files changed

+9
-3
lines changed

builtin-checkout.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,7 @@ static int merge_working_tree(struct checkout_opts *opts,
269269
}
270270

271271
/* 2-way merge to the new branch */
272-
topts.initial_checkout = (!active_nr &&
273-
(old->commit == new->commit));
272+
topts.initial_checkout = is_cache_unborn();
274273
topts.update = 1;
275274
topts.merge = 1;
276275
topts.gently = opts->merge;

builtin-read-tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
206206
break;
207207
case 2:
208208
opts.fn = twoway_merge;
209-
opts.initial_checkout = !active_nr;
209+
opts.initial_checkout = is_cache_unborn();
210210
break;
211211
case 3:
212212
default:

cache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ static inline void remove_name_hash(struct cache_entry *ce)
255255

256256
#define read_cache() read_index(&the_index)
257257
#define read_cache_from(path) read_index_from(&the_index, (path))
258+
#define is_cache_unborn() is_index_unborn(&the_index)
258259
#define read_cache_unmerged() read_index_unmerged(&the_index)
259260
#define write_cache(newfd, cache, entries) write_index(&the_index, (newfd))
260261
#define discard_cache() discard_index(&the_index)
@@ -360,6 +361,7 @@ extern int init_db(const char *template_dir, unsigned int flags);
360361
/* Initialize and use the cache information */
361362
extern int read_index(struct index_state *);
362363
extern int read_index_from(struct index_state *, const char *path);
364+
extern int is_index_unborn(struct index_state *);
363365
extern int read_index_unmerged(struct index_state *);
364366
extern int write_index(const struct index_state *, int newfd);
365367
extern int discard_index(struct index_state *);

read-cache.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,6 +1239,11 @@ int read_index_from(struct index_state *istate, const char *path)
12391239
die("index file corrupt");
12401240
}
12411241

1242+
int is_index_unborn(struct index_state *istate)
1243+
{
1244+
return (!istate->cache_nr && !istate->alloc && !istate->timestamp);
1245+
}
1246+
12421247
int discard_index(struct index_state *istate)
12431248
{
12441249
istate->cache_nr = 0;

0 commit comments

Comments
 (0)