/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.152.61 by Vincent Ladeuil
Fix bug #312686 and add an 'upload_auto_quiet' config variable.
1
# Copyright (C) 2008, 2009 Canonical Ltd
0.152.1 by Vincent Ladeuil
Empty shell
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
0.152.6 by Vincent Ladeuil
Really use the transports and test against all targeted protocols.
17
"""Upload a working tree, incrementally.
18
0.152.63 by Vincent Ladeuil
Make the doc more easily discoverable.
19
Quickstart
20
----------
21
22
To get started, it's as simple as running::
23
24
    bzr upload sftp://user@host/location/on/webserver
25
26
This will initially upload the whole working tree, and leave a file on the
27
remote location indicating the last revision that was uploaded
28
(.bzr-upload.revid), in order to avoid uploading unnecessary information the
29
next time.
30
31
If you would like to upload a specific revision, you just do:
32
33
    bzr upload -r X  sftp://user@host/location/on/webserver
34
35
bzr-upload, just as bzr does, will remember the location where you upload the 
36
first time, so you don't need to specify it every time.
37
38
If you need to re-upload the whole working tree for some reason, you can:
39
40
    bzr upload --full sftp://user@host/location/on/webserver
41
0.159.4 by Gary van der Merwe
Check that the revision we are uploading is a descendent from the revision that was uploaded.
42
This command only works on the revision beening uploaded is a decendent of the
43
revision that was previously uploaded, and that they are hence from branches
44
that have not diverged. Branches are considered diverged if the destination
45
branch's most recent commit is one that has not been merged (directly or
46
indirectly) by the source branch.
47
48
If branches have diverged, you can use 'bzr upload --overwrite' to replace
49
the other branch completely, discarding its unmerged changes.
50
0.152.63 by Vincent Ladeuil
Make the doc more easily discoverable.
51
52
Automatically Uploading
53
-----------------------
54
55
bzr-upload comes with a hook that can be used to trigger an upload whenever
56
the tip of the branch changes, including on commit, push, uncommit etc. This
57
would allow you to keep the code on the target up to date automatically.
58
59
The easiest way to enable this is to run upload with the --auto option.
60
61
     bzr upload --auto
62
63
will enable the hook for this branch. If you were to do a commit in this branch
64
now you would see it trigger the upload automatically.
65
66
If you wish to disable this for a branch again then you can use the --no-auto
67
option.
68
69
     bzr upload --no-auto
70
71
will disable the feature for that branch.
72
73
Since the auto hook is triggered automatically, you can't use the --quiet
74
option available for the upload command. Instead, you can set the
75
'upload_auto_quiet' configuration variable to True or False in either
76
bazaar.conf, locations.conf or branch.conf.
77
78
79
Storing the '.bzr-upload.revid' file
80
------------------------------------
81
0.152.17 by Vincent Ladeuil
Handle deletes (trivial implementation).
82
The only bzr-related info uploaded with the working tree is the corresponding
0.152.6 by Vincent Ladeuil
Really use the transports and test against all targeted protocols.
83
revision id. The uploaded working tree is not linked to any other bzr data.
84
0.152.63 by Vincent Ladeuil
Make the doc more easily discoverable.
85
If the layout of your remote server is such that you can't write in the
86
root directory but only in the directories inside that root, you will need
87
to use the 'upload_revid_location' configuration variable to specify the
88
relative path to be used. That configuration variable can be specified in
89
locations.conf or branch.conf.
90
91
For example, given the following layout:
92
93
  Project/
94
    private/
95
    public/
96
97
you may have write access in 'private' and 'public' but in 'Project'
98
itself. In that case, you can add the following in your locations.conf or
99
branch.conf file:
100
101
  upload_revid_location = private/.bzr-upload.revid
102
103
104
Upload from Remote Location
105
---------------------------
106
107
It is possible to upload to a remote location from another remote location by
108
specifying it with the --directory option:
109
110
    bzr upload ftp://public.example.com --directory sftp://private.example.com 
111
112
This, together with --auto, can be used to upload when you push to your
113
central branch, rather than when you commit to your local branch.
114
115
Note that you will consume more bandwith this way than uploading from a local
116
branch.
117
0.160.5 by Martin Albisetti
Add docs
118
Ignoring certain files
119
-----------------------
120
121
If you want to version a file, but not upload it, you can create a file called
122
.bzrignore-upload, which works in the same way as the regular .bzrignore file,
123
but only applies to bzr-upload.
124
125
0.152.63 by Vincent Ladeuil
Make the doc more easily discoverable.
126
Collaborating
127
-------------
128
129
While we don't have any platform setup, you can branch from trunk:
130
131
    bzr branch lp:bzr-upload
132
0.160.5 by Martin Albisetti
Add docs
133
And change anything you'd like, and file a merge proposal on Launchpad.
0.152.63 by Vincent Ladeuil
Make the doc more easily discoverable.
134
135
136
Known Issues
137
------------
138
139
 * Symlinks are not supported
140
141
0.152.6 by Vincent Ladeuil
Really use the transports and test against all targeted protocols.
142
"""
0.152.1 by Vincent Ladeuil
Empty shell
143
0.152.17 by Vincent Ladeuil
Handle deletes (trivial implementation).
144
# TODO: the chmod bits *can* be supported via the upload protocols
145
# (i.e. poorly), but since the web developers use these protocols to upload
146
# manually, it is expected that the associated web server is coherent with
147
# their presence/absence. In other words, if a web hosting provider requires
148
# chmod bits but don't provide an ftp server that support them, well, better
149
# find another provider ;-)
150
0.152.44 by Vincent Ladeuil
More robust full upload (at least regarding files changed to dirs and vice-versa).
151
# TODO: The message emitted in verbose mode displays local paths. That may be
152
# scary for the user when we say 'Deleting <path>' and are referring to
153
# remote files...
0.152.19 by Vincent Ladeuil
Handle kind_change. Trivial implementation, blocked by bug #205636.
154
0.152.3 by v.ladeuil+lp at free
Make the tests fail not error out.
155
from bzrlib import (
0.155.2 by James Westby
Add a post_change_branch_tip hook to upload.
156
    branch,
0.152.3 by v.ladeuil+lp at free
Make the tests fail not error out.
157
    commands,
0.152.18 by Vincent Ladeuil
Handle deletions. Robust implementation.
158
    lazy_import,
0.152.3 by v.ladeuil+lp at free
Make the tests fail not error out.
159
    option,
160
    )
