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