1
# Copyright (C) 2006 by Szilveszter Farkas (Phanatic) <szilveszter.farkas@gmail.com>
2
# Some parts of the code are:
3
# Copyright (C) 2005, 2006 by Canonical Ltd
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
import bzrlib.errors as errors
22
from bzrlib.branch import Branch
23
from bzrlib.workingtree import WorkingTree
25
from bzrlib.errors import (NotBranchError, PermissionDenied, BzrError)
27
class DifferentBranchesError(BzrError):
28
""" Occurs if the specified files are in different branches
35
class PrefixFormatError(BzrError):
36
""" Occurs if the prefix is badly formatted
43
class RevisionValueError(BzrError):
44
""" Invalid revision value provided
51
def diff(revision=None, file_list=None, diff_options=None, prefix=None):
52
""" Save the diff into a temporary file.
54
:param revision: a list of revision numbers (one or two elements)
56
:param file_list: list of files you want to diff
58
:param diff_options: external diff options
60
:param prefix: 0 - p0, 1 - p1, or specify prefixes in the form of old/:new/
62
:return: path to the temporary file which contains the diff output (the frontend has to remove it!)
64
from tempfile import mkstemp
66
from bzrlib.builtins import internal_tree_files
67
from bzrlib.diff import show_diff_trees
68
from bzrlib.revisionspec import RevisionSpec_int
69
from bzrlib.workingtree import WorkingTree
71
from info_helper import diff_helper
73
if (prefix is None) or (prefix == '0'):
82
raise PrefixFormatError
83
old_label, new_label = prefix.split(":")
86
tree1, file_list = internal_tree_files(file_list)
90
except errors.FileInWrongBranch:
91
if len(file_list) != 2:
92
raise DifferentBranchesError
94
tree1, file1 = WorkingTree.open_containing(file_list[0])
95
tree2, file2 = WorkingTree.open_containing(file_list[1])
97
if file1 != "" or file2 != "":
98
raise DifferentBranchesError
102
tmpfile = mkstemp(prefix='olive_')
103
tmpfp = open(tmpfile[1], 'w')
105
if revision is not None:
106
if tree2 is not None:
107
raise RevisionValueError
109
if len(revision) >= 1:
110
revision[0] = RevisionSpec_int(revision[0])
111
if len(revision) == 2:
112
revision[1] = RevisionSpec_int(revision[1])
114
if (len(revision) == 1) or (revision[1].spec is None):
115
ret = diff_helper(tree1, file_list, diff_options,
117
old_label=old_label, new_label=new_label,
119
elif len(revision) == 2:
120
ret = diff_helper(tree1, file_list, diff_options,
121
revision[0], revision[1],
122
old_label=old_label, new_label=new_label,
125
raise RevisionValueError
127
if tree2 is not None:
128
ret = show_diff_trees(tree1, tree2, tmpfp,
129
specific_files=file_list,
130
external_diff_options=diff_options,
131
old_label=old_label, new_label=new_label)
133
ret = diff_helper(tree1, file_list, diff_options,
134
old_label=old_label, new_label=new_label,
144
def get_push_location(location):
145
""" Get the stored push location of a branch.
147
:param location: the path to the branch
149
:return: the stored location
151
from bzrlib.branch import Branch
153
branch = Branch.open_containing(location)[0]
155
return branch.get_push_location()
158
""" Get info about branch, working tree, and repository
160
:param location: the location of the branch/working tree/repository
162
:return: the information in dictionary format
164
The following informations are delivered (if available):
165
ret['location']['lightcoroot']: Light checkout root
166
ret['location']['sharedrepo']: Shared repository
167
ret['location']['repobranch']: Repository branch
168
ret['location']['cobranch']: Checkout of branch
169
ret['location']['repoco']: Repository checkout
170
ret['location']['coroot']: Checkout root
171
ret['location']['branchroot']: Branch root
172
ret['related']['parentbranch']: Parent branch
173
ret['related']['publishbranch']: Publish to branch
174
ret['format']['control']: Control format
175
ret['format']['workingtree']: Working tree format
176
ret['format']['branch']: Branch format
177
ret['format']['repository']: Repository format
178
ret['locking']['workingtree']: Working tree lock status
179
ret['locking']['branch']: Branch lock status
180
ret['locking']['repository']: Repository lock status
181
ret['missing']['branch']: Missing revisions in branch
182
ret['missing']['workingtree']: Missing revisions in working tree
183
ret['wtstats']['unchanged']: Unchanged files
184
ret['wtstats']['modified']: Modified files
185
ret['wtstats']['added']: Added files
186
ret['wtstats']['removed']: Removed files
187
ret['wtstats']['renamed']: Renamed files
188
ret['wtstats']['unknown']: Unknown files
189
ret['wtstats']['ignored']: Ingnored files
190
ret['wtstats']['subdirs']: Versioned subdirectories
191
ret['brstats']['revno']: Revisions in branch
192
ret['brstats']['commiters']: Number of commiters
193
ret['brstats']['age']: Age of branch in days
194
ret['brstats']['firstrev']: Time of first revision
195
ret['brstats']['lastrev']: Time of last revision
196
ret['repstats']['revisions']: Revisions in repository
197
ret['repstats']['size']: Size of repository in bytes
199
import bzrlib.bzrdir as bzrdir
204
a_bzrdir = bzrdir.BzrDir.open_containing(location)[0]
207
working = a_bzrdir.open_workingtree()
210
branch = working.branch
211
repository = branch.repository
212
control = working.bzrdir
214
ret['location'] = info_helper.get_location_info(repository, branch, working)
215
ret['related'] = info_helper.get_related_info(branch)
216
ret['format'] = info_helper.get_format_info(control, repository, branch, working)
217
ret['locking'] = info_helper.get_locking_info(repository, branch, working)
219
ret['missing']['branch'] = info_helper.get_missing_revisions_branch(branch)
220
ret['missing']['workingtree'] = info_helper.get_missing_revisions_working(working)
221
ret['wtstats'] = info_helper.get_working_stats(working)
222
ret['brstats'] = info_helper.get_branch_stats(branch)
223
ret['repstats'] = info_helper.get_repository_stats(repository)
228
except (errors.NoWorkingTree, errors.NotLocalUrl):
232
branch = a_bzrdir.open_branch()
235
ret['location'] = info_helper.get_location_info(repository, branch)
236
ret['related'] = info_helper.get_related_info(branch)
237
ret['format'] = info_helper.get_format_info(control, repository, branch)
238
ret['locking'] = info_helper.get_locking_info(repository, branch)
239
ret['missing']['branch'] = info_helper.get_missing_revisions_branch(branch)
240
ret['brstats'] = info_helper.get_branch_stats(branch)
241
ret['repstats'] = info_helper.get_repository_stats(repository)
246
except errors.NotBranchError:
250
repository = a_bzrdir.open_repository()
251
repository.lock_read()
253
ret['location'] = info_helper.get_location_info(repository)
254
ret['format'] = info_helper.get_format_info(control, repository)
255
ret['locking'] = info_helper.get_locking_info(repository)
256
ret['repstats'] = info_helper.get_repository_stats(repository)
261
except errors.NoRepositoryPresent:
264
def is_branch(location):
265
""" Check if the location is a branch.
267
:param location: the location you want to check
269
:return: True or False respectively
272
branch = Branch.open_containing(location)[0]
273
except errors.NotBranchError:
279
def is_checkout(location):
280
""" Check if the location is a checkout.
282
:param location: the location you want to check
284
:return: True or False respectively
286
branch = Branch.open_containing(location)[0]
289
working = WorkingTree.open_containing(location)[0]
293
working_path = working.bzrdir.root_transport.base
294
branch_path = branch.bzrdir.root_transport.base
296
if working_path != branch_path:
297
# lightweight checkout
299
elif branch.get_bound_location():
305
def log(location, timezone='original', verbose=False, show_ids=False,
306
forward=False, revision=None, log_format=None, message=None,
307
long=False, short=False, line=False):
308
""" Print log into a temporary file.
310
:param location: location of local/remote branch or file
312
:param timzone: requested timezone
314
:param verbose: verbose output
318
:param forward: if True, start from the earliest entry
320
:param revision: revision range as a list ([from, to])
322
:param log_format: line, short, long
324
:param message: show revisions whose message matches this regexp
326
:param long: long log format
328
:param short: short log format
330
:param line: line log format
332
:return: full path to the temporary file containing the log (the frontend has to remove it!)
334
from tempfile import mkstemp
336
from bzrlib import bzrdir
337
from bzrlib.builtins import get_log_format
338
from bzrlib.log import log_formatter, show_log
339
from bzrlib.revisionspec import RevisionSpec_int
341
assert message is None or isinstance(message, basestring), \
342
"invalid message argument %r" % message
343
direction = (forward and 'forward') or 'reverse'
348
# find the file id to log:
349
dir, fp = bzrdir.BzrDir.open_containing(location)
350
b = dir.open_branch()
354
inv = dir.open_workingtree().inventory
355
except (errors.NotBranchError, errors.NotLocalUrl):
356
# either no tree, or is remote.
357
inv = b.basis_tree().inventory
358
file_id = inv.path2id(fp)
360
if revision is not None:
361
if len(revision) >= 1:
362
revision[0] = RevisionSpec_int(revision[0])
363
if len(revision) == 2:
364
revision[1] = RevisionSpec_int(revision[1])
369
elif len(revision) == 1:
370
rev1 = rev2 = revision[0].in_history(b).revno
371
elif len(revision) == 2:
372
if revision[0].spec is None:
373
# missing begin-range means first revision
376
rev1 = revision[0].in_history(b).revno
378
if revision[1].spec is None:
379
# missing end-range means last known revision
382
rev2 = revision[1].in_history(b).revno
384
raise RevisionValueError
386
# By this point, the revision numbers are converted to the +ve
387
# form if they were supplied in the -ve form, so we can do
388
# this comparison in relative safety
390
(rev2, rev1) = (rev1, rev2)
392
if (log_format == None):
393
default = b.get_config().log_format()
394
log_format = get_log_format(long=long, short=short, line=line,
397
tmpfile = mkstemp(prefix='olive_')
398
tmpfp = open(tmpfile[1], 'w')
400
lf = log_formatter(log_format,
403
show_timezone=timezone)
417
def nick(branch, nickname=None):
418
""" Get or set nickname.
420
:param branch: path to the branch
422
:param nickname: if specified, the nickname will be set
427
branch = Branch.open_containing(branch)[0]
428
except errors.NotBranchError:
431
if nickname is not None:
432
branch.nick = nickname
437
""" Get current revision number for specified branch
439
:param branch: path to the branch
441
:return: revision number
444
revno = Branch.open_containing(branch)[0].revno()
445
except errors.NotBranchError:
451
""" Get version information from bzr
453
:return: bzrlib version
455
return bzrlib.__version__
457
def whoami(branch=None, email=False):
458
""" Get user's data (name and email address)
460
:param branch: if specified, the user's data will be looked up in the branch's config
462
:param email: if True, only the email address will be returned
464
:return: user info (only email address if email is True)
466
from bzrlib.workingtree import WorkingTree
468
if branch is not None:
470
b = WorkingTree.open_containing(u'.')[0].branch
471
config = bzrlib.config.BranchConfig(b)
472
except NotBranchError:
473
config = bzrlib.config.GlobalConfig()
475
config = bzrlib.config.GlobalConfig()
478
return config.user_email()
480
return config.username()