/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz

« back to all changes in this revision

Viewing changes to backend/init.py

  • Committer: Szilveszter Farkas (Phanatic)
  • Date: 2006-06-10 16:28:58 UTC
  • mto: (0.14.1 main) (93.1.1 win32.bialix)
  • mto: This revision was merged to the branch mainline in revision 83.
  • Revision ID: Szilveszter.Farkas@gmail.com-20060610162858-38ce234fa35d4447
* backend/errors.py: some basic exceptions added
* backend/fileops.py: mkdir() and add() implemented 
* backend/init.py: init() and branch() implemented
* The work has begun :)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 by Szilveszter Farkas (Phanatic) <szilveszter.farkas@gmail.com>
 
1
# Copyright (C) 2006 by Szilveszter Farkas (Phanatic)
2
2
# Some parts of the code are:
3
3
# Copyright (C) 2005, 2006 by Canonical Ltd
4
4
 
19
19
import errno
20
20
import os
21
21
 
22
 
import bzrlib
23
 
import bzrlib.bzrdir as bzrdir
24
22
import bzrlib.errors as errors
25
23
 
26
 
from bzrlib.branch import Branch
27
 
 
28
24
from errors import (AlreadyBranchError, BranchExistsWithoutWorkingTree,
29
 
                    NonExistingParent, NonExistingRevision,
30
 
                    NonExistingSource, NotBranchError, TargetAlreadyExists)
 
25
                    NonExistingParent, NonExistingRevision, NonExistingSource,
 
26
                    RevisionValueError, TargetAlreadyExists)
 
27
 
 
28
def init(location):
 
29
    from bzrlib.builtins import get_format_type
 
30
    import bzrlib.bzrdir as bzrdir
 
31
 
 
32
    format = get_format_type('default')
 
33
 
 
34
    if not os.path.exists(location):
 
35
        os.mkdir(location)
 
36
 
 
37
    try:
 
38
        existing_bzrdir = bzrdir.BzrDir.open(location)
 
39
    except errors.NotBranchError:
 
40
        bzrdir.BzrDir.create_branch_convenience(location, format=format)
 
41
    else:
 
42
        if existing_bzrdir.has_branch():
 
43
            if existing_bzrdir.has_workingtree():
 
44
                raise AlreadyBranchError(location)
 
45
            else:
 
46
                raise BranchExistsWithoutWorkingTree(location)
 
47
        else:
 
48
            existing_bzrdir.create_branch()
 
49
            existing_bzrdir.create_workingtree()
31
50
 
32
51
def branch(from_location, to_location, revision=None):
33
 
    """ Create a branch from a local/remote location.
34
 
    
35
 
    :param from_location: the original location of the branch
36
 
    
37
 
    :param to_location: the directory where the branch should be created
38
 
    
39
 
    :param revision: if specified, the given revision will be branched
40
 
    
41
 
    :return: number of revisions branched
42
 
    """
 
52
    from bzrlib.branch import Branch
43
53
    from bzrlib.transport import get_transport
44
54
 
 
55
    if revision is None:
 
56
        revision = [None]
 
57
    elif len(revision) > 1:
 
58
        raise RevisionValueError('bzr branch --revision takes exactly 1 revision value')
 
59
 
45
60
    try:
46
61
        br_from = Branch.open(from_location)
47
62
    except OSError, e:
49
64
            raise NonExistingSource(from_location)
50
65
        else:
51
66
            raise
52
 
    except errors.NotBranchError:
53
 
        raise NotBranchError(from_location)
54
67
 
55
68
    br_from.lock_read()
56
69
 
57
70
    try:
58
71
        basis_dir = None
59
 
        if revision is not None:
60
 
            revision_id = br_from.get_rev_id(revision)
 
72
        if len(revision) == 1 and revision[0] is not None:
 
73
            revision_id = revision[0].in_history(br_from)[1]
61
74
        else:
62
 
            revision_id = None
 
75
            revision_id = br_from.last_revision()
63
76
 
