92e07f9b151df273a390b3649b3b3901aa748387
[be.git] / libbe / command / init.py
1 # Copyright (C) 2005-2011 Aaron Bentley <abentley@panoramicfeedback.com>
2 #                         Gianluca Montecchi <gian@grys.it>
3 #                         W. Trevor King <wking@drexel.edu>
4 #
5 # This file is part of Bugs Everywhere.
6 #
7 # Bugs Everywhere is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by the
9 # Free Software Foundation, either version 2 of the License, or (at your
10 # option) any later version.
11 #
12 # Bugs Everywhere is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Bugs Everywhere.  If not, see <http://www.gnu.org/licenses/>.
19
20 import os.path
21
22 import libbe
23 import libbe.bugdir
24 import libbe.command
25 import libbe.storage
26
27 class Init (libbe.command.Command):
28     """Create an on-disk bug repository
29
30     >>> import os, sys
31     >>> import libbe.storage.vcs
32     >>> import libbe.storage.vcs.base
33     >>> import libbe.util.utility
34     >>> io = libbe.command.StringInputOutput()
35     >>> io.stdout = sys.stdout
36     >>> ui = libbe.command.UserInterface(io=io)
37     >>> cmd = Init()
38
39     >>> dir = libbe.util.utility.Dir()
40     >>> vcs = libbe.storage.vcs.vcs_by_name('None')
41     >>> vcs.repo = dir.path
42     >>> try:
43     ...     vcs.connect()
44     ... except libbe.storage.ConnectionError:
45     ...     'got error'
46     'got error'
47     >>> ui.storage_callbacks.set_unconnected_storage(vcs)
48     >>> ui.run(cmd)
49     No revision control detected.
50     BE repository initialized.
51     >>> bd = libbe.bugdir.BugDir(vcs)
52     >>> vcs.disconnect()
53     >>> vcs.connect()
54     >>> bugdir = libbe.bugdir.BugDir(vcs, from_storage=True)
55     >>> vcs.disconnect()
56     >>> vcs.destroy()
57     >>> dir.cleanup()
58
59     >>> dir = libbe.util.utility.Dir()
60     >>> vcs = libbe.storage.vcs.installed_vcs()
61     >>> vcs.repo = dir.path
62     >>> vcs._vcs_init(vcs.repo)
63     >>> ui.storage_callbacks.set_unconnected_storage(vcs)
64     >>> if vcs.name in libbe.storage.vcs.base.VCS_ORDER:
65     ...     ui.run(cmd) # doctest: +ELLIPSIS
66     ... else:
67     ...     vcs.init()
68     ...     vcs.connect()
69     ...     print 'Using ... for revision control.\\nDirectory initialized.'
70     Using ... for revision control.
71     BE repository initialized.
72     >>> vcs.disconnect()
73     >>> vcs.connect()
74     >>> bugdir = libbe.bugdir.BugDir(vcs, from_storage=True)
75     >>> vcs.disconnect()
76     >>> vcs.destroy()
77     >>> dir.cleanup()
78     """
79     name = 'init'
80
81     def __init__(self, *args, **kwargs):
82         libbe.command.Command.__init__(self, *args, **kwargs)
83
84     def _run(self, **params):
85         storage = self._get_unconnected_storage()
86         if not os.path.isdir(storage.repo):
87             raise libbe.command.UserError(
88                 'No such directory: %s' % storage.repo)
89         try:
90             storage.connect()
91             raise libbe.command.UserError(
92                 'Directory already initialized: %s' % storage.repo)
93         except libbe.storage.ConnectionError:
94             pass
95         storage.init()
96         storage.connect()
97         self.ui.storage_callbacks.set_storage(storage)
98         bd = libbe.bugdir.BugDir(storage, from_storage=False)
99         self.ui.storage_callbacks.set_bugdir(bd)
100         if bd.storage.name is not 'None':
101             print >> self.stdout, \
102                 'Using %s for revision control.' % storage.name
103         else:
104             print >> self.stdout, 'No revision control detected.'
105         print >> self.stdout, 'BE repository initialized.'
106
107     def _long_help(self):
108         return """
109 This command initializes Bugs Everywhere support for the specified directory
110 and all its subdirectories.  It will auto-detect any supported revision control
111 system.  You can use "be set vcs_name" to change the vcs being used.
112
113 The directory defaults to your current working directory, but you can
114 change that by passing the --repo option to be
115   $ be --repo path/to/new/bug/root init
116
117 When initialized in a version-controlled directory, BE sinks to the
118 version-control root.  In that case, the BE repository will be created
119 under that directory, rather than the current directory or the one
120 passed in --repo.  Consider the following tree, versioned in Git.
121   ~
122   `--projectX
123      |-- .git
124      `-- src
125 Calling
126   ~$ be --repo ./projectX/src init
127 will create the BE repository rooted in projectX:
128   ~
129   `--projectX
130      |-- .be
131      |-- .git
132      `-- src
133 """