1 # Copyright (c) 2009, 2010, 2011 Google Inc. All rights reserved.
2 # Copyright (c) 2009 Apple Inc. All rights reserved.
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
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
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.
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.
30 from webkitpy.common.system.filesystem import FileSystem
31 from webkitpy.common.system.executive import Executive
33 from webkitpy.common.system.deprecated_logging import log
39 class SCMDetector(object):
40 def __init__(self, filesystem, executive):
41 self._filesystem = filesystem
42 self._executive = executive
44 def default_scm(self, patch_directories=None):
45 """Return the default SCM object as determined by the CWD and running code.
47 Returns the default SCM object for the current working directory; if the
48 CWD is not in a checkout, then we attempt to figure out if the SCM module
49 itself is part of a checkout, and return that one. If neither is part of
50 a checkout, None is returned.
52 cwd = self._filesystem.getcwd()
53 scm_system = self.detect_scm_system(cwd, patch_directories)
55 script_directory = self._filesystem.dirname(self._filesystem.path_to_module(self.__module__))
56 scm_system = self.detect_scm_system(script_directory, patch_directories)
58 log("The current directory (%s) is not a WebKit checkout, using %s" % (cwd, scm_system.checkout_root))
60 raise Exception("FATAL: Failed to determine the SCM system for either %s or %s" % (cwd, script_directory))
63 def detect_scm_system(self, path, patch_directories=None):
64 absolute_path = self._filesystem.abspath(path)
66 if patch_directories == []:
67 patch_directories = None
69 if SVN.in_working_directory(absolute_path):
70 return SVN(cwd=absolute_path, patch_directories=patch_directories, filesystem=self._filesystem, executive=self._executive)
72 if Git.in_working_directory(absolute_path):
73 return Git(cwd=absolute_path, filesystem=self._filesystem, executive=self._executive)
78 # FIXME: These free functions are all deprecated:
80 def detect_scm_system(path, patch_directories=None):
81 return SCMDetector(FileSystem(), Executive()).detect_scm_system(path, patch_directories)