-
Notifications
You must be signed in to change notification settings - Fork 22
/
git-webcommit.php
executable file
·1359 lines (1052 loc) · 35.4 KB
/
git-webcommit.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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
error_reporting (E_ALL);
/// settings ///
// configure the list of repositories, only one support at the moment
$repos = Array ('/tmp/git-webcommit');
// set the default repository, you probably want to keep it set to 0
$defaultrepo = 0;
// configure the authentication method:
$authmethod = 'httpbasic';
// $authmethod = 'htpasswd';
// $authmethod = 'none';
// when using htpasswd, the 'pass' entry isn't needed
// 'name' and 'email' are required.
$auth = Array (
'testuser2' => Array ('pass' => 'a94a8fe5ccb19ba61c4c0873d391e987982fbbd3', 'name' => 'Test User', 'email' => 'test@somewhere')
);
// passwords are sha1 hashes, uncomment next line to create the password hash:
// exit (sha1 ('my pass'));
// author for when you don't use authentication
$author = $auth ['testuser2']['name'] . '<' . $auth ['testuser2']['email'] . '>'; // 'firstname lastname <email-address>'
// $author = '';
$title = '';
$enable_stats = false; // not available yet
$disable_push_pull = false;
$debug = false;
// $debug = true;
$gitpath = 'git';
$diffpath = 'diff';
if (file_exists (dirname (__FILE__) . '/config-git-webcommit.php'))
include (dirname (__FILE__) . '/config-git-webcommit.php');
/// main ///
if ($authmethod === 'httpbasic')
$author = handle_basic_auth ();
elseif ($authmethod === 'htpasswd')
$author = handle_htpasswd_auth ();
@ob_end_clean ();
flush ();
$dir = $repos [$defaultrepo];
if (!chdir ($dir))
exit ('directory not found: '.$dir);
$_handles = Array ();
$_handlecount = 0;
$md5_empty_string = 'd41d8cd98f00b204e9800998ecf8427e';
$sha1_empty_string = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';
$somethingstaged = false;
echo html_header ();
if ($_SERVER ['REQUEST_METHOD'] == 'POST') {
if (isset ($_POST ['commit_message']))
$commit_message = $_POST ['commit_message'];
else
$commit_message = '';
debug ($_POST);
if (isset ($_POST ['change_staged']) && $_POST ['change_staged'] && isset ($_POST ['statushash']) && $_POST ['statushash'])
handle_change_staged_req ();
elseif (isset ($_POST ['commit']) && $_POST ['commit'] && isset ($_POST ['statushash']) && $_POST ['statushash'] && isset ($_POST ['commit_message']) && $_POST ['commit_message'] != '')
handle_commit_req ();
elseif (isset ($_POST ['refresh']) && $_POST ['refresh'])
handle_refresh_req ();
elseif (isset ($_POST ['pull']) && $_POST ['pull'])
handle_pull_req ();
elseif (isset ($_POST ['push']) && $_POST ['push'])
handle_push_req ();
else {
error ('POST failure');
exit ();
}
} else {
echo html_header_message (' ');
echo html_form_start ();
view_result ();
}
/// functions ///
function view_result ($status = '') {
global $somethingstaged, $enable_stats;
if ($status == '')
$status = get_status ();
debug ($status);
if ($status ['disable_commit'] !== true)
$something_to_commit = $somethingstaged;
echo html_form_end ($something_to_commit, $status ['hash']);
echo html_footer ();
}
function handle_refresh_req () {
echo html_header_message ('refreshing...');
echo html_form_start ();
view_result ();
echo html_header_message_update ('refreshing... done');
}
function handle_pull_req () {
global $disable_push_pull;
if (isset ($disable_push_pull) && $disable_push_pull === true)
return false;
echo html_header_message ('pulling...');
echo html_form_start ();
do_git_action('pull');
view_result ();
echo html_header_message_update ('pulling... done');
}
function handle_push_req () {
global $disable_push_pull;
if (isset ($disable_push_pull) && $disable_push_pull === true)
return false;
echo html_header_message ('pushing...');
echo html_form_start ();
do_git_action('push');
view_result ();
echo html_header_message_update ('pushing... done');
}
function handle_change_staged_req () {
global $enable_stats;
echo html_header_message ('checking, before handling staging...');
echo html_form_start ();
$status = get_status (true, false, false);
if ($status ['hash'] != $_POST ['statushash'])
error ('something changed in the directory and/or repository, not doing any changes ! Sorry');
else {
echo html_header_message_update ('doing staging/unstaging...');
$num1 = 0;
$num2 = 0;
$arr = Array ();
foreach ($status ['lines'] as $v)
$arr [] = $v ['file'];
$poststaged = Array ();
if (isset ($_POST ['stagecheckbox']))
foreach ($_POST ['stagecheckbox'] as $v) {
$key = array_search ($v, $_POST ['hash']);
if ($key !== false)
$poststaged [$key] = 'Y';
}
$max = count ($_POST ['filename']);
if ($max !== count ($arr))
staged_change_checker_error ();
for ($i = 0; $i < $max; $i++) {
if ($_POST ['filename'] [$i] !== $arr [$i])
staged_change_checker_error ();
if ($status ['lines'][$i]['staged'] == 'N' && isset ($poststaged [$i]) && $poststaged [$i] == 'Y')
stage_file ($arr [$i], $status ['lines'][$i]);
if ($status ['lines'][$i]['staged'] == 'Y' && !isset ($poststaged [$i]))
unstage_file ($arr [$i], $status ['lines'][$i]);
echo html_js_remove_container ($status ['lines'][$i]['prefix']);
}
$status = get_status (false, true, $enable_stats);
}
view_result ($status);
echo html_header_message_update ('doing staging/unstaging... done');
}
function staged_change_checker_error () {
error ('something went wrong when comparing the POST and current status of the files on disk, eventhough the previously calculated hashes were OK. Processing stopped. Sorry.');
exit ();
}
function handle_commit_req () {
global $enable_stats, $author;
echo html_header_message ('checking, before doing commit...');
echo html_form_start ();
$status = get_status (true, false, false);
if ($status ['hash'] != $_POST ['statushash'])
error ('something changed in the directory and/or repository, not doing any changes ! Sorry');
else {
do_commit ($_POST ['commit_message'], $author);
$max = count ($_POST ['filename']);
for ($i = 0; $i < $max; $i++)
echo html_js_remove_container ($status ['lines'][$i]['prefix']);
$status = get_status ();
}
view_result ($status);
}
///////////////////////////////
function stage_file ($file, $status) {
global $gitpath;
echo html_header_message_update ("staging file $file");
if ($status ['state'] == 'Deleted')
$args = Array ('rm', $file);
else
$args = Array ('add', $file);
debug ('git ' . implode (' ', $args));
$h = start_command ($gitpath, $args);
list ($stdout, $stderr) = get_all_data ($h, Array ('stdout', 'stderr'));
debug ("stdout: $stdout");
debug ("stderr: $stderr");
$exit = get_exit_code ($h);
debug ($exit);
clean_up ($h);
if ($exit === 0)
echo html_header_message_update ("staging file $file: OK");
else {
echo html_header_message_update ("staging file $file: ".'<span class="error">FAILED</a>', true);
if (trim ($stderr) != '')
error ("$stderr");
echo html_form_end ();
exit ();
}
}
function unstage_file ($file, $status) {
global $gitpath;
echo html_header_message_update ("unstaging file: $file");
debug ($status);
$args = Array ('reset', 'HEAD', $file);
debug ('git ' . implode (' ', $args));
$h = start_command ($gitpath, $args);
list ($stdout, $stderr) = get_all_data ($h, Array ('stdout', 'stderr'));
debug ("stdout: $stdout");
debug ("stderr: $stderr");
$exit = get_exit_code ($h);
debug ($exit);
clean_up ($h);
if ($exit == 0 || $exit == 1) // 0 is nothing staged, 1 still something staged
echo html_header_message_update ("unstaging file: $file: OK");
else {
echo html_header_message_update ("unstaging file: $file: ".'<span class="error">FAILED</a>', true);
if (trim ($stderr) != '')
error ("$stderr");
echo html_form_end ();
exit ();
}
}
function do_commit ($msg = false, $author = '') {
global $commit_message, $gitpath;
$tmp = tempnam ('/tmp', 'git-commit');
$fp = fopen ($tmp, 'w+');
fwrite ($fp, $msg);
fclose ($fp);
echo html_header_message_update ("commiting changed files...");
$args = Array ('commit', '--no-status', '-F', $tmp);
if ($author != '') {
$args [] = '--author';
$args [] = '"'.$author.'"';
}
debug ('git ' . implode (' ', $args));
$h = start_command ($gitpath, $args);
list ($stdout, $stderr) = get_all_data ($h, Array ('stdout', 'stderr'));
debug ("stdout: $stdout");
debug ("stderr: $stderr");
$exit = get_exit_code ($h);
debug ($exit);
clean_up ($h);
unlink ($tmp);
if ($exit === 0) {
echo html_header_message_update ("commiting changed files... OK");
$commit_message = '';
} else {
echo html_header_message_update ('commiting changed files...: <span class="error">FAILED</a>', true);
if (trim ($stderr) != '')
error ("$stderr");
echo html_form_end ();
exit ();
}
}
function do_git_action ($action) {
global $commit_message, $gitpath;
echo html_header_message_update ($action."ing...");
$args = Array ($action);
debug ('git ' . implode (' ', $args));
$h = start_command ($gitpath, $args);
list ($stdout, $stderr) = get_all_data ($h, Array ('stdout', 'stderr'));
debug ("stdout: $stdout");
debug ("stderr: $stderr");
$exit = get_exit_code ($h);
debug ($exit);
clean_up ($h);
if ($exit === 0) {
echo html_header_message_update ($action."ing... OK");
} else {
echo html_header_message_update ($action.'ing...: <span class="error">FAILED</a>', true);
if (trim ($stderr) != '')
error ("$stderr");
echo html_form_end ();
exit ();
}
}
///////////////////////////////
function error ($str = '') {
echo '<pre>ERROR: '.$str . "</pre>\n";
return false;
}
function debug ($input = '', $force = false) {
global $debug;
if ($force === false && $debug === false)
return true;
if (is_string ($input))
echo "<pre>$input</pre>";
elseif (is_array ($input)) {
echo "<pre>";
print_r ($input);
echo "</pre>";
} else {
echo "<pre>";
var_dump ($input);
echo "</pre>";
}
}
///////////////////////////////
function make_one_hash (&$rs) {
$str = '';
if (isset ($rs ['lines']) && is_array ($rs ['lines']))
foreach ($rs ['lines'] as $v)
$str .= $v ['hash'];
$str .= $rs ['outputhash'];
$rs ['hash'] = sha1 ($str);
}
function get_status ($disabled = false, $makediff = true, $stats = false) {
global $somethingstaged, $gitpath;
static $firstrun;
if (!isset ($firstrun))
$firstrun = true;
else
$firstrun = false;
if (!$firstrun)
echo close_and_add_filelist_parent ();
$result = Array ('lines' => Array ());
$somethingstaged = false;
clearstatcache ();
$h = start_command ($gitpath, Array ('status', '--porcelain'), false);
if ($h === false)
return error ('command failed to start');
else {
close_stdin ($h);
$err = '';
$out = '';
while (!is_done ($h)) {
$line = get_stdout_line ($h);
if ($line != '') {
debug ($line);
$parsed = parse_line ($line);
$int = interpret ($parsed, $disabled, $makediff, $stats);
if ($int !== false) {
if (isset ($int ['dir'])) {
$list = Array ();
$list = add_directory_listing ($parsed ['dir'], $disabled, $makediff, $stats, $list);
$result ['lines'] = array_merge ($result ['lines'], $list);
} else
$result ['lines'][] = $int;
}
flush ();
}
}
$exit = get_exit_code ($h);
if ($exit !== 0) {
$errors = get_all_data ($h, Array ('stdout', 'stderr'));
if (!is_array ($errors))
$errors = Array ();
return error ("command failed with exitcode ".$exit.":\n".implode (' ', $errors));
}
$result ['output'] = get_all_data ($h);
$result ['outputhash'] = sha1 ($result['output']);
$result ['disable_commit'] = false;
make_one_hash ($result);
}
clean_up ($h);
return $result;
}
function get_file_hash ($file) {
if (file_exists ($file))
return sha1 ($file . sha1_file ($file));
return false;
}
function parse_line ($str) {
global $sha1_empty_string;
$str = rtrim ($str);
$file = substr ($str, 3);
if (file_exists ($file) ) {
$res = Array ('strstaged' => $str [0], 'strmodified' => $str [1], 'str' => $str);
$type = filetype ($file);
if ($type === 'file') {
$res ['hash'] = get_file_hash ($file);
$res ['file'] = $file;
} elseif ($type == 'dir') {
$res ['dir'] = $file;
} else
$res = Array ('str' => $str);
return $res;
} elseif ($str [0] == ' ' && $str [1] == 'D') {
$res = Array ('strstaged' => ' ', 'strmodified' => 'D', 'str' => $str, 'file' => $file, 'hash' => sha1 ($file . $sha1_empty_string));
} elseif ($str [0] == 'D' && $str [1] == ' ') {
$res = Array ('strstaged' => 'D', 'strmodified' => ' ', 'str' => $str, 'file' => $file, 'hash' => sha1 ($file . $sha1_empty_string));
} elseif ($str [0] == 'R') {
$sub = substr ($str, 4);
$arr = explode (' -> ', $sub);
$res = Array ('strstaged' => 'R', 'strmodified' => ' ', 'str' => $str, 'oldfile' => $arr [0], 'newfile' => $arr [1], 'hash' => get_file_hash ($arr[1]), 'file' => $arr[1]);
} elseif ($str [0] == 'C') {
preg_match ('/(.*) -> (.*)/', $file, $filenames);
$res = Array ('strstaged' => $str [0], 'strmodified' => ' ', 'oldfile' => $filenames [1], 'newfile' => $filenames [2], 'str' => $str, 'hash' => get_file_hash ($filenames [2]), 'file' => $filenames [2]);
} else
$res = Array ('str' => $str);
return $res;
}
function interpret ($parsed, $disabled = false, $makediff = true, $stats = false) {
global $gitpath;
if (isset ($parsed ['file'])) {
if ($parsed ['strstaged'] == '?' || $parsed ['strstaged'] == 'A') {
if ($makediff) {
$command = 'diff';
$args = Array ('-u', '/dev/null', $parsed ['file']);
if (isset ($parsed ['staged']) && $parsed ['staged'] == 'A') {
$args = Array ('diff', '--cached', $parsed ['file']);
$command = $gitpath;
}
$h = start_command ($command, $args);
close_stdin ($h);
$diff = htmlentities (get_all_data ($h));
clean_up ($h);
} else
$diff = false;
$parsed ['state'] = set_state ($parsed ['strmodified'], $parsed ['strstaged']);
$parsed ['staged'] = set_staged ($parsed ['strmodified'], $parsed ['strstaged']);
list ($str, $prefix) = html_file ($parsed ['file'], $parsed ['state'], $parsed ['staged'], $parsed ['hash'], $diff, $disabled);
echo $str;
$parsed ['prefix'] = $prefix;
} elseif (( $parsed ['strmodified'] == 'M') || ($parsed ['strstaged'] == 'M' && $parsed ['strmodified'] == ' ')) {
if ($makediff) {
$args = Array ('diff', $parsed ['file']);
if ($parsed ['strstaged'] == 'M' && $parsed ['strmodified'] == ' ')
$args = Array ('diff', '--cached', $parsed ['file']);
$h = start_command ($gitpath, $args);
close_stdin ($h);
$str = get_all_data ($h);
$diff = htmlentities ($str);
$exit = get_exit_code ($h);
clean_up ($h);
} else
$diff = false;
$parsed ['state'] = set_state ($parsed ['strmodified'], $parsed ['strstaged']);
$parsed ['staged'] = set_staged ($parsed ['strmodified'], $parsed ['strstaged']);
list ($str, $prefix) = html_file ($parsed ['file'], $parsed ['state'], $parsed ['staged'], $parsed ['hash'], $diff, $disabled);
echo $str;
$parsed ['prefix'] = $prefix;
} elseif ($parsed ['strmodified'] == 'D') {
if ($makediff) {
$args = Array ('diff', '--', $parsed ['file']);
$h = start_command ($gitpath, $args);
close_stdin ($h);
$str = get_all_data ($h);
$diff = htmlentities ($str);
$exit = get_exit_code ($h);
clean_up ($h);
} else
$diff = false;
$parsed ['state'] = set_state ($parsed ['strmodified'], $parsed ['strstaged']);
$parsed ['staged'] = set_staged ($parsed ['strmodified'], $parsed ['strstaged']);
list ($str, $prefix) = html_file ($parsed ['file'], $parsed ['state'], $parsed ['staged'], $parsed ['hash'], $diff, $disabled);
echo $str;
$parsed ['prefix'] = $prefix;
} elseif ($parsed ['strstaged'] == 'D') {
if ($makediff) {
$args = Array ('diff', '--cached', '--', $parsed ['file']);
$h = start_command ($gitpath, $args);
close_stdin ($h);
$str = get_all_data ($h);
$diff = htmlentities ($str);
$exit = get_exit_code ($h);
clean_up ($h);
} else
$diff = false;
$parsed ['state'] = set_state ($parsed ['strmodified'], $parsed ['strstaged']);
$parsed ['staged'] = set_staged ($parsed ['strmodified'], $parsed ['strstaged']);
list ($str, $prefix) = html_file ($parsed ['file'], $parsed ['state'], $parsed ['staged'], $parsed ['hash'], $diff, $disabled);
echo $str;
$parsed ['prefix'] = $prefix;
} elseif ($parsed ['strstaged'] == 'R') {
if ($makediff) {
$args = Array ('diff', '--cached', '--', $parsed ['file']);
$h = start_command ($gitpath, $args);
close_stdin ($h);
$str = get_all_data ($h);
$diff = htmlentities ($str);
$exit = get_exit_code ($h);
clean_up ($h);
} else
$diff = false;
$parsed ['state'] = set_state ($parsed ['strmodified'], $parsed ['strstaged']);
$parsed ['staged'] = set_staged ($parsed ['strmodified'], $parsed ['strstaged']);
list ($str, $prefix) = html_file ($parsed ['file'], $parsed ['state'], $parsed ['staged'], $parsed ['hash'], $diff, $disabled);
echo $str;
$parsed ['prefix'] = $prefix;
} elseif ($parsed ['strstaged'] == 'C') {
$info = htmlentities ('file ' . $parsed ['newfile'] . ' is a copy of ' . $parsed ['oldfile']);
$parsed ['state'] = set_state ($parsed ['strmodified'], $parsed ['strstaged']);
$parsed ['staged'] = set_staged ($parsed ['strmodified'], $parsed ['strstaged']);
list ($str, $prefix) = html_file ($parsed ['file'], $parsed ['state'], $parsed ['staged'], $parsed ['hash'], $info, $disabled);
echo $str;
$parsed ['prefix'] = $prefix;
} else
interpret_not_supported ($parsed, __FILE__, __LINE__);
} else {
if (isset ($parsed ['dir']) && $parsed ['strmodified'] == '?' && $parsed ['strstaged'] == '?') {
// is a dir, handled outside this function
} else {
// if type == 'link' -> readlink ( see Github issue #3 )
interpret_not_supported ($parsed, __FILE__, __LINE__);
}
}
return $parsed;
}
function add_directory_listing ($dir, $disabled, $makediff, $stats, &$list) {
global $diffpath;
$handle = opendir ($dir);
while (($entry = readdir ($handle)) !== false)
if ($entry != '.' && $entry != '..') {
$type = filetype ($dir . $entry);
if ($type == 'file') {
$file = $dir . $entry;
$hash = get_file_hash ($file);
$parsed = Array ('file' => $file, 'hash' => $hash);
$parsed ['state'] = 'New';
$parsed ['staged'] = 'N';
if ($makediff) {
$command = $diffpath;
$args = Array ('-u', '/dev/null', $parsed ['file']);
$h = start_command ($command, $args);
close_stdin ($h);
$diff = htmlentities (get_all_data ($h));
clean_up ($h);
} else
$diff = false;
list ($str, $prefix) = html_file ($file, $parsed ['state'], $parsed ['staged'], $parsed ['hash'], $diff, $disabled);
echo $str;
$parsed ['prefix'] = $prefix;
$list [] = $parsed;
} elseif ($type == 'dir') {
add_directory_listing ($dir . $entry . '/', $disabled, $makediff, $stats, $list);
} else
interpret_not_supported ($dir . $entry, __FILE__, __LINE__);
}
return $list;
}
function interpret_not_supported ($debug, $file = false, $line = false) {
if ($file === false)
$file = '';
else
$file = $file . ': ';
if ($line === false)
$line = '';
else
$line = $line . ': ';
error ($file.$line.'Not implemented: Only changed, added, deleted files is supported right now. Found something else in the output of git status, debug output is below. Sorry.');
debug ($debug, true);
exit ();
}
function set_state ($modified, $staged) {
$rv = $modified; // last resort ?
if ($modified == '?' || $staged == 'A')
$rv = 'New';
if ($modified == 'D' || $staged == 'D')
$rv = 'Deleted';
if ($modified == 'M' || $staged == 'M')
$rv = 'Modified';
if ($staged == 'R')
$rv = 'Renamed';
if ($staged == 'C')
$rv = 'Copied';
return $rv;
}
function set_staged ($modified, $staged) {
$rv = $staged; // last resort ?
if ($staged == ' ' || $staged == '?')
$rv = 'N';
if ($staged == 'Y' || $staged == 'M' || $staged == 'A' || $staged == 'D' || $staged == 'R' || $staged == 'C')
$rv = 'Y';
return $rv;
}
///////////////////////////////
function start_command ($command, $argarr, $blocking = true) {
$descriptorspec = array(
0 => Array ('pipe', 'r'), // stdin
1 => Array ('pipe', 'w'), // stdout
2 => Array ('pipe', 'w'), // stderr
);
$pipes = Array ();
$args = '';
foreach ($argarr as $v)
$args .= ' '. escapeshellarg ($v);
$command = escapeshellcmd ($command);
$proc = proc_open($command . ' ' . $args, $descriptorspec, $pipes);
if (is_resource($proc)) {
if ($blocking === false) {
stream_set_blocking ($pipes [0], 0);
stream_set_blocking ($pipes [1], 0);
stream_set_blocking ($pipes [2], 0);
} else {
// should already be the default
stream_set_blocking ($pipes [0], 1);
stream_set_blocking ($pipes [1], 1);
stream_set_blocking ($pipes [2], 1);
}
global $_handles, $_handlecount;
$h = ++$_handlecount;
$_handles [$h] = Array ('proc' => $proc, 0 => $pipes [0], 1 => $pipes [1], 2 => $pipes [2]);
return $h;
} else
return false;
}
function end_command ($h) {
global $_handles;
if (!isset ($_handles [$h]))
return false;
// It is important that you close any pipes before calling
// proc_close in order to avoid a deadlock
@fclose ($_handles [$h][0]);
@fclose ($_handles [$h][1]);
@fclose ($_handles [$h][2]);
if ((!isset ($_handles [$h]['running'])) || ($_handles [$h]['running'] !== false))
$rv = proc_close ($_handles [$h] ['proc']);
else
$rv = $_handles [$h]['rv'];
$_handles [$h]['running'] = false;
return $rv;
}
function is_done ($h) {
global $_handles;
if (!isset ($_handles [$h]))
return true; // closest thing to an error
if (isset ($_handles [$h]['done']) && $_handles [$h]['done'] === true)
return true;
return false;
}
function put_in_stdin ($h, $str) {
global $_handles;
if (!isset ($_handles [$h]))
return false;
return fwrite ($_handles [$h][0]);
}
function close_stdin ($h) {
global $_handles;
if (!isset ($_handles [$h]))
return false;
return @fclose ($_handles [$h][0]);
}
function get_stdout_line ($h) {
return _get_line ($h, 0);
}
function get_stderr_line ($h) {
return _get_line ($h, 1);
}
function _get_line ($h, $num) {
global $_handles;
if (!isset ($_handles [$h]))
return ""; // closest thing to an error
$rv = _get_data ($h);
if ($rv === false)
return ""; // closest thing to an error
return $rv [$num];
}
function _get_data ($h) {
global $_handles;
if (!isset ($_handles [$h]))
return false;
$read = array($_handles[$h][1], $_handles[$h][2]);
$write = NULL;
$except = NULL;
$wait = 120;
$rv = Array ('', '');
if (false === ($num_changed_streams = stream_select($read, $write, $except, $wait))) {
/* Error handling */
$_handles [$h]['done'] = true; // ?
return error ("error occured");
} elseif ($num_changed_streams > 0) {
/* At least on one of the streams something interesting happened */
$alleof = true;
$newout = fgets ($_handles[$h][1], 8192);
$newerr = fgets ($_handles[$h][2], 8192);
if (!isset ($_handles[$h]['stdout']))
$_handles[$h]['stdout'] = '';
if (!isset ($_handles[$h]['stderr']))
$_handles[$h]['stderr'] = '';
if ($newout !== false)
$_handles[$h]['stdout'] .= $newout;
if ($newerr !== false)
$_handles[$h]['stderr'] .= $newerr;
if (!feof ($_handles[$h][1]) && $newout != '')
$alleof = false;
if (!feof ($_handles[$h][2]) && $newerr != '')
$alleof = false;
if ($newout != '')
$rv [0] = $newout;
if ($newerr != '')
$rv [1] = $newerr;
if ($alleof) {
$_handles [$h]['done'] = true;
$status = proc_get_status ($_handles[$h] ['proc']);
if ($status ['running'] === false)
$return_value = $status ['exitcode'];
else
$return_value = end_command ($h);
$_handles [$h]['rv'] = $return_value;
$_handles [$h]['running'] = false;
}
} else
debug ( "hier" );
return $rv;
}
function get_all_data ($h, $intype = 'stdout') {
global $_handles;
if (!isset ($_handles [$h]))
return false;
if (!is_array ($intype))
$types = Array ($intype);
else
$types = $intype;
$rv = Array ();
if (isset ($_handles [$h]['done']) && $_handles [$h]['done']) {
foreach ($types as $k => $type)
$rv [$k] = $_handles [$h][$type];
if (!is_array ($intype))
return $rv [0];
return $rv;
}
// XXX BUG: possibly a second call to function will fail, if done was false the first time and done = true second time.
foreach ($types as $k => $type) {
// if $rv == false we return false at the end
if ($type == 'stdout')
$rv [$k] = stream_get_contents ($_handles[$h][1]);
elseif ($type == 'stderr')
$rv [$k] = stream_get_contents ($_handles[$h][2]);
}
$status = proc_get_status ($_handles[$h]['proc']);
if ($status ['running'] === false)
$return_value = $status ['exitcode'];
else
$return_value = end_command ($h);
$_handles [$h]['rv'] = $return_value;
$_handles [$h]['running'] = false;
if (!is_array ($intype))
return $rv [0];
return $rv;
}
function get_exit_code ($h) {
global $_handles;
if (!isset ($_handles [$h]))
return false;
if (!isset ($_handles [$h]['rv']))
return false;
return $_handles [$h]['rv'];
}
function clean_up ($h) {
global $_handles;
if (!isset ($_handles [$h]))
return false;
if (isset ($_handles [$h]['done']) && $_handles [$h]['done'] !== true)
end_command ($h);
unset ($_handles [$h]);
}
///////////////////////////////
function handle_basic_auth ($realm = 'private area') {
global $auth;
if (isset ($_SERVER['PHP_AUTH_USER'])
&& isset ($_SERVER['PHP_AUTH_PW'])
&& isset ($auth [$_SERVER['PHP_AUTH_USER']])
&& $auth [$_SERVER['PHP_AUTH_USER']]['pass'] === sha1 ($_SERVER['PHP_AUTH_PW'])
) {
return $auth [$_SERVER['PHP_AUTH_USER']]['name'] . ' <'.$auth [$_SERVER['PHP_AUTH_USER']]['email'].'>';
}
header('WWW-Authenticate: Basic realm="'.$realm.'"');
header('HTTP/1.0 401 Unauthorized');
exit ('You did not supply any or the wrong username/password combination');
}
function handle_htpasswd_auth () {
global $auth;
if (!isset ($_SERVER['REMOTE_USER']))
exit ('htpasswd not setup correctly');
if (isset ($_SERVER['REMOTE_USER']) && isset ($auth [$_SERVER ['REMOTE_USER']]))
return $auth [$_SERVER ['REMOTE_USER']]['name'] . ' <'.$auth [$_SERVER ['REMOTE_USER']]['email'].'>';
exit ('htpasswd user unknown to git-webcommit');
}