git-cvsimport: select cvsimport-[23] automatically

By checking which of $CVSPS2_PATH and $CVSPS3_PATH exists, choose which
of the git-cvsimport-X scripts to invoke.  This can be overridden by
setting GIT_CVSPS_VERSION to either 2 or 3 before executing the script.

Signed-off-by: John Keeping <john@keeping.me.uk>
git-cvsimport-3: comply with PEP 8 some more

Introduce 2 blank lines between top-level items and one between class
member functions, as recommended by PEP 8.

Signed-off-by: John Keeping <john@keeping.me.uk>
git-cvsimport-3: use title case for class names

Signed-off-by: John Keeping <john@keeping.me.uk>
git-cvsimport-3: extract main as a method

This is the first step to making it easier to debug the script using a
Python REPL.

Signed-off-by: John Keeping <john@keeping.me.uk>
git-cvsimport-3: remove a redundant version check

We already check the Python version at the top of the script, so doing
it again in main() is unnecessary.

Signed-off-by: John Keeping <john@keeping.me.uk>
git-cvsimport-3: don't quit when filesource is requested

Signed-off-by: John Keeping <john@keeping.me.uk>
git-cvsimport-3: extract error handler from main()

This will allow us to handle errors during option parsing by simply
raising an exception, and removes a layer of indentation.

Also change the error from "git_cvsimport" to "git cvsimport".

The patch is much clearer with 'git diff -b'.

Signed-off-by: John Keeping <john@keeping.me.uk>
git-cvsimport-3: use Fatal consistently

Instead of doing "sys.stderr.write(...); sys.exit(1)", raise a Fatal
exception when a fatal error occurs.  This change means that we can
remove the "git cvsimport: " prefix from all fatal error messages since
it is added by the top-level exception handler when printing the message
from a Fatal exception.

Signed-off-by: John Keeping <john@keeping.me.uk>
git-cvsimport-3: remove unused parameter to *_or_die

The legend parameters to do_or_die() and capture_or_die() are never
used.  Remove them.

Signed-off-by: John Keeping <john@keeping.me.uk>
git-cvsimport-3: handle the backend option first

Most of the options to git-cvsimport-3 are simply passed to the backend,
this means the if the '-e <backend>' option does not occur first any
options preceding it will effectively be ignored.

Extract the '-e' option handling as a special case before the main
option handling loop to avoid this issue.

Signed-off-by: John Keeping <john@keeping.me.uk>
git-cvsimport-3: extract subprocess error handling

By changing subprocess.call into subprocess.check_call all invocations
of subprocesses will throw a CalledProcessError if the child fails.
This means that we can use common handling for subprocess errors and
extract this to the top-level of the script.

Signed-off-by: John Keeping <john@keeping.me.uk>
git-cvsimport-3: don't execute subcommands with the shell

Creating command strings to execute subcommands via the shell means that
care needs to be taken to quote any user-supplied values, but Python
provides a better way by passing a list of arguments when creating
subprocesses.

Make this change, changing pipelines that currently use the shell to use
pipelined subprocesses.

Signed-off-by: John Keeping <john@keeping.me.uk>
git-cvsimport-3: actually invoke git-branch

The command called if a branch is renamed is "branch -m".  This should
clearly by "git branch -m".

Signed-off-by: John Keeping <john@keeping.me.uk>
git-cvsimport-3: fix moving tags

The command "git tag -f $tag $changed" moves $tag to point at commit
$changed but $changed is the new name for the tag.  Fix this by
reordering the arguments to "git tag".

Also add a call to "git update-ref -d" to remove the old tag.

Signed-off-by: John Keeping <john@keeping.me.uk>
git-cvsimport-3: use for-each-ref not "branch -l"

git-branch is a porcelain command, switch to the plumbing command
git-for-each-ref so that we're not relying on the output format of
git-branch.

Signed-off-by: John Keeping <john@keeping.me.uk>
git-cvsimport-3: use for-each-ref not "tag -l"

git-tag is a porcelain command, switch to the plumbing command
git-for-each-ref so that we're not relying on the output format of
git-tag.

Signed-off-by: John Keeping <john@keeping.me.uk>
git-cvsimport-3: use git-update-ref to modify refs

Instead of the porcelain command "git-tag" use "git-update-ref" when
tags need to be renamed.

Signed-off-by: John Keeping <john@keeping.me.uk>
---
Is there a way to move a branch's reflog using plumbing commands?
git-cvsimport-3: checkout the new tree correctly

Once the import has finished, the branch pointed at by HEAD may have
moved.  "git checkout -q" isn't sufficient to change the working tree -
use "git checkout -f" as git-cvsimport-2.perl does.

Signed-off-by: John Keeping <john@keeping.me.uk>
---
Should we be asserting that the working tree is not dirty before
starting the import, in the same way the git-rebase does?
git-cvsimport-3: don't allow more than one module

Raise an error if more than one module is specified on the command line.

Signed-off-by: John Keeping <john@keeping.me.uk>
git-cvsimport-3: read configuration from git config

git-cvsimport-2 supports reading options from the Git repository into
which it was importing.  Make git-cvsimport-3 do the same.

Signed-off-by: John Keeping <john@keeping.me.uk>