0.152.18 by Vincent Ladeuil
Handle deletions. Robust implementation.
161
lazy_import.lazy_import(globals(), """
0.152.44 by Vincent Ladeuil
More robust full upload (at least regarding files changed to dirs and vice-versa).
162
import stat
163
0.152.18 by Vincent Ladeuil
Handle deletions. Robust implementation.
164
from bzrlib import (
0.158.1 by Gary van der Merwe
Don't require a working tree.
165
    bzrdir,
0.152.18 by Vincent Ladeuil
Handle deletions. Robust implementation.
166
    errors,
0.160.3 by Martin Albisetti
Start implementing
167
    ignores,
0.152.18 by Vincent Ladeuil
Handle deletions. Robust implementation.
168
    revisionspec,
169
    transport,
0.152.44 by Vincent Ladeuil
More robust full upload (at least regarding files changed to dirs and vice-versa).
170
    osutils,
0.152.40 by Martin Albisetti
We need to import urlutils if we are going to use it
171
    urlutils,
0.152.27 by Martin Albisetti
* Added error message if the working tree has uncommited changes
172
    workingtree,
0.159.2 by Gary van der Merwe
If there has not been an upload, treat the uploaded revid as revision.NULL_REVISION. This enables this request to be cached.
173
    revision
0.152.18 by Vincent Ladeuil
Handle deletions. Robust implementation.
174
    )
175
""")
0.152.16 by Vincent Ladeuil
Handle renames. Robust implementation.
176
0.152.54 by Vincent Ladeuil
Let's start dev for a 1.0.
177
version_info = (1, 0, 0, 'dev', 0)
0.152.23 by Martin Albisetti
Added version_info and plugin_name
178
plugin_name = 'upload'
179
0.155.1 by James Westby
Switch most of the logic to a class outside of the command class.
180
0.155.3 by James Westby
Add some tests for the hook, rename the option to "upload_auto"
181
def _get_branch_option(branch, option):
182
    return branch.get_config().get_user_option(option)
183
0.152.64 by Vincent Ladeuil
Fix compatibility with bzr.
184
# FIXME: Get rid of that as soon as we depend on a bzr API that includes
185
# get_user_option_as_bool
186
def _get_branch_bool_option(branch, option):
187
    conf = branch.get_config()
188
    if hasattr(conf, 'get_user_option_as_bool'):
189
        value = conf.get_user_option_as_bool(option)
190
    else:
191
        value = conf.get_user_option(option)
192
        if value is not None:
193
            if value.lower().strip() == 'true':
194
                value = True
195
            else:
196
                value = False
197
    return value
0.152.61 by Vincent Ladeuil
Fix bug #312686 and add an 'upload_auto_quiet' config variable.
198
0.155.3 by James Westby
Add some tests for the hook, rename the option to "upload_auto"
199
def _set_branch_option(branch, option, value):
200
    branch.get_config().set_user_option(option, value)
201
0.152.61 by Vincent Ladeuil
Fix bug #312686 and add an 'upload_auto_quiet' config variable.
202
0.155.3 by James Westby
Add some tests for the hook, rename the option to "upload_auto"
203
def get_upload_location(branch):
204
    return _get_branch_option(branch, 'upload_location')
205
0.152.61 by Vincent Ladeuil
Fix bug #312686 and add an 'upload_auto_quiet' config variable.
206
0.155.1 by James Westby
Switch most of the logic to a class outside of the command class.
207
def set_upload_location(branch, location):
0.155.3 by James Westby
Add some tests for the hook, rename the option to "upload_auto"
208
    _set_branch_option(branch, 'upload_location', location)
209
0.152.61 by Vincent Ladeuil
Fix bug #312686 and add an 'upload_auto_quiet' config variable.
210
0.152.62 by Vincent Ladeuil
Fix bug #423331 by adding a way to configure the path used to
211
# FIXME: Add more tests around invalid paths used here or relative paths that
212
# doesn't exist on remote (if only to get proper error messages)
213
def get_upload_revid_location(branch):
214
    loc =  _get_branch_option(branch, 'upload_revid_location')
