1
1
# Copyright (C) 2005 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
11
# GNU General Public License for more details.
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18
from os.path import dirname
19
20
import bzrlib.errors as errors
54
def add_action_null(inv, parent_ie, path, kind):
55
"""Absorb add actions and do nothing."""
59
def add_action_print(inv, parent_ie, path, kind):
60
"""Print a line to stdout for each file that would be added."""
61
print "added", bzrlib.osutils.quotefn(path)
64
def add_action_add(inv, parent_ie, path, kind):
65
"""Add each file to the given inventory. Produce no output."""
66
if parent_ie is not None:
67
entry = bzrlib.inventory.make_entry(
68
kind, bzrlib.osutils.basename(path), parent_ie.file_id)
71
entry = inv.add_path(path, kind=kind)
72
# mutter("added %r kind %r file_id={%s}" % (path, kind, entry.file_id))
75
def add_action_add_and_print(inv, parent_ie, path, kind):
76
"""Add each file to the given inventory, and print a line to stdout."""
77
add_action_add(inv, parent_ie, path, kind)
78
add_action_print(inv, parent_ie, path, kind)
81
def smart_add(file_list, recurse=True, action=add_action_add):
55
class AddAction(object):
56
"""A class which defines what action to take when adding a file."""
58
def __init__(self, to_file=None, should_add=None, should_print=None):
59
self._to_file = to_file
61
self._to_file = sys.stdout
62
self.should_add = False
63
if should_add is not None:
64
self.should_add = should_add
65
self.should_print = False
66
if should_print is not None:
67
self.should_print = should_print
69
def __call__(self, inv, parent_ie, path, kind):
70
"""Add path to inventory.
72
The default action does nothing.
74
:param inv: The inventory we are working with.
75
:param path: The path being added
76
:param kind: The kind of the object being added.
79
self._add_to_inv(inv, parent_ie, path, kind)
81
self._print(inv, parent_ie, path, kind)
83
def _print(self, inv, parent_ie, path, kind):
84
"""Print a line to self._to_file for each file that would be added."""
85
self._to_file.write('added ')
86
self._to_file.write(bzrlib.osutils.quotefn(path))
87
self._to_file.write('\n')
89
def _add_to_inv(self, inv, parent_ie, path, kind):
90
"""Add each file to the given inventory. Produce no output."""
91
if parent_ie is not None:
92
entry = bzrlib.inventory.make_entry(
93
kind, bzrlib.osutils.basename(path), parent_ie.file_id)
96
entry = inv.add_path(path, kind=kind)
97
# mutter("added %r kind %r file_id={%s}", path, kind, entry.file_id)
100
# TODO: jam 20050105 These could be used for compatibility
101
# however, they bind against the current stdout, not the
102
# one which exists at the time they are called, so they
103
# don't work for the test suite.
105
add_action_null = AddAction()
106
add_action_add = AddAction(should_add=True)
107
add_action_print = AddAction(should_print=True)
108
add_action_add_and_print = AddAction(should_add=True, should_print=True)
111
def smart_add(file_list, recurse=True, action=None):
82
112
"""Add files to version, optionally recursing into directories.
84
114
This is designed more towards DWIM for humans than API simplicity.
89
119
file_list = _prepare_file_list(file_list)
90
120
tree = WorkingTree.open_containing(file_list[0])[0]
91
return smart_add_tree(tree, file_list, recurse, action)
94
def smart_add_tree(tree, file_list, recurse=True, action=add_action_add):
121
return smart_add_tree(tree, file_list, recurse, action=action)
124
def smart_add_tree(tree, file_list, recurse=True, action=None):
95
125
"""Add files to version, optionally recursing into directories.
97
127
This is designed more towards DWIM for humans than API simplicity.