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

Re: [PATCH 3/4] revert: simplify insn parsing logic

From
Junio C Hamano <gitster@pobox.com>
Date
Apr 2, 2012, 20:33 UTC
Message-ID
<7vr4w5euvg.fsf@alter.siamese.dyndns.org>
In-Reply-To
<1333029495-10034-4-git-send-email-artagnon@gmail.com>
Ramkumar Ramachandra <artagnon@gmail.com> writes:
Show 8 quoted lines
> Our instruction sheet parser first looks for a valid action by
> checking that the buffer starts with either "pick" or "revert".  Then,
> it looks for either spaces or tabs before looking for the object name,
> erroring out if it doesn't find any.  Simplify this logic without
> making any functional changes by looking for ("pick " or "pick\t") or
> ("revert " or "revert\t") in the first place.
>
> Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
Hrm.

In the longer term, I think the right approach is to cut the first token on the line, and then switch the action based on what the token is.

The reason why the original complains against "pickle <commit> <msg>" is not because the headword "pickle" is an unknown verb, but because the recognised token "pick" is followed by 'l' which is not a whitespace, and the updated code may slightly be closer to the ideal, but not that much.

Perhaps something like this?

I suspect that this may be a bit over-engineered, but on the other hand, if we do not foresee that we would be adding many other verbs, I do not think there is much point in your patch to clean up the verb parsing part, either, so...

-- >8 --
static struct {
	const char *name;
	int len;
	enum replay_action action;
} replay_action[] = {
	{ "pick", 4, REPLAY_PICK },
	{ "revert", 6, REPLAY_REVERT },
};
static enum replay_action parse_action(char **cp_, int *namelen_)
{
	char *cp = *cp_;
        int namelen = strcspn(cp, "\t ");
	int i, padlen;
	for (i = 0; i < ARRAY_SIZE(replay_action); i++)
        	if (namelen == replay_action[i].len &&
                    !memcmp(cp, replay_action[i].name, namelen)) {
			if (namelen_)
				*namelen_ = namelen;
			padlen = strspn(cp + namelen, "\t ");
                        *cp_ = cp + namelen + padlen;
			return replay_action[i].action;
		}
	return -1;
}
static struct commit *parse_insn_line(char *bol, ...)
{
	...
	action = parse_action(&bol, NULL);
	/*
         * Verify that the action matches ...
Previous: Ramkumar RamachandraNext: Ramkumar Ramachandra
Message 9 of 11 in “Minor sequencer.c cleanups”
  1. 0/4 Minor sequencer.c cleanupsRamkumar Ramachandra, Mar 29, 2012
  2. 1/4 sha1_name: introduce getn_sha1() to take lengthRamkumar Ramachandra, Mar 29, 2012
  3. Jonathan NiederApr 3, 2012
  4. Matthieu MoyApr 4, 2012
  5. Ramkumar RamachandraApr 4, 2012
  6. Jonathan NiederApr 4, 2012
  7. 2/4 revert: use getn_sha1() to simplify insn parsingRamkumar Ramachandra, Mar 29, 2012
  8. 3/4 revert: simplify insn parsing logicRamkumar Ramachandra, Mar 29, 2012
  9. Junio C HamanoApr 2, 2012
  10. Ramkumar RamachandraApr 3, 2012
  11. 4/4 revert: report fine-grained errors from insn parserRamkumar Ramachandra, Mar 29, 2012

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.