git.py revision 12244
1# Copyright (c) 2013, 2015-2017 ARM Limited
2# All rights reserved.
3#
4# The license below extends only to copyright in the software and shall
5# not be construed as granting a license to any other intellectual
6# property including but not limited to intellectual property relating
7# to a hardware implementation of the functionality of the software
8# licensed hereunder.  You may use the software subject to the license
9# terms below provided that you ensure that this notice is replicated
10# unmodified and in its entirety in all distributions of the software,
11# modified or unmodified, in source code or in binary form.
12#
13# Copyright (c) 2011 Advanced Micro Devices, Inc.
14# Copyright (c) 2009 The Hewlett-Packard Development Company
15# Copyright (c) 2004-2005 The Regents of The University of Michigan
16# All rights reserved.
17#
18# Redistribution and use in source and binary forms, with or without
19# modification, are permitted provided that the following conditions are
20# met: redistributions of source code must retain the above copyright
21# notice, this list of conditions and the following disclaimer;
22# redistributions in binary form must reproduce the above copyright
23# notice, this list of conditions and the following disclaimer in the
24# documentation and/or other materials provided with the distribution;
25# neither the name of the copyright holders nor the names of its
26# contributors may be used to endorse or promote products derived from
27# this software without specific prior written permission.
28#
29# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40
41import os
42
43import gem5_scons.util
44from m5.util import readCommand
45
46git_style_message = """
47You're missing the gem5 style or commit message hook. These hooks help
48to ensure that your code follows gem5's style rules on git commit.
49This script will now install the hook in your .git/hooks/ directory.
50Press enter to continue, or ctrl-c to abort: """
51
52def install_style_hooks(env):
53    try:
54        gitdir = env.Dir(readCommand(
55            ["git", "rev-parse", "--git-dir"]).strip("\n"))
56    except Exception, e:
57        print "Warning: Failed to find git repo directory: %s" % e
58        return
59
60    git_hooks = gitdir.Dir("hooks")
61    def hook_exists(hook_name):
62        hook = git_hooks.File(hook_name)
63        return hook.exists()
64
65    def hook_install(hook_name, script):
66        hook = git_hooks.File(hook_name)
67        if hook.exists():
68            print "Warning: Can't install %s, hook already exists." % hook_name
69            return
70
71        if hook.islink():
72            print "Warning: Removing broken symlink for hook %s." % hook_name
73            os.unlink(hook.get_abspath())
74
75        if not git_hooks.exists():
76            os.mkdir(git_hooks.get_abspath())
77            git_hooks.clear()
78
79        abs_symlink_hooks = git_hooks.islink() and \
80            os.path.isabs(os.readlink(git_hooks.get_abspath()))
81
82        # Use a relative symlink if the hooks live in the source directory,
83        # and the hooks directory is not a symlink to an absolute path.
84        if hook.is_under(env.root) and not abs_symlink_hooks:
85            script_path = os.path.relpath(
86                os.path.realpath(script.get_abspath()),
87                os.path.realpath(hook.Dir(".").get_abspath()))
88        else:
89            script_path = script.get_abspath()
90
91        try:
92            os.symlink(script_path, hook.get_abspath())
93        except:
94            print "Error updating git %s hook" % hook_name
95            raise
96
97    if hook_exists("pre-commit") and hook_exists("commit-msg"):
98        return
99
100    print git_style_message,
101    try:
102        raw_input()
103    except:
104        print "Input exception, exiting scons.\n"
105        sys.exit(1)
106
107    git_style_script = env.root.Dir("util").File("git-pre-commit.py")
108    git_msg_script = env.root.Dir("ext").File("git-commit-msg")
109
110    hook_install("pre-commit", git_style_script)
111    hook_install("commit-msg", git_msg_script)
112
113def generate(env):
114    if exists(env) and not gem5_scons.util.ignore_style():
115        install_style_hooks(env)
116
117def exists(env):
118    return env.Entry('#.git').exists()
119