~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to shelver.py

  • Committer: Aaron Bentley
  • Date: 2008-10-08 14:48:14 UTC
  • mto: This revision was merged to the branch mainline in revision 3823.
  • Revision ID: aaron@aaronbentley.com-20081008144814-pwwoi3jsacwn701w
Improve prompting

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
 
32
32
class Shelver(object):
33
33
 
34
 
    def __init__(self, work_tree, target_tree, path, auto=False):
 
34
    def __init__(self, work_tree, target_tree, path, auto=False,
 
35
                 auto_apply=False):
35
36
        self.work_tree = work_tree
36
37
        self.target_tree = target_tree
37
38
        self.path = path
41
42
        self.diff_writer = colordiff.DiffWriter(sys.stdout, False)
42
43
        self.manager = prepare_shelf.ShelfManager.for_tree(work_tree)
43
44
        self.auto = auto
 
45
        self.auto_apply = auto_apply
44
46
 
45
47
    @classmethod
46
48
    def from_args(klass, revision=None, all=False):
47
49
        tree, path = workingtree.WorkingTree.open_containing('.')
48
50
        target_tree = builtins._get_one_revision_tree('shelf2', revision,
49
51
            tree.branch, tree)
50
 
        return klass(tree, target_tree, path, all)
 
52
        return klass(tree, target_tree, path, all, all)
51
53
 
52
54
    def run(self):
53
55
        creator = prepare_shelf.ShelfCreator(self.work_tree, self.target_tree)
59
61
                    changes_shelved += self.handle_modify_text(creator,
60
62
                                                               change[1])
61
63
                if change[0] == 'add file':
62
 
                    if self.prompt('Shelve adding file?') == 'y':
 
64
                    if self.prompt_bool('Shelve adding file?'):
63
65
                        creator.shelve_creation(change[1])
64
66
                        changes_shelved += 1
65
67
                if change[0] == 'delete file':
66
 
                    if self.prompt('Shelve deleting file?') == 'y':
 
68
                    if self.prompt_bool('Shelve deleting file?'):
67
69
                        creator.shelve_deletion(change[1])
68
70
                        changes_shelved += 1
69
71
                if change[0] == 'rename':
70
 
                    if self.prompt('Shelve renaming %s => %s?' %
71
 
                                   change[2:]) == 'y':
 
72
                    if self.prompt_bool('Shelve renaming %s => %s?' %
 
73
                                   change[2:]):
72
74
                        creator.shelve_rename(change[1])
73
75
                        changes_shelved += 1
74
76
            if changes_shelved > 0:
75
 
                choice = self.prompt('Shelve %d change(s)? [y/n]' %
76
 
                                     changes_shelved)
77
 
                if choice == 'y':
 
77
                if (self.prompt_bool('Shelve %d change(s)? [y/n]' %
 
78
                    changes_shelved, auto=self.auto_apply)):
78
79
                    shelf_id, shelf_file = self.manager.new_shelf()
79
80
                    try:
80
81
                        creator.write_shelf(shelf_file)
98
99
        finally:
99
100
            self.diff_file.truncate(0)
100
101
 
101
 
    def prompt(self, question):
102
 
        if self.auto:
103
 
            return 'y'
 
102
    def prompt_bool(self, question, auto=None):
 
103
        if auto == None:
 
104
            auto = self.auto
 
105
        if auto:
 
106
            return True
104
107
        print question,
105
108
        char = getchar()
106
 
        print ""
107
 
        return char
 
109
        print "\r",
 
110
        if char == 'y':
 
111
            return True
 
112
        elif char == 'f':
 
113
            self.auto = True
 
114
            return True
 
115
        else:
 
116
            return False
108
117
 
109
118
    def get_patched_text(self, file_id, patch):
110
119
        target_file = self.target_tree.get_file(file_id)
135
144
        if not self.auto:
136
145
            for hunk in parsed.hunks:
137
146
                self.diff_writer.write(str(hunk))
138
 
                char = self.prompt('Shelve? [y/n]')
139
 
                if char == 'n':
 
147
                if not self.prompt_bool('Shelve? [y/n/f]'):
140
148
                    final_patch.hunks.append(hunk)
141
149
        patched_text = self.get_patched_text(file_id, final_patch)
142
150
        creator.shelve_text(file_id, patched_text)