3 * Gestion du téléporteur GIT.
5 * @plugin SVP pour SPIP
7 * @package SPIP\SVP\Teleporteur
10 if (!defined('_GIT_COMMAND')) {
11 define('_GIT_COMMAND', 'git');
12 } // Securite : on peut indiquer le chemin absolu dans mes_options.php
15 * Téléporter et déballer un composant GIT
17 * Déployer un repository GIT depuis une source et une révision données
19 * @param string $methode
20 * Méthode de téléportation : http|git|svn|...
21 * @param string $source
22 * URL de la source GIT
24 * Chemin du répertoire de destination
25 * @param array $options
26 * Tableau d'options. Index possibles :
27 * - revision => 'ae89'
30 * True si l'opération réussie, false sinon.
32 function teleporter_git_dist($methode, $source, $dest, $options = array()) {
34 $branche = (isset($options['branche']) ?
$options['branche'] : 'master');
36 $infos = teleporter_git_read($dest, array('format' => 'assoc'));
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']
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);
51 if (isset($options['revision'])) {
52 $command = _GIT_COMMAND
. " checkout " . escapeshellarg($options['revision']);
53 teleporter_git_exec($dest, $command);
55 $command = _GIT_COMMAND
. " checkout " . escapeshellarg($branche);
56 teleporter_git_exec($dest, $command);
59 spip_log("$dest deja sur GIT $source Revision " . $options['revision'], "teleport");
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);
73 // verifier que tout a bien marche
74 $infos = teleporter_git_read($dest);
83 * Lire l'état GIT du repository
85 * Retourne les informations GIT d'un répertoire donné
88 * Chemin du répertoire à tester
89 * @param array $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.
99 function teleporter_git_read($dest, $options = array()) {
101 if (!is_dir("$dest/.git")) {
108 exec(_GIT_COMMAND
. " remote -v", $output);
109 $output = implode("\n", $output);
112 if (preg_match(",(\w+://.*)\s+\(fetch\)$,Uims", $output, $m)) {
114 } elseif (preg_match(",([^@\s]+@[^:\s]+:.*)\s+\(fetch\)$,Uims", $output, $m)) {
126 exec(_GIT_COMMAND
. " log -1", $output);
127 $hash = explode(" ", reset($output));
130 // [TODO] lire la branche ?
133 if (preg_match(",[^0-9a-f],i", $hash)) {
139 'revision' => substr($hash, 0, 7),
146 * Exécuter une commande GIT
148 * @param string $dest
149 * Répertoire de destination
150 * @param string $command
151 * Commande à exécuter
154 function teleporter_git_exec($dest, $command) {
155 spip_log("{$dest}:{$command}", "teleport");
164 * Tester si la commande 'git' est disponible
167 * true si on peut utiliser la commande svn
169 function teleporter_git_tester() {
170 static $erreurs = null;
171 if (is_null($erreurs)) {
172 exec(_GIT_COMMAND
. " --version", $output, $erreurs);