215
    if loc is None:
216
        loc = '.bzr-upload.revid'
217
    return loc
218
219
220
def set_upload_revid_location(branch, location):
221
    _set_branch_option(branch, 'upload_revid_location', location)
222
223
0.155.3 by James Westby
Add some tests for the hook, rename the option to "upload_auto"
224
def get_upload_auto(branch):
0.152.64 by Vincent Ladeuil
Fix compatibility with bzr.
225
    auto = _get_branch_bool_option(branch, 'upload_auto')
0.152.61 by Vincent Ladeuil
Fix bug #312686 and add an 'upload_auto_quiet' config variable.
226
    if auto is None:
227
        auto = False # Default to False if not specified
228
    return auto
229
0.155.3 by James Westby
Add some tests for the hook, rename the option to "upload_auto"
230
231
def set_upload_auto(branch, auto):
0.152.61 by Vincent Ladeuil
Fix bug #312686 and add an 'upload_auto_quiet' config variable.
232
    # FIXME: What's the point in allowing a boolean here instead of requiring
233
    # the callers to use strings instead ?
0.155.3 by James Westby
Add some tests for the hook, rename the option to "upload_auto"
234
    if auto:
235
        auto_str = "True"
236
    else:
237
        auto_str = "False"
238
    _set_branch_option(branch, 'upload_auto', auto_str)
0.155.1 by James Westby
Switch most of the logic to a class outside of the command class.
239
240
0.152.61 by Vincent Ladeuil
Fix bug #312686 and add an 'upload_auto_quiet' config variable.
241
def get_upload_auto_quiet(branch):
0.152.64 by Vincent Ladeuil
Fix compatibility with bzr.
242
    quiet = _get_branch_bool_option(branch, 'upload_auto_quiet')
0.152.61 by Vincent Ladeuil
Fix bug #312686 and add an 'upload_auto_quiet' config variable.
243
    if quiet is None:
244
        quiet = False # Default to False if not specified
245
    return quiet
246
247
248
def set_upload_auto_quiet(branch, quiet):
249
    _set_branch_option(branch, 'upload_auto_quiet', quiet)
250
251
0.155.1 by James Westby
Switch most of the logic to a class outside of the command class.
252
class BzrUploader(object):
253
254
    def __init__(self, branch, to_transport, outf, tree, rev_id,
0.152.62 by Vincent Ladeuil
Fix bug #423331 by adding a way to configure the path used to
255
                 quiet=False):
0.155.1 by James Westby
Switch most of the logic to a class outside of the command class.
256
        self.branch = branch
257
        self.to_transport = to_transport
258
        self.outf = outf
259
        self.tree = tree
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
260
        self.rev_id = rev_id
0.155.1 by James Westby
Switch most of the logic to a class outside of the command class.
261
        self.quiet = quiet
0.152.18 by Vincent Ladeuil
Handle deletions. Robust implementation.
262
        self._pending_deletions = []
0.155.1 by James Westby
Switch most of the logic to a class outside of the command class.
263
        self._pending_renames = []
0.159.1 by Gary van der Merwe
Cache the uploaded_revid, so that we can query it more that once, with out having to worry about extra network calls.
264
        self._uploaded_revid = None
0.152.12 by Vincent Ladeuil
Implement 'upload_location' in config files.
265
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
266
    def set_uploaded_revid(self, rev_id):
0.152.10 by Vincent Ladeuil
Fix incremental upload cheat.
267
        # XXX: Add tests for concurrent updates, etc.
0.152.62 by Vincent Ladeuil
Fix bug #423331 by adding a way to configure the path used to
268
        revid_path = get_upload_revid_location(self.branch)
269
        self.to_transport.put_bytes(revid_path, rev_id)
0.159.1 by Gary van der Merwe
Cache the uploaded_revid, so that we can query it more that once, with out having to worry about extra network calls.
270
        self._uploaded_revid = rev_id
0.152.10 by Vincent Ladeuil
Fix incremental upload cheat.
271
272
    def get_uploaded_revid(self):
0.159.1 by Gary van der Merwe
Cache the uploaded_revid, so that we can query it more that once, with out having to worry about extra network calls.
273
        if self._uploaded_revid is None:
274
            revid_path = get_upload_revid_location(self.branch)
0.159.2 by Gary van der Merwe
If there has not been an upload, treat the uploaded revid as revision.NULL_REVISION. This enables this request to be cached.
275
            try:
276
                self._uploaded_revid = self.to_transport.get_bytes(revid_path)
277
            except errors.NoSuchFile:
278
                # We have not upload to here.
279
                self._uploaded_revid = revision.NULL_REVISION
0.159.1 by Gary van der Merwe
Cache the uploaded_revid, so that we can query it more that once, with out having to worry about extra network calls.
280
        return self._uploaded_revid
0.152.7 by Vincent Ladeuil
Slight refactoring.
281
0.160.3 by Martin Albisetti
Start implementing
282
    def get_ignored(self):
