git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [RFC PATCH] An option parser for the sequencer
@ 2011-07-09 15:41 Ramkumar Ramachandra
  2011-07-09 15:41 ` [RFC PATCH] revert: Persist per-session opts Ramkumar Ramachandra
  2011-07-10  8:15 ` [RFC PATCH] An option parser for the sequencer Christian Couder
  0 siblings, 2 replies; 6+ messages in thread
From: Ramkumar Ramachandra @ 2011-07-09 15:41 UTC (permalink / raw)
  To: Git List; +Cc: Jonathan Nieder, Junio C Hamano, Christian Couder,
	Daniel Barkalow

Hi,

I've decided not to support arbitrary command-line options in the
instruction sheet.  A typical instruction sheet will looks like this
(inspired heavily by the rebase -i instruction sheet format):

pick 3b36854 t: add tests for cloning remotes with detached HEAD
pick 61adfd3 consider only branches in guess_remote_head
pick 8537f0e submodule add: test failure when url is not configured in superproject
pick 4d68932 submodule add: allow relative repository path even when no url is set
pick f22a17e submodule add: clean up duplicated code
pick 59a5775 make copy_ref globally available
pick c1921c1 clone: always fetch remote HEAD

For persisting one set of options for every "git cherry-pick"/ "git
revert" invocation, I've decided to use a simple "key = value" format
and put it in .git/sequencer/opts (to sit beside .git/sequencer/head
and .git/sequencer/todo).  For strategy-option, I thought it would be
cute to separate the various options using ' | '.  So, it'll look
something like this in the end:

signoff = true
mainline = 1
strategy-option = recursive | ours

The implementation is a little rough around the edges, but I'm pretty
happy with the overall design: it looks like a scaled-down version of
parse-options.  Quite a lot of context is missing (where did
sequencer.h come from?!), but I hope it's clear enough to convey the
idea.

Thanks for reading.

Ramkumar Ramachandra (1):
  revert: Persist per-session opts

 builtin/revert.c |  143 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 sequencer.h      |    8 +++
 2 files changed, 151 insertions(+), 0 deletions(-)

-- 
1.7.5.GIT

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [RFC PATCH] revert: Persist per-session opts
  2011-07-09 15:41 [RFC PATCH] An option parser for the sequencer Ramkumar Ramachandra
