1# Copyright (c) 2009, 2010, 2011 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
30import logging
31
32from webkitpy.common.system.filesystem import FileSystem
33from webkitpy.common.system.executive import Executive
34
35from .svn import SVN
36from .git import Git
37
38_log = logging.getLogger(__name__)
39
40
41class SCMDetector(object):
42    def __init__(self, filesystem, executive):
43        self._filesystem = filesystem
44        self._executive = executive
45
46    def default_scm(self, patch_directories=None):
47        """Return the default SCM object as determined by the CWD and running code.
48
49        Returns the default SCM object for the current working directory; if the
50        CWD is not in a checkout, then we attempt to figure out if the SCM module
51        itself is part of a checkout, and return that one. If neither is part of
52        a checkout, None is returned.
53        """
54        cwd = self._filesystem.getcwd()
55        scm_system = self.detect_scm_system(cwd, patch_directories)
56        if not scm_system:
57            script_directory = self._filesystem.dirname(self._filesystem.path_to_module(self.__module__))
58            scm_system = self.detect_scm_system(script_directory, patch_directories)
59            if scm_system:
60                _log.info("The current directory (%s) is not a WebKit checkout, using %s" % (cwd, scm_system.checkout_root))
61            else:
62                raise Exception("FATAL: Failed to determine the SCM system for either %s or %s" % (cwd, script_directory))
63        return scm_system
64
65    def detect_scm_system(self, path, patch_directories=None):
66        absolute_path = self._filesystem.abspath(path)
67
68        if patch_directories == []:
69            patch_directories = None
70
71        if SVN.in_working_directory(absolute_path, executive=self._executive):
72            return SVN(cwd=absolute_path, patch_directories=patch_directories, filesystem=self._filesystem, executive=self._executive)
73
74        if Git.in_working_directory(absolute_path, executive=self._executive):
75            return Git(cwd=absolute_path, filesystem=self._filesystem, executive=self._executive)
76
77        return None
78
79
80# FIXME: These free functions are all deprecated:
81
82def detect_scm_system(path, patch_directories=None):
83    return SCMDetector(FileSystem(), Executive()).detect_scm_system(path, patch_directories)
84