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 | * Formate l'auteur en Nom <email> si jamais seul l'email est fourni |
---|
30 | * @param string $author |
---|
31 | * @return string |
---|
32 | */ |
---|
33 | function salvatore_git_format_author($author) { |
---|
34 | if (strpos($author, '<') !== false and strpos($author, '>') !== false) { |
---|
35 | return $author; |
---|
36 | } |
---|
37 | else { |
---|
38 | $name = explode('@', $author); |
---|
39 | $name = reset($name); |
---|
40 | return "$name <$author>"; |
---|
41 | } |
---|
42 | } |
---|
43 | |
---|
44 | /** |
---|
45 | * Lire la date de derniere modif d'un fichier versionne |
---|
46 | * (retourne 0 si le fichier n'est pas versionne) |
---|
47 | * @param string $dir_repo |
---|
48 | * @param string $file |
---|
49 | * @return false|int |
---|
50 | */ |
---|
51 | function salvatore_git_lastmodified_file($dir_repo, $file) { |
---|
52 | |
---|
53 | $d = getcwd(); |
---|
54 | chdir($dir_repo); |
---|
55 | $file = escapeshellarg($file); |
---|
56 | $lastmodified = exec("git log -1 -c --pretty=tformat:'%ct' $file | head -1"); |
---|
57 | $lastmodified = intval(trim($lastmodified)); |
---|
58 | chdir($d); |
---|
59 | return $lastmodified; |
---|
60 | } |
---|
61 | |
---|
62 | /** |
---|
63 | * Afficher le status d'un ou plusieurs fichiers |
---|
64 | * @param string $dir_repo |
---|
65 | * @param string|array $file_or_files |
---|
66 | * @return string |
---|
67 | */ |
---|
68 | function salvatore_git_status_file($dir_repo, $file_or_files) { |
---|
69 | |
---|
70 | if (is_array($file_or_files)) { |
---|
71 | $file_or_files = array_map('escapeshellarg', $file_or_files); |
---|
72 | $file_or_files = implode(' ', $file_or_files); |
---|
73 | } |
---|
74 | else { |
---|
75 | $file_or_files = escapeshellarg($file_or_files); |
---|
76 | } |
---|
77 | |
---|
78 | $d = getcwd(); |
---|
79 | chdir($dir_repo); |
---|
80 | $output = array(); |
---|
81 | exec("git status --short $file_or_files 2>&1", $output); |
---|
82 | //exec("svn status $files_list 2>&1", $output); |
---|
83 | chdir($d); |
---|
84 | return implode("\n", $output); |
---|
85 | } |
---|
86 | |
---|
87 | /** |
---|
88 | * Commit une liste de fichiers avec un message et auteur fourni |
---|
89 | * on utilise pas $user et $pass en git pour commit |
---|
90 | * @param string $dir_repo |
---|
91 | * @param array $files |
---|
92 | * @param string $message |
---|
93 | * @param string $author |
---|
94 | * @param string $user |
---|
95 | * @param string $pass |
---|
96 | * @return array |
---|
97 | */ |
---|
98 | function salvatore_git_commit_files($dir_repo, $files, $message, $author, $user=null, $pass=null) { |
---|
99 | $files = array_map('escapeshellarg', $files); |
---|
100 | $files = implode(' ', $files); |
---|
101 | |
---|
102 | $d = getcwd(); |
---|
103 | chdir($dir_repo); |
---|
104 | $output = array(); |
---|
105 | $res = true; |
---|
106 | // on ajoute tous les fichiers pour commit |
---|
107 | $commands = [ |
---|
108 | "git add $files 2>&1", |
---|
109 | "git commit -m " . escapeshellarg($message)." --author=".escapeshellarg(salvatore_git_format_author($author)) . " 2>&1", |
---|
110 | ]; |
---|
111 | |
---|
112 | foreach ($commands as $command) { |
---|
113 | $output[] = "> $command"; |
---|
114 | $return_var = 0; |
---|
115 | exec($command, $output, $return_var); |
---|
116 | // si une erreur a eu lieu le signaler dans le retour |
---|
117 | if ($return_var) { |
---|
118 | $res = false; |
---|
119 | } |
---|
120 | } |
---|
121 | chdir($d); |
---|
122 | |
---|
123 | return array($res, implode("\n", $output)); |
---|
124 | } |
---|
125 | |
---|
126 | /** |
---|
127 | * on utilise pas $user et $pass en git pour push car ils sont dans le remote si c'est un https |
---|
128 | * et si c'est ssh il faut une cle pour le user www-data |
---|
129 | * |
---|
130 | * @param string $dir_repo |
---|
131 | * @param null $user |
---|
132 | * @param null $pass |
---|
133 | * @return array |
---|
134 | */ |
---|
135 | function salvatore_git_push_repository($dir_repo, $user=null, $pass=null) { |
---|
136 | $d = getcwd(); |
---|
137 | chdir($dir_repo); |
---|
138 | $output = array(); |
---|
139 | $res = true; |
---|
140 | // on ajoute tous les fichiers pour commit |
---|
141 | $commands = [ |
---|
142 | "git pull --rebase 2>&1", |
---|
143 | // TODO : activer le push quand on sera en prod |
---|
144 | //"git push 2>&1", |
---|
145 | ]; |
---|
146 | |
---|
147 | foreach ($commands as $command) { |
---|
148 | $output[] = "> $command"; |
---|
149 | $return_var = 0; |
---|
150 | exec($command, $output, $return_var); |
---|
151 | // si une erreur a eu lieu le signaler dans le retour |
---|
152 | if ($return_var) { |
---|
153 | $res = false; |
---|
154 | } |
---|
155 | } |
---|
156 | chdir($d); |
---|
157 | |
---|
158 | return array($res, implode("\n", $output)); |
---|
159 | } |
---|