entry.c: submodule recursing: respect force flag correctly
authorStefan Beller <sbeller@google.com>
Tue, 18 Apr 2017 21:37:22 +0000 (14:37 -0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 19 Apr 2017 04:18:29 +0000 (21:18 -0700)
In case of a non-forced worktree update, the submodule movement is tested
in a dry run first, such that it doesn't matter if the actual update is
done via the force flag. However for correctness, we want to give the
flag as specified by the user. All callers have been inspected and updated
if needed.

Signed-off-by: Stefan Beller <sbeller@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
entry.c
unpack-trees.c

diff --git a/entry.c b/entry.c
index d2b512d..d6b263f 100644 (file)
--- a/entry.c
+++ b/entry.c
@@ -208,7 +208,8 @@ static int write_entry(struct cache_entry *ce,
                sub = submodule_from_ce(ce);
                if (sub)
                        return submodule_move_head(ce->name,
-                               NULL, oid_to_hex(&ce->oid), SUBMODULE_MOVE_HEAD_FORCE);
+                               NULL, oid_to_hex(&ce->oid),
+                               state->force ? SUBMODULE_MOVE_HEAD_FORCE : 0);
                break;
        default:
                return error("unknown file mode for %s in index", path);
@@ -282,12 +283,11 @@ int checkout_entry(struct cache_entry *ce,
                                        unlink_or_warn(ce->name);
 
                                return submodule_move_head(ce->name,
-                                       NULL, oid_to_hex(&ce->oid),
-                                       SUBMODULE_MOVE_HEAD_FORCE);
+                                       NULL, oid_to_hex(&ce->oid), 0);
                        } else
                                return submodule_move_head(ce->name,
                                        "HEAD", oid_to_hex(&ce->oid),
-                                       SUBMODULE_MOVE_HEAD_FORCE);
+                                       state->force ? SUBMODULE_MOVE_HEAD_FORCE : 0);
                }
 
                if (!changed)
index 6b7356d..4b3f951 100644 (file)
@@ -252,14 +252,18 @@ static int check_submodule_move_head(const struct cache_entry *ce,
                                     const char *new_id,
                                     struct unpack_trees_options *o)
 {
+       unsigned flags = SUBMODULE_MOVE_HEAD_DRY_RUN;
        const struct submodule *sub = submodule_from_ce(ce);
        if (!sub)
                return 0;
 
+       if (o->reset)
+               flags |= SUBMODULE_MOVE_HEAD_FORCE;
+
        switch (sub->update_strategy.type) {
        case SM_UPDATE_UNSPECIFIED:
        case SM_UPDATE_CHECKOUT:
-               if (submodule_move_head(ce->name, old_id, new_id, SUBMODULE_MOVE_HEAD_DRY_RUN))
+               if (submodule_move_head(ce->name, old_id, new_id, flags))
                        return o->gently ? -1 :
                                add_rejected_path(o, ERROR_WOULD_LOSE_SUBMODULE, ce->name);
                return 0;
@@ -308,6 +312,7 @@ static void unlink_entry(const struct cache_entry *ce)
                case SM_UPDATE_CHECKOUT:
                case SM_UPDATE_REBASE:
                case SM_UPDATE_MERGE:
+                       /* state.force is set at the caller. */
                        submodule_move_head(ce->name, "HEAD", NULL,
                                            SUBMODULE_MOVE_HEAD_FORCE);
                        break;