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
33
>>> bzrlib.commands.cmd_rocks()
36
Hey, nice place to begin.
38
The basic object is a Branch. We have a special helper class
39
ScratchBranch that automatically makes a directory and cleans itself
40
up, but is in other respects identical.
42
ScratchBranches are initially empty:
44
>>> b = bzrlib.ScratchBranch()
47
New files in that directory are, it is initially unknown:
49
>>> file(b.base + '/hello.c', 'wt').write('int main() {}')
53
That's not quite true; some files (like editor backups) are ignored by
56
>>> file(b.base + '/hello.c~', 'wt').write('int main() {}')
59
>>> list(b.unknowns())
62
The ``add`` command marks a file to be added in the next revision:
68
You can also add files that otherwise would be ignored. The ignore
69
patterns only apply to files that would be otherwise unknown, so they
70
have no effect once it's added.
77
It is an error to add a file that isn't present in the working copy:
80
Traceback (most recent call last):
82
BzrError: ('cannot add: not a regular file or directory: nothere', [])
84
If we add a file and then change our mind, we can either revert it or
85
remove the file. If we revert, we are left with the working copy (in
86
either I or ? state). If we remove, the working copy is gone. Let's
87
do that to the backup, presumably added accidentally.
89
>>> b.remove('hello.c~')
93
Now to commit, creating a new revision. (Fake the date and name for
96
>>> b.commit('start hello world', timestamp=0, committer='foo@nowhere')
98
>>> b.show_status(show_all=True)
103
We can look back at history
105
>>> r = b.get_revision(b.lookup_revision(1))
108
>>> b.write_log(show_timezone='utc')
109
----------------------------------------
111
committer: foo@nowhere
112
timestamp: Thu 1970-01-01 00:00:00 +0000
116
(The other fields will be a bit unpredictable, depending on who ran
119
As of 2005-02-21, we can also add subdirectories to the revision!
121
>>> os.mkdir(b.base + "/lib")
127
>>> b.commit('add subdir')
129
>>> b.show_status(show_all=True)
134
and we can also add files within subdirectories:
136
>>> file(b.base + '/lib/hello', 'w').write('hello!\n')
141
Tests for adding subdirectories, etc.
143
>>> b = bzrlib.branch.ScratchBranch()
144
>>> os.mkdir(b.abspath('d1'))
145
>>> os.mkdir(b.abspath('d2'))
146
>>> os.mkdir(b.abspath('d2/d3'))
147
>>> list(b.working_tree().unknowns())
150
Create some files, but they're not seen as unknown yet:
152
>>> file(b.abspath('d1/f1'), 'w').close()
153
>>> file(b.abspath('d2/f2'), 'w').close()
154
>>> file(b.abspath('d2/f3'), 'w').close()
155
>>> [v[0] for v in b.inventory.directories()]
157
>>> list(b.working_tree().unknowns())
160
Adding a directory, and we see the file underneath:
163
>>> [v[0] for v in b.inventory.directories()]
165
>>> list(b.working_tree().unknowns())
167
>>> # d2 comes first because it's in the top directory
170
>>> b.commit('add some stuff')
171
>>> list(b.working_tree().unknowns())
172
['d1/f1', 'd2/d3', 'd2/f2', 'd2/f3']
175
>>> list(b.working_tree().unknowns())
176
['d2/d3', 'd2/f2', 'd2/f3']
178
Tests for ignored files and patterns:
180
>>> b = ScratchBranch(dirs=['src', 'doc'],
181
... files=['configure.in', 'configure',
182
... 'doc/configure', 'foo.c',
184
>>> list(b.unknowns())
185
['configure', 'configure.in', 'doc', 'foo', 'foo.c', 'src']
186
>>> b.add(['doc', 'foo.c', 'src', 'configure.in'])
187
>>> list(b.unknowns())
188
['configure', 'foo', 'doc/configure']
189
>>> f = file(b.abspath('.bzrignore'), 'w')
190
>>> f.write('./configure\n'
193
>>> b.add('.bzrignore')
194
>>> list(b.unknowns())
196
>>> b.commit("commit 1")
197
>>> list(b.unknowns())
199
>>> b.add("doc/configure")
200
>>> b.commit("commit more")
205
>>> b = ScratchBranch(files=['foo'], dirs=['subdir'])
206
>>> b.add(['foo', 'subdir'])
207
>>> b.commit('add foo')
208
>>> list(b.unknowns())
210
>>> b.move(['foo'], 'subdir')
214
>>> bzrlib.commands.cmd_renames(b.base)
216
>>> b.commit("move foo to subdir")
217
>>> isfile(b.abspath('foo'))
219
>>> isfile(b.abspath('subdir/foo'))