@ 2011-07-09 15:41 ` Ramkumar Ramachandra
  2011-07-10  8:02   ` Christian Couder
  2011-07-10  8:15 ` [RFC PATCH] An option parser for the sequencer Christian Couder
  1 sibling, 1 reply; 6+ messages in thread
From: Ramkumar Ramachandra @ 2011-07-09 15:41 UTC (permalink / raw)
  To: Git List; +Cc: Jonathan Nieder, Junio C Hamano, Christian Couder,
	Daniel Barkalow

Save the replay_opts struct in .git/sequencer/opts using a simple "key
= value" format.  Parse it and populate the options structure before
replaying.

Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
---
 builtin/revert.c |  143 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 sequencer.h      |    8 +++
 2 files changed, 151 insertions(+), 0 deletions(-)

diff --git a/builtin/revert.c b/builtin/revert.c
index 1feecd5..0693fd8 100644
--- a/builtin/revert.c
+++ b/builtin/revert.c
@@ -635,6 +635,32 @@ static void read_and_refresh_cache(const char *me, struct replay_opts *opts)
 	rollback_lock_file(&index_lock);
 }
 
+static void format_opts(struct strbuf *buf, struct replay_opts *opts)
+{
+	int i;
+
+	if (opts->no_commit)
+		strbuf_addstr(buf, "no-commit = true\n");
+	if (opts->edit)
+		strbuf_addstr(buf, "edit = true\n");
+	if (opts->signoff)
+		strbuf_addstr(buf, "signoff = true\n");
+	if (opts->record_origin)
+		strbuf_addstr(buf, "record-origin = true\n");
+	if (opts->allow_ff)
+		strbuf_addstr(buf, "ff = true\n");
+	if (opts->mainline)
+		strbuf_addf(buf, "mainline = %d\n", opts->mainline);
+	if (opts->strategy)
+		strbuf_addf(buf, "strategy = %s\n", opts->strategy);
+	if (opts->xopts) {
+		strbuf_addf(buf, "strategy-option = ");
+		for (i = 0; i < opts->xopts_nr - 1; i ++)
+			strbuf_addf(buf, "%s | ", opts->xopts[i]);
+		strbuf_addf(buf, "%s\n", opts->xopts[i]);
+	}
+}
+
 static void format_todo(struct strbuf *buf, struct commit_list *todo_list,
 			struct replay_opts *opts)
 {
@@ -733,6 +759,102 @@ error:
 	die(_("Malformed instruction sheet: %s"), git_path(SEQ_TODO_FILE));
 }
 
+static char *parse_opt_value(char *p, void *key, enum seq_opt_type type,
+			parse_opt_cb *cb_function) {
+	struct option opt;
+	char *val, *cur, *end;
+
+	if (!(val = strchr(p, '=')))
+		goto error;
+	if (!*(val + 1))
+		goto error;
+	if (!(end = strchr(p, '\n')))
+		goto error;
+	val += 2;
+	*end = '\0'; /* Remove trailing '\n' */
+
+	switch (type) {
+	case SEQ_OPTION_BOOLEAN:
+		if (!strncmp(val, "true", strlen("true")))
+			*(int *)key = 1;
+		else if (!strncmp(val, "false", strlen("false")))
+			*(int *)key = 0;
+		else
+			goto error;
+		break;
+	case SEQ_OPTION_INTEGER:
+		*(int *)key = strtol(val, NULL, 10);
+		break;
+	case SEQ_OPTION_STRING:
+		*(char **)key = xstrdup(val);
+		break;
+	case SEQ_OPTION_CALLBACK:
+		opt.value = (struct replay_opts **)key;
+		while (val) {
+			if ((cur = strchr(val, '|'))) {
+				*(cur - 1) = '\0';
+				(*cb_function)(&opt, val, 0);
+				val = cur + 2;
+			} else {
+				(*cb_function)(&opt, val, 0);
+				break;
+			}
+		}
+		break;
+	default:
+		die(_("program error"));
+	}
+	return end + 1;
+error:
+	die(_("Malformed options sheet: %s"), git_path(SEQ_OPTS_FILE));
+}
+
+static void read_populate_opts(struct replay_opts **opts_ptr)
+{
+	struct replay_opts *opts = *opts_ptr;
+	struct strbuf buf = STRBUF_INIT;
+	char *p;
+	int fd;
+
+	fd = open(git_path(SEQ_OPTS_FILE), O_RDONLY);
+	if (fd < 0) {
+		strbuf_release(&buf);
+		die_errno(_("Could not open %s."), git_path(SEQ_OPTS_FILE));
+	}
+	if (strbuf_read(&buf, fd, 0) < buf.len) {
+		close(fd);
+		strbuf_release(&buf);
+		die(_("Could not read %s."), git_path(SEQ_OPTS_FILE));
+	}
+	close(fd);
+
+	for (p = buf.buf; *p;) {
+		if (!strncmp(p, "no-commit ", strlen("no-commit ")))
+			p = parse_opt_value(p, &opts->no_commit, SEQ_OPTION_BOOLEAN, NULL);
+		else if (!strncmp(p, "edit ", strlen("edit ")))
+			p = parse_opt_value(p, &opts->edit, SEQ_OPTION_BOOLEAN, NULL);
+		else if (!strncmp(p, "signoff ", strlen("signoff ")))
+			p = parse_opt_value(p, &opts->signoff, SEQ_OPTION_BOOLEAN, NULL);
+		else if (!strncmp(p, "mainline ", strlen("mainline ")))
+			p = parse_opt_value(p, &opts->mainline, SEQ_OPTION_INTEGER, NULL);
+		else if (!strncmp(p, "record-origin ", strlen("record-origin ")))
+			p = parse_opt_value(p, &opts->record_origin, SEQ_OPTION_BOOLEAN, NULL);
+		else if (!strncmp(p, "ff ", strlen("ff ")))
+			p = parse_opt_value(p, &opts->allow_ff, SEQ_OPTION_BOOLEAN, NULL);
+		else if (!strncmp(p, "strategy ", strlen("strategy ")))
+			p = parse_opt_value(p, &opts->strategy, SEQ_OPTION_STRING, NULL);
+		else if (!strncmp(p, "strategy-option ", strlen("strategy-option ")))
+			p = parse_opt_value(p, &opts, SEQ_OPTION_CALLBACK, option_parse_x);
+		else
+			goto error;
+	}
+	strbuf_release(&buf);
+	return;
+error:
+	strbuf_release(&buf);
+	die(_("Malformed options sheet: %s"), git_path(SEQ_OPTS_FILE));
+}
+
 static void walk_revs_populate_todo(struct commit_list **todo_list,
 				struct replay_opts *opts)
 {
@@ -800,6 +922,25 @@ static void save_todo(struct commit_list *todo_list, struct replay_opts *opts)
 	strbuf_release(&buf);
 }
 
+static void save_opts(struct replay_opts *opts)
+{
+	static struct lock_file opts_lock;
+	struct strbuf buf = STRBUF_INIT;
+	int fd;
+
+	fd = hold_lock_file_for_update(&opts_lock, git_path(SEQ_OPTS_FILE), LOCK_DIE_ON_ERROR);
+	format_opts(&buf, opts);
+	if (write_in_full(fd, buf.buf, buf.len) < 0) {
+		strbuf_release(&buf);
+		die_errno(_("Could not write to %s."), git_path(SEQ_OPTS_FILE));
+	}
+	if (commit_lock_file(&opts_lock) < 0) {
+		strbuf_release(&buf);
+		die(_("Error wrapping up %s"), git_path(SEQ_OPTS_FILE));
+	}
+	strbuf_release(&buf);
+}
+
 static int pick_commits(struct commit_list *todo_list, struct replay_opts *opts)
 {
 	struct commit_list *cur;
@@ -849,6 +990,7 @@ static int process_continuation(struct replay_opts *opts)
 	} else if (opts->subcommand == REPLAY_CONTINUE) {
 		if (!file_exists(git_path(SEQ_TODO_FILE)))
 			goto error;
+		read_populate_opts(&opts);
 		read_populate_todo(&todo_list, opts);
 
 		/* Verify that the conflict has been resolved */
@@ -871,6 +1013,7 @@ static int process_continuation(struct replay_opts *opts)
 		create_seq_dir();
 		if (!get_sha1("HEAD", sha1))
 			save_head(sha1_to_hex(sha1));
+		save_opts(opts);
 		save_todo(todo_list, opts);
 	}
 	return pick_commits(todo_list, opts);
diff --git a/sequencer.h b/sequencer.h
index d6fe6e0..e7bef5d 100644
--- a/sequencer.h
+++ b/sequencer.h
@@ -5,6 +5,14 @@
 #define SEQ_OLD_DIR	"sequencer-old"
 #define SEQ_HEAD_FILE	"sequencer/head"
 #define SEQ_TODO_FILE	"sequencer/todo"
+#define SEQ_OPTS_FILE	"sequencer/opts"
+
+enum seq_opt_type {
+	SEQ_OPTION_BOOLEAN,
+	SEQ_OPTION_INTEGER,
+	SEQ_OPTION_STRING,
+	SEQ_OPTION_CALLBACK,
+};
 
 /* Removes SEQ_OLD_DIR and renames SEQ_DIR to SEQ_OLD_DIR, ignoring
  * any errors.  Intended to be used by 'git reset --hard'.
-- 
1.7.5.GIT

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [RFC PATCH] revert: Persist per-session opts
  2011-07-09 15:41 ` [RFC PATCH] revert: Persist per-session opts Ramkumar Ramachandra
