-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit-msg
More file actions
153 lines (130 loc) · 4.76 KB
/
Copy pathcommit-msg
File metadata and controls
153 lines (130 loc) · 4.76 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/bin/env php
<?php
// Define our Directory Seperator
define('DS', DIRECTORY_SEPARATOR);
// Path to the index folder
$path = dirname(dirname(dirname(__FILE__)));
// Version file
$index_file = realpath($path . DS .'index.php');
// Get our arguments
$message = file_get_contents($argv[1]);
// Do our processing
process($message);
// Exit 0, telling Git there was no errors, and to continue
exit(0);
// Our main processing function!
function process($message)
{
global $path, $index_file;
if (strlen($message) < 5)
{
echo 'A commit message at least five characters';
exit(1);
}
// Make sure we have a new build number
$line = substr($message,0,5);
if(!preg_match('/\[([0-9]+)\]/', $line, $output))
{
echo 'No build number found. Please correct the commit message by including the build number "[100]"';
exit(1);
}
// Get the contents from our file
$index = file_get_contents($index_file);
// Match the cms build
if(preg_match('/CMS_BUILD\', ([0-9]+)/', $index, $matches))
{
// Update the commit message and index file
$version = $matches[1] + 1;
// Make sure versions match!
if($version != $output[1])
{
echo 'Commit message version does NOT match the index.php version (+1)!';
exit(1);
}
// Get a list of added/changed/removed files for the commit.info page
exec('git diff --cached --name-status', $files);
// Create 3 output arrays! We want to add new files first, then proccess modifed, and finally removed files
$added = $removed = $modified = array();
// make a fancy array for the updater
foreach($files as $mod)
{
if(strpos($mod, "\t"))
{
$file = explode("\t", $mod);
// make sure the file is not null
if(isset($file[0]) && isset($file[1]))
{
// Skip the index file
if( $file[1] == 'index.php' ) continue;
// Do we have a strange 3rd char?
if(isset($file[2]))
{
$array = array('status' => $file[0], 'file' => $file[1], 'additional' => $file[2]);
}
else
{
$array = array('status' => $file[0], 'file' => $file[1]);
}
// Add to the corrisponding array, and double check for syntax errors in files
$c = strtoupper( substr($file[0], 0, 1) );
switch( $c )
{
case "A":
checkSyntax($file[1]);
$added[] = $array;
break;
case "D":
$removed[] = $array;
break;
case "M":
checkSyntax($file[1]);
$modified[] = $array;
break;
}
}
}
}
// Combine the arrays
$modified[] = array('status' => "M", 'file' => 'index.php');
$array = array_merge($added, $modified, $removed);
// Write to the index file, the new version
$contents = str_replace($matches[1], $version, $matches[0]);
$write = file_put_contents($index_file, str_replace($matches[0], $contents, $index));
// Make sure the write is successful!
if(!$write)
{
echo 'Was unable to write the new build number in the index.php!';
exit(1);
}
// Now tell GIT to add the index.php modification to this commit
exec('git add index.php', $output);
// Write the commit contents to the commit info file
if(!empty($array))
{
file_put_contents($path . DS .'commit.info', json_encode( $array ));
exec('git add commit.info', $output);
}
}
else
{
echo 'No build number found. Please correct the commit message by including the build number "[100]"';
exit(1);
}
}
// Function used to check the syntax of each file
function checkSyntax($fileName)
{
global $path;
// Only check php files!
$parts = explode('.', $fileName);
if(!is_array($parts) || (end($parts) != 'php')) return;
// Get the realpath to the file
$file = realpath( $path . DS . $fileName );
exec('php -l ' . $file, $output, $failed);
if($failed)
{
echo "\n\nSyntax error in $fileName: " . $output[1];
exit(1);
}
}
?>