/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/builtins.py

  • Committer: Andrew Bennetts
  • Date: 2006-11-21 08:19:35 UTC
  • mfrom: (2018.8.1 split smart)
  • mto: (2018.5.35 hpss)
  • mto: This revision was merged to the branch mainline in revision 2435.
  • Revision ID: andrew.bennetts@canonical.com-20061121081935-6440ef860ef00262
Merge from bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
import codecs
24
24
import errno
25
25
import sys
 
26
import tempfile
26
27
 
27
28
import bzrlib
28
29
from bzrlib import (
212
213
                self.outf.write(b.repository.get_revision_xml(rev_id).decode('utf-8'))
213
214
    
214
215
 
 
216
class cmd_remove_tree(Command):
 
217
    """Remove the working tree from a given branch/checkout.
 
218
 
 
219
    Since a lightweight checkout is little more than a working tree
 
220
    this will refuse to run against one.
 
221
    """
 
222
 
 
223
    hidden = True
 
224
 
 
225
    takes_args = ['location?']
 
226
 
 
227
    def run(self, location='.'):
 
228
        d = bzrdir.BzrDir.open(location)
 
229
        
 
230
        try:
 
231
            working = d.open_workingtree()
 
232
        except errors.NoWorkingTree:
 
233
            raise errors.BzrCommandError("No working tree to remove")
 
234
        except errors.NotLocalUrl:
 
235
            raise errors.BzrCommandError("You cannot remove the working tree of a "
 
236
                                         "remote path")
 
237
        
 
238
        working_path = working.bzrdir.root_transport.base
 
239
        branch_path = working.branch.bzrdir.root_transport.base
 
240
        if working_path != branch_path:
 
241
            raise errors.BzrCommandError("You cannot remove the working tree from "
 
242
                                         "a lightweight checkout")
 
243
        
 
244
        d.destroy_workingtree()
 
245
        
 
246
 
215
247
class cmd_revno(Command):
216
248
    """Show current revision number.
217
249
 
945
977
        tree, relpath = WorkingTree.open_containing(filename)
946
978
        i = tree.inventory.path2id(relpath)
947
979
        if i is None:
948
 
            raise errors.BzrError("%r is not a versioned file" % filename)
 
980
            raise errors.NotVersionedError(filename)
949
981
        else:
950
982
            self.outf.write(i + '\n')
951
983
 
966
998
        inv = tree.inventory
967
999
        fid = inv.path2id(relpath)
968
1000
        if fid is None:
969
 
            raise errors.BzrError("%r is not a versioned file" % filename)
 
1001
            raise errors.NotVersionedError(filename)
970
1002
        for fip in inv.get_idpath(fid):
971
1003
            self.outf.write(fip + '\n')
972
1004
 
1197
1229
            new_label = 'new/'
1198
1230
        else:
1199
1231
            if not ':' in prefix:
1200
 
                 raise errors.BzrError("--diff-prefix expects two values"
1201
 
                                       " separated by a colon")
 
1232
                 raise BzrCommandError(
 
1233
                     "--diff-prefix expects two values separated by a colon")
1202
1234
            old_label, new_label = prefix.split(":")
1203
1235
        
1204
1236
        try:
1688
1720
            rev_id = b.last_revision()
1689
1721
        else:
1690
1722
            if len(revision) != 1:
1691
 
                raise errors.BzrError('bzr export --revision takes exactly'
1692
 
                                      ' 1 argument')
 
1723
                raise errors.BzrCommandError('bzr export --revision takes exactly 1 argument')
1693
1724
            rev_id = revision[0].in_history(b).rev_id
1694
1725
        t = b.repository.revision_tree(rev_id)
1695
1726
        try:
1802
1833
                StrictCommitFailed)
1803
1834
        from bzrlib.msgeditor import edit_commit_message, \
1804
1835
                make_commit_message_template
1805
 
        from tempfile import TemporaryFile
1806
1836
 
1807
1837
        # TODO: Need a blackbox test for invoking the external editor; may be
1808
1838
        # slightly problematic to run this cross-platform.
1809
1839
 
1810
1840
        # TODO: do more checks that the commit will succeed before 
1811
1841
        # spending the user's valuable time typing a commit message.
1812
 
        #
1813
 
        # TODO: if the commit *does* happen to fail, then save the commit 
1814
 
        # message to a temporary file where it can be recovered
1815
1842
        tree, selected_list = tree_files(selected_list)
1816
1843
        if selected_list == ['']:
1817
1844
            # workaround - commit of root of tree should be exactly the same
1840
1867
            reporter = ReportCommitToLog()
1841
1868
        else:
1842
1869
            reporter = NullCommitReporter()
1843
 
        
 
1870
 
 
1871
        msgfilename = self._save_commit_message(message, tree.basedir)
1844
1872
        try:
1845
1873
            tree.commit(message, specific_files=selected_list,
1846
1874
                        allow_pointless=unchanged, strict=strict, local=local,
1847
1875
                        reporter=reporter)
 
1876
            if msgfilename is not None:
 
1877
                try:
 
1878
                    os.unlink(msgfilename)
 
1879
                except IOError, e:
 
1880
                    warning("failed to unlink %s: %s; ignored", msgfilename, e)
1848
1881
        except PointlessCommit:
1849
1882
            # FIXME: This should really happen before the file is read in;
1850
1883
            # perhaps prepare the commit; get the message; then actually commit
1851
 
            raise errors.BzrCommandError("no changes to commit."
1852
 
                                         " use --unchanged to commit anyhow")
 
1884
            if msgfilename is not None:
 
1885
                raise errors.BzrCommandError("no changes to commit."
 
1886
                                  " use --unchanged to commit anyhow\n"
 
1887
                                  "Commit message saved. To reuse the message,"
 
1888
                                  " do\nbzr commit --file " + msgfilename)
 
1889
            else:
 
1890
                raise errors.BzrCommandError("no changes to commit."
 
1891
                                  " use --unchanged to commit anyhow")
1853
1892
        except ConflictsInTree:
1854
 
            raise errors.BzrCommandError("Conflicts detected in working tree.  "
1855
 
                'Use "bzr conflicts" to list, "bzr resolve FILE" to resolve.')
 
1893
            if msgfilename is not None:
 
1894
                raise errors.BzrCommandError('Conflicts detected in working '
 
1895
                    'tree.  Use "bzr conflicts" to list, "bzr resolve FILE" to'
 
1896
                    ' resolve.\n'
 
1897
                    'Commit message saved. To reuse the message,'
 
1898
                    ' do\nbzr commit --file ' + msgfilename)
 
1899
            else:
 
1900
                raise errors.BzrCommandError('Conflicts detected in working '
 
1901
                    'tree.  Use "bzr conflicts" to list, "bzr resolve FILE" to'
 
1902
                    ' resolve.')
1856
1903
        except StrictCommitFailed:
1857
 
            raise errors.BzrCommandError("Commit refused because there are unknown "
1858
 
                                         "files in the working tree.")
 
1904
            if msgfilename is not None:
 
1905
                raise errors.BzrCommandError("Commit refused because there are"
 
1906
                                  " unknown files in the working tree.\n"
 
1907
                                  "Commit message saved. To reuse the message,"
 
1908
                                  " do\nbzr commit --file " + msgfilename)
 
1909
            else:
 
1910
                raise errors.BzrCommandError("Commit refused because there are"
 
1911
                                  " unknown files in the working tree.")
1859
1912
        except errors.BoundBranchOutOfDate, e:
1860
 
            raise errors.BzrCommandError(str(e) + "\n"
 
1913
            if msgfilename is not None:
 
1914
                raise errors.BzrCommandError(str(e) + "\n"
 
1915
                'To commit to master branch, run update and then commit.\n'
 
1916
                'You can also pass --local to commit to continue working '
 
1917
                'disconnected.\n'
 
1918
                'Commit message saved. To reuse the message,'
 
1919
                ' do\nbzr commit --file ' + msgfilename)
 
1920
            else:
 
1921
                raise errors.BzrCommandError(str(e) + "\n"
1861
1922
                'To commit to master branch, run update and then commit.\n'
1862
1923
                'You can also pass --local to commit to continue working '
1863
1924
                'disconnected.')
1864
1925
 
 
1926
    def _save_commit_message(self, message, basedir):
 
1927
        # save the commit message and only unlink it if the commit was
 
1928
        # successful
 
1929
        msgfilename = None
 
1930
        try:
 
1931
            tmp_fileno, msgfilename = tempfile.mkstemp(prefix='bzr-commit-',
 
1932
                                                       dir=basedir)
 
1933
        except OSError:
 
1934
            try:
 
1935
                # No access to working dir, try $TMP
 
1936
                tmp_fileno, msgfilename = tempfile.mkstemp(prefix='bzr-commit-')
 
1937
            except OSError:
 
1938
                # We can't create a temp file, try to work without it
 
1939
                return None
 
1940
        try:
 
1941
            os.write(tmp_fileno, message.encode(bzrlib.user_encoding, 'replace'))
 
1942
        finally:
 
1943
            os.close(tmp_fileno)
 
1944
        return msgfilename
 
1945
 
 
1946
 
1865
1947
class cmd_check(Command):
1866
1948
    """Validate consistency of branch history.
1867
1949
 
1881
1963
        check(branch, verbose)
1882
1964
 
1883
1965
 
1884
 
class cmd_scan_cache(Command):
1885
 
    hidden = True
1886
 
    def run(self):
1887
 
        from bzrlib.hashcache import HashCache
1888
 
 
1889
 
        c = HashCache(u'.')
1890
 
        c.read()
1891
 
        c.scan()
1892
 
            
1893
 
        print '%6d stats' % c.stat_count
1894
 
        print '%6d in hashcache' % len(c._cache)
1895
 
        print '%6d files removed from cache' % c.removed_count
1896
 
        print '%6d hashes updated' % c.update_count
1897
 
        print '%6d files changed too recently to cache' % c.danger_count
1898
 
 
1899
 
        if c.needs_write:
1900
 
            c.write()
1901
 
 
1902
 
 
1903
1966
class cmd_upgrade(Command):
1904
1967
    """Upgrade branch storage to current format.
1905
1968
 
2057
2120
 
2058
2121
        if cache_dir is not None:
2059
2122
            tree_creator.TreeCreator.CACHE_ROOT = osutils.abspath(cache_dir)
2060
 
        # we don't want progress meters from the tests to go to the
2061
 
        # real output; and we don't want log messages cluttering up
2062
 
        # the real logs.
2063
 
        save_ui = ui.ui_factory
2064
2123
        print '%10s: %s' % ('bzr', osutils.realpath(sys.argv[0]))
2065
2124
        print '%10s: %s' % ('bzrlib', bzrlib.__path__[0])
2066
2125
        print
2067
 
        info('running tests...')
 
2126
        if testspecs_list is not None:
 
2127
            pattern = '|'.join(testspecs_list)
 
2128
        else:
 
2129
            pattern = ".*"
 
2130
        if benchmark:
 
2131
            test_suite_factory = benchmarks.test_suite
 
2132
            if verbose is None:
 
2133
                verbose = True
 
2134
            # TODO: should possibly lock the history file...
 
2135
            benchfile = open(".perf_history", "at")
 
2136
        else:
 
2137
            test_suite_factory = None
 
2138
            if verbose is None:
 
2139
                verbose = False
 
2140
            benchfile = None
2068
2141
        try:
2069
 
            ui.ui_factory = ui.SilentUIFactory()
2070
 
            if testspecs_list is not None:
2071
 
                pattern = '|'.join(testspecs_list)
2072
 
            else:
2073
 
                pattern = ".*"
2074
 
            if benchmark:
2075
 
                test_suite_factory = benchmarks.test_suite
2076
 
                if verbose is None:
2077
 
                    verbose = True
2078
 
                # TODO: should possibly lock the history file...
2079
 
                benchfile = open(".perf_history", "at")
2080
 
            else:
2081
 
                test_suite_factory = None
2082
 
                if verbose is None:
2083
 
                    verbose = False
2084
 
                benchfile = None
2085
 
            try:
2086
 
                result = selftest(verbose=verbose, 
2087
 
                                  pattern=pattern,
2088
 
                                  stop_on_failure=one, 
2089
 
                                  keep_output=keep_output,
2090
 
                                  transport=transport,
2091
 
                                  test_suite_factory=test_suite_factory,
2092
 
                                  lsprof_timed=lsprof_timed,
2093
 
                                  bench_history=benchfile)
2094
 
            finally:
2095
 
                if benchfile is not None:
2096
 
                    benchfile.close()
2097
 
            if result:
2098
 
                info('tests passed')
2099
 
            else:
2100
 
                info('tests failed')
2101
 
            return int(not result)
 
2142
            result = selftest(verbose=verbose, 
 
2143
                              pattern=pattern,
 
2144
                              stop_on_failure=one, 
 
2145
                              keep_output=keep_output,
 
2146
                              transport=transport,
 
2147
                              test_suite_factory=test_suite_factory,
 
2148
                              lsprof_timed=lsprof_timed,
 
2149
                              bench_history=benchfile)
2102
2150
        finally:
2103
 
            ui.ui_factory = save_ui
 
2151
            if benchfile is not None:
 
2152
                benchfile.close()
 
2153
        if result:
 
2154
            info('tests passed')
 
2155
        else:
 
2156
            info('tests failed')
 
2157
        return int(not result)
2104
2158
 
2105
2159
 
2106
2160
class cmd_version(Command):
2400
2454
        else:
2401
2455
            return 0
2402
2456
 
 
2457
 
2403
2458
class cmd_revert(Command):
2404
2459
    """Revert files to a previous revision.
2405
2460
 
2451
2506
 
2452
2507
class cmd_assert_fail(Command):
2453
2508
    """Test reporting of assertion failures"""
 
2509
    # intended just for use in testing
 
2510
 
2454
2511
    hidden = True
 
2512
 
2455
2513
    def run(self):
2456
 
        assert False, "always fails"
 
2514
        raise AssertionError("always fails")
2457
2515
 
2458
2516
 
2459
2517
class cmd_help(Command):
2460
2518
    """Show help on a command or other topic.
2461
2519
 
2462
 
    For a list of all available commands, say 'bzr help commands'."""
 
2520
    For a list of all available commands, say 'bzr help commands'.
 
2521
    """
2463
2522
    takes_options = [Option('long', 'show help on all commands')]
2464
2523
    takes_args = ['topic?']
2465
2524
    aliases = ['?', '--help', '-?', '-h']
2466
2525
    
2467
2526
    @display_command
2468
2527
    def run(self, topic=None, long=False):
2469
 
        import help
 
2528
        import bzrlib.help
2470
2529
        if topic is None and long:
2471
2530
            topic = "commands"
2472
 
        help.help(topic)
 
2531
        bzrlib.help.help(topic)
2473
2532
 
2474
2533
 
2475
2534
class cmd_shell_complete(Command):
2476
2535
    """Show appropriate completions for context.
2477
2536
 
2478
 
    For a list of all available commands, say 'bzr shell-complete'."""
 
2537
    For a list of all available commands, say 'bzr shell-complete'.
 
2538
    """
2479
2539
    takes_args = ['context?']
2480
2540
    aliases = ['s-c']
2481
2541
    hidden = True
2489
2549
class cmd_fetch(Command):
2490
2550
    """Copy in history from another branch but don't merge it.
2491
2551
 
2492
 
    This is an internal method used for pull and merge."""
 
2552
    This is an internal method used for pull and merge.
 
2553
    """
2493
2554
    hidden = True
2494
2555
    takes_args = ['from_branch', 'to_branch']
2495
2556
    def run(self, from_branch, to_branch):
2502
2563
class cmd_missing(Command):
2503
2564
    """Show unmerged/unpulled revisions between two branches.
2504
2565
 
2505
 
    OTHER_BRANCH may be local or remote."""
 
2566
    OTHER_BRANCH may be local or remote.
 
2567
    """
2506
2568
    takes_args = ['other_branch?']
2507
2569
    takes_options = [Option('reverse', 'Reverse the order of revisions'),
2508
2570
                     Option('mine-only',