Files
git-upstream/git_upstream/main.py
Darragh Bailey c195b69c6b Fix broken interactive mode to be usable
Switch rebase editor class to exec rebase so that it takes over the
console to allow the user to continue/skip/abort as they would
normally. Update the instruction set for rebase to re-call
git-upstream as a final step for user convenience to finish the import.

When the EDITOR or GIT_EDITOR is defined to change what editor is used
to edit the sequence of instructions to git-rebase, git will recall the
same editor in those variables if one of the instructions is 'reword',
'edit', or 'squash' in order to amend the commit message. Change the
rebaseeditor script to be able to handle being called with a
COMMIT_EDITMSG file when this occurs.

This allows the 'edit', 'reword', 'squash' & 'fixup' commands to git
rebase to function as expected by the user irrespective of whether a
conflict has been encountered first or not.

Still results in git rebase outputting additional information after
the import has completed, but avoids the main issue of the interactive
mode being unusable due to ignoring the result of the git-rebase call
unless it exits with an error, which only occurs for conflicts.

Change-Id: Ic125c85b868d816ef13fac6f503092ac3142b4de
Closes-Bug: #1402032
2016-08-31 12:09:52 +01:00

163 lines
5.3 KiB
Python

#
# Copyright (c) 2012, 2013, 2014 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""
Command-line tool for tracking upstream revisions
"""
import argparse
import logging
import os
import sys
import git
from git_upstream import __version__
from git_upstream import commands
from git_upstream.errors import GitUpstreamError
from git_upstream import log
try:
import argcomplete
argparse_loaded = True
except ImportError:
argparse_loaded = False
def build_parsers():
parser = argparse.ArgumentParser(
description=__doc__.strip(),
epilog='See "%(prog)s help COMMAND" for help on a specific command.',
add_help=False)
parser.add_argument('--version', action='version',
version='%(prog)s ' + __version__)
parser.add_argument('-h', '--help', action='help',
help='show this help message and exit')
group = parser.add_mutually_exclusive_group()
group.add_argument('-q', '--quiet', action='store_true',
help='Suppress additional output except for errors, '
'conflicts with --verbose')
group.add_argument('-v', '--verbose', action='count', default=1,
help='Increase verbosity from commands, conflicts '
'with --quiet. May be set more than once.')
# support logging to files as hidden options until we can hide them in
# normal help output, while showing them in extended help or generated
# man pages and documentation.
parser.add_argument('--log-level', dest='log_level', default='notset',
help=argparse.SUPPRESS)
parser.add_argument('--log-file', dest='log_file', help=argparse.SUPPRESS)
subcommand_parsers = commands.get_subcommands(parser)
# calculate the correct path to allow the program be re-executed
program = sys.argv[0]
if os.path.split(program)[-1] != 'git-upstream':
script_cmdline = ['python', program]
else:
script_cmdline = [program]
parser.set_defaults(script_cmdline=script_cmdline)
return subcommand_parsers, parser
def setup_console_logging(options):
options.log_level = getattr(logging, options.log_level.upper(),
logging.NOTSET)
console_log_level = getattr(logging,
log.get_increment_level(options.verbose),
logging.NOTSET)
if options.quiet:
console_log_level = logging.NOTSET
# determine maximum logging requested for file and console provided they
# are not disabled, and including stderr which is fixed at ERROR
main_log_level = min([value
for value in (options.log_level, console_log_level)
if value != logging.NOTSET
] + [logging.ERROR])
logger = log.get_logger()
logger.setLevel(main_log_level)
if not options.quiet:
# configure logging to console for verbose/quiet messages
console = logging.StreamHandler(sys.stdout)
console.setLevel(console_log_level)
console.addFilter(log.LevelFilterIgnoreAbove(logging.ERROR))
console.setFormatter(logging.Formatter("%(message)s"))
logger.addHandler(console)
# make sure error and critical messages go to stderr and aren't suppressed
err_con = logging.StreamHandler(sys.stderr)
err_con.setLevel(logging.ERROR)
err_con.addFilter(log.LevelFilterIgnoreBelow(logging.ERROR))
err_con.setFormatter(logging.Formatter("%(levelname)-8s: %(message)s"))
logger.addHandler(err_con)
if options.log_file:
filehandler = logging.FileHandler(options.log_file)
filehandler.setLevel(options.log_level)
_format = "%(asctime)s - %(name)s - %(levelname)s: %(message)s"
filehandler.setFormatter(logging.Formatter(_format))
logger.addHandler(filehandler)
return logger
def main(argv=None):
# We default argv to None and assign to sys.argv[1:] below because having
# an argument default value be a mutable type in Python is a gotcha. See
# http://bit.ly/1o18Vff
if not argv:
argv = sys.argv[1:]
(cmds, parser) = build_parsers()
if not argv:
parser.print_help()
return 0
if argparse_loaded:
argcomplete.autocomplete(parser)
args = parser.parse_args(argv)
logger = setup_console_logging(args)
if args.cmd.name == "help":
args.cmd.run(args, parser)
return 0
if git.Git().version_info < (1, 7, 5):
logger.fatal("Git-Upstream requires git version 1.7.5 or later")
sys.exit(1)
try:
args.cmd.run(args)
except GitUpstreamError as e:
logger.fatal("%s", e[0])
logger.debug("Git-Upstream: %s", e[0], exc_info=e)
sys.exit(1)
if __name__ == '__main__':
main()
# vim:sw=4:sts=4:ts=4:et: