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
>>> bzrlib.commands.cmd_rocks()
35
Hey, nice place to begin.
37
The basic object is a Branch. We have a special helper class
38
ScratchBranch that automatically makes a directory and cleans itself
39
up, but is in other respects identical.
41
ScratchBranches are initially empty:
43
>>> b = bzrlib.ScratchBranch()
46
New files in that directory are, it is initially unknown:
48
>>> file(b.base + '/hello.c', 'wt').write('int main() {}')
52
That's not quite true; some files (like editor backups) are ignored by
55
>>> file(b.base + '/hello.c~', 'wt').write('int main() {}')
58
>>> list(b.unknowns())
61
The ``add`` command marks a file to be added in the next revision:
67
You can also add files that otherwise would be ignored. The ignore
68
patterns only apply to files that would be otherwise unknown, so they
69
have no effect once it's added.
76
It is an error to add a file that isn't present in the working copy:
79
Traceback (most recent call last):
81
BzrError: ('cannot add: not a regular file or directory: nothere', [])
83
If we add a file and then change our mind, we can either revert it or
84
remove the file. If we revert, we are left with the working copy (in
85
either I or ? state). If we remove, the working copy is gone. Let's
86
do that to the backup, presumably added accidentally.
88
>>> b.remove('hello.c~')
92
Now to commit, creating a new revision. (Fake the date and name for
95
>>> b.commit('start hello world', timestamp=0, committer='foo@nowhere')
97
>>> b.show_status(show_all=True)
102
We can look back at history
104
>>> r = b.get_revision(b.lookup_revision(1))
107
>>> b.write_log(show_timezone='utc')
108
----------------------------------------
110
committer: foo@nowhere
111
timestamp: Thu 1970-01-01 00:00:00 +0000
115
(The other fields will be a bit unpredictable, depending on who ran
118
As of 2005-02-21, we can also add subdirectories to the revision!
120
>>> os.mkdir(b.base + "/lib")
126
>>> b.commit('add subdir')
128
>>> b.show_status(show_all=True)
133
and we can also add files within subdirectories:
135
>>> file(b.base + '/lib/hello', 'w').write('hello!\n')
140
Tests for adding subdirectories, etc.
142
>>> b = bzrlib.branch.ScratchBranch()
143
>>> os.mkdir(b.abspath('d1'))
144
>>> os.mkdir(b.abspath('d2'))
145
>>> os.mkdir(b.abspath('d2/d3'))
146
>>> list(b.working_tree().unknowns())
149
Create some files, but they're not seen as unknown yet:
151
>>> file(b.abspath('d1/f1'), 'w').close()
152
>>> file(b.abspath('d2/f2'), 'w').close()
153
>>> file(b.abspath('d2/f3'), 'w').close()
154
>>> [v[0] for v in b.inventory.directories()]
156
>>> list(b.working_tree().unknowns())
159
Adding a directory, and we see the file underneath:
162
>>> [v[0] for v in b.inventory.directories()]
164
>>> list(b.working_tree().unknowns())
166
>>> # d2 comes first because it's in the top directory
169
>>> b.commit('add some stuff')
170
>>> list(b.working_tree().unknowns())
171
['d1/f1', 'd2/d3', 'd2/f2', 'd2/f3']
174
>>> list(b.working_tree().unknowns())
175
['d2/d3', 'd2/f2', 'd2/f3']
177
Tests for ignored files and patterns:
179
>>> b = ScratchBranch(dirs=['src', 'doc'],
180
... files=['configure.in', 'configure',
181
... 'doc/configure', 'foo.c',
183
>>> list(b.unknowns())
184
['configure', 'configure.in', 'doc', 'foo', 'foo.c', 'src']
185
>>> b.add(['doc', 'foo.c', 'src', 'configure.in'])
186
>>> list(b.unknowns())
187
['configure', 'doc/configure', 'foo']
188
>>> f = file(b.abspath('.bzrignore'), 'w')
189
>>> f.write('./configure\n'
192
>>> b.add('.bzrignore')
193
>>> list(b.unknowns())
194
['configure', 'doc/configure', 'foo']