write_locked_index(): add flag to avoid writing unchanged index

We have several callers like

	if (active_cache_changed && write_locked_index(...))
		handle_error();
	rollback_lock_file(...);

where the final rollback is needed because "!active_cache_changed"
shortcuts the if-expression. There are also a few variants of this,
including some if-else constructs that make it more clear when the
explicit rollback is really needed.

Teach `write_locked_index()` to take a new flag SKIP_IF_UNCHANGED and
simplify the callers. Leave the most complicated of the callers (in
builtin/update-index.c) unchanged. Rewriting it to use this new flag
would end up duplicating logic.

We could have made the new flag behave the other way round
("FORCE_WRITE"), but that could break existing users behind their backs.
Let's take the more conservative approach. We can still migrate existing
callers to use our new flag. Later we might even be able to flip the
default, possibly without entirely ignoring the risk to in-flight or
out-of-tree topics.

Suggested-by: Jeff King <peff@peff.net>
Signed-off-by: Martin Ågren <martin.agren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Martin Ågren 2018-03-01 21:40:20 +01:00 коммит произвёл Junio C Hamano
Родитель 350292a1ef
Коммит 610008146e
10 изменённых файлов: 37 добавлений и 40 удалений

Просмотреть файл

@ -534,10 +534,9 @@ int cmd_add(int argc, const char **argv, const char *prefix)
unplug_bulk_checkin(); unplug_bulk_checkin();
finish: finish:
if (active_cache_changed) { if (write_locked_index(&the_index, &lock_file,
if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) COMMIT_LOCK | SKIP_IF_UNCHANGED))
die(_("Unable to write new index file")); die(_("Unable to write new index file"));
}
UNLEAK(pathspec); UNLEAK(pathspec);
UNLEAK(dir); UNLEAK(dir);

Просмотреть файл

@ -389,13 +389,9 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
if (active_cache_changed if (active_cache_changed
|| !cache_tree_fully_valid(active_cache_tree)) || !cache_tree_fully_valid(active_cache_tree))
update_main_cache_tree(WRITE_TREE_SILENT); update_main_cache_tree(WRITE_TREE_SILENT);
if (active_cache_changed) { if (write_locked_index(&the_index, &index_lock,
if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK | SKIP_IF_UNCHANGED))
COMMIT_LOCK)) die(_("unable to write new_index file"));
die(_("unable to write new_index file"));
} else {
rollback_lock_file(&index_lock);
}
commit_style = COMMIT_AS_IS; commit_style = COMMIT_AS_IS;
ret = get_index_file(); ret = get_index_file();
goto out; goto out;

Просмотреть файл

@ -651,10 +651,9 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
hold_locked_index(&lock, LOCK_DIE_ON_ERROR); hold_locked_index(&lock, LOCK_DIE_ON_ERROR);
refresh_cache(REFRESH_QUIET); refresh_cache(REFRESH_QUIET);
if (active_cache_changed && if (write_locked_index(&the_index, &lock,
write_locked_index(&the_index, &lock, COMMIT_LOCK)) COMMIT_LOCK | SKIP_IF_UNCHANGED))
return error(_("Unable to write index.")); return error(_("Unable to write index."));
rollback_lock_file(&lock);
if (!strcmp(strategy, "recursive") || !strcmp(strategy, "subtree")) { if (!strcmp(strategy, "recursive") || !strcmp(strategy, "subtree")) {
int clean, x; int clean, x;
@ -691,10 +690,9 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
remoteheads->item, reversed, &result); remoteheads->item, reversed, &result);
if (clean < 0) if (clean < 0)
exit(128); exit(128);
if (active_cache_changed && if (write_locked_index(&the_index, &lock,
write_locked_index(&the_index, &lock, COMMIT_LOCK)) COMMIT_LOCK | SKIP_IF_UNCHANGED))
die (_("unable to write %s"), get_index_file()); die (_("unable to write %s"), get_index_file());
rollback_lock_file(&lock);
return clean ? 0 : 1; return clean ? 0 : 1;
} else { } else {
return try_merge_command(strategy, xopts_nr, xopts, return try_merge_command(strategy, xopts_nr, xopts,
@ -810,10 +808,9 @@ static int merge_trivial(struct commit *head, struct commit_list *remoteheads)
hold_locked_index(&lock, LOCK_DIE_ON_ERROR); hold_locked_index(&lock, LOCK_DIE_ON_ERROR);
refresh_cache(REFRESH_QUIET); refresh_cache(REFRESH_QUIET);
if (active_cache_changed && if (write_locked_index(&the_index, &lock,
write_locked_index(&the_index, &lock, COMMIT_LOCK)) COMMIT_LOCK | SKIP_IF_UNCHANGED))
return error(_("Unable to write index.")); return error(_("Unable to write index."));
rollback_lock_file(&lock);
write_tree_trivial(&result_tree); write_tree_trivial(&result_tree);
printf(_("Wonderful.\n")); printf(_("Wonderful.\n"));

Просмотреть файл

@ -293,8 +293,8 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
if (gitmodules_modified) if (gitmodules_modified)
stage_updated_gitmodules(&the_index); stage_updated_gitmodules(&the_index);
if (active_cache_changed && if (write_locked_index(&the_index, &lock_file,
write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) COMMIT_LOCK | SKIP_IF_UNCHANGED))
die(_("Unable to write new index file")); die(_("Unable to write new index file"));
return 0; return 0;

Просмотреть файл

@ -385,10 +385,9 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
stage_updated_gitmodules(&the_index); stage_updated_gitmodules(&the_index);
} }
if (active_cache_changed) { if (write_locked_index(&the_index, &lock_file,
if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) COMMIT_LOCK | SKIP_IF_UNCHANGED))
die(_("Unable to write new index file")); die(_("Unable to write new index file"));
}
return 0; return 0;
} }

