[SPIP] +spip v3.0.17
[lhc/web/clavette_www.git] / www / plugins-dist / svp / teleporter / git.php
1 <?php
2 /**
3 * Gestion du téléporteur GIT.
4 *
5 * @plugin SVP pour SPIP
6 * @license GPL
7 * @package SPIP\SVP\Teleporteur
8 */
9
10 /**
11 * Téléporter et déballer un composant GIT
12 *
13 * Déployer un repository GIT depuis une source et une révision données
14 *
15 * @param string $methode
16 * Méthode de téléportation : http|git|svn|...
17 * @param string $source
18 * URL de la source GIT
19 * @param string $dest
20 * Chemin du répertoire de destination
21 * @param array $options
22 * Tableau d'options. Index possibles :
23 * - revision => 'ae89'
24 * - branche => 'xxx'
25 * @return bool
26 * True si l'opération réussie, false sinon.
27 */
28 function teleporter_git_dist($methode,$source,$dest,$options=array()){
29
30 $branche = (isset($options['branche'])?$options['branche']:'master');
31 if (is_dir($dest)){
32 $infos = teleporter_git_read($dest,array('format'=>'assoc'));
33 if (!$infos){
34 spip_log("Suppression de $dest qui n'est pas au format GIT","teleport");
35 $old = teleporter_nettoyer_vieille_version($dest);
36 }
37 elseif ($infos['source']!==$source) {
38 spip_log("Suppression de $dest qui n'est pas sur le bon repository GIT","teleport");
39 $old = teleporter_nettoyer_vieille_version($dest);
40 }
41 elseif (!isset($options['revision'])
42 OR $options['revision']!=$infos['revision']){
43 $command = "git checkout ".escapeshellarg($branche);
44 teleporter_git_exec($dest,$command);
45 $command = "git pull --all";
46 teleporter_git_exec($dest,$command);
47
48 if (isset($options['revision'])){
49 $command = "git checkout ".escapeshellarg($options['revision']);
50 teleporter_git_exec($dest,$command);
51 }
52 else {
53 $command = "git checkout ".escapeshellarg($branche);
54 teleporter_git_exec($dest,$command);
55 }
56 }
57 else {
58 spip_log("$dest deja sur GIT $source Revision ".$options['revision'],"teleport");
59 }
60 }
61
62 if (!is_dir($dest)){
63 $command = "git clone ";
64 $command .= escapeshellarg($source)." ".escapeshellarg($dest);
65 teleporter_git_exec($dest,$command);
66 if (isset($options['revision'])){
67 $command = "git checkout ".escapeshellarg($options['revision']);
68 teleporter_git_exec($dest,$command);
69 }
70 }
71
72 // verifier que tout a bien marche
73 $infos = teleporter_git_read($dest);
74 if (!$infos) return false;
75
76 return true;
77 }
78
79 /**
80 * Lire l'état GIT du repository
81 *
82 * Retourne les informations GIT d'un répertoire donné
83 *
84 * @param string $dest
85 * Chemin du répertoire à tester
86 * @param array $options
87 * Tableau d'options
88 * @return string|bool|array
89 * - Chaîne vide si pas un dépot GIT
90 * - False si erreur sur le dépot GIT
91 * - array sinon. Tableau avec 3 index :
92 * -- source : Source du dépot GIT à cette destination
93 * -- revision : Révision du dépot
94 * -- dest : Répertoire du dépot.
95 */
96 function teleporter_git_read($dest, $options=array()) {
97
98 if (!is_dir("$dest/.git"))
99 return "";
100
101 $curdir = getcwd();
102 chdir($dest);
103
104 exec("git remote -v",$output);
105 $output = implode("\n",$output);
106
107 $source = "";
108 if (preg_match(",(\w+://.*)\s+\(fetch\)$,Uims",$output,$m))
109 $source = $m[1];
110 elseif (preg_match(",([^@\s]+@[^:\s]+:.*)\s+\(fetch\)$,Uims",$output,$m))
111 $source = $m[1];
112
113 if (!$source){
114 chdir($curdir);
115 return "";
116 }
117
118 $source = $m[1];
119
120 exec("git log -1",$output);
121 $hash = explode(" ",reset($output));
122 $hash = end($hash);
123
124 // [TODO] lire la branche ?
125 chdir($curdir);
126
127 if (preg_match(",[^0-9a-f],i",$hash))
128 return false;
129
130 return array(
131 'source' => $source,
132 'revision' => substr($hash,0,7),
133 'dest' => $dest
134 );
135 }
136
137
138 /**
139 * Exécuter une commande GIT
140 *
141 * @param string $dest
142 * Répertoire de destination
143 * @param string $command
144 * Commande à exécuter
145 * @return void
146 */
147 function teleporter_git_exec($dest,$command) {
148 spip_log("{$dest}:{$command}","teleport");
149 $curdir = getcwd();
150 chdir($dest);
151 exec($command);
152 chdir($curdir);
153 }