@ 2011-07-10  8:02   ` Christian Couder
  2011-07-11  6:12     ` Ramkumar Ramachandra
  0 siblings, 1 reply; 6+ messages in thread
From: Christian Couder @ 2011-07-10  8:02 UTC (permalink / raw)
  To: Ramkumar Ramachandra
  Cc: Git List, Jonathan Nieder, Junio C Hamano, Daniel Barkalow

Hi Ram,

On Saturday 09 July 2011 17:41:58 Ramkumar Ramachandra wrote:
> Save the replay_opts struct in .git/sequencer/opts using a simple "key
> = value" format.  Parse it and populate the options structure before
> replaying.

[...]

>  static void format_todo(struct strbuf *buf, struct commit_list *todo_list,
>  			struct replay_opts *opts)
>  {
> @@ -733,6 +759,102 @@ error:
>  	die(_("Malformed instruction sheet: %s"), git_path(SEQ_TODO_FILE));
>  }
> 
> +static char *parse_opt_value(char *p, void *key, enum seq_opt_type type,
> +			parse_opt_cb *cb_function) {
> +	struct option opt;
> +	char *val, *cur, *end;
> +
> +	if (!(val = strchr(p, '=')))
> +		goto error;
> +	if (!*(val + 1))
> +		goto error;
> +	if (!(end = strchr(p, '\n')))
> +		goto error;
> +	val += 2;

It looks like you rely on all lines ending with \n and having a space after 
the '='. It may be a little bit too fragile.

> +	*end = '\0'; /* Remove trailing '\n' */
> +
> +	switch (type) {
> +	case SEQ_OPTION_BOOLEAN:
> +		if (!strncmp(val, "true", strlen("true")))
> +			*(int *)key = 1;
> +		else if (!strncmp(val, "false", strlen("false")))
> +			*(int *)key = 0;
> +		else
> +			goto error;
> +		break;
> +	case SEQ_OPTION_INTEGER:
> +		*(int *)key = strtol(val, NULL, 10);
> +		break;
> +	case SEQ_OPTION_STRING:
> +		*(char **)key = xstrdup(val);
> +		break;
> +	case SEQ_OPTION_CALLBACK:
> +		opt.value = (struct replay_opts **)key;
> +		while (val) {
> +			if ((cur = strchr(val, '|'))) {
> +				*(cur - 1) = '\0';
> +				(*cb_function)(&opt, val, 0);
> +				val = cur + 2;
> +			} else {
> +				(*cb_function)(&opt, val, 0);
> +				break;
> +			}
> +		}
> +		break;
> +	default:
> +		die(_("program error"));
> +	}
> +	return end + 1;
> +error:
> +	die(_("Malformed options sheet: %s"), git_path(SEQ_OPTS_FILE));
> +}

