diff: apply flake8 suggestions
[git-cola.git] / bin / git-dag
blob9e23299a426c959b46e32a8b9bc1966f9a6535a0
1 #!/usr/bin/env python
2 # -*- python-mode -*-
3 """git-dag: An advanced git DAG visualizer
4 """
6 from __future__ import division, absolute_import, unicode_literals
7 import os
8 import sys
9 from argparse import ArgumentParser
12 __copyright__ = """
13 Copyright (C) 2007-2016 David Aguilar and contributors
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License version 2 as
17 published by the Free Software Foundation.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 """
27 def setup_environment():
28 """Provides access to the cola modules"""
29 # Try to detect where it is run from and set prefix and the search path.
30 # It is assumed that the user installed Cola using the --prefix= option
31 abspath = os.path.abspath
32 dirname = os.path.dirname
33 realpath = os.path.realpath
34 join = os.path.join
36 python2 = sys.version_info[0] == 2
37 prefix = dirname(dirname(realpath(abspath(__file__))))
39 if python2:
40 cola_mod = join(prefix, str('cola'), str('__init__.py'))
41 install_lib = join(prefix, str('share'), str('git-cola'), str('lib'))
42 else:
43 # Look for modules in the source or install trees
44 cola_mod = os.path.join(prefix, 'cola', '__init__.py')
45 install_lib = os.path.join(prefix, 'share', 'git-cola', 'lib')
47 if os.path.exists(cola_mod):
48 # Source tree
49 sys.path.insert(1, prefix)
50 else:
51 # Install tree
52 sys.path.insert(1, install_lib)
53 setup_environment()
56 from cola.app import add_common_arguments
57 from cola.app import application_init
58 from cola.app import application_start
59 from cola.widgets.dag import git_dag
62 def main():
63 args = parse_args()
64 return args.func(args)
67 def parse_args():
68 parser = ArgumentParser()
69 parser.set_defaults(func=cmd_dag)
71 add_common_arguments(parser)
72 parser.add_argument('-c', '--count', '--max-count', metavar='<count>',
73 type=int, default=1000,
74 help='number of commits to display')
75 parser.add_argument('args', nargs='*', metavar='<args>',
76 help='git log arguments')
77 args, rest = parser.parse_known_args()
78 if rest:
79 # splice unknown arguments to the beginning ~
80 # these are forwarded to git-log(1).
81 args.args[:0] = rest
82 return args
85 def cmd_dag(args):
86 context = application_init(args)
87 view = git_dag(context.model, args=args, settings=args.settings)
88 return application_start(context, view, monitor_refs_only=True)
91 if __name__ == '__main__':
92 sys.exit(main())