read-tree: convert unhelpful usage()'s to helpful die()'s
PATCH 1/2 read-tree: convert unhelpful usage()'s to helpful die()'s
Printing the usage message when encountering bad option combinations is not very helpful. Instead, die with a message which tells the user exactly what combination is invalid.
--- builtin-read-tree.c | 6 +++--- 1 files changed, 3 insertions(+), 3 deletions(-)
builtin-read-tree.c +3 -3
@@ -145,9 +145,8 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)continue ; } -/* using -u and -i at the same time makes no sense */ if (1 < opts.index_only + opts.update) -usage (read_tree_usage); +die ("-u and -i at the same time makes no sense" );if (get_sha1 (arg, sha1))die ("Not a valid object name %s" , arg);
@@ -156,7 +155,8 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix) stage++; }if ((opts.update||opts.index_only) && !opts.merge) -usage (read_tree_usage); +die ("%s is meaningless without -m" , + opts.update ?"-u" :"-i" );if ((opts.dir && !opts.update))die ("--exclude-per-directory is meaningless unless -u" );if (opts.merge && !opts.index_only)
Signature · 1 line
1.6.3.3.334.g916e1
PATCH 2/2 read-tree: migrate to parse-options
Cleanup the documentation to explicitly state that --exclude-directory is only meaningful when used with -u. Also make the documentation more consistent with the usage message printed with read-tree --help-all.
--- Documentation/git-read-tree.txt | 5 +- builtin-read-tree.c | 220 +++++++++++++++++++++----------------- 2 files changed, 126 insertions(+), 99 deletions(-)
Documentation/git-read-tree.txt +4 -1
@@ -8,7 +8,10 @@ git-read-tree - Reads tree information into the index SYNOPSIS -------- -'git read-tree' (<tree-ish> | [[-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>] [-u | -i]] [--exclude-per-directory=<gitignore>] [--index-output=<file>] <tree-ish1> [<tree-ish2> [<tree-ish3>]]) +'git read-tree' [--index-output=<file>] <treeish> +'git read-tree' [[-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>] + [-u [--exclude-per-directory=<gitignore>] | -i]] + [--index-output=<file>] <tree-ish1> [<tree-ish2> [<tree-ish3>]] DESCRIPTION
builtin-read-tree.c +121 -97
250 lines not on this page · raw message
Signature · 1 line
1.6.3.3.334.g916e1
PATCH v2 2/2 read-tree: migrate to parse-options
Cleanup the documentation to explicitly state that --exclude-directory is only meaningful when used with -u. Also make the documentation more consistent with the usage message printed with read-tree --help-all.
The -m, --prefix, and --reset options are performing similar actions (setting some flags, read_cache_unmerged(), checking for illegal option combinations). Instead of performing these actions when the options are parsed, we delay doing them until after parse-opts has finished.
---
I updated the two usage strings for --prefix and --reset too.
Documentation/git-read-tree.txt | 5 +- builtin-read-tree.c | 183 ++++++++++++++++++--------------------- 2 files changed, 89 insertions(+), 99 deletions(-)
Documentation/git-read-tree.txt +4 -1
@@ -8,7 +8,10 @@ git-read-tree - Reads tree information into the index SYNOPSIS -------- -'git read-tree' (<tree-ish> | [[-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>] [-u | -i]] [--exclude-per-directory=<gitignore>] [--index-output=<file>] <tree-ish1> [<tree-ish2> [<tree-ish3>]]) +'git read-tree' [--index-output=<file>] <treeish> +'git read-tree' [[-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>] + [-u [--exclude-per-directory=<gitignore>] | -i]] + [--index-output=<file>] <tree-ish1> [<tree-ish2> [<tree-ish3>]] DESCRIPTION
builtin-read-tree.c +82 -95
211 lines not on this page · raw message
Signature · 1 line
1.6.3.3.334.g916e1
Re: [PATCHv2 2/2] read-tree: migrate to parse-options
Documentation/git-read-tree.txt git-read-tree - Reads tree information into the index · line 14
Quoted 9 lines · Stephen Boyd
Documentation/git-read-tree.txt +4
-1@@ -8,7 +8,10 @@ git-read-tree - Reads tree information into the index SYNOPSIS -------- -'git read-tree' (<tree-ish> | [[-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>] [-u | -i]] [--exclude-per-directory=<gitignore>] [--index-output=<file>] <tree-ish1> [<tree-ish2> [<tree-ish3>]]) +'git read-tree' [--index-output=<file>] <treeish> +'git read-tree' [[-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>] + [-u [--exclude-per-directory=<gitignore>] | -i]] + [--index-output=<file>] <tree-ish1> [<tree-ish2> [<tree-ish3>]]
Multi-line synopsis must begin with [verse].
If you write more than one mode of operation, the subsequent text should better reference them, but the current text does not do that. I think it is OK if you leave only the second, particularly because the first is only a subset of the second.
builtin-read-tree.c int cmd_read_tree(int argc, const char **argv, const char *unused_prefix) · line 129
Quoted 7 lines
builtin-read-tree.c +7
-0+ opts.update = update ?1 :0 ; + opts.index_only = index_only ?1 :0 ; + opts.trivial_merges_only = trivial_merges_only ?1 :0 ; + opts.aggressive = aggressive ?1 :0 ; + opts.verbose_update = verbose ?1 :0 ; + opts.reset = reset ?1 :0 ; + stage = opts.merge = (reset || merge || prefix_set) ?1 :0 ;
I don't think that the bitfields of struct unpack_trees_options are cast in stone. IMHO it is fine to make them regular struct members, so that you can take their address for read_tree_options and these foo ? 1 : 0 become unnecessary.
-- Hannes
Re: [PATCHv2 2/2] read-tree: migrate to parse-options
Quoted 5 lines · Johannes Sixt
If you write more than one mode of operation, the subsequent text should better reference them, but the current text does not do that. I think it is OK if you leave only the second, particularly because the first is only a subset of the second.
I was contemplating this change, but I left it out because the single tree case felt special. So special that I felt the merging and the reading were two different modes. The description section hints at the two types of uses, but I think you want it to be more explicit? I'll have to think about this more.
Quoted 4 lines
I don't think that the bitfields of struct
unpack_trees_optionsare cast in stone. IMHO it is fine to make them regular struct members, so that you can take their address forread_tree_optionsand these foo ? 1 : 0 become unnecessary
Thanks. I'll fix this up and resend the series.
Re: [PATCH 2/2] read-tree: migrate to parse-options
In reply to Stephen BoydQuoted 9 lines · Stephen Boyd
Cleanup the documentation to explicitly state that --exclude-directory is only meaningful when used with -u. Also make the documentation more consistent with the usage message printed with read-tree --help-all.
Signed-off-by Stephen Boyd <[email protected]>--- Documentation/git-read-tree.txt | 5 +- builtin-read-tree.c | 220 +++++++++++++++++++++----------------- 2 files changed, 126 insertions(+), 99 deletions(-)
Sorry, but I have to ask: Why?
Re: [PATCH 2/2] read-tree: migrate to parse-options
Quoted 1 line · Junio C Hamano
Sorry, but I have to ask: Why?
I think there are advantages to parse-optification, for plumbing and porcelains. Two reasons I see are:
1. Providing a unified way of handling options
2. Providing a consistent usage message format
Obviously, 1 reduces the bugs associated with parsing options (strcmp vs. prefixcmp, incorrect argv+offset). For number 2, I think it helps users when they see the same style of usage messages with each command. It's also nice to get a quick help message without opening the man pages or using git help <command>.
Admittedly this patch ends up adding 20 or so lines. Do the above points justify this? I think so. I think the added lines can be attributed to the rougher edges of parse-opts exposed by this patch. You can't take the address of bit fields, so 6 lines are dealing with this problem. Where are the other 15 lines coming from?
Looking over it again, I think I may be able to cut the overhead down by refactoring three of the callbacks into boolean options. There's a lot of duplication there, which can be simplified. I was trying to make this a straight port which is probably not so good for convincing you that it's worthwhile.
For now, I don't mind this patch being dropped (there's an ambiguity in the callbacks returning non-zero I'd like to fix too). I'll try and get a new patch (or maybe this patch with the oneline fix and a refactoring patch) out later tonight that actually reduces the amount of lines, instead of grossly adding to them.
PATCH v3 1/2 read-tree: convert unhelpful usage()'s to helpful die()'s
In reply to Stephen BoydPrinting the usage message when encountering bad option combinations is not very helpful. Instead, die with a message which tells the user exactly what combination is invalid.
---
Added --prefix and --reset to the second die message.
builtin-read-tree.c | 6 +++--- 1 files changed, 3 insertions(+), 3 deletions(-)
builtin-read-tree.c +3 -3
@@ -145,9 +145,8 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)continue ; } -/* using -u and -i at the same time makes no sense */ if (1 < opts.index_only + opts.update) -usage (read_tree_usage); +die ("-u and -i at the same time makes no sense" );if (get_sha1 (arg, sha1))die ("Not a valid object name %s" , arg);
@@ -156,7 +155,8 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix) stage++; }if ((opts.update||opts.index_only) && !opts.merge) -usage (read_tree_usage); +die ("%s is meaningless without -m, --reset, or --prefix" , + opts.update ?"-u" :"-i" );if ((opts.dir && !opts.update))die ("--exclude-per-directory is meaningless unless -u" );if (opts.merge && !opts.index_only)
Signature · 1 line
1.6.3.3.334.g916e1
PATCH v3 2/2 read-tree: migrate to parse-options
Cleanup the documentation to explicitly state that --exclude-directory is only meaningful when used with -u. Also make the documentation more consistent with the usage message printed with read-tree --help-all.
The -m, --prefix, --reset options are performing similar actions (setting some flags, read_cache_unmerged(), checking for illegal option combinations). Instead of performing these actions when the options are parsed, we delay performing them until after parse-opts has finished.
The bit fields in struct unpack_trees_options have been promoted to full unsigned ints. This is necessary to avoid "foo ? 1 : 0" constructs to set these fields.
---
After some more thought, Hannes has convinced me that removing the first mode is OK. I was trying to rewrite the documentation, and it just seemed better the way it was.
A couple changes here:
- reordered read_tree_options a bit to group the options with their dependent options better.
- replaced OPT_BOOLEAN with OPT_SET_INT to avoid increment nature of OPT_BOOLEAN messing up logic depending on 1's and 0's only.
Documentation/git-read-tree.txt | 5 +- builtin-read-tree.c | 174 +++++++++++++++++---------------------- unpack-trees.h | 24 +++--- 3 files changed, 92 insertions(+), 111 deletions(-)
Documentation/git-read-tree.txt +4 -1
@@ -8,7 +8,10 @@ git-read-tree - Reads tree information into the index SYNOPSIS -------- -'git read-tree' (<tree-ish> | [[-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>] [-u | -i]] [--exclude-per-directory=<gitignore>] [--index-output=<file>] <tree-ish1> [<tree-ish2> [<tree-ish3>]]) +'git read-tree' [[-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>] + [-u [--exclude-per-directory=<gitignore>] | -i]] + [--index-output=<file>] + <tree-ish1> [<tree-ish2> [<tree-ish3>]] DESCRIPTION
builtin-read-tree.c +74 -96
203 lines not on this page · raw message
unpack-trees.h +12 -12
@@ -17,18 +17,18 @@ struct unpack_trees_error_msgs { };struct unpack_trees_options { -unsigned int reset:1 , - merge:1 , - update:1 , - index_only:1 , - nontrivial_merge:1 , - trivial_merges_only:1 , - verbose_update:1 , - aggressive:1 , - skip_unmerged:1 , - initial_checkout:1 , - diff_index_cached:1 , - gently:1 ; +unsigned int reset, + merge, + update, + index_only, + nontrivial_merge, + trivial_merges_only, + verbose_update, + aggressive, + skip_unmerged, + initial_checkout, + diff_index_cached, + gently;const char *prefix;int pos;struct dir_struct *dir;
Signature · 1 line
1.6.3.3.334.g916e1
Re: [PATCHv3 2/2] read-tree: migrate to parse-options
Quoted 15 lines · Stephen Boyd
struct
unpack_trees_options{(quoted diff) +2
-12- unsigned int reset:1, - merge:1, - update:1, - index_only:1, - nontrivial_merge:1, - trivial_merges_only:1, - verbose_update:1, - aggressive:1, - skip_unmerged:1, - initial_checkout:1, - diff_index_cached:1, - gently:1; + unsigned int reset, + merge,
Review
Quoted 10 lines
(quoted diff) +10
-0+ update, + index_only, + nontrivial_merge, + trivial_merges_only, + verbose_update, + aggressive, + skip_unmerged, + initial_checkout, + diff_index_cached, + gently;
Sorry I went a little overboard with s/:1// on unpack_tree_options. You'll probably want to squash this on top.
unpack-trees.h +5 -5
@@ -21,14 +21,14 @@ struct unpack_trees_options { merge, update, index_only, - nontrivial_merge, + nontrivial_merge:1, trivial_merges_only, verbose_update, aggressive, - skip_unmerged, - initial_checkout, - diff_index_cached, - gently; + skip_unmerged:1, + initial_checkout:1, + diff_index_cached:1, + gently:1;const char *prefix;int pos;struct dir_struct *dir;
Re: [PATCHv3 2/2] read-tree: migrate to parse-options
Quoted 14 lines · Stephen Boyd
Sorry I went a little overboard with s/:1// on
unpack_tree_options. You'll probably want to squash this on top.unpack-trees.h +1
-1@@ -21,14 +21,14 @@ struct unpack_trees_options { merge, update, index_only, - nontrivial_merge, + nontrivial_merge:1, trivial_merges_only,
unpack-trees.h struct unpack_trees_options { · line 31
Quoted 10 lines
unpack-trees.h +4
-4verbose_update, aggressive, - skip_unmerged, - initial_checkout, - diff_index_cached, - gently; + skip_unmerged:1, + initial_checkout:1, + diff_index_cached:1, + gently:1;
Let's look at this (not this follow-up patch) the other way around.
Six months down the load, somebody may ask you:
Is there a good reason why many are not bitfields but only selected few are bitfields in this structure? Most of these can and should be bitfields, as far as I can see, because the code uses them as booleans, and the only breakage it may cause if we change them to bitfields to shrink this structure would be the option parsing code.
What would be your answer? Doesn't it feel wrong to do such a conversion only to work around the current limitation of parseopt?
By the way, has it been verified that all the users of these fields are OK with this change when they use these fields? I am not worried about them reading the value command line option parser set, but more worried about reading after other codepaths set/modified these fields. The command line parser that uses parseopt may correctly set only 0 or 1 to these fields initially and we should be able to verify that from the patch text, but there is no guarantee that this conversion is even correct at runtime without an audit, no?
The callers have long relied on the fact that reading from these bitfields yields either 0 or 1 and never 2 or larger, but they are now widened to full-width unsigned. A pattern like this:
uto.field = ~uto.field;
if (uto.field == 1) {
field now set;
} else {
field now unset;
}would be broken by widening "unsigned field:1" to "unsigned field", right? I am not saying this is the only pattern that would break, nor I know there are codepaths that use this pattern, but I think you got my point.
Re: [PATCHv3 2/2] read-tree: migrate to parse-options
Quoted 5 lines · Junio C Hamano
Let's look at this (not this follow-up patch) the other way around.
Six months down the load, somebody may ask you:
Quoted 8 lines
Is there a good reason why many are not bitfields but only selected few are bitfields in this structure? Most of these can and should be bitfields, as far as I can see, because the code uses them as booleans, and the only breakage it may cause if we change them to bitfields to shrink this structure would be the option parsing code.
What would be your answer? Doesn't it feel wrong to do such a conversion only to work around the current limitation of parseopt?
I understand. It does feel wrong to do this just to workaround parseopts, but I was assuming this wasn't a performance critical struct because Hannes said it wasn't set in stone. Of course, it also feels wrong to have the foo ? 1 : 0, but I think it's less wrong. This is why I had the foo ? 1 : 0 constructs in v2, because I felt that making this more radical change would lead to just this question. As a bonus, having these ugly constructs encourages someone to come up with a way to handle bit fields in parseopts.
Quoted 21 lines
By the way, has it been verified that all the users of these fields are OK with this change when they use these fields? I am not worried about them reading the value command line option parser set, but more worried about reading after other codepaths set/modified these fields. The command line parser that uses parseopt may correctly set only 0 or 1 to these fields initially and we should be able to verify that from the patch text, but there is no guarantee that this conversion is even correct at runtime without an audit, no?
The callers have long relied on the fact that reading from these bitfields yields either 0 or 1 and never 2 or larger, but they are now widened to full-width unsigned. A pattern like this:
uto.field = ~uto.field; if (uto.field == 1) { field now set; } else { field now unset; }
Quoted 3 lines
would be broken by widening "unsigned field:1" to "unsigned field", right? I am not saying this is the only pattern that would break, nor I know there are codepaths that use this pattern, but I think you got my point.
Yes. I glanced over the users of unpack_trees_options and didn't see anything dangerous like the above example. It wasn't a really thorough audit though because I was hoping that the callers were treating them as booleans, and not bits.