283
        """Get upload-specific ignored files from the current branch"""
0.160.8 by Martin Albisetti
Fix all the tests I broke :)
284
        try:
285
            ignore_file = self.tree.get_file_by_path('.bzrignore-upload')
286
            return ignores.parse_ignore_file(ignore_file)
287
        except errors.NoSuchId:
288
            return []
0.160.3 by Martin Albisetti
Start implementing
289
0.152.46 by Vincent Ladeuil
Handle x mode bit for files and provides default mode bits for
290
    def upload_file(self, relpath, id, mode=None):
0.160.4 by Martin Albisetti
Test passes!
291
        ignored_files = self.get_ignored()
292
        if relpath not in ignored_files:
293
            if mode is None:
294
                if self.tree.is_executable(id):
295
                    mode = 0775
296
                else:
297
                    mode = 0664
298
            if not self.quiet:
299
                self.outf.write('Uploading %s\n' % relpath)
300
            self.to_transport.put_bytes(relpath, self.tree.get_file_text(id), mode)
301
        else:
302
            if not self.quiet:
303
                self.outf.write('Ignoring %s\n' % relpath)
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
304
0.152.46 by Vincent Ladeuil
Handle x mode bit for files and provides default mode bits for
305
    def upload_file_robustly(self, relpath, id, mode=None):
0.152.44 by Vincent Ladeuil
More robust full upload (at least regarding files changed to dirs and vice-versa).
306
        """Upload a file, clearing the way on the remote side.
307
308
        When doing a full upload, it may happen that a directory exists where
309
        we want to put our file.
310
        """
311
        try:
312
            st = self.to_transport.stat(relpath)
313
            if stat.S_ISDIR(st.st_mode):
314
                # A simple rmdir may not be enough
315
                if not self.quiet:
316
                    self.outf.write('Clearing %s/%s\n' % (
317
                            self.to_transport.external_url(), relpath))
318
                self.to_transport.delete_tree(relpath)
319
        except errors.PathError:
320
            pass
0.152.46 by Vincent Ladeuil
Handle x mode bit for files and provides default mode bits for
321
        self.upload_file(relpath, id, mode)
322
323
    def make_remote_dir(self, relpath, mode=None):
0.160.12 by Martin Albisetti
Actually ignore the dirs. Test pass!
324
        ignored_files = self.get_ignored()
325
        if relpath not in ignored_files:
326
            if mode is None:
327
                mode = 0775
328
            self.to_transport.mkdir(relpath, mode)
329
        else:
330
            if not self.quiet:
331
                self.outf.write('Ignoring %s\n' % relpath)
0.152.46 by Vincent Ladeuil
Handle x mode bit for files and provides default mode bits for
332
333
    def make_remote_dir_robustly(self, relpath, mode=None):
0.152.44 by Vincent Ladeuil
More robust full upload (at least regarding files changed to dirs and vice-versa).
334
        """Create a remote directory, clearing the way on the remote side.
335
336
        When doing a full upload, it may happen that a file exists where we
337
        want to create our directory.
338
        """
339
        try:
340
            st = self.to_transport.stat(relpath)
341
            if not stat.S_ISDIR(st.st_mode):
342
                if not self.quiet:
343
                    self.outf.write('Deleting %s/%s\n' % (
344
                            self.to_transport.external_url(), relpath))
345
                self.to_transport.delete(relpath)
0.152.47 by Vincent Ladeuil
Don't fail a full upload on an already existing dir.
346
            else:
347
                # Ok the remote dir already exists, nothing to do
348
                return
0.152.44 by Vincent Ladeuil
More robust full upload (at least regarding files changed to dirs and vice-versa).
349
        except errors.PathError:
350
            pass
0.152.46 by Vincent Ladeuil
Handle x mode bit for files and provides default mode bits for
351
        self.make_remote_dir(relpath, mode)
0.152.44 by Vincent Ladeuil
More robust full upload (at least regarding files changed to dirs and vice-versa).
352
0.152.18 by Vincent Ladeuil
Handle deletions. Robust implementation.
353
    def delete_remote_file(self, relpath):
0.152.34 by Martin Albisetti
* Change the default behaviour to be more verbose
354
        if not self.quiet:
0.152.30 by Vincent Ladeuil
Comply to verbose.
355
            self.outf.write('Deleting %s\n' % relpath)
0.152.17 by Vincent Ladeuil
Handle deletes (trivial implementation).
356
        self.to_transport.delete(relpath)
357
0.152.18 by Vincent Ladeuil
Handle deletions. Robust implementation.
358
    def delete_remote_dir(self, relpath):
0.152.34 by Martin Albisetti
* Change the default behaviour to be more verbose
359
        if not self.quiet:
0.152.30 by Vincent Ladeuil
Comply to verbose.
360
            self.outf.write('Deleting %s\n' % relpath)
0.152.18 by Vincent Ladeuil
Handle deletions. Robust implementation.
361
        self.to_transport.rmdir(relpath)
362
363
    def delete_remote_dir_maybe(self, relpath):
364
        """Try to delete relpath, keeping failures to retry later."""
365
        try:
366
            self.to_transport.rmdir(relpath)
367
        # any kind of PathError would be OK, though we normally expect