Просмотреть файл

@ -599,6 +599,7 @@ extern int read_index_unmerged(struct index_state *);
/* For use with `write_locked_index()`. */ /* For use with `write_locked_index()`. */
#define COMMIT_LOCK (1 << 0) #define COMMIT_LOCK (1 << 0)
#define SKIP_IF_UNCHANGED (1 << 1)
/* /*
* Write the index while holding an already-taken lock. Close the lock, * Write the index while holding an already-taken lock. Close the lock,
@ -615,6 +616,9 @@ extern int read_index_unmerged(struct index_state *);
* With `COMMIT_LOCK`, the lock is always committed or rolled back. * With `COMMIT_LOCK`, the lock is always committed or rolled back.
* Without it, the lock is closed, but neither committed nor rolled * Without it, the lock is closed, but neither committed nor rolled
* back. * back.
*
* If `SKIP_IF_UNCHANGED` is given and the index is unchanged, nothing
* is written (and the lock is rolled back if `COMMIT_LOCK` is given).
*/ */
extern int write_locked_index(struct index_state *, struct lock_file *lock, unsigned flags); extern int write_locked_index(struct index_state *, struct lock_file *lock, unsigned flags);

Просмотреть файл

@ -2223,10 +2223,9 @@ int merge_recursive_generic(struct merge_options *o,
return clean; return clean;
} }
if (active_cache_changed && if (write_locked_index(&the_index, &lock,
write_locked_index(&the_index, &lock, COMMIT_LOCK)) COMMIT_LOCK | SKIP_IF_UNCHANGED))
return err(o, _("Unable to write index.")); return err(o, _("Unable to write index."));
rollback_lock_file(&lock);
return clean ? 0 : 1; return clean ? 0 : 1;
} }

Просмотреть файл

@ -2538,6 +2538,12 @@ int write_locked_index(struct index_state *istate, struct lock_file *lock,
int new_shared_index, ret; int new_shared_index, ret;
struct split_index *si = istate->split_index; struct split_index *si = istate->split_index;
if ((flags & SKIP_IF_UNCHANGED) && !istate->cache_changed) {
if (flags & COMMIT_LOCK)
rollback_lock_file(lock);
return 0;
}
if (istate->fsmonitor_last_update) if (istate->fsmonitor_last_update)
fill_fsmonitor_bitmap(istate); fill_fsmonitor_bitmap(istate);

Просмотреть файл

@ -719,11 +719,9 @@ static void update_paths(struct string_list *update)
item->string); item->string);
} }
if (active_cache_changed) { if (write_locked_index(&the_index, &index_lock,
if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK)) COMMIT_LOCK | SKIP_IF_UNCHANGED))
die("Unable to write new index file"); die("Unable to write new index file");
} else
rollback_lock_file(&index_lock);
} }
static void remove_variant(struct rerere_id *id) static void remove_variant(struct rerere_id *id)

Просмотреть файл

@ -517,15 +517,14 @@ static int do_recursive_merge(struct commit *base, struct commit *next,
return clean; return clean;
} }
if (active_cache_changed && if (write_locked_index(&the_index, &index_lock,
write_locked_index(&the_index, &index_lock, COMMIT_LOCK)) COMMIT_LOCK | SKIP_IF_UNCHANGED))
/* /*
* TRANSLATORS: %s will be "revert", "cherry-pick" or * TRANSLATORS: %s will be "revert", "cherry-pick" or
* "rebase -i". * "rebase -i".
*/ */
return error(_("%s: Unable to write new index file"), return error(_("%s: Unable to write new index file"),
_(action_name(opts))); _(action_name(opts)));
rollback_lock_file(&index_lock);
if (!clean) if (!clean)
append_conflicts_hint(msgbuf); append_conflicts_hint(msgbuf);
@ -1713,13 +1712,13 @@ static int read_and_refresh_cache(struct replay_opts *opts)
_(action_name(opts))); _(action_name(opts)));
} }
refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL, NULL); refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL, NULL);
if (the_index.cache_changed && index_fd >= 0) { if (index_fd >= 0) {
if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK)) { if (write_locked_index(&the_index, &index_lock,
COMMIT_LOCK | SKIP_IF_UNCHANGED)) {
return error(_("git %s: failed to refresh the index"), return error(_("git %s: failed to refresh the index"),
_(action_name(opts))); _(action_name(opts)));
} }
} }
rollback_lock_file(&index_lock);
return 0; return 0;
} }