1
# Copyright (C) 2006 by Szilveszter Farkas (Phanatic)
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
24
from errors import (NotBranchError)
26
def diff(revision=None, file_list=None, diff_options=None, prefix=None):
27
""" Save the diff into a temporary file.
29
:param revision: a list of revisions (one or two elements)
31
:param file_list: list of files you want to diff
33
:param diff_options: external diff options
35
:param prefix: 0 - p0, 1 - p1, or specify prefixes in the form of old/:new/
37
:return: path to the temporary file which contains the diff output
39
from tempfile import mkstemp
41
from bzrlib.builtins import internal_tree_files
42
from bzrlib.diff import show_diff_trees
43
from bzrlib.workingtree import WorkingTree
45
from info_helper import diff_helper
47
if (prefix is None) or (prefix == '0'):
56
raise errors.BzrError("--diff-prefix expects two values separated by a colon")
57
old_label, new_label = prefix.split(":")
60
tree1, file_list = internal_tree_files(file_list)
64
except errors.FileInWrongBranch:
65
if len(file_list) != 2:
66
raise errors.BzrCommandError("Files are in different branches")
68
tree1, file1 = WorkingTree.open_containing(file_list[0])
69
tree2, file2 = WorkingTree.open_containing(file_list[1])
71
if file1 != "" or file2 != "":
72
# FIXME diff those two files. rbc 20051123
73
raise errors.BzrCommandError("Files are in different branches")
77
tmpfile = mkstemp(prefix='olive_')
78
tmpfp = open(tmpfile[1], 'w')
80
if revision is not None:
82
raise errors.BzrCommandError("Can't specify -r with two branches")
84
if (len(revision) == 1) or (revision[1].spec is None):
85
ret = diff_helper(tree1, file_list, diff_options,
87
old_label=old_label, new_label=new_label,
89
elif len(revision) == 2:
90
ret = diff_helper(tree1, file_list, diff_options,
91
revision[0], revision[1],
92
old_label=old_label, new_label=new_label,
95
raise errors.BzrCommandError('bzr diff --revision takes exactly one or two revision identifiers')
98
ret = show_diff_trees(tree1, tree2, tmpfp,
99
specific_files=file_list,
100
external_diff_options=diff_options,
101
old_label=old_label, new_label=new_label)
103
ret = diff_helper(tree1, file_list, diff_options,
104
old_label=old_label, new_label=new_label,
115
""" Get info about branch, working tree, and repository
117
:param location: the location of the branch/working tree/repository
119
:return: the information in dictionary format
121
The following informations are delivered (if available):
122
ret['location']['lightcoroot']: Light checkout root
123
ret['location']['sharedrepo']: Shared repository
124
ret['location']['repobranch']: Repository branch
125
ret['location']['cobranch']: Checkout of branch
126
ret['location']['repoco']: Repository checkout
127
ret['location']['coroot']: Checkout root
128
ret['location']['branchroot']: Branch root
129
ret['related']['parentbranch']: Parent branch
130
ret['related']['publishbranch']: Publish to branch
131
ret['format']['control']: Control format
132
ret['format']['workingtree']: Working tree format
133
ret['format']['branch']: Branch format
134
ret['format']['repository']: Repository format
135
ret['locking']['workingtree']: Working tree lock status
136
ret['locking']['branch']: Branch lock status
137
ret['locking']['repository']: Repository lock status
138
ret['mrevbranch']['missing']: Missing revisions in branch
139
ret['mrevworking']['missing']: Missing revisions in working tree
140
ret['wtstats']['unchanged']: Unchanged files
141
ret['wtstats']['modified']: Modified files
142
ret['wtstats']['added']: Added files
143
ret['wtstats']['removed']: Removed files
144
ret['wtstats']['renamed']: Renamed files
145
ret['wtstats']['unknown']: Unknown files
146
ret['wtstats']['ignored']: Ingnored files
147
ret['wtstats']['subdirs']: Versioned subdirectories
148
ret['brstats']['revno']: Revisions in branch
149
ret['brstats']['commiters']: Number of commiters
150
ret['brstats']['age']: Age of branch in days
151
ret['brstats']['firstrev']: Time of first revision
152
ret['brstats']['lastrev']: Time of last revision
153
ret['repstats']['revisions']: Revisions in repository
154
ret['repstats']['size']: Size of repository in bytes
156
import bzrlib.bzrdir as bzrdir
161
a_bzrdir = bzrdir.BzrDir.open_containing(location)[0]
163
working = a_bzrdir.open_workingtree()
166
branch = working.branch
167
repository = branch.repository
168
control = working.bzrdir
170
ret['location'] = info_helper.get_location_info(repository, branch, working)
171
ret['related'] = info_helper.get_related_info(branch)
172
ret['format'] = info_helper.get_format_info(control, repository, branch, working)
173
ret['locking'] = info_helper.get_locking_info(repository, branch, working)
174
ret['mrevbranch'] = info_helper.get_missing_revisions_branch(branch)
175
ret['mrevworking'] = info_helper.get_missing_revisions_working(working)
176
ret['wtstats'] = info_helper.get_working_stats(working)
177
ret['brstats'] = info_helper.get_branch_stats(branch)
178
ret['repstats'] = info_helper.get_repository_stats(repository)
183
except (errors.NoWorkingTree, errors.NotLocalUrl):
187
branch = a_bzrdir.open_branch()
190
ret['location'] = info_helper.get_location_info(repository, branch)
191
ret['related'] = info_helper.get_related_info(branch)
192
ret['format'] = info_helper.get_format_info(control, repository, branch)
193
ret['locking'] = info_helper.get_locking_info(repository, branch)
194
ret['mrevbranch'] = info_helper.get_missing_revisions_branch(branch)
195
ret['brstats'] = info_helper.get_branch_stats(branch)
196
ret['repstats'] = info_helper.get_repository_stats(repository)
201
except errors.NotBranchError:
205
repository = a_bzrdir.open_repository()
206
repository.lock_read()
208
ret['location'] = info_helper.get_location_info(repository)
209
ret['format'] = info_helper.get_format_info(control, repository)
210
ret['locking'] = info_helper.get_locking_info(repository)
211
ret['repstats'] = info_helper.get_repository_stats(repository)
216
except errors.NoRepositoryPresent:
219
def nick(branch, nickname=None):
220
""" Get or set nickname.
222
:param branch: path to the branch
224
:param nickname: if specified, the nickname will be set
229
branch = Branch.open_containing(branch)[0]
230
except errors.NotBranchError:
233
if nickname is not None:
234
branch.nick = nickname
239
""" Get current revision number for specified branch
241
:param branch: path to the branch
243
:return: revision number
246
revno = Branch.open_containing(branch)[0].revno()
247
except errors.NotBranchError:
253
""" Get version information from bzr
255
:return: bzrlib version
257
return bzrlib.__version__
259
def whoami(branch=None, email=False):
260
""" Get user's data (name and email address)
262
:param branch: if specified, the user's data will be looked up in the branch's config
264
:param email: if True, only the email address will be returned
266
:return: user info (only email address if email is True)
268
from bzrlib.workingtree import WorkingTree
270
if branch is not None:
272
b = WorkingTree.open_containing(u'.')[0].branch
273
config = bzrlib.config.BranchConfig(b)
274
except NotBranchError:
275
config = bzrlib.config.GlobalConfig()
277
config = bzrlib.config.GlobalConfig()
280
return config.user_email()
282
return config.username()