1 | <?php |
---|
2 | |
---|
3 | /* |
---|
4 | This file is part of Salvatore, the translation robot of Trad-lang (SPIP) |
---|
5 | |
---|
6 | Salvatore is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2 of the License, or |
---|
9 | (at your option) any later version. |
---|
10 | |
---|
11 | Trad-Lang is distributed in the hope that it will be useful, |
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | GNU General Public License for more details. |
---|
15 | |
---|
16 | You should have received a copy of the GNU General Public License |
---|
17 | along with Trad-Lang; if not, write to the Free Software |
---|
18 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
19 | |
---|
20 | Copyright 2003-2020 |
---|
21 | Florent Jugla <florent.jugla@eledo.com>, |
---|
22 | Philippe Riviere <fil@rezo.net>, |
---|
23 | Chryjs <chryjs!@!free!.!fr>, |
---|
24 | kent1 <kent1@arscenic.info> |
---|
25 | Cerdic <cedric@yterium.com> |
---|
26 | */ |
---|
27 | |
---|
28 | |
---|
29 | // il commit et push les fichiers modifies |
---|
30 | |
---|
31 | include_spip('inc/salvatore_git'); |
---|
32 | include_spip('inc/salvatore_svn'); |
---|
33 | |
---|
34 | /** |
---|
35 | * @param array $liste_sources |
---|
36 | * @param string|null $tmp |
---|
37 | * @return bool |
---|
38 | * @throws Exception |
---|
39 | */ |
---|
40 | function salvatore_pousser($liste_sources, $dir_modules=null, $dir_depots=null) { |
---|
41 | include_spip('inc/salvatore'); |
---|
42 | salvatore_init(); |
---|
43 | |
---|
44 | if (is_null($dir_modules)) { |
---|
45 | $dir_modules = _DIR_SALVATORE_MODULES; |
---|
46 | } |
---|
47 | salvatore_check_dir($dir_modules); |
---|
48 | |
---|
49 | if (is_null($dir_depots)) { |
---|
50 | $dir_depots = _DIR_SALVATORE_DEPOTS; |
---|
51 | } |
---|
52 | salvatore_check_dir($dir_depots); |
---|
53 | |
---|
54 | $url_gestionnaire = salvatore_get_self_url(); |
---|
55 | |
---|
56 | foreach ($liste_sources as $source){ |
---|
57 | salvatore_log("\n<info>--- Module " . $source['module'] . " | " . $source['dir_module'] . " | " . $source['url']."</info>"); |
---|
58 | |
---|
59 | $dir_module = $dir_modules . $source['dir_module']; |
---|
60 | $module = $source['module']; |
---|
61 | |
---|
62 | // on peut poser un .salvatore.ignore.{module} manuellement pour forcer salvatore a ne jamais pousser certains modules |
---|
63 | // (gestion de tensions sur certains plugins/modules) |
---|
64 | |
---|
65 | |
---|
66 | if (file_exists($dir_module . '/.salvatore.ignore.' . $module)) { |
---|
67 | salvatore_log("<comment>Module $module ignoré</comment>"); |
---|
68 | } |
---|
69 | else { |
---|
70 | $res = savatore_commit_and_push_module($source, $dir_modules, $dir_depots, $url_gestionnaire); |
---|
71 | if ($res) { |
---|
72 | salvatore_log("Module $module <info>OK</info>\n"); |
---|
73 | } |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | return true; |
---|
78 | } |
---|
79 | |
---|
80 | /** |
---|
81 | * Commiter et pusher les modifs sur un module |
---|
82 | * @param array $source |
---|
83 | * @param string $dir_modules |
---|
84 | * @param string $dir_depots |
---|
85 | * @param string $url_gestionnaire |
---|
86 | * @return bool |
---|
87 | * @throws Exception |
---|
88 | */ |
---|
89 | function savatore_commit_and_push_module($source, $dir_modules, $dir_depots, $url_gestionnaire) { |
---|
90 | |
---|
91 | $dir_module = $dir_modules . $source['dir_module']; |
---|
92 | $module = $source['module']; |
---|
93 | |
---|
94 | $file_commit = $dir_module . '/' . $module . '.commit.json'; |
---|
95 | |
---|
96 | if (!file_exists($file_commit) |
---|
97 | or !$commit_infos = file_get_contents($file_commit) |
---|
98 | or !$commit_infos = json_decode($commit_infos, true)) { |
---|
99 | salvatore_log("<comment>Module $module rien à faire (pas de fichier $file_commit ou fichier invalide)</comment>"); |
---|
100 | return false; |
---|
101 | } |
---|
102 | |
---|
103 | // on a la liste des fichiers a commit |
---|
104 | $message_commit = ''; |
---|
105 | if (isset($commit_infos['.message'])) { |
---|
106 | $message_commit = trim($commit_infos['.message']) . "\n"; |
---|
107 | unset($commit_infos['.message']); |
---|
108 | } |
---|
109 | |
---|
110 | $subdir = ''; |
---|
111 | if (isset($source['dir'])) { |
---|
112 | $subdir = $source['dir'] . DIRECTORY_SEPARATOR; |
---|
113 | } |
---|
114 | |
---|
115 | // reorganiser les fichiers a commit et preparer les messages de commit |
---|
116 | // - ignorer les fichiers non modifies, ou non versionnes et qui ne doivent pas etre ajoutes |
---|
117 | // - regrouper par auteur |
---|
118 | |
---|
119 | $commits_todo = array(); |
---|
120 | $salvatore_status_file = "salvatore_" . $source['methode'] . "_status_file"; |
---|
121 | $salvatore_commit_files = "salvatore_" . $source['methode'] . "_commit_files"; |
---|
122 | $salvatore_push_repository = "salvatore_" . $source['methode'] . "_push_repository"; |
---|
123 | |
---|
124 | foreach ($commit_infos as $what => $commit_info) { |
---|
125 | |
---|
126 | $file = $commit_info['file_name']; |
---|
127 | |
---|
128 | if ($commit_info['lastmodified'] or $commit_info['must_add']) { |
---|
129 | $status = $salvatore_status_file($dir_depots . $source['dir_checkout'], $subdir . $file); |
---|
130 | |
---|
131 | // fichier nouveau ou modifie (sinon on l'ignore) |
---|
132 | if ($status) { |
---|
133 | $author = 0; |
---|
134 | if (!empty($commit_info['author'])) { |
---|
135 | $author = $commit_info['author']; |
---|
136 | } |
---|
137 | // si c'est le xml et qu'on a un seul auteur de commit, on lui fait commit aussi le xml |
---|
138 | elseif($what === '.xml' and count($commits_todo)===1) { |
---|
139 | $author = array_keys($commits_todo); |
---|
140 | $author = reset($author); |
---|
141 | } |
---|
142 | |
---|
143 | if (!isset($commits_todo[$author])) { |
---|
144 | $commits_todo[$author] = array( |
---|
145 | 'files' => array(), |
---|
146 | 'message' => [] |
---|
147 | ); |
---|
148 | if ($message_commit) { |
---|
149 | $commits_todo[$author]['message'][] = $message_commit; |
---|
150 | } |
---|
151 | } |
---|
152 | $commits_todo[$author]['files'][] = $subdir . $file; |
---|
153 | if ($what === '.xml') { |
---|
154 | $message = "[Salvatore] [source:$subdir $module] Mise a jour du bilan depuis $url_gestionnaire"; |
---|
155 | } |
---|
156 | else { |
---|
157 | $message = "[Salvatore] [source:$subdir $module] Export depuis $url_gestionnaire"; |
---|
158 | } |
---|
159 | if (!empty($commit_info['lang'])) { |
---|
160 | $message .= " de la langue " . $commit_info['lang']; |
---|
161 | } |
---|
162 | if (!empty($commit_info['message'])) { |
---|
163 | $message .= "\n " . $commit_info['message']; |
---|
164 | } |
---|
165 | $commits_todo[$author]['message'][] = $message; |
---|
166 | } |
---|
167 | |
---|
168 | } |
---|
169 | |
---|
170 | } |
---|
171 | |
---|
172 | // on peut maintenant lancer les commits |
---|
173 | // ajoutons les credentials dans la source pour pouvoir commit ou push |
---|
174 | $url_with_credentials = salvatore_set_credentials($source['methode'], $source['url'], $source['module']); |
---|
175 | $parts = parse_url($url_with_credentials); |
---|
176 | if (!empty($parts['user']) and !empty($parts['pass'])){ |
---|
177 | $source['user'] = $parts['user']; |
---|
178 | $source['pass'] = $parts['pass']; |
---|
179 | } |
---|
180 | |
---|
181 | foreach ($commits_todo as $author => $commit_todo) { |
---|
182 | if (!$author) { |
---|
183 | $author = _SALVATORE_AUTHOR_COMMITS; |
---|
184 | } |
---|
185 | $message = implode("\n", $commit_todo['message']); |
---|
186 | salvatore_log("Commit de <info>$author</info> :" . implode(', ', $commit_todo['files'])); |
---|
187 | salvatore_log("\t" . str_replace("\n", "\n\t", $message)); |
---|
188 | |
---|
189 | list($res,$out) = $salvatore_commit_files($dir_depots . $source['dir_checkout'], $commit_todo['files'], $message, $author, empty($source['user']) ? null : $source['user'], empty($source['pass']) ? null : $source['pass']); |
---|
190 | salvatore_log($out); |
---|
191 | if (!$res) { |
---|
192 | salvatore_fail("[Pousseur] Erreur sur $module", "Erreur lors du commit :\n$out"); |
---|
193 | } |
---|
194 | } |
---|
195 | |
---|
196 | // tous les commits sont faits |
---|
197 | // on peut supprimer le fichier qui liste les commits |
---|
198 | @unlink($file_commit); |
---|
199 | |
---|
200 | // et push si besoin |
---|
201 | // ne fera rien en svn (deja pushe) |
---|
202 | list($res,$out) = $salvatore_push_repository($dir_depots . $source['dir_checkout'], empty($source['user']) ? null : $source['user'], empty($source['pass']) ? null : $source['pass']); |
---|
203 | salvatore_log($out); |
---|
204 | if (!$res) { |
---|
205 | salvatore_fail("[Pousseur] Erreur sur $module", "Erreur lors du commit :\n$out"); |
---|
206 | } |
---|
207 | |
---|
208 | return true; |
---|
209 | } |
---|