git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [GSoC][PATCH v4 2/3] rebase -i: rewrite checkout_onto() in C

From
Junio C Hamano <gitster@pobox.com>
Date
Jun 26, 2018, 17:35 UTC
Message-ID
<xmqqsh59lad7.fsf@gitster-ct.c.googlers.com>
In-Reply-To
<20180625134419.18435-3-alban.gruin@gmail.com>
Alban Gruin <alban.gruin@gmail.com> writes:
> This rewrites checkout_onto() from shell to C. The new version is called
> detach_onto(), given its role.

The name, given its role, may be good, but is the implementtaion robust enough to fulfill the promise its name gives?

Show 5 quoted lines
>  	git rebase--helper --check-todo-list || {
>  		ret=$?
> -		checkout_onto
> +		git rebase--helper --detach-onto "$onto_name" "$onto" \
> +		    "$orig_head" ${verbose:+--verbose}

Here, $onto_name is what the end-user gave us (e.g. it is "master..." in "git rebase --onto=master... base"), while $onto is a 40-hex object name of the commit. $orig_head is also a 40-hex object name.

And this call shows how the above shell scriptlet calls into the detach_onto() thing ...

> +	if (command == DETACH_ONTO && argc == 4)
> +		return !!detach_onto(&opts, argv[1], argv[2], argv[3], verbose);
... which is defined like so:
Show 9 quoted lines
> +int detach_onto(struct replay_opts *opts,
> +		const char *onto_name, const char *onto,
> +		const char *orig_head, unsigned verbose)
> +{
> +	struct object_id oid;
> +	const char *action = reflog_message(opts, "start", "checkout %s", onto_name);
> +
> +	if (get_oid(orig_head, &oid))
> +		return error(_("%s: not a valid OID"), orig_head);

Which means that this can be more strict to use get_oid_hex() to catch possible mistakes in the caller.

> +	if (run_git_checkout(opts, onto, verbose, action)) {

And this could be a bit problematic, as we can see below how the "checkout" thing does not guarantee "detaching" at all ...

Show 8 quoted lines
> +		apply_autostash(opts);
> +		sequencer_remove_state(opts);
> +		return error(_("could not detach HEAD"));
> +	}
> +
> +	return update_ref(NULL, "ORIG_HEAD", &oid, NULL, 0, UPDATE_REFS_MSG_ON_ERR);
> +}
> +
... which can be seen here ...
Show 16 quoted lines
> +static int run_git_checkout(struct replay_opts *opts, const char *commit,
> +				int verbose, const char *action)
> +{
> +	struct child_process cmd = CHILD_PROCESS_INIT;
> +
> +	cmd.git_cmd = 1;
> +
> +	argv_array_push(&cmd.args, "checkout");
> +	argv_array_push(&cmd.args, commit);
> +	argv_array_pushf(&cmd.env_array, GIT_REFLOG_ACTION "=%s", action);
> +
> +	if (verbose)
> +		return run_command(&cmd);
> +	else
> +		return run_command_silent_on_success(&cmd);
> +}

This drives the external command "git checkout" with _any_ string the caller passes in "commit". If the variable happens to have 'master', for example, it would be "git checkout master" and if you have a branch with that name, it will not detach but check out the branch to build on it. It is a caller's responsibility to give a suitable "commit" if it wants to use this helper to detach.

So perhaps the caller of this function in detach_onto() should pass "%s^0" or even do something like

	struct object_id onto_oid;
	char onto_hex[GIT_MAX_HEXSZ + 1];
	if (get_oid(onto, &onto_oid) || oid_to_hex_r(onto_hex, &onto_oid))
		return error(...);
	if (run_git_checkout(opts, onto_hex, verbose, action)) {
		...
to ensure that it keeps the promise its name gives.

I can hear "Oh, but it is a bug in the caller to give anything that won't result in detaching in 'onto'" but that is not a valid excuse, given that this _public_ function is called "detach_onto". Making sure detachment happens is its responsibility, not its callers'.

Or we could do a cop-out alternative of commenting the function in *.h file to say "onto must be given as 40-hex", with a code to make sure the caller really gave us a 40-hex and not a branch name. That is a less ideal but probably acceptable alternative.

Show 18 quoted lines
>  static const char rescheduled_advice[] =
>  N_("Could not execute the todo command\n"
>  "\n"
> diff --git a/sequencer.h b/sequencer.h
> index 35730b13e..9f0ac5e75 100644
> --- a/sequencer.h
> +++ b/sequencer.h
> @@ -100,6 +100,10 @@ int update_head_with_reflog(const struct commit *old_head,
>  void commit_post_rewrite(const struct commit *current_head,
>  			 const struct object_id *new_head);
>  
> +int detach_onto(struct replay_opts *opts,
> +		const char *onto_name, const char *onto,
> +		const char *orig_head, unsigned verbose);
> +
>  #define SUMMARY_INITIAL_COMMIT   (1 << 0)
>  #define SUMMARY_SHOW_AUTHOR_DATE (1 << 1)
>  void print_commit_summary(const char *prefix, const struct object_id *oid,
Previous: Alban GruinNext: Alban Gruin
Message 41 of 51 in “rebase -i: rewrite reflog operations in C”
  1. Alban GruinJun 18, 2018
  2. [GSoC][PATCH 1/3] sequencer: add a new function to silence a command, except if it fails.Alban Gruin, Jun 18, 2018
  3. Phillip WoodJun 18, 2018
  4. Alban GruinJun 18, 2018
  5. Christian CouderJun 18, 2018
  6. Alban GruinJun 18, 2018
  7. [GSoC][PATCH 2/3] rebase -i: rewrite setup_reflog_action() in CAlban Gruin, Jun 18, 2018
  8. Phillip WoodJun 18, 2018
  9. Alban GruinJun 18, 2018
  10. Stefan BellerJun 18, 2018
  11. Johannes SchindelinJun 19, 2018
  12. [GSoC][PATCH 3/3] rebase -i: rewrite checkout_onto() in CAlban Gruin, Jun 18, 2018
  13. Phillip WoodJun 18, 2018
  14. Alban GruinJun 18, 2018
  15. [GSoC][PATCH v2 0/3] rebase -i: rewrite reflog operations in CAlban Gruin, Jun 19, 2018
  16. [GSoC][PATCH v2 1/3] sequencer: add a new function to silence a command, except if it fails.Alban Gruin, Jun 19, 2018
  17. Johannes SchindelinJun 21, 2018
  18. Alban GruinJun 21, 2018
  19. [GSoC][PATCH v2 2/3] rebase -i: rewrite setup_reflog_action() in CAlban Gruin, Jun 19, 2018
  20. Johannes SchindelinJun 21, 2018
  21. [GSoC][PATCH v2 3/3] rebase -i: rewrite checkout_onto() in CAlban Gruin, Jun 19, 2018
  22. Johannes SchindelinJun 21, 2018
  23. Stefan BellerJun 19, 2018
  24. Johannes SchindelinJun 21, 2018
  25. [GSoC][PATCH v3 0/3] rebase -i: rewrite reflog operations in CAlban Gruin, Jun 21, 2018
  26. [GSoC][PATCH v3 1/3] sequencer: add a new function to silence a command, except if it fails.Alban Gruin, Jun 21, 2018
  27. Junio C HamanoJun 21, 2018
  28. Alban GruinJun 22, 2018
  29. [GSoC][PATCH v3 2/3] rebase -i: rewrite setup_reflog_action() in CAlban Gruin, Jun 21, 2018
  30. Junio C HamanoJun 22, 2018
  31. Alban GruinJun 22, 2018
  32. Junio C HamanoJun 25, 2018
  33. Alban GruinJun 25, 2018
  34. Johannes SchindelinJun 25, 2018
  35. Pratik KarkiJun 26, 2018
  36. Junio C HamanoJun 26, 2018
  37. [GSoC][PATCH v3 3/3] rebase -i: rewrite checkout_onto() in CAlban Gruin, Jun 21, 2018
  38. [GSoC][PATCH v4 0/3] rebase -i: rewrite reflog operations in CAlban Gruin, Jun 25, 2018
  39. [GSoC][PATCH v4 1/3] sequencer: extract a function to silence a command, except if it failsAlban Gruin, Jun 25, 2018
  40. [GSoC][PATCH v4 2/3] rebase -i: rewrite checkout_onto() in CAlban Gruin, Jun 25, 2018
  41. Junio C HamanoJun 26, 2018
  42. [GSoC][PATCH v4 3/3] rebase -i: rewrite setup_reflog_action() in CAlban Gruin, Jun 25, 2018
  43. [GSoC][PATCH v5 0/3] rebase -i: rewrite reflog operations in CAlban Gruin, Jun 29, 2018
  44. [GSoC][PATCH v5 1/3] sequencer: add a new function to silence a command, except if it fails.Alban Gruin, Jun 29, 2018
  45. [GSoC][PATCH v5 2/3] rebase -i: rewrite setup_reflog_action() in CAlban Gruin, Jun 29, 2018
  46. Junio C HamanoJun 29, 2018
  47. [GSoC][PATCH v5 3/3] rebase -i: rewrite checkout_onto() in CAlban Gruin, Jun 29, 2018
  48. Junio C HamanoJun 29, 2018
  49. Junio C HamanoJun 29, 2018
  50. Alban GruinJul 2, 2018
  51. Junio C HamanoJul 3, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.