368
        # DirectoryNotEmpty
369
        except errors.PathError:
370
            self._pending_deletions.append(relpath)
371
372
    def finish_deletions(self):
373
        if self._pending_deletions:
374
            # Process the previously failed deletions in reverse order to
375
            # delete children before parents
376
            for relpath in reversed(self._pending_deletions):
377
                self.to_transport.rmdir(relpath)
378
            # The following shouldn't be needed since we use it once per
379
            # upload, but better safe than sorry ;-)
380
            self._pending_deletions = []
381
0.152.16 by Vincent Ladeuil
Handle renames. Robust implementation.
382
    def rename_remote(self, old_relpath, new_relpath):
383
        """Rename a remote file or directory taking care of collisions.
384
385
        To avoid collisions during bulk renames, each renamed target is
386
        temporarily assigned a unique name. When all renames have been done,
387
        each target get its proper name.
388
        """
389
        # We generate a sufficiently random name to *assume* that
390
        # no collisions will occur and don't worry about it (nor
391
        # handle it).
392
        import os
393
        import random
394
        import time
395
396
        stamp = '.tmp.%.9f.%d.%d' % (time.time(),
397
                                     os.getpid(),
398
                                     random.randint(0,0x7FFFFFFF))
0.152.34 by Martin Albisetti
* Change the default behaviour to be more verbose
399
        if not self.quiet:
0.152.30 by Vincent Ladeuil
Comply to verbose.
400
            self.outf.write('Renaming %s to %s\n' % (old_relpath, new_relpath))
0.152.16 by Vincent Ladeuil
Handle renames. Robust implementation.
401
        self.to_transport.rename(old_relpath, stamp)
402
        self._pending_renames.append((stamp, new_relpath))
403
404
    def finish_renames(self):
405
        for (stamp, new_path) in self._pending_renames:
406
            self.to_transport.rename(stamp, new_path)
407
        # The following shouldn't be needed since we use it once per upload,
408
        # but better safe than sorry ;-)
409
        self._pending_renames = []
410
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
411
    def upload_full_tree(self):
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
412
        self.to_transport.ensure_base() # XXX: Handle errors (add
413
                                        # --create-prefix option ?)
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
414
        self.tree.lock_read()
415
        try:
0.152.44 by Vincent Ladeuil
More robust full upload (at least regarding files changed to dirs and vice-versa).
416
            for relpath, ie in self.tree.inventory.iter_entries():
0.160.4 by Martin Albisetti
Test passes!
417
                if relpath in ('', '.bzrignore', '.bzrignore-upload'):
0.152.10 by Vincent Ladeuil
Fix incremental upload cheat.
418
                    # skip root ('')
419
                    # .bzrignore has no meaning outside of a working tree
420
                    # so do not upload it
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
421
                    continue
0.152.8 by Vincent Ladeuil
Handle uploading directories.
422
                if ie.kind == 'file':
0.152.44 by Vincent Ladeuil
More robust full upload (at least regarding files changed to dirs and vice-versa).
423
                    self.upload_file_robustly(relpath, ie.file_id)
0.152.8 by Vincent Ladeuil
Handle uploading directories.
424
                elif ie.kind == 'directory':
0.152.44 by Vincent Ladeuil
More robust full upload (at least regarding files changed to dirs and vice-versa).
425
                    self.make_remote_dir_robustly(relpath)
0.152.8 by Vincent Ladeuil
Handle uploading directories.
426
                else:
427
                    raise NotImplementedError
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
428
            self.set_uploaded_revid(self.rev_id)
429
        finally:
430
            self.tree.unlock()
431
0.152.10 by Vincent Ladeuil
Fix incremental upload cheat.
432
    def upload_tree(self):
0.152.24 by Martin Albisetti
Changed the way we upload the full tree if its never been uploaded
433
        # If we can't find the revid file on the remote location, upload the
434
        # full tree instead
0.159.2 by Gary van der Merwe
If there has not been an upload, treat the uploaded revid as revision.NULL_REVISION. This enables this request to be cached.
435
        rev_id = self.get_uploaded_revid()
0.160.2 by Martin Albisetti
Clean up whitespace to appease the neurotic in me
436
0.159.2 by Gary van der Merwe
If there has not been an upload, treat the uploaded revid as revision.NULL_REVISION. This enables this request to be cached.
437
        if rev_id == revision.NULL_REVISION:
0.152.34 by Martin Albisetti
* Change the default behaviour to be more verbose
438
            if not self.quiet:
0.152.29 by Vincent Ladeuil
Work around test fixture limitation regarding self.outf (cough). All tests passing again.
439
                self.outf.write('No uploaded revision id found,'
0.152.34 by Martin Albisetti
* Change the default behaviour to be more verbose
440
                                ' switching to full upload\n')
0.152.24 by Martin Albisetti
Changed the way we upload the full tree if its never been uploaded
441
            self.upload_full_tree()
0.152.29 by Vincent Ladeuil
Work around test fixture limitation regarding self.outf (cough). All tests passing again.
442
            # We're done
443
            return
0.152.24 by Martin Albisetti
Changed the way we upload the full tree if its never been uploaded
444
0.152.35 by Martin Albisetti
* Tell the user if the remote location is already up to date
445
        # Check if the revision hasn't already been uploaded