Otherwise it looks good to me.

Thanks,
Christian.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC PATCH] An option parser for the sequencer
  2011-07-09 15:41 [RFC PATCH] An option parser for the sequencer Ramkumar Ramachandra
  2011-07-09 15:41 ` [RFC PATCH] revert: Persist per-session opts Ramkumar Ramachandra
@ 2011-07-10  8:15 ` Christian Couder
  2011-07-11  6:11   ` Ramkumar Ramachandra
  1 sibling, 1 reply; 6+ messages in thread
From: Christian Couder @ 2011-07-10  8:15 UTC (permalink / raw)
  To: Ramkumar Ramachandra
  Cc: Git List, Jonathan Nieder, Junio C Hamano, Daniel Barkalow

Hi Ram,

On Saturday 09 July 2011 17:41:57 Ramkumar Ramachandra wrote:
> Hi,
> 
> I've decided not to support arbitrary command-line options in the
> instruction sheet.

It may be a good decision, but could you explain why? You could say for 
example that the series would be already an improvement over what we have if 
we don't parse arbitrary command line options.

> A typical instruction sheet will looks like this
> (inspired heavily by the rebase -i instruction sheet format):

> pick 3b36854 t: add tests for cloning remotes with detached HEAD
> pick 61adfd3 consider only branches in guess_remote_head
> pick 8537f0e submodule add: test failure when url is not configured in
> superproject pick 4d68932 submodule add: allow relative repository path
> even when no url is set pick f22a17e submodule add: clean up duplicated
> code
> pick 59a5775 make copy_ref globally available
> pick c1921c1 clone: always fetch remote HEAD

Would it be easy to change the format to support arbitrary command line 
options if we want to do it afterwards, especially after the end of the GSoC?

> For persisting one set of options for every "git cherry-pick"/ "git
> revert" invocation, I've decided to use a simple "key = value" format
> and put it in .git/sequencer/opts (to sit beside .git/sequencer/head
> and .git/sequencer/todo).  For strategy-option, I thought it would be
> cute to separate the various options using ' | '.  So, it'll look
> something like this in the end:
> 
> signoff = true
> mainline = 1
> strategy-option = recursive | ours

Is it the same format as the .git/config file format? Would it be possible to 
reuse some config parsing/writing code?

> The implementation is a little rough around the edges, but I'm pretty
> happy with the overall design: it looks like a scaled-down version of
> parse-options.  Quite a lot of context is missing (where did
> sequencer.h come from?!), but I hope it's clear enough to convey the
> idea.
> 
> Thanks for reading.
> 
> Ramkumar Ramachandra (1):
>   revert: Persist per-session opts
> 
>  builtin/revert.c |  143
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++ sequencer.h      | 
>   8 +++
>  2 files changed, 151 insertions(+), 0 deletions(-)

The subject should have been "[RFC PATCH 0/1] ..." instead of "[RFC PATCH] 
..." because this is just a patch series header.

Thanks,
Christian.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC PATCH] An option parser for the sequencer
  2011-07-10  8:15 ` [RFC PATCH] An option parser for the sequencer Christian Couder
