~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to clean_tree.py

  • Committer: Michael Ellerman
  • Date: 2005-11-29 07:12:26 UTC
  • mto: (0.3.1 shelf-dev) (325.1.2 bzrtools)
  • mto: This revision was merged to the branch mainline in revision 334.
  • Revision ID: michael@ellerman.id.au-20051129071226-a04b3f827880025d
Unshelve --pick was broken, because we deleted the whole patch, even when only
part of it was unshelved. So now if we unshelve part of a patch, the patch is
replaced with a new patch that has just the unshelved parts. That's a long way
of saying it does what you'd expect.

Implementing this required changing HunkSelector to return both the selected,
and unselected hunks (ie. patches to shelve, and patches to keep).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 by Aaron Bentley
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
 
import errno
17
 
import os
18
 
import shutil
19
 
import sys
20
 
 
21
 
from bzrlib.osutils import has_symlinks, isdir
22
 
from bzrlib.trace import note
23
 
from bzrlib.workingtree import WorkingTree
24
 
 
25
 
 
26
 
def is_detritus(subp):
27
 
    """Return True if the supplied path is detritus, False otherwise"""
28
 
    return subp.endswith('.THIS') or subp.endswith('.BASE') or\
29
 
        subp.endswith('.OTHER') or subp.endswith('~') or subp.endswith('.tmp')
30
 
 
31
 
 
32
 
def iter_deletables(tree, unknown=True, ignored=False, detritus=False):
33
 
    """Iterate through files that may be deleted"""
34
 
    for subp in tree.extras():
35
 
        if detritus and is_detritus(subp):
36
 
            yield tree.abspath(subp), subp
37
 
            continue
38
 
        if tree.is_ignored(subp):
39
 
            if ignored:
40
 
                yield tree.abspath(subp), subp
41
 
        else:
42
 
            if unknown:
43
 
                yield tree.abspath(subp), subp
44
 
 
45
 
 
46
 
def clean_tree(directory, ignored=False, detritus=False, dry_run=False):
47
 
    """Remove files in the specified classes from the tree"""
48
 
    tree = WorkingTree.open_containing(directory)[0]
49
 
    deletables = iter_deletables(tree, ignored=ignored, detritus=detritus)
50
 
    delete_items(deletables, dry_run=dry_run)
51
 
 
52
 
 
53
 
def delete_items(deletables, dry_run=False):
54
 
    """Delete files in the deletables iterable"""
55
 
    has_deleted = False
56
 
    for path, subp in deletables:
57
 
        if not has_deleted:
58
 
            note("deleting paths:")
59
 
            has_deleted = True
60
 
        note('  ' + subp)
61
 
        if not dry_run:
62
 
            if isdir(path):
63
 
                shutil.rmtree(path)
64
 
            else:
65
 
                os.unlink(path)
66
 
    if not has_deleted:
67
 
        note("No files deleted.")