summaryrefslogtreecommitdiff
path: root/site_scons/site_tools/git_decider.py
blob: 1d56a302e17286b41cf771f82b2c8cad5cdb45d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Copyright 2016 MongoDB Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# If available, uses Git metadata to decide whether files are out of date.


def generate(env, **kwargs):

    # Grab the existing decider functions out of the environment
    # so we can invoke them when we can't use Git.
    base_decider = env.decide_target
    if base_decider != env.decide_source:
        raise Exception("Decider environment seems broken")

    from git import Git

    thisRepo = Git(env.Dir("#").abspath)
    currentGitState = thisRepo.ls_files("--stage")
    lines = currentGitState.split("\n")

    file_sha1_map = {}
    for line in lines:
        line_content = line.split()
        file_sha1_map[env.File(line_content[3]).path] = line_content[1]

    for m in thisRepo.ls_files("-m").split("\n"):
        if m:
            del file_sha1_map[env.File(m).path]

    def is_known_to_git(dependency):
        return str(dependency) in file_sha1_map

    def git_says_file_is_up_to_date(dependency, prev_ni):
        gitInfoForDep = file_sha1_map[str(dependency)]

        if prev_ni is None:
            dependency.get_ninfo().csig = gitInfoForDep
            return False

        if not (hasattr(prev_ni, "csig")):
            prev_ni.csig = gitInfoForDep

        result = gitInfoForDep == prev_ni.csig
        return result

    def MongoGitDecider(dependency, target, prev_ni, node):
        if not is_known_to_git(dependency):
            return base_decider(dependency, target, prev_ni, node)
        return not git_says_file_is_up_to_date(dependency, prev_ni)

    env.Decider(MongoGitDecider)


def exists(env):
    try:
        from git import Git

        Git(env.Dir("#").abspath).ls_files("--stage")
        return True
    except:
        return False