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

Re: [RFC/PATCH 08/18] revert: refactor code into a new pick_commits() function

From
Daniel Barkalow <barkalow@iabervon.org>
Date
Nov 27, 2010, 03:50 UTC
Message-ID
<alpine.LNX.2.00.1011262215540.14365@iabervon.org>
In-Reply-To
<20101125212050.5188.13304.chriscool@tuxfamily.org>
On Thu, 25 Nov 2010, Christian Couder wrote:
Show 72 quoted lines
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
>  builtin/revert.c |   38 ++++++++++++++++++++++----------------
>  1 files changed, 22 insertions(+), 16 deletions(-)
> 
> diff --git a/builtin/revert.c b/builtin/revert.c
> index 443b529..1f20251 100644
> --- a/builtin/revert.c
> +++ b/builtin/revert.c
> @@ -578,36 +578,28 @@ static void read_and_refresh_cache(const char *me)
>  	rollback_lock_file(&index_lock);
>  }
>  
> -static int revert_or_cherry_pick(int argc, const char **argv, int revert, int edit)
> +static int pick_commits(struct args_info *infos)
>  {
> -	struct args_info infos;
>  	struct rev_info revs;
>  	struct commit *commit;
>  
> -	memset(&infos, 0, sizeof(infos));
> -	git_config(git_default_config, NULL);
> -	infos.action = revert ? REVERT : CHERRY_PICK;
> -	me = revert ? "revert" : "cherry-pick";
> -	setenv(GIT_REFLOG_ACTION, me, 0);
> -	parse_args(argc, argv, &infos);
> -
> -	if (infos.allow_ff) {
> -		if (infos.signoff)
> +	if (infos->allow_ff) {
> +		if (infos->signoff)
>  			die("cherry-pick --ff cannot be used with --signoff");
> -		if (infos.no_commit)
> +		if (infos->no_commit)
>  			die("cherry-pick --ff cannot be used with --no-commit");
> -		if (infos.no_replay)
> +		if (infos->no_replay)
>  			die("cherry-pick --ff cannot be used with -x");
> -		if (infos.edit)
> +		if (infos->edit)
>  			die("cherry-pick --ff cannot be used with --edit");
>  	}
>  
>  	read_and_refresh_cache(me);
>  
> -	prepare_revs(&revs, &infos);
> +	prepare_revs(&revs, infos);
>  
>  	while ((commit = get_revision(&revs))) {
> -		int res = do_pick_commit(&infos, commit);
> +		int res = do_pick_commit(infos, commit);
>  		if (res)
>  			return res;
>  	}
> @@ -615,6 +607,20 @@ static int revert_or_cherry_pick(int argc, const char **argv, int revert, int ed
>  	return 0;
>  }
>  
> +static int revert_or_cherry_pick(int argc, const char **argv, int revert, int edit)
> +{
> +	struct args_info infos;
> +
> +	git_config(git_default_config, NULL);
> +	me = revert ? "revert" : "cherry-pick";
> +	setenv(GIT_REFLOG_ACTION, me, 0);
> +	memset(&infos, 0, sizeof(infos));
> +	infos.action = revert ? REVERT : CHERRY_PICK;
> +	parse_args(argc, argv, &infos);
> +
> +	return pick_commits(&infos);
> +}
> +

I think it would be more obvious to put this into cmd_revert and cmd_cherry_pick, and have them call pick_commits directly. In fact, you could probably make things more clear by calling your "struct args_info" instead "struct pick_commits_args" (like a lot of other "struct {cmd}_args" we already have for similar situations).

While there's no reason to do it here, pick_commits() is a sensible operation that other builtins might want to call, particularly with the error return instead of die(), so it would be nice to name things suitably for that usage. That also avoids Junio's objection to the arguments to revert_or_cherry_pick() by not having the function with the objectionable arguments at all.

For that matter, you have a lot of commits in this series that put globals into a struct and pass the struct around and change the arguments to the functions that actually do things. I think it would be easier to understand if you squashed all of these together into a single commit, which does all of the necessary changes to function prototypes. And I think it would be similarly better to have a single commit that makes all of the places that call die() not do that, rather than getting some of them in each of several patches.

Show 8 quoted lines
>  int cmd_revert(int argc, const char **argv, const char *prefix)
>  {
>  	return revert_or_cherry_pick(argc, argv, 1, isatty(0));
> -- 
> 1.7.3.2.504.g59d466
> 
> 
> 
Previous: Christian CouderNext: Christian Couder
Message 16 of 27 in “WIP implement cherry-pick/revert --continue”
  1. 00/18 WIP implement cherry-pick/revert --continueChristian Couder, Nov 25, 2010
  2. 01/18 advice: add error_resolve_conflict() functionChristian Couder, Nov 25, 2010
  3. Jonathan NiederNov 26, 2010
  4. 02/18 revert: change many die() calls into "return error()" callsChristian Couder, Nov 25, 2010
  5. Jonathan NiederNov 26, 2010
  6. 03/18 usage: implement error_errno() the same way as die_errno()Christian Couder, Nov 25, 2010
  7. Jonathan NiederNov 26, 2010
  8. Junio C HamanoNov 26, 2010
  9. 04/18 revert: don't die when write_message() failsChristian Couder, Nov 25, 2010
  10. 05/18 commit: move reverse_commit_list() into commit.{h, c}Christian Couder, Nov 25, 2010
  11. 06/18 revert: remove "commit" global variableChristian Couder, Nov 25, 2010
  12. 07/18 revert: put option information in an option structChristian Couder, Nov 25, 2010
  13. Jonathan NiederNov 26, 2010
  14. Junio C HamanoNov 26, 2010
  15. 08/18 revert: refactor code into a new pick_commits() functionChristian Couder, Nov 25, 2010
  16. Daniel BarkalowNov 27, 2010
  17. 09/18 revert: make pick_commits() return an error on --ff incompatible optionChristian Couder, Nov 25, 2010
  18. 10/18 revert: make read_and_refresh_cache() and prepare_revs() return errorsChristian Couder, Nov 25, 2010
  19. 11/18 revert: add get_todo_content() and create_todo_file()Christian Couder, Nov 25, 2010
  20. 12/18 revert: write TODO and DONE files in case of failureChristian Couder, Nov 25, 2010
  21. 13/18 revert: add option parsing for option --continueChristian Couder, Nov 25, 2010
  22. 14/18 revert: move global variable "me" into "struct args_info"Christian Couder, Nov 25, 2010
  23. 15/18 revert: add NONE action and make parse_args() manage itChristian Couder, Nov 25, 2010
  24. 16/18 revert: implement parsing TODO and DONE filesChristian Couder, Nov 25, 2010
  25. 17/18 revert: add remaining instructions in todo fileChristian Couder, Nov 25, 2010
  26. 18/18 revert: implement --continue processingChristian Couder, Nov 25, 2010
  27. Jonathan NiederNov 26, 2010

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.