-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgit.inc.php
690 lines (588 loc) · 16.1 KB
/
git.inc.php
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
<?php
/*
Git manipulation and caching for Sausage Machine
Copyright (C) 2015 Gottfried Haider for PublishingLab
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@require_once('config.inc.php');
require_once('util.inc.php');
/**
* Return the path for a cache key
* @param $cache_key cache key
* @return string, without trailing slash
*/
function cache_dir($cache_key) {
return rtrim(config('content_dir', 'content'), '/') . '/cache/' . $cache_key;
}
/**
* Delete repositories in the cache that haven't been used recently
* @return true if successful, false if not
*/
function check_cache_lru() {
$max_cached = config('repo_max_cached', 0);
if ($max_cached == 0) {
// check disabled
return true;
}
$fns = @scandir(cache_dir());
if ($fns === false) {
// cache_dir might not yet exist
return true;
}
$success = true;
$cached = array();
foreach ($fns as $fn) {
if (in_array($fn, array('.', '..'))) {
continue;
}
if (!@is_dir(cache_dir($fn))) {
continue;
}
$mtime = @filemtime(cache_dir($fn));
if ($mtime === false) {
$success = false;
}
$cached[$fn] = $mtime;
}
// newer repos come first
arsort($cached);
// delete excess cached repositories
for ($i=$max_cached; $i < count($cached); $i++) {
$cache_key = array_keys($cached)[$i];
if (false === rm_recursive(cache_dir($cache_key))) {
$success = false;
}
}
return $success;
}
/**
* Make sure the content directory contains a cache and tmp subdirectory
* @return true if successful, false if not
*/
function check_content_dir() {
$content_dir = rtrim(config('content_dir', 'content'), '/');
if (!is_dir($content_dir . '/cache')) {
$old_umask = @umask(0000);
$ret = @mkdir($content_dir . '/cache');
@umask($old_umask);
if ($ret === false) {
return false;
}
}
if (!is_dir($content_dir . '/tmp')) {
$old_umask = @umask(0000);
$ret = @mkdir($content_dir . '/tmp');
@umask($old_umask);
if ($ret === false) {
return false;
}
}
return true;
}
/**
* Delete leftover copies of repositories in the tmp directory
* @return true if successful, false if not
*/
function check_tmp_dir_age() {
$max_age = config('temp_max_age', 0);
if ($max_age == 0) {
// check disabled
return true;
}
$fns = @scandir(tmp_dir());
if ($fns === false) {
// cache_dir might not yet exist
return true;
}
$success = true;
foreach ($fns as $fn) {
if (in_array($fn, array('.', '..'))) {
continue;
}
if (!@is_dir(tmp_dir($fn))) {
continue;
}
$mtime = @filemtime(tmp_dir($fn));
if ($mtime === false) {
$success = false;
continue;
}
// delete old temporary repositories
if ($max_age < time()-$mtime) {
if (false === rm_recursive(tmp_dir($fn))) {
$success = false;
}
}
}
return $success;
}
/**
* Get a private copy of a remote Git repository
*
* The private copy can be manipulated with the returned tmp key. Call
* release_repo() after it is no longer needed.
* @param $url Git (clone) URL
* @param $branch branch to check out (default is "master")
* @param $force_update force a fetch
* @return tmp key, or false if unsuccessful
*/
function get_repo($url, $branch = 'master', $force_update = false) {
$tmp_key = tmp_key();
// get a cached copy, currently on the master branch
$cache_key = get_repo_for_reading($url, $force_update);
if ($cache_key === false) {
return false;
}
// create files and directories as permissive as possible
$old_umask = @umask(0000);
// copy to tmp
if (false === cp_recursive(cache_dir($cache_key), tmp_dir($tmp_key))) {
// copying failed, remove again
rm_recursive(tmp_dir($tmp_key));
@umask($old_umask);
return false;
}
if ($branch === 'master') {
// XXX (later): revisit & document - IIRC the reason for this was that
// same of the permissions were off after a regular copy (w/ umask) and
// that running the git command somehow fix them?
$ret = repo_cleanup($tmp_key);
} else {
$ret = repo_checkout_branch($tmp_key, $branch);
// XXX (later): revisit - does this also need repo_cleanup()?
}
@umask($old_umask);
if ($ret === false) {
// copying failed, remove again
rm_recursive(tmp_dir($tmp_key));
return false;
}
// XXX (later): this could be done asynchronously
check_tmp_dir_age();
return $tmp_key;
}
/**
* Get a remote Git repo for reading only
*
* This will always return the default (master) branch. Don't call release_repo()
* together with this function.
* @param $url Git (clone) URL
* @param $force_update force a fetch
* @return cache key, or false if unsuccessful
*/
function get_repo_for_reading($url, $force_update = false) {
$cache_key = git_url_to_cache_key($url);
if ($cache_key === false) {
return false;
}
// make sure the content directory is set up
if (false === check_content_dir()) {
return false;
}
// serve from cache, if possible, or clone from remote
if (is_dir(cache_dir($cache_key))) {
if (false === repo_check_for_update($cache_key, $force_update)) {
return false;
}
} else {
if (false === repo_add_to_cache($url)) {
return false;
}
}
// XXX (later): this could be done asynchronously
check_cache_lru();
return $cache_key;
}
/**
* Convert a Git URL to a key to lookup the local cache
* @param $url Git (clone) URL
* @return string
*/
function git_url_to_cache_key($url) {
$pos = strpos($url, '://');
if ($pos === false) {
return false;
}
// strip protocol that might be variable
$key = substr($url, $pos+3);
// replace slashes
$key = str_replace('/', '-', $key);
// replace .git suffix that is optional with many services
if (substr($key, -4) == '.git') {
$key = substr($key, 0, -4);
}
return $key;
}
/**
* Release a repository after it is no longer being used
*
* Use this together with get_repo().
* @param $tmp_key tmp key
* @return true if successful, false if not
*/
function release_repo($tmp_key) {
return rm_recursive(tmp_dir($tmp_key));
}
/**
* Helper function to add a remote repository to the cache
*
* Called by get_repo_in_cache(). Assumes that the repository isn't yet cached.
* @param $url Git (clone) URL
* @return true if successful, false if not
*/
function repo_add_to_cache($url) {
$cache_key = git_url_to_cache_key($url);
if ($cache_key === false) {
return false;
}
$old_cwd = getcwd();
$old_umask = @umask(0000);
// create directory in cache
if (false === @mkdir(cache_dir($cache_key))) {
@umask($old_umask);
return false;
}
@chdir(cache_dir($cache_key));
// all repos in cache are on the master branch
@exec('git clone -b master ' . escapeshellarg($url) . ' . 2>&1', $out, $ret_val);
@umask($old_umask);
@chdir($old_cwd);
if ($ret_val !== 0) {
// cloning failed, remote directory in cache
rm_recursive(tmp_dir($cache_key));
return false;
} else {
return true;
}
}
/**
* Fetch the remote repository, if this hasn't been done recently, and checkout the remote master branch
* @param $cache_key cache key
* @param $force force a fetch
* @return true if succesful, false if not
*/
function repo_check_for_update($cache_key, $force = false) {
$mtime = @filemtime(cache_dir($cache_key));
if ($mtime === false) {
return false;
}
if (!$force && time()-$mtime < config('repo_cache_time')) {
// current version is recent enough
return true;
}
$old_cwd = getcwd();
$old_umask = @umask(0000);
// update file modification time for LRU
@touch(cache_dir($cache_key));
@chdir(cache_dir($cache_key));
@exec('git fetch --all 2>&1', $out, $ret_val);
if ($ret_val === 0) {
@exec('git reset --hard origin/master 2>&1', $out, $ret_val);
}
@umask($old_umask);
@chdir($old_cwd);
return ($ret_val === 0);
}
/**
* Switch the branch of a repository
* @param $tmp_key tmp key
* @param $branch branch to check out
* @return true if sucessful, false if not
*/
function repo_checkout_branch($tmp_key, $branch = 'master') {
$old_cwd = getcwd();
$old_umask = @umask(0000);
@chdir(tmp_dir($tmp_key));
@exec('git checkout -f -B ' . escapeshellarg($branch) . ' ' . escapeshellarg('origin/' . $branch) . ' 2>&1', $out, $ret_val);
@umask($old_umask);
@chdir($old_cwd);
return ($ret_val === 0);
}
/**
* Reset a repository to its original state
*
* This also removes uncommitted files.
* @param $tmp_key tmp key
* @return true if successful, false if not
*/
function repo_cleanup($tmp_key) {
$old_cwd = getcwd();
@chdir(tmp_dir($tmp_key));
@exec('git reset --hard 2>&1', $out, $ret_val);
if ($ret_val !== 0) {
@chdir($old_cwd);
return false;
}
@exec('git clean -f -x 2>&1', $out, $ret_val);
@chdir($old_cwd);
if ($ret_val !== 0) {
return false;
} else {
return true;
}
}
/**
* Commit changes to a Git repository
* @param $tmp_key tmp key
* @param $msg commit message
* @param $author commit author
* @return true if successful, false if not
*/
function repo_commit($tmp_key, $msg, $author = 'Git User <[email protected]>') {
$old_cwd = getcwd();
@chdir(tmp_dir($tmp_key));
@exec('git commit -a --author=' . escapeshellarg($author) . ' -m ' . escapeshellarg($msg) . ' --allow-empty 2>&1', $out, $ret_val);
@chdir($old_cwd);
if ($ret_val !== 0) {
return false;
} else {
return true;
}
}
function repo_get_all_files($repo_dir) {
$fns = list_files_recursive($repo_dir);
if ($fns === false) {
return false;
}
$ret = array();
foreach ($fns as $fn) {
// filter the actual git repository
if (strpos($fn, '.git/') !== false) {
continue;
}
// and .gitignore files
if (strpos($fn, '.gitignore') !== false) {
continue;
}
$ret[] = $fn;
}
return $ret;
}
/**
* Return the current commit (hash)
* @param $path path
* @return String or false if not sucessful
*/
function repo_get_current_commit($path) {
$old_cwd = getcwd();
@chdir($path);
@exec('git rev-parse HEAD 2>&1', $out, $ret_val);
@chdir($old_cwd);
if ($ret_val !== 0) {
return false;
} else {
return trim($out[0]);
}
}
/**
* Return all modified files in the working directory of a Git repository that were last modified after a given timestamp
* @param $tmp_key tmp key
* @param $after timestamp
* @return array of filenames, or false if unsuccessful
*/
function repo_get_modified_files_after($tmp_key, $after) {
$modified = repo_get_modified_files($tmp_key);
if ($modified === false) {
return false;
}
// check file modification times
$modified_after = array();
foreach ($modified as $fn) {
$mtime = @filemtime(tmp_dir($tmp_key) . '/' . $fn);
if ($mtime !== false && $after < $mtime) {
$modified_after[] = $fn;
}
}
return $modified_after;
}
/**
* Return all modified files in the working directory of a Git repository
* @param $tmp_key tmp key
* @return array of filenames, or false if unsuccessful
*/
function repo_get_modified_files($tmp_key) {
$old_cwd = getcwd();
@chdir(tmp_dir($tmp_key));
// outputs "modified" (seems to include: deleted) and "other" (e.g. untracked) files
@exec('git ls-files -m -o 2>&1', $out, $ret_val);
@chdir($old_cwd);
if ($ret_val !== 0) {
return false;
} else {
return $out;
}
}
/**
* Get remote URL of a Git repository
* @param $tmp_key tmp key
* @return String, or false if unsuccessful
*/
function repo_get_url($tmp_key) {
$old_cwd = getcwd();
@chdir(tmp_dir($tmp_key));
@exec('git config --get remote.origin.url 2>&1', $out, $ret_val);
@chdir($old_cwd);
if ($ret_val !== 0) {
return false;
} else {
return $out[0];
}
}
/**
* Return whether the working directory of a Git repository has modified files
* @param $tmp_key tmp key
* @return bool
*/
function repo_has_modified_files($tmp_key) {
$modified = repo_get_modified_files($tmp_key);
if ($modified === false) {
return false;
} else {
return (0 < count($modified));
}
}
/**
* Push all branches of a Git repository to a remote URL
* @param $tmp_key tmp key
* @param $url Git Push URL (e.g. ssh://[email protected]/...)
* @return true if successful, false if not
*/
function repo_push($tmp_key, $url) {
$old_cwd = getcwd();
@chdir(tmp_dir($tmp_key));
@exec('git push --all ' . escapeshellarg($url). ' 2>&1', $out, $ret_val);
@chdir($old_cwd);
if ($ret_val === 1) {
// not fast-forward, tell caller to fetch & try again
return NULL;
} else if ($ret_val !== 0) {
return false;
} else {
return true;
}
}
/**
* Reset the index of a Git repository to some earlier state
* @param $tmp_key tmp key
* @param $offset 1 rewinds to the second most recent commit
* @return true if successful, false if not
*/
function repo_rewind($tmp_key, $offset = 1) {
$old_cwd = getcwd();
@chdir(tmp_dir($tmp_key));
@exec('git reset --hard HEAD~' . intval($offset) . ' 2>&1', $out, $ret_val);
@chdir($old_cwd);
if ($ret_val !== 0) {
return false;
} else {
return true;
}
}
/**
* Add files to be committed
* @param $tmp_key tmp key
* @param $files array of filenames
* @return true if successful, false if not
*/
function repo_stage_files($tmp_key, $files = array()) {
$old_cwd = getcwd();
@chdir(tmp_dir($tmp_key));
$escaped_files = '';
foreach ($files as $file) {
if (!empty($escaped_files)) {
$escaped_files .= ' ';
}
$escaped_files .= escapeshellarg($file);
}
@exec('git add -f --ignore-errors ' . $escaped_files . ' 2>&1', $out, $ret_val);
@chdir($old_cwd);
if ($ret_val !== 0) {
return false;
} else {
return true;
}
}
/**
* Prevent the repository from being automatically cleaned after a set time
* @param $tmp_key tmp key
*/
function repo_touch($tmp_key) {
// update file modification time
@touch(tmp_dir($tmp_key));
}
/**
* Add a host to the webserver's known_hosts file
* @param $host hostname
* @param true if sucessful, false if not
*/
function server_add_known_host($host) {
// see also http://serverfault.com/questions/132970/can-i-automatically-add-a-new-host-to-known-hosts/316100#316100
@exec('ssh-keyscan ' . $host . ',' . gethostbyname($host) . ' >> ' . escapeshellarg(get_server_home_dir() . '/.ssh/known_hosts'), $out, $ret_val);
return ($ret_val === 0);
}
/**
* Generate a SSH key pair in the context of the webserver process, overwrites existing files
* @param $comment comment to add to the key
* @return true if successful, false if not
*/
function server_generate_ssh_key($comment = 'Created by Sausage Machine') {
@exec('echo -e "y\n" | ssh-keygen -q -t rsa -N "" -C ' . escapeshellarg($comment) . ' -f ' . escapeshellarg(get_server_home_dir() . '/.ssh/id_rsa') . ' 2>&1', $out, $ret_val);
return ($ret_val === 0);
}
/**
* Returns the SSH public key associated with the webserver process
* @return String if sucessful, false if not
*/
function server_get_ssh_public_key() {
return @trim(file_get_contents(get_server_home_dir() . '/.ssh/id_rsa.pub'));
}
/**
* Check if a host is in the webserver's known_hosts file
* @param $host hostname
* @return bool
*/
function server_has_known_host($host) {
@exec('ssh-keygen -f ' . escapeshellarg(get_server_home_dir() . '/.ssh/known_hosts') . ' -H -F ' . escapeshellarg($host), $out, $ret_val);
return (0 < count($out));
}
/**
* Return whether the webserver process has a SSH keypair associated with it
* @return bool
*/
function server_has_ssh_key() {
if (@is_file(get_server_home_dir() . '/.ssh/id_rsa') &&
@is_file(get_server_home_dir() . '/.ssh/id_rsa.pub')) {
return true;
} else {
return false;
}
}
/**
* Return the path for a tmp key
* @param $tmp_key tmp key
* @return string, without trailing slash
*/
function tmp_dir($tmp_key) {
return rtrim(config('content_dir', 'content'), '/') . '/tmp/' . $tmp_key;
}
/**
* Return a tmp key based on the current request's timestamp
* @return string tmp key
*/
function tmp_key() {
if (isset($_SERVER['REQUEST_TIME_FLOAT'])) {
// seems to be at least four decimal places
return strval(floor($_SERVER['REQUEST_TIME_FLOAT']*10000));
} else {
return strval(floor(microtime(true)*10000));
}
}