Subject: Re: [PATCH/WIP v3 10/31] am: refresh the index at start

From: Junio C Hamano <[email-masked]>
Date: Thu, 18 Jun 2015 14:28:04 -0700

Paul Tan <[email-masked]> writes:

> If a file is unchanged but stat-dirty, git-apply may erroneously fail to
> apply patches, thinking that they conflict with a dirty working tree.
>
> As such, since 2a6f08a (am: refresh the index at start and --resolved,
> 2011-08-15), git-am will refresh the index before applying patches.
> Re-implement this behavior.

Good.

I would actually have expected to see this as part of 08/31, though.

>
> Signed-off-by: Paul Tan <[email-masked]>
> ---
>  builtin/am.c | 20 ++++++++++++++++++++
>  1 file changed, 20 insertions(+)
>
> diff --git a/builtin/am.c b/builtin/am.c
> index dfb6f7e..a7efe85 100644
> --- a/builtin/am.c
> +++ b/builtin/am.c
> @@ -13,6 +13,7 @@
>  #include "cache-tree.h"
>  #include "refs.h"
>  #include "commit.h"
> +#include "lockfile.h"
>  
>  /**
>   * Returns 1 if the file is empty or does not exist, 0 otherwise.
> @@ -471,6 +472,20 @@ static const char *msgnum(const struct am_state *state)
>  }
>  
>  /**
> + * Refresh and write index.
> + */
> +static void refresh_and_write_cache(void)
> +{
> +	static struct lock_file lock_file;
> +
> +	hold_locked_index(&lock_file, 1);
> +	refresh_cache(REFRESH_QUIET);
> +	if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
> +		die(_("unable to write index file"));
> +	rollback_lock_file(&lock_file);
> +}
> +
> +/**
>   * Parses `patch` using git-mailinfo. state->msg will be set to the patch
>   * message. state->author_name, state->author_email, state->author_date will be
>   * set to the patch author's name, email and date respectively. The patch's
> @@ -607,6 +622,8 @@ static void do_commit(const struct am_state *state)
>   */
>  static void am_run(struct am_state *state)
>  {
> +	refresh_and_write_cache();
> +
>  	while (state->cur <= state->last) {
>  		const char *patch = am_path(state, msgnum(state));
>  
> @@ -696,6 +713,9 @@ int cmd_am(int argc, const char **argv, const char *prefix)
>  
>  	argc = parse_options(argc, argv, prefix, am_options, am_usage, 0);
>  
> +	if (read_index_preload(&the_index, NULL) < 0)
> +		die(_("failed to read the index"));
> +
>  	if (am_in_progress(&state))
>  		am_load(&state);
>  	else {