Subject: Re: [PATCH] commit: Add -f, --fixes <commit> option to add Fixes: line

From: Stefan Beller <[email-masked]>
Date: Sun, 27 Oct 2013 18:03:12 +0100

On 10/27/2013 05:30 PM, Thomas Rast wrote:
> Stefan Beller <[email-masked]> writes:
> 
>> I assembled an overview table, which plots the long options of 
>> git commands by the short letters.
> [...]
>> (In case thunderbird messes it up, here it is again http://pastebin.com/raw.php?i=JBci2Krx)
>>
>> As you can see, f is always --force except for git-config, where it is --file
> 
> Woah!  Impressive work.  Did you autogenerate this?  If so, can we have
> it as a small make target somewhere?  If not, can you send a patch to
> put your table in Documentation somewhere?
> 

I thought about generating it by parsing the man pages, 
but I felt it would not be reliable enough and quite time consuming 
to come up with a parser. Parsing the C sources however also seemed time consuming,
so I decided to come up with this patch:
--8<--
Subject: [PATCH] parse-options: print all options having short and long form and exit

This patch basically only prints all options which have a long and a short form
and then aborts the program. A typical output looks like this:
./git-add
add,  n, dry-run
add,  v, verbose
add,  i, interactive
add,  p, patch
add,  e, edit
add,  f, force
add,  u, update
add,  N, intent-to-add
add,  A, all
---
 parse-options.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/parse-options.c b/parse-options.c
index 62e9b1c..b356ca9 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -500,6 +500,12 @@ int parse_options(int argc, const char **argv, const char *prefix,
 {
 	struct parse_opt_ctx_t ctx;
 
+	for (; options->type != OPTION_END; options++) {
+		if (options->long_name && options->short_name)
+			printf("%s,  %c, %s\n", argv[0], options->short_name, options->long_name);
+	}
+	exit(1);
+
 	parse_options_start(&ctx, argc, argv, prefix, options, flags);
 	switch (parse_options_step(&ctx, options, usagestr)) {
 	case PARSE_OPT_HELP:
-- 
1.8.4.1.605.g23c6912

Unfortunately we can only check git commands, which are written in C. 
You'll notice all the perl/shell written commands are missing (rebase, etc).
Also a few commands written in C cannot easily be picked up, as they do stuff
before calling parse_options. [typically something like "if (argc != 4) print_usage();"]
These commands are also not contained.

The generation of the table however was just a little python:

--8<--
#!/usr/bin/python

cmds="""git-add
git-apply
git-archive
git-branch
git-check-attr
git-check-ignore
git-check-mailmap
git-checkout
git-checkout-index
git-cherry
git-cherry-pick
git-clean
git-clone
git-column
git-commit
git-config
git-count-objects
git-credential-cache
git-credential-store
git-describe
git-fetch
git-fmt-merge-msg
git-for-each-ref
git-format-patch
git-fsck
git-fsck-objects
git-gc
git-grep
git-hash-object
git-help
git-init
git-init-db
git-log
git-ls-files
git-ls-tree
git-merge
git-merge-base
git-merge-file
git-merge-ours
git-mktree
git-mv
git-name-rev
git-notes
git-pack-objects
git-pack-refs
git-prune
git-prune-packed
git-push
git-read-tree
git-reflog
git-remote
git-repack
git-replace
git-rerere
git-reset
git-revert
git-rev-parse
git-rm
git-show
git-show-branch
git-show-ref
git-stage
git-status
git-symbolic-ref
git-tag
git-update-index
git-update-ref
git-update-server-info
git-verify-pack
git-verify-tag
git-whatchanged
git-write-tree"""

import subprocess

shorts={}
cmdoptions={}

for cmd in cmds.split("\n"):
	p = subprocess.Popen("./"+cmd, stdout=subprocess.PIPE)
	p.wait()
	lines = p.stdout.read()
	for line in lines.split("\n"):
		if not len(line):
			continue

		name, short, long = line.split(",")
		if not short in shorts:
			shorts[short] = len(long)
		else:
			shorts[short] = max(shorts[short], len(long))

		if not name in cmdoptions:
			cmdoptions[name] = {}
		cmdoptions[name][short] = long

longest_cmd = 0
for cmd in cmdoptions:
	longest_cmd = max(longest_cmd, len(cmd))

print " "*(longest_cmd-len("Name\\short")), "Name\\short",

for short in shorts:
	print "|" + " "*(1+shorts[short]-len(short)) + short,
print

for cmd in cmdoptions:
	print " "*(longest_cmd-len(cmd)), cmd,
	for short in shorts:
		s = ""
		if short in cmdoptions[cmd]:
			s = cmdoptions[cmd][short]
		print "|" + " "*(1+shorts[short]-len(s)) + s,
	print "  ", cmd

--8<--

I am not sure if we should add such code to the git code base, as it would need some cleanup. 
The existing table however would become outdated fast?
So I do not have a good idea, how such a table could be easily incorporated and kept up to date.

Thanks,
Stefan