446
        if rev_id == self.rev_id:
447
            if not self.quiet:
448
                self.outf.write('Remote location already up to date\n')
449
0.152.10 by Vincent Ladeuil
Fix incremental upload cheat.
450
        # XXX: errors out if rev_id not in branch history (probably someone
451
        # uploaded from a different branch).
452
        from_tree = self.branch.repository.revision_tree(rev_id)
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
453
        self.to_transport.ensure_base() # XXX: Handle errors (add
454
                                        # --create-prefix option ?)
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
455
        changes = self.tree.changes_from(from_tree)
456
        self.tree.lock_read()
457
        try:
0.152.17 by Vincent Ladeuil
Handle deletes (trivial implementation).
458
            for (path, id, kind) in changes.removed:
459
                if kind is 'file':
0.152.18 by Vincent Ladeuil
Handle deletions. Robust implementation.
460
                    self.delete_remote_file(path)
461
                elif kind is  'directory':
462
                    self.delete_remote_dir_maybe(path)
0.152.17 by Vincent Ladeuil
Handle deletes (trivial implementation).
463
                else:
464
                    raise NotImplementedError
0.152.18 by Vincent Ladeuil
Handle deletions. Robust implementation.
465
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
466
            for (old_path, new_path, id, kind,
467
                 content_change, exec_change) in changes.renamed:
0.152.52 by Vincent Ladeuil
Fix bug #270219 by handling content changes during renames.
468
                if content_change:
469
                    # We update the old_path content because renames and
470
                    # deletions are differed.
471
                    self.upload_file(old_path, id)
0.152.16 by Vincent Ladeuil
Handle renames. Robust implementation.
472
                self.rename_remote(old_path, new_path)
473
            self.finish_renames()
0.152.18 by Vincent Ladeuil
Handle deletions. Robust implementation.
474
            self.finish_deletions()
0.152.16 by Vincent Ladeuil
Handle renames. Robust implementation.
475
0.152.19 by Vincent Ladeuil
Handle kind_change. Trivial implementation, blocked by bug #205636.
476
            for (path, id, old_kind, new_kind) in changes.kind_changed:
477
                if old_kind is 'file':
478
                    self.delete_remote_file(path)
479
                elif old_kind is  'directory':
480
                    self.delete_remote_dir(path)
481
                else:
482
                    raise NotImplementedError
483
484
                if new_kind is 'file':
485
                    self.upload_file(path, id)
486
                elif new_kind is 'directory':
487
                    self.make_remote_dir(path)
488
                else:
489
                    raise NotImplementedError
490
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
491
            for (path, id, kind) in changes.added:
492
                if kind is 'file':
0.152.7 by Vincent Ladeuil
Slight refactoring.
493
                    self.upload_file(path, id)
0.152.8 by Vincent Ladeuil
Handle uploading directories.
494
                elif kind is 'directory':
495
                    self.make_remote_dir(path)
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
496
                else:
497
                    raise NotImplementedError
0.152.18 by Vincent Ladeuil
Handle deletions. Robust implementation.
498
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
499
            # XXX: Add a test for exec_change
500
            for (path, id, kind,
501
                 content_change, exec_change) in changes.modified:
502
                if kind is 'file':
0.152.7 by Vincent Ladeuil
Slight refactoring.
503
                    self.upload_file(path, id)
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
504
                else:
505
                    raise NotImplementedError
0.152.18 by Vincent Ladeuil
Handle deletions. Robust implementation.
506
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
507
            self.set_uploaded_revid(self.rev_id)
508
        finally:
509
            self.tree.unlock()
0.152.4 by v.ladeuil+lp at free
Implement a trivial implementation to make one test pass.
510
0.152.57 by Vincent Ladeuil
Fix minor 2.4 compatibility bug.
511
0.158.19 by Vincent Ladeuil
Bzr has facilities for exceptions, let's use them.
512
class CannotUploadToWorkingTreeError(errors.BzrCommandError):
513
514
    _fmt = 'Cannot upload to a bzr managed working tree: %(url)s".'
515
0.152.1 by Vincent Ladeuil
Empty shell
516
0.155.1 by James Westby
Switch most of the logic to a class outside of the command class.
517
class cmd_upload(commands.Command):
518
    """Upload a working tree, as a whole or incrementally.
519
520
    If no destination is specified use the last one used.
521
    If no revision is specified upload the changes since the last upload.
0.152.56 by Vincent Ladeuil
Small tweaks including doc fix (#275538).
522
523
    Changes include files added, renamed, modified or removed.
0.155.1 by James Westby
Switch most of the logic to a class outside of the command class.
524
    """
0.152.63 by Vincent Ladeuil
Make the doc more easily discoverable.
525
    _see_also = ['plugins/upload']
0.155.1 by James Westby
Switch most of the logic to a class outside of the command class.
526
    takes_args = ['location?']