@ 2011-07-11  6:11   ` Ramkumar Ramachandra
  0 siblings, 0 replies; 6+ messages in thread
From: Ramkumar Ramachandra @ 2011-07-11  6:11 UTC (permalink / raw)
  To: Christian Couder
  Cc: Git List, Jonathan Nieder, Junio C Hamano, Daniel Barkalow

Hi,

Christian Couder writes:
> On Saturday 09 July 2011 17:41:57 Ramkumar Ramachandra wrote:
>> I've decided not to support arbitrary command-line options in the
>> instruction sheet.
>
> It may be a good decision, but could you explain why? You could say for
> example that the series would be already an improvement over what we have if
> we don't parse arbitrary command line options.

Here's an elaborate justification:
1. Parsing arbitrary command-line options means that we'd have to
support two versions of every command: the short version and the long
version.  As a result, two configuration files may be functionally
identical, but not really identical because one uses short versions
and the other uses long versions.   This is ugly/ inelegant.
2. The format can be quite loose: for example "--ff -s" is equivalent
to "---ff   '-s'".  We'll either have to go through the trouble to
sanitize everything at the time of persistence, or go through that
trouble while reading.  We can't entirely avoid this ugliness.
3. Where do we stop? Is something like "for i in "moo foo"; do $i;
done;" allowed?  How do we draw clear boundaries?
4. Most importantly, we're NOT building a fast git shell.  There's no
point implementing a reduced shell parser inside Git -- there are many
existing shells we can leverage, if that's what we want.

Besides, I can't think of a good enough usecase for per-instruction
command-line options right now.  How often would someone want to
cherry-pick 10 commits, but signoff on 3 of them somewhere in the
middle?  Not to say that we should not support per-instruction
command-line options: just that we can wait until someone figures out
a great usecase + a good implementation.  It's not critical.

Let's go back to the problem at hand -- what are we trying to do?  The
problem is to get cherry-pick/ revert to support '--continue', and
'--reset' as soon as possible and get it merged.  So, all we need is
one set of options to persist along with the head and todo.  A simple
"key = value" format is both simple and elegant; so we can go ahead
with it, and see how things play out later.  Since none of this is
exposed to the end-user, I don't see us locking ourselves into
short-sighted design at any rate.

>> A typical instruction sheet will looks like this
>> (inspired heavily by the rebase -i instruction sheet format):
>
>> pick 3b36854 t: add tests for cloning remotes with detached HEAD
>> pick 61adfd3 consider only branches in guess_remote_head
>> pick 8537f0e submodule add: test failure when url is not configured in
>> superproject pick 4d68932 submodule add: allow relative repository path
>> even when no url is set pick f22a17e submodule add: clean up duplicated
>> code
>> pick 59a5775 make copy_ref globally available
>> pick c1921c1 clone: always fetch remote HEAD
>
> Would it be easy to change the format to support arbitrary command line
> options if we want to do it afterwards, especially after the end of the GSoC?

Why not?  We aren't explicitly showing this to the end-user yet, so I
see no problem with changing it.  We should worry about that only when
we develop a feature like "cherry-pick -i".

>> For persisting one set of options for every "git cherry-pick"/ "git
>> revert" invocation, I've decided to use a simple "key = value" format
>> and put it in .git/sequencer/opts (to sit beside .git/sequencer/head
>> and .git/sequencer/todo).  For strategy-option, I thought it would be
>> cute to separate the various options using ' | '.  So, it'll look
>> something like this in the end:
>>
>> signoff = true
>> mainline = 1
>> strategy-option = recursive | ours
>
> Is it the same format as the .git/config file format? Would it be possible to
> reuse some config parsing/writing code?

Yes; I'm currently working on it.  I just wanted to show a simple PoC,
but I will try to avoid introducing new parsers and the bugs that come
along with it.

Thanks.

-- Ram

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC PATCH] revert: Persist per-session opts
  2011-07-10  8:02   ` Christian Couder
