• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (C) 2007 Apple Inc.  All rights reserved.
2#
3# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions
5# are met:
6#
7# 1.  Redistributions of source code must retain the above copyright
8#     notice, this list of conditions and the following disclaimer.
9# 2.  Redistributions in binary form must reproduce the above copyright
10#     notice, this list of conditions and the following disclaimer in the
11#     documentation and/or other materials provided with the distribution.
12# 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
13#     its contributors may be used to endorse or promote products derived
14#     from this software without specific prior written permission.
15#
16# THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
17# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19# DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
20# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27# Module to share code to work with various version control systems.
28
29use strict;
30use warnings;
31use File::Spec;
32use webkitdirs;
33
34BEGIN {
35   use Exporter   ();
36   our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
37   $VERSION     = 1.00;
38   @ISA         = qw(Exporter);
39   @EXPORT      = qw(&isGitDirectory &isGit &isSVNDirectory &isSVN &makeFilePathRelative);
40   %EXPORT_TAGS = ( );
41   @EXPORT_OK   = ();
42}
43
44our @EXPORT_OK;
45
46my $isGit;
47my $isSVN;
48my $gitBranch;
49my $isGitBranchBuild;
50
51sub isGitDirectory($)
52{
53    my ($dir) = @_;
54    return system("cd $dir && git rev-parse > /dev/null 2>&1") == 0;
55}
56
57sub isGit()
58{
59    return $isGit if defined $isGit;
60
61    $isGit = isGitDirectory(".");
62    return $isGit;
63}
64
65sub gitBranch()
66{
67    unless (defined $gitBranch) {
68        chomp($gitBranch = `git symbolic-ref -q HEAD`);
69        $gitBranch = "" if exitStatus($?);
70        $gitBranch =~ s#^refs/heads/##;
71        $gitBranch = "" if $gitBranch eq "master";
72    }
73
74    return $gitBranch;
75}
76
77sub isGitBranchBuild()
78{
79    my $branch = gitBranch();
80    chomp(my $override = `git config --bool branch.$branch.webKitBranchBuild`);
81    return 1 if $override eq "true";
82    return 0 if $override eq "false";
83
84    unless (defined $isGitBranchBuild) {
85        chomp(my $gitBranchBuild = `git config --bool core.webKitBranchBuild`);
86        $isGitBranchBuild = $gitBranchBuild eq "true";
87    }
88
89    return $isGitBranchBuild;
90}
91
92sub isSVNDirectory($)
93{
94    my ($dir) = @_;
95
96    return -d File::Spec->catdir($dir, ".svn");
97}
98
99sub isSVN()
100{
101    return $isSVN if defined $isSVN;
102
103    $isSVN = isSVNDirectory(".");
104    return $isSVN;
105}
106
107my $gitRoot;
108sub makeFilePathRelative($)
109{
110    my ($path) = @_;
111    return $path unless isGit();
112
113    unless (defined $gitRoot) {
114        chomp($gitRoot = `git rev-parse --show-cdup`);
115    }
116    return $gitRoot . $path;
117}
118
1191;
120