527
    takes_options = [
528
        'revision',
529
        'remember',
0.159.4 by Gary van der Merwe
Check that the revision we are uploading is a descendent from the revision that was uploaded.
530
        'overwrite',
0.155.1 by James Westby
Switch most of the logic to a class outside of the command class.
531
        option.Option('full', 'Upload the full working tree.'),
532
        option.Option('quiet', 'Do not output what is being done.',
533
                       short_name='q'),
534
        option.Option('directory',
535
                      help='Branch to upload from, '
536
                      'rather than the one containing the working directory.',
537
                      short_name='d',
538
                      type=unicode,
539
                      ),
0.155.5 by James Westby
Hook up the --upload option and document it.
540
        option.Option('auto',
541
                      'Trigger an upload from this branch whenever the tip '
542
                      'revision changes.')
0.155.1 by James Westby
Switch most of the logic to a class outside of the command class.
543
       ]
544
545
    def run(self, location=None, full=False, revision=None, remember=None,
0.159.4 by Gary van der Merwe
Check that the revision we are uploading is a descendent from the revision that was uploaded.
546
            directory=None, quiet=False, auto=None, overwrite=False
0.155.1 by James Westby
Switch most of the logic to a class outside of the command class.
547
            ):
548
        if directory is None:
549
            directory = u'.'
0.158.7 by Gary van der Merwe
Clean up white space.
550
0.157.1 by James Westby
Don't try and register the hook if install_named_hook is not available
551
        if auto and not auto_hook_available:
552
            raise BzrCommandError("Your version of bzr does not have the "
553
                    "hooks necessary for --auto to work")
554
0.158.19 by Vincent Ladeuil
Bzr has facilities for exceptions, let's use them.
555
        (wt, branch,
556
         relpath) = bzrdir.BzrDir.open_containing_tree_or_branch(directory)
0.160.2 by Martin Albisetti
Clean up whitespace to appease the neurotic in me
557
0.158.1 by Gary van der Merwe
Don't require a working tree.
558
        if wt:
0.159.4 by Gary van der Merwe
Check that the revision we are uploading is a descendent from the revision that was uploaded.
559
            wt.lock_read()
560
        else:
561
            branch.lock_read()
0.158.9 by Gary van der Merwe
Add a check to make sure we are not uploading to an existing wt.
562
        try:
0.159.4 by Gary van der Merwe
Check that the revision we are uploading is a descendent from the revision that was uploaded.
563
            if wt:
564
                changes = wt.changes_from(wt.basis_tree())
0.160.2 by Martin Albisetti
Clean up whitespace to appease the neurotic in me
565
0.159.4 by Gary van der Merwe
Check that the revision we are uploading is a descendent from the revision that was uploaded.
566
                if revision is None and  changes.has_changed():
567
                    raise errors.UncommittedChanges(wt)
0.160.2 by Martin Albisetti
Clean up whitespace to appease the neurotic in me
568
0.159.4 by Gary van der Merwe
Check that the revision we are uploading is a descendent from the revision that was uploaded.
569
            if location is None:
570
                stored_loc = get_upload_location(branch)
571
                if stored_loc is None:
572
                    raise errors.BzrCommandError('No upload location'
573
                                                 ' known or specified.')
574
                else:
575
                    # FIXME: Not currently tested
576
                    display_url = urlutils.unescape_for_display(stored_loc,
577
                            self.outf.encoding)
578
                    self.outf.write("Using saved location: %s\n" % display_url)
579
                    location = stored_loc
0.160.2 by Martin Albisetti
Clean up whitespace to appease the neurotic in me
580
0.159.4 by Gary van der Merwe
Check that the revision we are uploading is a descendent from the revision that was uploaded.
581
            to_transport = transport.get_transport(location)
0.160.2 by Martin Albisetti
Clean up whitespace to appease the neurotic in me
582
0.159.4 by Gary van der Merwe
Check that the revision we are uploading is a descendent from the revision that was uploaded.
583
            # Check that we are not uploading to a existing working tree.
584
            try:
585
                to_bzr_dir = bzrdir.BzrDir.open_from_transport(to_transport)
586
                has_wt = to_bzr_dir.has_workingtree()
587
            except errors.NotBranchError:
588
                has_wt = False
589
            except errors.NotLocalUrl:
590
                # The exception raised is a bit weird... but that's life.
591
                has_wt = True
0.160.2 by Martin Albisetti
Clean up whitespace to appease the neurotic in me
592
0.159.4 by Gary van der Merwe
Check that the revision we are uploading is a descendent from the revision that was uploaded.
593
            if has_wt:
594
                raise CannotUploadToWorkingTreeError(url=location)
0.160.2 by Martin Albisetti
Clean up whitespace to appease the neurotic in me
595
0.159.4 by Gary van der Merwe
Check that the revision we are uploading is a descendent from the revision that was uploaded.
596
            if revision is None:
597
                rev_id = branch.last_revision()
598
            else:
599
                if len(revision) != 1:
600
                    raise errors.BzrCommandError(
601
                        'bzr upload --revision takes exactly 1 argument')
602
                rev_id = revision[0].in_history(branch).rev_id
0.160.2 by Martin Albisetti
Clean up whitespace to appease the neurotic in me
603
0.159.4 by Gary van der Merwe
Check that the revision we are uploading is a descendent from the revision that was uploaded.
604
            tree = branch.repository.revision_tree(rev_id)
