~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/builtins.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-11-02 07:51:26 UTC
  • mfrom: (2111.1.1 32054)
  • Revision ID: pqm@pqm.ubuntu.com-20061102075126-af381cad72be3c1e
(edward,mbp) #32054 store message if commit fails

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 (
1802
1803
                StrictCommitFailed)
1803
1804
        from bzrlib.msgeditor import edit_commit_message, \
1804
1805
                make_commit_message_template
1805
 
        from tempfile import TemporaryFile
1806
1806
 
1807
1807
        # TODO: Need a blackbox test for invoking the external editor; may be
1808
1808
        # slightly problematic to run this cross-platform.
1809
1809
 
1810
1810
        # TODO: do more checks that the commit will succeed before 
1811
1811
        # 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
1812
        tree, selected_list = tree_files(selected_list)
1816
1813
        if selected_list == ['']:
1817
1814
            # workaround - commit of root of tree should be exactly the same
1840
1837
            reporter = ReportCommitToLog()
1841
1838
        else:
1842
1839
            reporter = NullCommitReporter()
1843
 
        
 
1840
 
 
1841
        msgfilename = self._save_commit_message(message, tree.basedir)
1844
1842
        try:
1845
1843
            tree.commit(message, specific_files=selected_list,
1846
1844
                        allow_pointless=unchanged, strict=strict, local=local,
1847
1845
                        reporter=reporter)
 
1846
            if msgfilename is not None:
 
1847
                try:
 
1848
                    os.unlink(msgfilename)
 
1849
                except IOError, e:
 
1850
                    warning("failed to unlink %s: %s; ignored", msgfilename, e)
1848
1851
        except PointlessCommit:
1849
1852
            # FIXME: This should really happen before the file is read in;
1850
1853
            # perhaps prepare the commit; get the message; then actually commit
1851
 
            raise errors.BzrCommandError("no changes to commit."
1852
 
                                         " use --unchanged to commit anyhow")
 
1854
            if msgfilename is not None:
 
1855
                raise errors.BzrCommandError("no changes to commit."
 
1856
                                  " use --unchanged to commit anyhow\n"
 
1857
                                  "Commit message saved. To reuse the message,"
 
1858
                                  " do\nbzr commit --file " + msgfilename)
 
1859
            else:
 
1860
                raise errors.BzrCommandError("no changes to commit."
 
1861
                                  " use --unchanged to commit anyhow")
1853
1862
        except ConflictsInTree:
1854
 
            raise errors.BzrCommandError("Conflicts detected in working tree.  "
1855
 
                'Use "bzr conflicts" to list, "bzr resolve FILE" to resolve.')
 
1863
            if msgfilename is not None:
 
1864
                raise errors.BzrCommandError('Conflicts detected in working '
 
1865
                    'tree.  Use "bzr conflicts" to list, "bzr resolve FILE" to'
 
1866
                    ' resolve.\n'
 
1867
                    'Commit message saved. To reuse the message,'
 
1868
                    ' do\nbzr commit --file ' + msgfilename)
 
1869
            else:
 
1870
                raise errors.BzrCommandError('Conflicts detected in working '
 
1871
                    'tree.  Use "bzr conflicts" to list, "bzr resolve FILE" to'
 
1872
                    ' resolve.')
1856
1873
        except StrictCommitFailed:
1857
 
            raise errors.BzrCommandError("Commit refused because there are unknown "
1858
 
                                         "files in the working tree.")
 
1874
            if msgfilename is not None:
 
1875
                raise errors.BzrCommandError("Commit refused because there are"
 
1876
                                  " unknown files in the working tree.\n"
 
1877
                                  "Commit message saved. To reuse the message,"
 
1878
                                  " do\nbzr commit --file " + msgfilename)
 
1879
            else:
 
1880
                raise errors.BzrCommandError("Commit refused because there are"
 
1881
                                  " unknown files in the working tree.")
1859
1882
        except errors.BoundBranchOutOfDate, e:
1860
 
            raise errors.BzrCommandError(str(e) + "\n"
 
1883
            if msgfilename is not None:
 
1884
                raise errors.BzrCommandError(str(e) + "\n"
 
1885
                'To commit to master branch, run update and then commit.\n'
 
1886
                'You can also pass --local to commit to continue working '
 
1887
                'disconnected.\n'
 
1888
                'Commit message saved. To reuse the message,'
 
1889
                ' do\nbzr commit --file ' + msgfilename)
 
1890
            else:
 
1891
                raise errors.BzrCommandError(str(e) + "\n"
1861
1892
                'To commit to master branch, run update and then commit.\n'
1862
1893
                'You can also pass --local to commit to continue working '
1863
1894
                'disconnected.')
1864
1895
 
 
1896
    def _save_commit_message(self, message, basedir):
 
1897
        # save the commit message and only unlink it if the commit was
 
1898
        # successful
 
1899
        msgfilename = None
 
1900
        try:
 
1901
            tmp_fileno, msgfilename = tempfile.mkstemp(prefix='bzr-commit-',
 
1902
                                                       dir=basedir)
 
1903
        except OSError:
 
1904
            try:
 
1905
                # No access to working dir, try $TMP
 
1906
                tmp_fileno, msgfilename = tempfile.mkstemp(prefix='bzr-commit-')
 
1907
            except OSError:
 
1908
                # We can't create a temp file, try to work without it
 
1909
                return None
 
1910
        try:
 
1911
            os.write(tmp_fileno, message.encode(bzrlib.user_encoding, 'replace'))
 
1912
        finally:
 
1913
            os.close(tmp_fileno)
 
1914
        return msgfilename
 
1915
 
 
1916
 
1865
1917
class cmd_check(Command):
1866
1918
    """Validate consistency of branch history.
1867
1919