doorstop.core.vcs   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
eloc 32
dl 0
loc 57
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A find_root() 0 23 3
A load() 0 8 3
1
# SPDX-License-Identifier: LGPL-3.0-only
2
3 1
"""Interfaces to version control systems."""
4 1
5
import logging
6 1
import os
7 1
8 1
from doorstop import common
9
from doorstop.common import DoorstopError
10 1
from doorstop.core.vcs import git, mercurial, mockvcs, subversion, veracity
11 1
12
DEFAULT = mockvcs.WorkingCopy
13
DIRECTORIES = {
14
    git.WorkingCopy.DIRECTORY: git.WorkingCopy,
15
    subversion.WorkingCopy.DIRECTORY: subversion.WorkingCopy,
16
    veracity.WorkingCopy.DIRECTORY: veracity.WorkingCopy,
17
    mercurial.WorkingCopy.DIRECTORY: mercurial.WorkingCopy,
18
    DEFAULT.DIRECTORY: DEFAULT,
19 1
}
20
21
log = common.logger(__name__)
22 1
23
24
def find_root(cwd):
25
    """Find the root of the working copy.
26
27
    :param cwd: current working directory
28
29
    :raises: :class:`doorstop.common.DoorstopError` if the root cannot be found
30
31
    :return: path to root of working copy
32 1
33
    """
34 1
    path = cwd
35 1
36 1
    log.debug("looking for working copy from {}...".format(path))
37 1
    log.debug("options: {}".format(', '.join([d for d in DIRECTORIES])))
38 1
    while not any(d in DIRECTORIES for d in os.listdir(path)):
39 1
        parent = os.path.dirname(path)
40 1
        if path == parent:
41
            msg = "no working copy found from: {}".format(cwd)
42 1
            raise DoorstopError(msg)
43
        path = parent
44 1
45 1
    log.debug("found working copy: {}".format(path))
46
    return path
47
48 1
49
def load(path):
50 1
    """Return a working copy for the specified path."""
51 1
    for directory in os.listdir(path):
52 1
        if directory in DIRECTORIES:
53
            return DIRECTORIES[directory](path)  # type: ignore
54 1
55 1
    log.warning("no working copy found at: {}".format(path))
56
    return DEFAULT(path)
57