@ 2011-07-11  6:12     ` Ramkumar Ramachandra
  0 siblings, 0 replies; 6+ messages in thread
From: Ramkumar Ramachandra @ 2011-07-11  6:12 UTC (permalink / raw)
  To: Christian Couder
  Cc: Git List, Jonathan Nieder, Junio C Hamano, Daniel Barkalow

Hi Christian,

Christian Couder writes:
> On Saturday 09 July 2011 17:41:58 Ramkumar Ramachandra wrote:
>> Save the replay_opts struct in .git/sequencer/opts using a simple "key
>> = value" format.  Parse it and populate the options structure before
>> replaying.
>
> [...]
>
>>  static void format_todo(struct strbuf *buf, struct commit_list *todo_list,
>>                       struct replay_opts *opts)
>>  {
>> @@ -733,6 +759,102 @@ error:
>>       die(_("Malformed instruction sheet: %s"), git_path(SEQ_TODO_FILE));
>>  }
>>
>> +static char *parse_opt_value(char *p, void *key, enum seq_opt_type type,
>> +                     parse_opt_cb *cb_function) {
>> +     struct option opt;
>> +     char *val, *cur, *end;
>> +
>> +     if (!(val = strchr(p, '=')))
>> +             goto error;
>> +     if (!*(val + 1))
>> +             goto error;
>> +     if (!(end = strchr(p, '\n')))
>> +             goto error;
>> +     val += 2;
>
> It looks like you rely on all lines ending with \n and having a space after
> the '='. It may be a little bit too fragile.

Right.  Thanks for the review -- will try to reuse bits of the
git-config parser.

-- Ram

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2011-07-11  6:13 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-07-09 15:41 [RFC PATCH] An option parser for the sequencer Ramkumar Ramachandra
2011-07-09 15:41 ` [RFC PATCH] revert: Persist per-session opts Ramkumar Ramachandra
2011-07-10  8:02   ` Christian Couder
2011-07-11  6:12     ` Ramkumar Ramachandra
2011-07-10  8:15 ` [RFC PATCH] An option parser for the sequencer Christian Couder
2011-07-11  6:11   ` Ramkumar Ramachandra

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).