/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.16.1 by Aaron Bentley
Begin implementing UI
1
# Copyright (C) 2008 Aaron Bentley <aaron@aaronbentley.com>
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
17
18
import copy
19
from cStringIO import StringIO
20
import sys
21
22
from bzrlib import (diff, osutils, patches, workingtree)
23
from bzrlib.plugins.bzrtools import hunk_selector
24
from bzrlib.plugins.shelf2 import prepare_shelf
25
26
27
class Shelver(object):
28
29
    def __init__(self, work_tree, target_tree, path):
30
        self.work_tree = work_tree
31
        self.target_tree = target_tree
32
        self.path = path
33
        self.diff_file = StringIO()
34
        self.text_differ = diff.DiffText(self.work_tree, self.target_tree,
35
                                         self.diff_file)
36
37
    @classmethod
38
    def from_args(klass):
39
        tree, path = workingtree.WorkingTree.open_containing('.')
40
        return klass(tree, tree.basis_tree(), path)
41
42
    def run(self):
43
        creator = prepare_shelf.ShelfCreator(self.work_tree)
44
        try:
45
            self.target_tree.lock_read()
46
            try:
47
                for change in creator:
48
                    if change[0] == 'modify text':
49
                        self.handle_modify_text(change[1])
50
            finally:
51
                self.target_tree.unlock()
52
        finally:
53
            creator.finalize()
54
55
    def get_parsed_patch(self, file_id):
56
        old_path = self.work_tree.id2path(file_id)
57
        new_path = self.target_tree.id2path(file_id)
58
        try:
59
            patch = self.text_differ.diff(file_id, old_path, new_path, 'file',
60
                                          'file')
61
            self.diff_file.seek(0)
62
            return patches.parse_patch(self.diff_file)
63
        finally:
64
            self.diff_file.truncate(0)
65
66
    def __getchar(self):
67
        import tty
68
        import termios
69
        fd = sys.stdin.fileno()
70
        settings = termios.tcgetattr(fd)
71
        try:
72
            tty.setraw(fd)
73
            ch = sys.stdin.read(1)
74
        finally:
75
            termios.tcsetattr(fd, termios.TCSADRAIN, settings)
76
        return ch
77
78
    def prompt(self, question):
79
        print question,
80
        char = self.__getchar()
81
        print ""
82
        return char
83
84
    def handle_modify_text(self, file_id):
85
        parsed = self.get_parsed_patch(file_id)
86
        selected_hunks = []
87
        final_patch = copy.copy(parsed)
88
        final_patch.hunks = []
89
        for hunk in parsed.hunks:
90
            print hunk
91
            char = self.prompt('Shelve? [y/n]')
92
            if char == 'y':
93
                final_patch.hunks.append(hunk)