aboutsummaryrefslogtreecommitdiffstats
path: root/libbe/command/init.py
blob: 2a78147ad01da9316b4fe155f96dfb66a2cbdb3f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# Copyright (C) 2005-2009 Aaron Bentley and Panometrics, Inc.
#                         Gianluca Montecchi <gian@grys.it>
#                         W. Trevor King <wking@drexel.edu>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import os.path

import libbe
import libbe.bugdir
import libbe.command
import libbe.storage

class Init (libbe.command.Command):
    """Create an on-disk bug repository

    >>> import os, sys
    >>> import libbe.storage.vcs
    >>> import libbe.storage.vcs.base
    >>> import libbe.util.utility
    >>> io = libbe.command.StringInputOutput()
    >>> io.stdout = sys.stdout
    >>> ui = libbe.command.UserInterface(io=io)
    >>> cmd = Init()

    >>> dir = libbe.util.utility.Dir()
    >>> vcs = libbe.storage.vcs.vcs_by_name('None')
    >>> vcs.repo = dir.path
    >>> try:
    ...     vcs.connect()
    ... except libbe.storage.ConnectionError:
    ...     'got error'
    'got error'
    >>> ui.storage_callbacks.set_unconnected_storage(vcs)
    >>> ui.run(cmd)
    No revision control detected.
    BE repository initialized.
    >>> bd = libbe.bugdir.BugDir(vcs)
    >>> vcs.disconnect()
    >>> vcs.destroy()
    >>> dir.cleanup()

    >>> dir = libbe.util.utility.Dir()
    >>> vcs = libbe.storage.vcs.installed_vcs()
    >>> vcs.repo = dir.path
    >>> vcs._vcs_init(vcs.repo)
    >>> ui.storage_callbacks.set_unconnected_storage(vcs)
    >>> if vcs.name in libbe.storage.vcs.base.VCS_ORDER:
    ...     ui.run(cmd) # doctest: +ELLIPSIS
    ... else:
    ...     vcs.init()
    ...     vcs.connect()
    ...     print 'Using ... for revision control.\\nDirectory initialized.'
    Using ... for revision control.
    BE repository initialized.
    >>> vcs.disconnect()
    >>> vcs.destroy()
    >>> dir.cleanup()
    """
    name = 'init'

    def __init__(self, *args, **kwargs):
        libbe.command.Command.__init__(self, *args, **kwargs)

    def _run(self, **params):
        storage = self._get_unconnected_storage()
        if not os.path.isdir(storage.repo):
            raise libbe.command.UserError(
                'No such directory: %s' % storage.repo)
        try:
            storage.connect()
            raise libbe.command.UserError(
                'Directory already initialized: %s' % storage.repo)
        except libbe.storage.ConnectionError:
            pass
        storage.init()
        storage.connect()
        bd = libbe.bugdir.BugDir(storage, from_storage=False)
        bd.save()
        if bd.storage.name is not 'None':
            print >> self.stdout, \
                'Using %s for revision control.' % storage.name
        else:
            print >> self.stdout, 'No revision control detected.'
        print >> self.stdout, 'BE repository initialized.'

    def _long_help(self):
        return """
This command initializes Bugs Everywhere support for the specified directory
and all its subdirectories.  It will auto-detect any supported revision control
system.  You can use "be set vcs_name" to change the vcs being used.

The directory defaults to your current working directory, but you can
change that by passing the --repo option to be
  $ be --repo path/to/new/bug/root init

When initialized in a version-controlled directory, BE sinks to the
version-control root.  In that case, the BE repository will be created
under that directory, rather than the current directory or the one
passed in --repo.  Consider the following tree, versioned in Git.
  ~
  `--projectX
     |-- .git
     `-- src
Calling
  ~$ be --repo ./projectX/src init
will create the BE repository rooted in projectX:
  ~
  `--projectX
     |-- .be
     |-- .git
     `-- src
"""