Re: [PATCH 4/5] sequencer: refactor the code to detach HEAD to checkout.c

2018-06-28 Thread Stefan Beller
Hi Pratik,
On Thu, Jun 28, 2018 at 12:48 AM Pratik Karki  wrote:
>
> The motivation behind this commit is to extract the core part of
> do_reset() from sequencer.c and move it to a new detach_head_to()
> function in checkout.c.
>
[...]
>
> The new function will be used in the next commit by the builtin rebase,
> to perform the initial checkout.

This sounds like the actual motivation, which is fine.

> Here the index only gets locked after performing the first part of
> `do_reset()` rather than before which essentially derives the `oid`
> from the specified label/name passed to the `do_reset()` function.
> It also fixes two bugs: there were two `return error()` statements in
> the `[new root]` case that would have failed to unlock the index.

This sounds as if this fixes a problem? If so it would be nice to have
a test that demonstrates that these specific problems go away.
(but I think we could just argue based on the motivation above that this
is a good change on its own, with or without demonstrating these
additional issues)

> Signed-off-by: Pratik Karki 
> ---
>  checkout.c  | 64 +
>  checkout.h  |  3 +++
>  sequencer.c | 58 +---
>  3 files changed, 72 insertions(+), 53 deletions(-)
>
> diff --git a/checkout.c b/checkout.c
> index bdefc888b..da68915fd 100644
> --- a/checkout.c
> +++ b/checkout.c
> @@ -2,6 +2,11 @@
>  #include "remote.h"
>  #include "refspec.h"
>  #include "checkout.h"
> +#include "unpack-trees.h"
> +#include "lockfile.h"
> +#include "refs.h"
> +#include "tree.h"
> +#include "cache-tree.h"
>
>  struct tracking_name_data {
> /* const */ char *src_ref;
> @@ -42,3 +47,62 @@ const char *unique_tracking_name(const char *name, struct 
> object_id *oid)
> free(cb_data.dst_ref);
> return NULL;
>  }
> +
> +int detach_head_to(struct object_id *oid, const char *action,
> +  const char *reflog_message)
> +{
> +   struct strbuf ref_name = STRBUF_INIT;
> +   struct tree_desc desc;
> +   struct lock_file lock = LOCK_INIT;
> +   struct unpack_trees_options unpack_tree_opts;
> +   struct tree *tree;
> +   int ret = 0;
> +
> +   if (hold_locked_index(, LOCK_REPORT_ON_ERROR) < 0)
> +   return -1;
> +
> +   memset(_tree_opts, 0, sizeof(unpack_tree_opts));
> +   setup_unpack_trees_porcelain(_tree_opts, action);
> +   unpack_tree_opts.head_idx = 1;
> +   unpack_tree_opts.src_index = _index;
> +   unpack_tree_opts.dst_index = _index;
> +   unpack_tree_opts.fn = oneway_merge;
> +   unpack_tree_opts.merge = 1;
> +   unpack_tree_opts.update = 1;
> +
> +   if (read_cache_unmerged()) {
> +   rollback_lock_file();
> +   strbuf_release(_name);
> +   return error_resolve_conflict(_(action));
> +   }
> +
> +   if (!fill_tree_descriptor(, oid)) {
> +   error(_("failed to find tree of %s"), oid_to_hex(oid));
> +   rollback_lock_file();
> +   free((void *)desc.buffer);
> +   strbuf_release(_name);

These lines are repeated as a very similar pattern after each
failing function. Maybe we can make it more readable by moving
all these to the end and then using goto to jump there.

For example see "write_pseudoref" in refs.c, that has some interesting
patterns to learn from, e.g. how the return code is constructed
(start off with setting it -1 and only if we go through the whole
function, just before the jump label, we'd set it to 0) and
how all the free/strbuf_releases are at the end (no need to
repeat them).

> +   return -1;
> +   }
> +
> +   if (unpack_trees(1, , _tree_opts)) {
> +   rollback_lock_file();
> +   free((void *)desc.buffer);
> +   strbuf_release(_name);
> +   return -1;
> +   }
> +
> +   tree = parse_tree_indirect(oid);

Awesome, the _indirect function can take commits/tags or trees.

> +   prime_cache_tree(_index, tree);

As there is a larger movement to get rid of globals, and the_index is one
of them[1]. So maybe just use the_repository->index already (the_repository
suffers a similar problem, but I think that is more futureproof for
the time being
as we'd want to kill off the_repository in library code eventually as
well and pass
through a repository struct. But for now I'd just use the_repository instead of
having a repository argument)

[1] c.f. https://public-inbox.org/git/20180616054157.32433-1-pclo...@gmail.com/


> -   struct lock_file lock = LOCK_INIT;
> -   struct tree_desc desc;
> -   struct tree *tree;
> -   struct unpack_trees_options unpack_tree_opts;
> -   int ret = 0, i;

[...]

Oh I misspoke above, this is moving code (I should have understood
the hint with 'extracting' by the commit message), so in this case we'd
rather want to move code most verbatim to make review easier, which
it is. So 

Re: [PATCH 4/5] sequencer: refactor the code to detach HEAD to checkout.c

2018-06-28 Thread Christian Couder
On Thu, Jun 28, 2018 at 9:46 AM, Pratik Karki  wrote:
> The motivation behind this commit is to extract the core part of
> do_reset() from sequencer.c and move it to a new detach_head_to()
> function in checkout.c.

If this is independent from your other patches and if this can be used
by Alban's work, then it might be a good idea to send this patch
separately (and then to state in this patch series that it depends on
the separate patch) or at least to move this patch to the beginning of
the patch series.