1# Copyright 2016 MongoDB Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15
16# If available, uses Git metadata to decide whether files are out of date.
17
18def generate(env, **kwargs):
19
20    # Grab the existing decider functions out of the environment
21    # so we can invoke them when we can't use Git.
22    base_decider = env.decide_target
23    if (base_decider != env.decide_source):
24        raise Exception("Decider environment seems broken")
25
26    from git import Git
27    thisRepo = Git(env.Dir('#').abspath)
28    currentGitState = thisRepo.ls_files('--stage')
29    lines = currentGitState.split('\n')
30
31    file_sha1_map = {}
32    for line in lines:
33        line_content = line.split()
34        file_sha1_map[env.File(line_content[3]).path] = line_content[1]
35
36    for m in thisRepo.ls_files('-m').split('\n'):
37        if (m):
38            del file_sha1_map[env.File(m).path]
39
40    def is_known_to_git(dependency):
41        return str(dependency) in file_sha1_map
42
43    def git_says_file_is_up_to_date(dependency, prev_ni):
44        gitInfoForDep = file_sha1_map[str(dependency)]
45
46        if prev_ni is None:
47            dependency.get_ninfo().csig = gitInfoForDep
48            return False
49
50        if not(hasattr(prev_ni, 'csig')):
51            prev_ni.csig = gitInfoForDep
52
53        result = gitInfoForDep == prev_ni.csig
54        return result
55
56    def MongoGitDecider(dependency, target, prev_ni):
57        if not is_known_to_git(dependency):
58            return base_decider(dependency, target, prev_ni)
59        return not git_says_file_is_up_to_date(dependency, prev_ni)
60
61    env.Decider(MongoGitDecider)
62
63def exists(env):
64    try:
65        from git import Git
66        Git(env.Dir('#').abspath).ls_files('--stage')
67        return True
68    except:
69        return False
70