• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2009, Google Inc. All rights reserved.
2# Copyright (c) 2009 Apple Inc. All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met:
7#
8#     * Redistributions of source code must retain the above copyright
9# notice, this list of conditions and the following disclaimer.
10#     * Redistributions in binary form must reproduce the above
11# copyright notice, this list of conditions and the following disclaimer
12# in the documentation and/or other materials provided with the
13# distribution.
14#     * Neither the name of Google Inc. nor the names of its
15# contributors may be used to endorse or promote products derived from
16# this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29#
30# Python module for interacting with an SCM system (like SVN or Git)
31
32import logging
33import re
34import sys
35
36from webkitpy.common.system.executive import Executive, ScriptError
37from webkitpy.common.system.filesystem import FileSystem
38
39_log = logging.getLogger(__name__)
40
41
42# SCM methods are expected to return paths relative to self.checkout_root.
43class SCM:
44    def __init__(self, cwd, executive=None, filesystem=None):
45        self.cwd = cwd
46        self._executive = executive or Executive()
47        self._filesystem = filesystem or FileSystem()
48        self.checkout_root = self.find_checkout_root(self.cwd)
49
50    # A wrapper used by subclasses to create processes.
51    def _run(self, args, cwd=None, input=None, error_handler=None, return_exit_code=False, return_stderr=True, decode_output=True):
52        # FIXME: We should set cwd appropriately.
53        return self._executive.run_command(args,
54                           cwd=cwd,
55                           input=input,
56                           error_handler=error_handler,
57                           return_exit_code=return_exit_code,
58                           return_stderr=return_stderr,
59                           decode_output=decode_output)
60
61    # SCM always returns repository relative path, but sometimes we need
62    # absolute paths to pass to rm, etc.
63    def absolute_path(self, repository_relative_path):
64        return self._filesystem.join(self.checkout_root, repository_relative_path)
65
66    def _run_status_and_extract_filenames(self, status_command, status_regexp):
67        filenames = []
68        # We run with cwd=self.checkout_root so that returned-paths are root-relative.
69        for line in self._run(status_command, cwd=self.checkout_root).splitlines():
70            match = re.search(status_regexp, line)
71            if not match:
72                continue
73            # status = match.group('status')
74            filename = match.group('filename')
75            filenames.append(filename)
76        return filenames
77
78    @staticmethod
79    def _subclass_must_implement():
80        raise NotImplementedError("subclasses must implement")
81
82    @classmethod
83    def in_working_directory(cls, path, executive=None):
84        SCM._subclass_must_implement()
85
86    def find_checkout_root(self, path):
87        SCM._subclass_must_implement()
88
89    def add(self, path, return_exit_code=False):
90        self.add_list([path], return_exit_code)
91
92    def add_list(self, paths, return_exit_code=False):
93        self._subclass_must_implement()
94
95    def delete(self, path):
96        self.delete_list([path])
97
98    def delete_list(self, paths):
99        self._subclass_must_implement()
100
101    def move(self, origin, destination):
102        self._subclass_must_implement()
103
104    def exists(self, path):
105        self._subclass_must_implement()
106
107    def changed_files(self, git_commit=None):
108        self._subclass_must_implement()
109
110    def _added_files(self):
111        self._subclass_must_implement()
112
113    def _deleted_files(self):
114        self._subclass_must_implement()
115
116    def display_name(self):
117        self._subclass_must_implement()
118
119    def _head_svn_revision(self):
120        return self.svn_revision(self.checkout_root)
121
122    def svn_revision(self, path):
123        """Returns the latest svn revision found in the checkout."""
124        self._subclass_must_implement()
125
126    def timestamp_of_revision(self, path, revision):
127        self._subclass_must_implement()
128
129    def blame(self, path):
130        self._subclass_must_implement()
131
132    def has_working_directory_changes(self):
133        self._subclass_must_implement()
134
135    #--------------------------------------------------------------------------
136    # Subclasses must indicate if they support local commits,
137    # but the SCM baseclass will only call local_commits methods when this is true.
138    @staticmethod
139    def supports_local_commits():
140        SCM._subclass_must_implement()
141
142    def commit_locally_with_message(self, message, commit_all_working_directory_changes=True):
143        _log.error("Your source control manager does not support local commits.")
144        sys.exit(1)
145