git.class.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. class git
  3. {
  4. public static function fetch(){
  5. exec('git fetch --all');
  6. }
  7. public static function checkError(string $_string = NULL){
  8. $return = NULL;
  9. if($_string != NULL){
  10. if(strpos($_string, '(use "git pull" to update your local branch)') === FALSE){
  11. $return = FALSE;
  12. } else {
  13. return TRUE;
  14. }
  15. return $return;
  16. } else {
  17. return TRUE;
  18. }
  19. }
  20. public static function getCommitHash($_target = NULL){
  21. if($_target == NULL){
  22. $target = "HEAD";
  23. } else {
  24. $target = "origin/".$_target;
  25. }
  26. return substr(trim(exec('git log --pretty="%H" -n1 ' . $target)), 0, 10);
  27. }
  28. public static function getCommitDate($_target = NULL){
  29. if($_target == NULL){
  30. $target = "HEAD";
  31. } else {
  32. $target = "origin/".$_target;
  33. }
  34. $commitDate = new \DateTime(trim(exec('git log -n1 --pretty=%ci ' . $target)));
  35. $commitDate->setTimezone(new \DateTimeZone('Europe/Paris'));
  36. return $commitDate->format('d/m/Y à H:i:s');
  37. }
  38. public static function getCommitAuthor($_target = NULL){
  39. if($_target == NULL){
  40. $target = "HEAD";
  41. } else {
  42. $target = "origin/".$_target;
  43. }
  44. return exec('git log --pretty="%cn" -n1 ' . $target);
  45. }
  46. public static function getCommitName($_target = NULL){
  47. if($_target == NULL){
  48. $target = "HEAD";
  49. } else {
  50. $target = "origin/".$_target;
  51. }
  52. return exec('git log --pretty="%s" -n1 ' . $target);
  53. }
  54. public static function getBranchName($_target = NULL){
  55. if($_target == NULL){
  56. return exec('git rev-parse --abbrev-ref HEAD');
  57. } else {
  58. return $_target;
  59. }
  60. }
  61. public static function printVersion($_target = NULL){
  62. echo sprintf(
  63. '<h6>%s <span class="badge bg-info">%s</span> <span class="badge bg-success">%s</span></h6>
  64. <code>%s</code>
  65. <div class="media-body pb-3 mb-0 small lh-125">Le %s</div>',
  66. self::getCommitAuthor($_target),
  67. self::getBranchName($_target),
  68. self::getCommitHash($_target),
  69. self::getCommitName($_target),
  70. self::getCommitDate($_target)
  71. );
  72. }
  73. }