Message ID | 20190626235032.177551-7-emilyshaffer@google.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | example implementation of revwalk tutorial | expand |
On Wed, Jun 26, 2019 at 7:51 PM Emily Shaffer <emilyshaffer@google.com> wrote: > Add the final steps needed and implement the walk loop itself. We add a > method walken_commit_walk() which performs the final setup to revision.c > and then iterates over commits from get_revision(). > [...] > Signed-off-by: Emily Shaffer <emilyshaffer@google.com> > --- > diff --git a/builtin/walken.c b/builtin/walken.c > +/* > + * walken_commit_walk() is invoked by cmd_walken() after initialization. It > + * does the commit walk only. > + */ "only" as opposed to what? Maybe just say: ... after initialization. It performs the actual commit walk. > +static void walken_commit_walk(struct rev_info *rev) > +{ > + struct commit *commit; > + struct strbuf prettybuf = STRBUF_INIT; > + > + /* > + * prepare_revision_walk() gets the final steps ready for a revision > + * walk. We check the return value for errors. > + */ You have some funky mix of spaces and tabs indenting the comment lines. Same for the next comment block. > + if (prepare_revision_walk(rev)) { > + die(_("revision walk setup failed")); > + } > + > + /* > + * Now we can start the real commit walk. get_revision grabs the next > + * revision based on the contents of rev. > + */ s/get_revision/&()/ > + rev->diffopt.close_file = 0; Why this? And, why isn't it set up where other 'rev' options are initialized? > + while ((commit = get_revision(rev))) { > + if (!commit) > + continue; If get_revision() returns NULL, then the while-loop exits, which means that the "if (!commit)" condition will never be satisfied, thus is unnecessary code. > + strbuf_reset(&prettybuf); > + pp_commit_easy(CMIT_FMT_ONELINE, commit, &prettybuf); > + /* > + * We expect this part of the output to be machine-parseable - > + * one commit message per line - so we must not localize it. > + */ > + puts(prettybuf.buf); Meh, but there isn't any literal text here to localize anyway, so the comment talking about not localizing it is just confusing. > + } Leaking 'prettybuf'. Add here: strbuf_release(&prettybuf); > +}
On Thu, Jun 27, 2019 at 01:16:58AM -0400, Eric Sunshine wrote: > On Wed, Jun 26, 2019 at 7:51 PM Emily Shaffer <emilyshaffer@google.com> wrote: > > Add the final steps needed and implement the walk loop itself. We add a > > method walken_commit_walk() which performs the final setup to revision.c > > and then iterates over commits from get_revision(). > > [...] > > Signed-off-by: Emily Shaffer <emilyshaffer@google.com> > > --- > > diff --git a/builtin/walken.c b/builtin/walken.c > > +/* > > + * walken_commit_walk() is invoked by cmd_walken() after initialization. It > > + * does the commit walk only. > > + */ > > "only" as opposed to what? Maybe just say: > > ... after initialization. It performs the actual commit walk. Done. > > > +static void walken_commit_walk(struct rev_info *rev) > > +{ > > + struct commit *commit; > > + struct strbuf prettybuf = STRBUF_INIT; > > + > > + /* > > + * prepare_revision_walk() gets the final steps ready for a revision > > + * walk. We check the return value for errors. > > + */ > > You have some funky mix of spaces and tabs indenting the comment > lines. Same for the next comment block. Done. > > > + if (prepare_revision_walk(rev)) { > > + die(_("revision walk setup failed")); > > + } > > + > > + /* > > + * Now we can start the real commit walk. get_revision grabs the next > > + * revision based on the contents of rev. > > + */ > > s/get_revision/&()/ Done. > > > + rev->diffopt.close_file = 0; > > Why this? And, why isn't it set up where other 'rev' options are initialized? Removed. Artifact of closely mirroring log. > > > + while ((commit = get_revision(rev))) { > > + if (!commit) > > + continue; > > If get_revision() returns NULL, then the while-loop exits, which means > that the "if (!commit)" condition will never be satisfied, thus is > unnecessary code. Yep, removed. > > > + strbuf_reset(&prettybuf); > > + pp_commit_easy(CMIT_FMT_ONELINE, commit, &prettybuf); > > + /* > > + * We expect this part of the output to be machine-parseable - > > + * one commit message per line - so we must not localize it. > > + */ > > + puts(prettybuf.buf); > > Meh, but there isn't any literal text here to localize anyway, so the > comment talking about not localizing it is just confusing. Yeah, you're right. I'll change to "so we send it to stdout", which is less obvious from puts(). > > > + } > > Leaking 'prettybuf'. Add here: > > strbuf_release(&prettybuf); Thanks, done.
diff --git a/builtin/walken.c b/builtin/walken.c index c463eca843..335dcb6b21 100644 --- a/builtin/walken.c +++ b/builtin/walken.c @@ -6,8 +6,11 @@ #include "builtin.h" #include "revision.h" +#include "commit.h" #include "config.h" #include "parse-options.h" +#include "pretty.h" +#include "line-log.h" /* * All builtins are expected to provide a usage to provide a consistent user @@ -90,6 +93,41 @@ static int git_walken_config(const char *var, const char *value, void *cb) return git_default_config(var, value, cb); } +/* + * walken_commit_walk() is invoked by cmd_walken() after initialization. It + * does the commit walk only. + */ +static void walken_commit_walk(struct rev_info *rev) +{ + struct commit *commit; + struct strbuf prettybuf = STRBUF_INIT; + + /* + * prepare_revision_walk() gets the final steps ready for a revision + * walk. We check the return value for errors. + */ + if (prepare_revision_walk(rev)) { + die(_("revision walk setup failed")); + } + + /* + * Now we can start the real commit walk. get_revision grabs the next + * revision based on the contents of rev. + */ + rev->diffopt.close_file = 0; + while ((commit = get_revision(rev))) { + if (!commit) + continue; + strbuf_reset(&prettybuf); + pp_commit_easy(CMIT_FMT_ONELINE, commit, &prettybuf); + /* + * We expect this part of the output to be machine-parseable - + * one commit message per line - so we must not localize it. + */ + puts(prettybuf.buf); + } +} + int cmd_walken(int argc, const char **argv, const char *prefix) { struct option options[] = { @@ -115,12 +153,17 @@ int cmd_walken(int argc, const char **argv, const char *prefix) */ repo_init_revisions(the_repository, &rev, prefix); + /* We can set our traversal flags here. */ + rev.always_show_header = 1; + /* * Before we do the walk, we need to set a starting point by giving it * something to go in `pending` - that happens in here */ final_rev_info_setup(argc, argv, prefix, &rev); + walken_commit_walk(&rev); + /* * This line is "human-readable" and we are writing a plumbing command, * so we localize it and use the trace library to print only when
Add the final steps needed and implement the walk loop itself. We add a method walken_commit_walk() which performs the final setup to revision.c and then iterates over commits from get_revision(). This basic walk only prints the subject line of each commit in the history. It is nearly equivalent to `git log --oneline`. Signed-off-by: Emily Shaffer <emilyshaffer@google.com> Change-Id: If6dc5f3c9d14df077b99e42806cf790c96191582 --- builtin/walken.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+)