Re: [PATCH v4 04/44] builtin-am: implement patch queue mechanism

2015-07-07 Thread Paul Tan
On Mon, Jun 29, 2015 at 1:08 PM, Stefan Beller  wrote:
> (optional nit, bikeshedding)
> In conjunction with the previous patch I just wonder when we put a
> TODO and when we want to put a NEEDSWORK, or if we're being
> inconsistent here as both issues will be resolved in a later patch
> in the series.

That's a code style thing that I don't personally have a strong opinion about.

Not sure if the following means anything, but on master,

git grep '\bTODO\b' | wc -l
102

git grep '\bNEEDSWORK\b' | wc -l
45

git log -G'\bTODO\b --oneline | wc -l
185

git log -G'\bNEEDSWORK\b' --oneline | wc -l
120

So it does seem that temporary stuff is usually tagged with
"NEEDSWORK", rather than "TODO".

Anyway, it's probably better to be consistent, so I will switch to "NEEDSWORK".

Thanks,
Paul
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH v4 04/44] builtin-am: implement patch queue mechanism

2015-06-28 Thread Stefan Beller
On Sun, Jun 28, 2015 at 7:05 AM, Paul Tan  wrote:
> git-am applies a series of patches. If the process terminates
> abnormally, we want to be able to resume applying the series of patches.
> This requires the session state to be saved in a persistent location.
>
> Implement the mechanism of a "patch queue", represented by 2 integers --
> the index of the current patch we are applying and the index of the last
> patch, as well as its lifecycle through the following functions:
>
> * am_setup(), which will set up the state directory
>   $GIT_DIR/rebase-apply. As such, even if the process exits abnormally,
>   the last-known state will still persist.
>
> * am_load(), which is called if there is an am session in
>   progress, to load the last known state from the state directory so we
>   can resume applying patches.
>
> * am_run(), which will do the actual patch application. After applying a
>   patch, it calls am_next() to increment the current patch index. The
>   logic for applying and committing a patch is not implemented yet.
>
> * am_destroy(), which is finally called when we successfully applied all
>   the patches in the queue, to clean up by removing the state directory
>   and its contents.
>
> Helped-by: Junio C Hamano 
> Helped-by: Stefan Beller 
> Helped-by: Johannes Schindelin 
> Signed-off-by: Paul Tan 
> ---
>
> Notes:
> v4
>
> * Corrected docstring of read_state_file()
>
> * Corrected docstring of am_state_release()
>
> * am_state's "dir" field is now a char*. To help API users,
>   am_state_init() takes an additional const char *dir argument.
>
> * The opt_* option variables, am_options[] and am_usage[] have been
>   moved into cmd_am()'s scope.
>
> * signature of read_state_file() has been changed to
>   read_state_file(strbuf, state, file, trim)
>
>  builtin/am.c | 180 
> +++
>  1 file changed, 180 insertions(+)
>
> diff --git a/builtin/am.c b/builtin/am.c
> index fd32caf..5b4e9af 100644
> --- a/builtin/am.c
> +++ b/builtin/am.c
> @@ -6,9 +6,174 @@
>  #include "cache.h"
>  #include "builtin.h"
>  #include "exec_cmd.h"
> +#include "parse-options.h"
> +#include "dir.h"
> +
> +struct am_state {
> +   /* state directory path */
> +   char *dir;
> +
> +   /* current and last patch numbers, 1-indexed */
> +   int cur;
> +   int last;
> +};
> +
> +/**
> + * Initializes am_state with the default values. The state directory is set 
> to
> + * dir.
> + */
> +static void am_state_init(struct am_state *state, const char *dir)
> +{
> +   memset(state, 0, sizeof(*state));
> +
> +   assert(dir);
> +   state->dir = xstrdup(dir);
> +}
> +
> +/**
> + * Releases memory allocated by an am_state.
> + */
> +static void am_state_release(struct am_state *state)
> +{
> +   if (state->dir)
> +   free(state->dir);
> +}
> +
> +/**
> + * Returns path relative to the am_state directory.
> + */
> +static inline const char *am_path(const struct am_state *state, const char 
> *path)
> +{
> +   assert(state->dir);
> +   assert(path);
> +   return mkpath("%s/%s", state->dir, path);
> +}
> +
> +/**
> + * Returns 1 if there is an am session in progress, 0 otherwise.
> + */
> +static int am_in_progress(const struct am_state *state)
> +{
> +   struct stat st;
> +
> +   if (lstat(state->dir, &st) < 0 || !S_ISDIR(st.st_mode))
> +   return 0;
> +   if (lstat(am_path(state, "last"), &st) || !S_ISREG(st.st_mode))
> +   return 0;
> +   if (lstat(am_path(state, "next"), &st) || !S_ISREG(st.st_mode))
> +   return 0;
> +   return 1;
> +}
> +
> +/**
> + * Reads the contents of `file` in the `state` directory into `sb`. Returns 
> the
> + * number of bytes read on success, -1 if the file does not exist. If `trim` 
> is
> + * set, trailing whitespace will be removed.
> + */
> +static int read_state_file(struct strbuf *sb, const struct am_state *state,
> +   const char *file, int trim)
> +{
> +   strbuf_reset(sb);
> +
> +   if (strbuf_read_file(sb, am_path(state, file), 0) >= 0) {
> +   if (trim)
> +   strbuf_trim(sb);
> +
> +   return sb->len;
> +   }
> +
> +   if (errno == ENOENT)
> +   return -1;
> +
> +   die_errno(_("could not read '%s'"), am_path(state, file));
> +}
> +
> +/**
> + * Loads state from disk.
> + */
> +static void am_load(struct am_state *state)
> +{
> +   struct strbuf sb = STRBUF_INIT;
> +
> +   if (read_state_file(&sb, state, "next", 1) < 0)
> +   die("BUG: state file 'next' does not exist");
> +   state->cur = strtol(sb.buf, NULL, 10);
> +
> +   if (read_state_file(&sb, state, "last", 1) < 0)
> +   die("BUG: state file 'last' does not exist");
> +   state->last = strtol(sb.buf, NULL, 10);
> +
> +   strbuf_release(&sb);
> +}
> +
> +/**
> + * Removes the am_state directory, f