Push your git logs to MySQL with PHP


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. #!/usr/bin/env php
  2. /**
  3. CREATE TABLE `log` (
  4. `id` int(10) unsigned NOT NULL auto_increment,
  5. `repo` varchar(255) NOT NULL,
  6. `commit` varchar(40) NOT NULL,
  7. `date` datetime NOT NULL,
  8. `message` text NOT NULL,
  9. PRIMARY KEY (`id`),
  10. UNIQUE KEY `commit` (`commit`)
  11. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
  12. */
  13. <?php
  14. date_default_timezone_set('Europe/London');
  15. exec('pwd',$pwd);
  16. $repo = rtrim(array_shift($pwd),'/');
  17. $repo = substr($repo,strrpos($repo,'/') + 1);
  18. $db = new PDO('mysql:dbname=DB;host=127.0.0.1','USERNAME','PASSWORD');
  19. exec('git log --all --pretty=format:"%H%n%ct%n%s%n%b%n<><><>"',$capture,$log);
  20. if ($capture){
  21. // preprocess the log
  22. $commits = array();
  23. $current = array();
  24. foreach ($capture as $row){
  25. if (trim($row) === '<><><>') {
  26. $commits[] = $current;
  27. $current = array();
  28. } else {
  29. $current[] = $row;
  30. }
  31. }
  32. $v = array();
  33. $b = array();
  34. foreach ($commits as $commit){
  35. $sha = $commit[0];
  36. $m = $commit[2] . (trim($commit[3]) === '' ? '' : "\n\n" . implode("\n",array_slice($commit,3)));
  37. $d = date('Y-m-d H:i:s',$commit[1]);
  38. $v[] = '(?,?,?,?)';
  39. $b[] = $repo;
  40. $b[] = $sha;
  41. $b[] = $m;
  42. $b[] = $d;
  43. }
  44. $stmt = $db->prepare('insert ignore into log (repo,commit,message,`date`) values' . implode(',',$v));
  45. try {
  46. if ($stmt) {
  47. if (!$stmt->execute($b)) throw new PDOException;;
  48. } else Throw new PDOException;
  49. } catch (PDOException $e) {
  50. mail('EMAIL','Commit did not reach db',$e->getMessage());
  51. }
  52. }
  53. ?>

URL: http://jspr.tndy.me/2010/07/store-git-activity-in-mysql-with-php/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.