From: William Chargin <wchargin@gmail.com>
To: Git Mailing List <git@vger.kernel.org>
Subject: Parsing trailers
Date: Sun, 23 Dec 2018 14:41:20 -0800 [thread overview]
Message-ID: <CAFW+GMDazFSDzBrvzMqaPGwew=+CP7tw7G5FfDqcAUYd3qjPuQ@mail.gmail.com> (raw)
I'm interested in parsing the output of `git-interpret-trailers` in a
script. I had hoped that the `--parse` option would make this easy, but
it seems that the `trailer.separators` configuration option is used to
specify both the input format (which separators may indicate a trailer)
and the output format of `git interpret-trailers --parse`. Given that
`trailer.separators` may contain any (non-NUL) characters, including
whitespace, parsing the output is not straightforward.
Here's what I've come up with. The output format is "<tok><sep> <val>",
where "<tok>" and "<val>" have been trimmed and so have no leading or
trailing whitespace, but "<val>" may have internal whitespace while
"<tok>" may not. Thus, the first space character in the output may
correspond to either "<sep>" or the fixed space, but we should be able
to determine which is the case: the first space is immediately followed
by a second space if and only if the first space corresponds to "<sep>".
Assuming that the above analysis is correct, the following procedure
should suffice to safely parse the output:
- Let `i` be the index of the first space in `s`.
- If `s[i+1]` is a space, let `sep_pos` be `i`. Otherwise, let
`sep_pos` be `i - 1`.
- The substring `s[:sep_pos]` is the token.
- The character at index `sep_pos` is the separator.
- The character at index `sep_pos + 1` is the fixed space.
- The substring `s[sep_pos+2:nl]` is the value, where `nl` is the
index of the first newline in `s` after `sep_pos`.
(It seems unfortunately complicated when all we want to do is parse the
output of `--parse`, but I don't see a better approach!)
My questions:
- Is this accurate?
- Is this algorithm guaranteed to remain correct in future versions of
Git?
- Is there a simpler way to extract the token-value pairs from a
commit message string?
Would appreciate any advice.
Thanks!
WC
next reply other threads:[~2018-12-23 22:41 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-12-23 22:41 William Chargin [this message]
2018-12-24 10:58 ` Parsing trailers Christian Couder
2018-12-24 18:52 ` William Chargin
2018-12-26 4:33 ` Christian Couder
2018-12-26 21:30 ` William Chargin
2019-01-03 7:07 ` Jeff King
2019-01-03 7:43 ` William Chargin
2019-01-03 7:50 ` Jeff King
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAFW+GMDazFSDzBrvzMqaPGwew=+CP7tw7G5FfDqcAUYd3qjPuQ@mail.gmail.com' \
--to=wchargin@gmail.com \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).