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

Re: [PATCH 3/8] mv: flatten error handling code block

From
Junio C Hamano <gitster@pobox.com>
Date
Aug 8, 2014, 17:54 UTC
Message-ID
<xmqqbnrukgua.fsf@gitster.dls.corp.google.com>
In-Reply-To
<1407503462-32632-4-git-send-email-pclouds@gmail.com>
Nguyễn Thái Ngọc Duy  <pclouds@gmail.com> writes:
Show 55 quoted lines
> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
> ---
>  builtin/mv.c | 35 +++++++++++++++++------------------
>  1 file changed, 17 insertions(+), 18 deletions(-)
>
> diff --git a/builtin/mv.c b/builtin/mv.c
> index a7e02c0..5c6f58f 100644
> --- a/builtin/mv.c
> +++ b/builtin/mv.c
> @@ -58,6 +58,11 @@ static const char *add_slash(const char *path)
>  	return path;
>  }
>  
> +static void move_up_one(void *p, int nmemb, int size)
> +{
> +	memmove(p, (char*)p + size, nmemb * size);
> +}
> +
>  static struct lock_file lock_file;
>  #define SUBMODULE_WITH_GITDIR ((const char *)1)
>  
> @@ -224,24 +229,18 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
>  		else
>  			string_list_insert(&src_for_dst, dst);
>  
> -		if (bad) {
> -			if (ignore_errors) {
> -				if (--argc > 0) {
> -					memmove(source + i, source + i + 1,
> -						(argc - i) * sizeof(char *));
> -					memmove(destination + i,
> -						destination + i + 1,
> -						(argc - i) * sizeof(char *));
> -					memmove(modes + i, modes + i + 1,
> -						(argc - i) * sizeof(enum update_mode));
> -					memmove(submodule_gitfile + i,
> -						submodule_gitfile + i + 1,
> -						(argc - i) * sizeof(char *));
> -					i--;
> -				}
> -			} else
> -				die (_("%s, source=%s, destination=%s"),
> -				     bad, src, dst);
> +		if (!bad)
> +			continue;
> +		if (!ignore_errors)
> +			die (_("%s, source=%s, destination=%s"),
> +			     bad, src, dst);
> +		if (--argc > 0) {
> +			int n = argc - i;
> +			move_up_one(source + i, n, sizeof(*source));
> +			move_up_one(destination + i, n, sizeof(*destination));
> +			move_up_one(modes + i, n, sizeof(*modes));
> +			move_up_one(submodule_gitfile + i, n, sizeof(*submodule_gitfile));
> +			i--;

The resulting end-of-loop code structure certainly looks a lot better, even if the original memmove()s were left inline without the helper.

The helper itself however looks a bit half-hearted. It may be more appropriate to go one step further to have a macro whose use looks like this, perhaps, to avoid the last remaining repetition?

	MOVE_UP_BY_ONE(source, i, n);
>  		}
>  	}
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 6 of 11 in “builtin/mv.c cleanup”
  1. 0/8 builtin/mv.c cleanupNguyễn Thái Ngọc Duy, Aug 8, 2014
  2. 1/8 mv: mark strings for translationsNguyễn Thái Ngọc Duy, Aug 8, 2014
  3. 2/8 mv: no "Huh?" to the userNguyễn Thái Ngọc Duy, Aug 8, 2014
  4. Junio C HamanoAug 8, 2014
  5. 3/8 mv: flatten error handling code blockNguyễn Thái Ngọc Duy, Aug 8, 2014
  6. Junio C HamanoAug 8, 2014
  7. 4/8 mv: split submodule move preparation code outNguyễn Thái Ngọc Duy, Aug 8, 2014
  8. 5/8 mv: remove an "if" that's always trueNguyễn Thái Ngọc Duy, Aug 8, 2014
  9. 6/8 mv: move index search code outNguyễn Thái Ngọc Duy, Aug 8, 2014
  10. 7/8 mv: unindent one level for directory move codeNguyễn Thái Ngọc Duy, Aug 8, 2014
  11. 8/8 mv: combine two if(s)Nguyễn Thái Ngọc Duy, Aug 8, 2014

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.