2
# -*- coding: UTF-8 -*-
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
# XXX: We might prefer these to be in a text file rather than Python
21
# source, but that only works in doctest from Python 2.4 and later,
22
# which is not present in Warty.
28
These are run by ``bzr.doctest``.
31
>>> from bzrlib import ScratchBranch
32
>>> from bzrlib.osutils import isdir, isfile
34
The basic object is a Branch. We have a special helper class
35
ScratchBranch that automatically makes a directory and cleans itself
36
up, but is in other respects identical.
38
ScratchBranches are initially empty:
40
>>> b = bzrlib.ScratchBranch()
43
New files in that directory are, it is initially unknown:
45
>>> file(b.base + '/hello.c', 'wt').write('int main() {}')
49
That's not quite true; some files (like editor backups) are ignored by
52
>>> file(b.base + '/hello.c~', 'wt').write('int main() {}')
55
>>> list(b.unknowns())
58
The ``add`` command marks a file to be added in the next revision:
64
You can also add files that otherwise would be ignored. The ignore
65
patterns only apply to files that would be otherwise unknown, so they
66
have no effect once it's added.
73
It is an error to add a file that isn't present in the working copy:
76
Traceback (most recent call last):
78
BzrError: ('cannot add: not a regular file or directory: nothere', [])
80
If we add a file and then change our mind, we can either revert it or
81
remove the file. If we revert, we are left with the working copy (in
82
either I or ? state). If we remove, the working copy is gone. Let's
83
do that to the backup, presumably added accidentally.
85
>>> b.remove('hello.c~')
89
Now to commit, creating a new revision. (Fake the date and name for
92
>>> b.commit('start hello world', timestamp=0, committer='foo@nowhere')
94
>>> b.show_status(show_all=True)
99
We can look back at history
101
>>> r = b.get_revision(b.lookup_revision(1))
104
>>> bzrlib.show_log(b, show_timezone='utc')
105
----------------------------------------
107
committer: foo@nowhere
108
timestamp: Thu 1970-01-01 00:00:00 +0000
112
(The other fields will be a bit unpredictable, depending on who ran
115
As of 2005-02-21, we can also add subdirectories to the revision!
117
>>> os.mkdir(b.base + "/lib")
123
>>> b.commit('add subdir')
125
>>> b.show_status(show_all=True)
130
and we can also add files within subdirectories:
132
>>> file(b.base + '/lib/hello', 'w').write('hello!\n')
137
Tests for adding subdirectories, etc.
139
>>> b = bzrlib.branch.ScratchBranch()
140
>>> os.mkdir(b.abspath('d1'))
141
>>> os.mkdir(b.abspath('d2'))
142
>>> os.mkdir(b.abspath('d2/d3'))
143
>>> list(b.working_tree().unknowns())
146
Create some files, but they're not seen as unknown yet:
148
>>> file(b.abspath('d1/f1'), 'w').close()
149
>>> file(b.abspath('d2/f2'), 'w').close()
150
>>> file(b.abspath('d2/f3'), 'w').close()
151
>>> [v[0] for v in b.inventory.directories()]
153
>>> list(b.working_tree().unknowns())
156
Adding a directory, and we see the file underneath:
159
>>> [v[0] for v in b.inventory.directories()]
161
>>> list(b.working_tree().unknowns())
163
>>> # d2 comes first because it's in the top directory
166
>>> b.commit('add some stuff')
167
>>> list(b.working_tree().unknowns())
168
['d1/f1', 'd2/d3', 'd2/f2', 'd2/f3']
171
>>> list(b.working_tree().unknowns())
172
['d2/d3', 'd2/f2', 'd2/f3']
174
Tests for ignored files and patterns:
176
>>> b = ScratchBranch(dirs=['src', 'doc'],
177
... files=['configure.in', 'configure',
178
... 'doc/configure', 'foo.c',
180
>>> list(b.unknowns())
181
['configure', 'configure.in', 'doc', 'foo', 'foo.c', 'src']
182
>>> b.add(['doc', 'foo.c', 'src', 'configure.in'])
183
>>> list(b.unknowns())
184
['configure', 'foo', 'doc/configure']
185
>>> f = file(b.abspath('.bzrignore'), 'w')
186
>>> f.write('./configure\n'
189
>>> b.add('.bzrignore')
190
>>> list(b.unknowns())
192
>>> b.commit("commit 1")
193
>>> list(b.unknowns())
195
>>> b.add("doc/configure")
196
>>> b.commit("commit more")
201
>>> b = ScratchBranch(files=['foo'], dirs=['subdir'])
202
>>> b.add(['foo', 'subdir'])
203
>>> b.commit('add foo')
204
>>> list(b.unknowns())
206
>>> b.move(['foo'], 'subdir')
210
>>> b.commit("move foo to subdir")
211
>>> isfile(b.abspath('foo'))
213
>>> isfile(b.abspath('subdir/foo'))