3 * Gestion du téléporteur GIT.
5 * @plugin SVP pour SPIP
7 * @package SPIP\SVP\Teleporteur
11 * Téléporter et déballer un composant GIT
13 * Déployer un repository GIT depuis une source et une révision données
15 * @param string $methode
16 * Méthode de téléportation : http|git|svn|...
17 * @param string $source
18 * URL de la source GIT
20 * Chemin du répertoire de destination
21 * @param array $options
22 * Tableau d'options. Index possibles :
23 * - revision => 'ae89'
26 * True si l'opération réussie, false sinon.
28 function teleporter_git_dist($methode,$source,$dest,$options=array()){
30 $branche = (isset($options['branche'])?
$options['branche']:'master');
32 $infos = teleporter_git_read($dest,array('format'=>'assoc'));
34 spip_log("Suppression de $dest qui n'est pas au format GIT","teleport");
35 $old = teleporter_nettoyer_vieille_version($dest);
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);
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);
48 if (isset($options['revision'])){
49 $command = "git checkout ".escapeshellarg($options['revision']);
50 teleporter_git_exec($dest,$command);
53 $command = "git checkout ".escapeshellarg($branche);
54 teleporter_git_exec($dest,$command);
58 spip_log("$dest deja sur GIT $source Revision ".$options['revision'],"teleport");
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);
72 // verifier que tout a bien marche
73 $infos = teleporter_git_read($dest);
74 if (!$infos) return false;
80 * Lire l'état GIT du repository
82 * Retourne les informations GIT d'un répertoire donné
85 * Chemin du répertoire à tester
86 * @param array $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.
96 function teleporter_git_read($dest, $options=array()) {
98 if (!is_dir("$dest/.git"))
104 exec("git remote -v",$output);
105 $output = implode("\n",$output);
108 if (preg_match(",(\w+://.*)\s+\(fetch\)$,Uims",$output,$m))
110 elseif (preg_match(",([^@\s]+@[^:\s]+:.*)\s+\(fetch\)$,Uims",$output,$m))
120 exec("git log -1",$output);
121 $hash = explode(" ",reset($output));
124 // [TODO] lire la branche ?
127 if (preg_match(",[^0-9a-f],i",$hash))
132 'revision' => substr($hash,0,7),
139 * Exécuter une commande GIT
141 * @param string $dest
142 * Répertoire de destination
143 * @param string $command
144 * Commande à exécuter
147 function teleporter_git_exec($dest,$command) {
148 spip_log("{$dest}:{$command}","teleport");