64
77
        to_location = to_location + '/' + os.path.basename(from_location.rstrip("/\\"))
65
78
        name = None
86
99
        
87
100
    return branch.revno()
88
101
 
89
 
def checkout(branch_location, to_location, revision=None, lightweight=False):
90
 
    """ Create a new checkout of an existing branch.
91
 
    
92
 
    :param branch_location: the location of the branch you'd like to checkout
93
 
    
94
 
    :param to_location: the directory where the checkout should be created
95
 
    
96
 
    :param revision: the given revision will be checkout'ed (be aware!)
97
 
    
98
 
    :param lightweight: perform a lightweight checkout (be aware!)
99
 
    """
100
 
    try:
101
 
        source = Branch.open(branch_location)
102
 
    except errors.NotBranchError:
103
 
        raise NotBranchError(branch_location)
104
 
    
105
 
    if revision is not None:
106
 
        revision_id = source.get_rev_id(revision)
107
 
    else:
108
 
        revision_id = None
109
 
 
110
 
    # if the source and to_location are the same, 
111
 
    # and there is no working tree,
112
 
    # then reconstitute a branch
113
 
    if (bzrlib.osutils.abspath(to_location) ==
114
 
        bzrlib.osutils.abspath(branch_location)):
115
 
        try:
116
 
            source.bzrdir.open_workingtree()
117
 
        except errors.NoWorkingTree:
118
 
            source.bzrdir.create_workingtree()
119
 
            return
120
 
 
121
 
    to_location = to_location + '/' + os.path.basename(branch_location.rstrip("/\\"))
122
 
    
123
 
    try:
124
 
        os.mkdir(to_location)
125
 
    except OSError, e:
126
 
        if e.errno == errno.EEXIST:
127
 
            raise TargetAlreadyExists(to_location)
128
 
        if e.errno == errno.ENOENT:
129
 
            raise NonExistingParent(to_location)
130
 
        else:
131
 
            raise
132
 
 
133
 
    old_format = bzrlib.bzrdir.BzrDirFormat.get_default_format()
134
 
    bzrlib.bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
135
 
 
136
 
    try:
137
 
        if lightweight:
138
 
            checkout = bzrdir.BzrDirMetaFormat1().initialize(to_location)
139
 
            bzrlib.branch.BranchReferenceFormat().initialize(checkout, source)
140
 
        else:
141
 
            checkout_branch = bzrlib.bzrdir.BzrDir.create_branch_convenience(
142
 
                to_location, force_new_tree=False)
143
 
            checkout = checkout_branch.bzrdir
144
 
            checkout_branch.bind(source)
145
 
            if revision_id is not None:
146
 
                rh = checkout_branch.revision_history()
147
 
                checkout_branch.set_revision_history(rh[:rh.index(revision_id) + 1])
148
 
 
149
 
        checkout.create_workingtree(revision_id)
150
 
    finally:
151
 
        bzrlib.bzrdir.BzrDirFormat.set_default_format(old_format)
152
 
 
153
 
def init(location):
154
 
    """ Initialize a directory.
155
 
    
156
 
    :param location: full path to the directory you want to be versioned
157
 
    """
158
 
    from bzrlib.builtins import get_format_type
159
 
 
160
 
    format = get_format_type('default')
161
 
 
162
 
    if not os.path.exists(location):
163
 
        os.mkdir(location)
164
 
 
165
 
    try:
166
 
        existing_bzrdir = bzrdir.BzrDir.open(location)
167
 
    except errors.NotBranchError:
168
 
        bzrdir.BzrDir.create_branch_convenience(location, format=format)
169
 
    else:
170
 
        if existing_bzrdir.has_branch():
171
 
            if existing_bzrdir.has_workingtree():
172
 
                raise AlreadyBranchError(location)
173
 
            else:
174
 
                raise BranchExistsWithoutWorkingTree(location)
175
 
        else:
176
 
            existing_bzrdir.create_branch()
177
 
            existing_bzrdir.create_workingtree()
 
102
def checkout():
 
103
    """ FIXME - will be implemented later """