git.py revision 12244
19651SAndreas.Sandberg@ARM.com# Copyright (c) 2013, 2015-2017 ARM Limited
29651SAndreas.Sandberg@ARM.com# All rights reserved.
39651SAndreas.Sandberg@ARM.com#
49651SAndreas.Sandberg@ARM.com# The license below extends only to copyright in the software and shall
59651SAndreas.Sandberg@ARM.com# not be construed as granting a license to any other intellectual
69651SAndreas.Sandberg@ARM.com# property including but not limited to intellectual property relating
79651SAndreas.Sandberg@ARM.com# to a hardware implementation of the functionality of the software
89651SAndreas.Sandberg@ARM.com# licensed hereunder.  You may use the software subject to the license
99651SAndreas.Sandberg@ARM.com# terms below provided that you ensure that this notice is replicated
109651SAndreas.Sandberg@ARM.com# unmodified and in its entirety in all distributions of the software,
119651SAndreas.Sandberg@ARM.com# modified or unmodified, in source code or in binary form.
129651SAndreas.Sandberg@ARM.com#
139651SAndreas.Sandberg@ARM.com# Copyright (c) 2011 Advanced Micro Devices, Inc.
149651SAndreas.Sandberg@ARM.com# Copyright (c) 2009 The Hewlett-Packard Development Company
159651SAndreas.Sandberg@ARM.com# Copyright (c) 2004-2005 The Regents of The University of Michigan
169651SAndreas.Sandberg@ARM.com# All rights reserved.
179651SAndreas.Sandberg@ARM.com#
189651SAndreas.Sandberg@ARM.com# Redistribution and use in source and binary forms, with or without
199651SAndreas.Sandberg@ARM.com# modification, are permitted provided that the following conditions are
209651SAndreas.Sandberg@ARM.com# met: redistributions of source code must retain the above copyright
219651SAndreas.Sandberg@ARM.com# notice, this list of conditions and the following disclaimer;
229651SAndreas.Sandberg@ARM.com# redistributions in binary form must reproduce the above copyright
239651SAndreas.Sandberg@ARM.com# notice, this list of conditions and the following disclaimer in the
249651SAndreas.Sandberg@ARM.com# documentation and/or other materials provided with the distribution;
259651SAndreas.Sandberg@ARM.com# neither the name of the copyright holders nor the names of its
269651SAndreas.Sandberg@ARM.com# contributors may be used to endorse or promote products derived from
279651SAndreas.Sandberg@ARM.com# this software without specific prior written permission.
289651SAndreas.Sandberg@ARM.com#
299651SAndreas.Sandberg@ARM.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
309651SAndreas.Sandberg@ARM.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
319651SAndreas.Sandberg@ARM.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
329651SAndreas.Sandberg@ARM.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
339651SAndreas.Sandberg@ARM.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
349651SAndreas.Sandberg@ARM.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
359651SAndreas.Sandberg@ARM.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
369651SAndreas.Sandberg@ARM.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
379651SAndreas.Sandberg@ARM.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
389651SAndreas.Sandberg@ARM.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
399651SAndreas.Sandberg@ARM.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
409651SAndreas.Sandberg@ARM.com
419651SAndreas.Sandberg@ARM.comimport os
429651SAndreas.Sandberg@ARM.com
439651SAndreas.Sandberg@ARM.comimport gem5_scons.util
449651SAndreas.Sandberg@ARM.comfrom m5.util import readCommand
459651SAndreas.Sandberg@ARM.com
469651SAndreas.Sandberg@ARM.comgit_style_message = """
479651SAndreas.Sandberg@ARM.comYou're missing the gem5 style or commit message hook. These hooks help
489651SAndreas.Sandberg@ARM.comto ensure that your code follows gem5's style rules on git commit.
499760Sandreas@sandberg.pp.seThis script will now install the hook in your .git/hooks/ directory.
509651SAndreas.Sandberg@ARM.comPress enter to continue, or ctrl-c to abort: """
519651SAndreas.Sandberg@ARM.com
529683Sandreas@sandberg.pp.sedef install_style_hooks(env):
539753Sandreas@sandberg.pp.se    try:
549651SAndreas.Sandberg@ARM.com        gitdir = env.Dir(readCommand(
559651SAndreas.Sandberg@ARM.com            ["git", "rev-parse", "--git-dir"]).strip("\n"))
569651SAndreas.Sandberg@ARM.com    except Exception, e:
579651SAndreas.Sandberg@ARM.com        print "Warning: Failed to find git repo directory: %s" % e
589651SAndreas.Sandberg@ARM.com        return
599651SAndreas.Sandberg@ARM.com
609651SAndreas.Sandberg@ARM.com    git_hooks = gitdir.Dir("hooks")
619753Sandreas@sandberg.pp.se    def hook_exists(hook_name):
629753Sandreas@sandberg.pp.se        hook = git_hooks.File(hook_name)
639651SAndreas.Sandberg@ARM.com        return hook.exists()
649651SAndreas.Sandberg@ARM.com
659651SAndreas.Sandberg@ARM.com    def hook_install(hook_name, script):
6610073Sandreas@sandberg.pp.se        hook = git_hooks.File(hook_name)
679651SAndreas.Sandberg@ARM.com        if hook.exists():
689651SAndreas.Sandberg@ARM.com            print "Warning: Can't install %s, hook already exists." % hook_name
699651SAndreas.Sandberg@ARM.com            return
709651SAndreas.Sandberg@ARM.com
719651SAndreas.Sandberg@ARM.com        if hook.islink():
729651SAndreas.Sandberg@ARM.com            print "Warning: Removing broken symlink for hook %s." % hook_name
739651SAndreas.Sandberg@ARM.com            os.unlink(hook.get_abspath())
749652SAndreas.Sandberg@ARM.com
759652SAndreas.Sandberg@ARM.com        if not git_hooks.exists():
769651SAndreas.Sandberg@ARM.com            os.mkdir(git_hooks.get_abspath())
779651SAndreas.Sandberg@ARM.com            git_hooks.clear()
789651SAndreas.Sandberg@ARM.com
799651SAndreas.Sandberg@ARM.com        abs_symlink_hooks = git_hooks.islink() and \
809892Sandreas@sandberg.pp.se            os.path.isabs(os.readlink(git_hooks.get_abspath()))
819655SAndreas.Sandberg@ARM.com
829752Sandreas@sandberg.pp.se        # Use a relative symlink if the hooks live in the source directory,
839753Sandreas@sandberg.pp.se        # and the hooks directory is not a symlink to an absolute path.
849752Sandreas@sandberg.pp.se        if hook.is_under(env.root) and not abs_symlink_hooks:
859651SAndreas.Sandberg@ARM.com            script_path = os.path.relpath(
869651SAndreas.Sandberg@ARM.com                os.path.realpath(script.get_abspath()),
879651SAndreas.Sandberg@ARM.com                os.path.realpath(hook.Dir(".").get_abspath()))
889651SAndreas.Sandberg@ARM.com        else:
899651SAndreas.Sandberg@ARM.com            script_path = script.get_abspath()
909651SAndreas.Sandberg@ARM.com
919651SAndreas.Sandberg@ARM.com        try:
929651SAndreas.Sandberg@ARM.com            os.symlink(script_path, hook.get_abspath())
939651SAndreas.Sandberg@ARM.com        except:
949651SAndreas.Sandberg@ARM.com            print "Error updating git %s hook" % hook_name
959651SAndreas.Sandberg@ARM.com            raise
969651SAndreas.Sandberg@ARM.com
979651SAndreas.Sandberg@ARM.com    if hook_exists("pre-commit") and hook_exists("commit-msg"):
989651SAndreas.Sandberg@ARM.com        return
999651SAndreas.Sandberg@ARM.com
1009651SAndreas.Sandberg@ARM.com    print git_style_message,
1019651SAndreas.Sandberg@ARM.com    try:
1029651SAndreas.Sandberg@ARM.com        raw_input()
1039651SAndreas.Sandberg@ARM.com    except:
1049651SAndreas.Sandberg@ARM.com        print "Input exception, exiting scons.\n"
1059651SAndreas.Sandberg@ARM.com        sys.exit(1)
1069651SAndreas.Sandberg@ARM.com
1079651SAndreas.Sandberg@ARM.com    git_style_script = env.root.Dir("util").File("git-pre-commit.py")
1089651SAndreas.Sandberg@ARM.com    git_msg_script = env.root.Dir("ext").File("git-commit-msg")
1099651SAndreas.Sandberg@ARM.com
1109651SAndreas.Sandberg@ARM.com    hook_install("pre-commit", git_style_script)
1119651SAndreas.Sandberg@ARM.com    hook_install("commit-msg", git_msg_script)
1129651SAndreas.Sandberg@ARM.com
1139651SAndreas.Sandberg@ARM.comdef generate(env):
1149651SAndreas.Sandberg@ARM.com    if exists(env) and not gem5_scons.util.ignore_style():
1159651SAndreas.Sandberg@ARM.com        install_style_hooks(env)
1169651SAndreas.Sandberg@ARM.com
1179651SAndreas.Sandberg@ARM.comdef exists(env):
1189651SAndreas.Sandberg@ARM.com    return env.Entry('#.git').exists()
1199651SAndreas.Sandberg@ARM.com