0.160.2 by Martin Albisetti
Clean up whitespace to appease the neurotic in me
605
0.159.4 by Gary van der Merwe
Check that the revision we are uploading is a descendent from the revision that was uploaded.
606
            uploader = BzrUploader(branch, to_transport, self.outf, tree,
607
                                   rev_id, quiet=quiet)
0.160.2 by Martin Albisetti
Clean up whitespace to appease the neurotic in me
608
0.159.4 by Gary van der Merwe
Check that the revision we are uploading is a descendent from the revision that was uploaded.
609
            if not overwrite:
610
                prev_uploaded_rev_id = uploader.get_uploaded_revid()
611
                graph = branch.repository.get_graph()
612
                if not graph.is_ancestor(prev_uploaded_rev_id, rev_id):
613
                    raise DivergedError(rev_id, prev_uploaded_rev_id)
0.160.2 by Martin Albisetti
Clean up whitespace to appease the neurotic in me
614
0.159.4 by Gary van der Merwe
Check that the revision we are uploading is a descendent from the revision that was uploaded.
615
            if full:
616
                uploader.upload_full_tree()
617
            else:
618
                uploader.upload_tree()
619
        finally:
620
            if wt:
621
                wt.unlock()
622
            else:
623
                branch.unlock()
0.155.1 by James Westby
Switch most of the logic to a class outside of the command class.
624
625
        # We uploaded successfully, remember it
626
        if get_upload_location(branch) is None or remember:
627
            set_upload_location(branch, to_transport.base)
0.155.4 by James Westby
Add the groundwork for --auto that enables the hook for a branch.
628
        if auto is not None:
629
            set_upload_auto(branch, auto)
0.155.1 by James Westby
Switch most of the logic to a class outside of the command class.
630
631
0.152.1 by Vincent Ladeuil
Empty shell
632
commands.register_command(cmd_upload)
633
0.152.61 by Vincent Ladeuil
Fix bug #312686 and add an 'upload_auto_quiet' config variable.
634
def install_auto_upload_hook():
635
    from bzrlib.plugins.upload import auto_upload_hook
0.157.1 by James Westby
Don't try and register the hook if install_named_hook is not available
636
    branch.Branch.hooks.install_named_hook('post_change_branch_tip',
0.152.61 by Vincent Ladeuil
Fix bug #312686 and add an 'upload_auto_quiet' config variable.
637
            auto_upload_hook.auto_upload_hook,
0.157.1 by James Westby
Don't try and register the hook if install_named_hook is not available
638
            'Auto upload code from a branch when it is changed.')
0.152.61 by Vincent Ladeuil
Fix bug #312686 and add an 'upload_auto_quiet' config variable.
639
640
641
if hasattr(branch.Branch.hooks, "install_named_hook"):
642
    install_auto_upload_hook()
0.157.1 by James Westby
Don't try and register the hook if install_named_hook is not available
643
    auto_hook_available = True
644
else:
645
    auto_hook_available = False
0.155.2 by James Westby
Add a post_change_branch_tip hook to upload.
646
0.159.4 by Gary van der Merwe
Check that the revision we are uploading is a descendent from the revision that was uploaded.
647
class DivergedError(errors.BzrError):
648
649
    _fmt = ("The revision upload to this location is from a branch that is "
0.159.6 by Gary van der Merwe
Fix formating in DivergedError.
650
            "diverged from this branch: %(upload_revid)s")
0.160.2 by Martin Albisetti
Clean up whitespace to appease the neurotic in me
651
0.159.4 by Gary van der Merwe
Check that the revision we are uploading is a descendent from the revision that was uploaded.
652
    # We should probably tell the user to use merge on the diverged branch,
653
    # but how do we explan which branch they need to merge from!
654
655
    def __init__(self, revid, upload_revid):
656
        self.revid = revid
657
        self.upload_revid = upload_revid
0.155.2 by James Westby
Add a post_change_branch_tip hook to upload.
658
0.152.29 by Vincent Ladeuil
Work around test fixture limitation regarding self.outf (cough). All tests passing again.
659
def load_tests(basic_tests, module, loader):
0.153.1 by Vincent Ladeuil
Clean up references to verbose.
660
    # This module shouldn't define any tests but I don't know how to report
0.154.1 by Vincent Ladeuil
Create a simple setup.py and rework tests modules accordingly.
661
    # that. I prefer to update basic_tests with the other tests to detect
662
    # unwanted tests and I think that's sufficient.
0.152.1 by Vincent Ladeuil
Empty shell
663
664
    testmod_names = [
0.154.1 by Vincent Ladeuil
Create a simple setup.py and rework tests modules accordingly.
665
        'tests',
0.152.1 by Vincent Ladeuil
Empty shell
666
        ]
0.154.1 by Vincent Ladeuil
Create a simple setup.py and rework tests modules accordingly.
667
    basic_tests.addTest(loader.loadTestsFromModuleNames(
0.152.1 by Vincent Ladeuil
Empty shell
668
            ["%s.%s" % (__name__, tmn) for tmn in testmod_names]))
0.154.1 by Vincent Ladeuil
Create a simple setup.py and rework tests modules accordingly.
669
    return basic_tests