xaizek / rocketgit (License: AGPLv3+) (since 2018-12-09)
Light and fast Git hosting solution suitable to serve both as a hub or as a personal code storage with its tickets, pull requests, API and much more.
Commit 1f015ea792b00f3ba52cf88a7bb5b9975828fd8f

Now, bug inserts works
Author: Catalin(ux) M. BOIE
Author date (UTC): 2012-07-04 23:02
Committer name: Catalin(ux) M. BOIE
Committer date (UTC): 2012-07-04 23:02
Parent(s): 9d56cfb9031f9e09680e61e41614fce4f846e767
Signing key:
Tree: bd8b1c9b06c415bb3e6d933f12faa22028e262c5
File Lines added Lines deleted
TODO 1 0
inc/bug.inc.php 59 39
inc/repo.inc.php 21 46
inc/user/repo/bug/add/add.php 5 0
inc/util.inc.php 3 3
root/themes/default/repo/bug/bug_add.html 0 2
File TODO changed (mode: 100644) (index f1ed3fb..071cc16)
25 25
26 26
27 27 == Medium == == Medium ==
28 [ ] use do {} while(0) to respect profiling!
28 29 [ ] If we do ssh without any command, be nice and show how to clone, [ ] If we do ssh without any command, be nice and show how to clone,
29 30 show projects, rights etc. CHeck ssh.inc.php. show projects, rights etc. CHeck ssh.inc.php.
30 31 [ ] Enforce Signoff-by lines per project (a new permission) [ ] Enforce Signoff-by lines per project (a new permission)
File inc/bug.inc.php changed (mode: 100644) (index 1eb5d7b..f3976a7)
... ... function rg_bug_error()
20 20 return $rg_bug_error; return $rg_bug_error;
21 21 } }
22 22
23 /*
24 * This is valled from rg_repo_add
25 */
26 function rg_bug_add_max($db, $repo_id)
27 {
28 rg_prof_start("bug_add_max");
29
30 $sql = "INSERT INTO bugs_max (repo_id, last_bug_id)"
31 . " VALUES ($repo_id, 0)";
32 $res = rg_sql_query($db, $sql);
33 if ($res === FALSE) {
34 rg_bug_set_error("cannot init bug max (" . rg_sql_error() . ")");
35 return FALSE;
36 }
37 rg_sql_free_result($res);
38
39 rg_prof_end("bug_add_max");
40
41 return TRUE;
42 }
43
44 23 /* /*
45 24 * We want the bug number to be consecutive per project. * We want the bug number to be consecutive per project.
46 25 * This is why we use a separate table (bugs_max) to track last id. * This is why we use a separate table (bugs_max) to track last id.
 
... ... function rg_bug_next_id($db, $repo_id)
50 29 { {
51 30 rg_prof_start("bug_next_id"); rg_prof_start("bug_next_id");
52 31
53 $sql = "UPDATE bugs_max SET last_bug_id = last_bug_id + 1"
54 . " WHERE repo_id = $repo_id"
55 . " RETURNING last_bug_id AS next_bug_id";
56 $res = rg_sql_query($db, $sql);
57 if ($res === FALSE) {
58 rg_bug_set_error("cannot get/update max (" . rg_sql_error() . ")");
59 return FALSE;
60 }
61 $row = rg_sql_fetch_array($res);
62 $next_bug_id = $row['next_bug_id'];
32 $next_bug_id = FALSE;
33
34 do {
35 $sql = "UPDATE bugs_max SET last_bug_id = last_bug_id + 1"
36 . " WHERE repo_id = $repo_id"
37 . " RETURNING last_bug_id AS next_bug_id";
38 $res = rg_sql_query($db, $sql);
39 if ($res === FALSE) {
40 rg_bug_set_error("cannot get/update max (" . rg_sql_error() . ")");
41 break;
42 }
43
44 $rows = rg_sql_num_rows($res);
45 if ($rows == 1) {
46 $row = rg_sql_fetch_array($res);
47 $next_bug_id = $row['next_bug_id'];
48 }
49 rg_sql_free_result($res);
50
51 if ($rows == 1)
52 break;
53
54 /* This means this is the first bug */
55 $sql = "LOCK TABLE bugs_max IN ACCESS EXCLUSIVE MODE";
56 $res = rg_sql_query($db, $sql);
57 if ($res === FALSE) {
58 rg_bug_set_error("cannot lock max table (" . rg_sql_error() . ")");
59 break;
60 }
61
62 $sql = "INSERT INTO bugs_max (repo_id, last_bug_id)"
63 . " VALUES ($repo_id, 1)";
64 $res = rg_sql_query($db, $sql);
65 if ($res === FALSE) {
66 rg_bug_set_error("cannot insert into max table (" . rg_sql_error() . ")");
67 break;
68 }
69 rg_sql_free_result($res);
70 $next_bug_id = 1;
71 } while (0);
72
63 73 rg_log("\tDEBUG: next_bug_id=" . $next_bug_id); rg_log("\tDEBUG: next_bug_id=" . $next_bug_id);
64 rg_sql_free_result($res);
65 74
66 75 rg_prof_end("bug_next_id"); rg_prof_end("bug_next_id");
67 76
 
... ... function rg_bug_add($db, $repo_id, $uid, $data)
181 190 if ($err !== TRUE) if ($err !== TRUE)
182 191 break; break;
183 192
184 $sql = "INSERT INTO bugs force_fail_to_test_consecutive_bug_id (bug_id, itime, utime, repo_id, uid, ip"
185 . ", title, body, assigned_uid, deleted)"
186 . " VALUES ($bug_id, $itime, 0, $repo_id, $uid, '$ip'"
187 . ", '" . $e_data['title'] . "'"
193 $sql = "INSERT INTO bugs (bug_id, itime, utime, repo_id, uid"
194 . ", ip, title, body, assigned_uid, deleted)"
195 . " VALUES ($bug_id, $itime, 0, $repo_id, $uid"
196 . ", '$ip', '" . $e_data['title'] . "'"
188 197 . ", '" . $e_data['body'] . "'" . ", '" . $e_data['body'] . "'"
189 198 . ", " . $e_data['assigned_uid'] . ", " . $e_data['assigned_uid']
190 199 . ", 0)"; . ", 0)";
 
... ... function rg_bug_label_string2array($s)
432 441 { {
433 442 $ret = array(); $ret = array();
434 443
435 $s = preg_replace('/\w/', ',', $s);
444 $s = preg_replace('/\r/', ',', $s);
445 $s = preg_replace('/\n/', ',', $s);
446 rg_log("DEBUG: s=[$s]");
436 447 $list = explode(",", $s); $list = explode(",", $s);
437 448 if (empty($list)) if (empty($list))
438 449 return array(); return array();
439 450
440 foreach ($list as $label)
441 $ret[] = trim($label);
451 foreach ($list as $label) {
452 $label = trim($label);
453
454 if (empty($label))
455 continue;
456
457 $ret[] = $label;
458 }
442 459
443 460 return $ret; return $ret;
444 461 } }
 
... ... function rg_bug_label_insert($db, $repo_id, $bug_id, $labels)
498 515 rg_prof_start("bug_label_insert"); rg_prof_start("bug_label_insert");
499 516
500 517 $labels = rg_bug_label_string2array($labels); $labels = rg_bug_label_string2array($labels);
518 rg_log("DEBUG: labels: " . print_r($labels, TRUE));
501 519 if (empty($labels)) if (empty($labels))
502 520 return TRUE; return TRUE;
503 521
504 522 $existing = rg_bug_label_get($db, $repo_id, $bug_id); $existing = rg_bug_label_get($db, $repo_id, $bug_id);
523 rg_log("DEBUG: existing: " . print_r($existing, TRUE));
505 524 if ($existing === FALSE) if ($existing === FALSE)
506 525 return FALSE; return FALSE;
507 526
508 527 $diff = rg_bug_label_diff($labels, $existing); $diff = rg_bug_label_diff($labels, $existing);
528 rg_log("DEBUG: diff: " . print_r($diff, TRUE));
509 529 if (empty($diff)) if (empty($diff))
510 530 return TRUE; return TRUE;
511 531
File inc/repo.inc.php changed (mode: 100644) (index f3f2fa0..d38bac8)
... ... require_once($INC . "/user.inc.php");
6 6 require_once($INC . "/git.inc.php"); require_once($INC . "/git.inc.php");
7 7 require_once($INC . "/rights.inc.php"); require_once($INC . "/rights.inc.php");
8 8 require_once($INC . "/prof.inc.php"); require_once($INC . "/prof.inc.php");
9 require_once($INC . "/bug.inc.php");
10 9
11 10 $rg_repo_error = ""; $rg_repo_error = "";
12 11
 
... ... function rg_repo_create($db, $master, $rg_ui, $name, $max_commit_size,
224 223
225 224 // TODO: test if user is allowed to add a repository // TODO: test if user is allowed to add a repository
226 225
227 if (rg_repo_ok($name) === FALSE)
228 return FALSE;
229
230 // First, test if it already exists
231 $rr = array("user" => $rg_ui['username'], "repo" => $name);
232 $ri = rg_repo_info($db, $rr);
233 if ($ri['ok'] != 1)
234 return FALSE;
235 if ($ri['exists'] == 1) {
236 rg_repo_set_error("Repository already exists.");
237 return FALSE;
238 }
239
240 $e_name = rg_sql_escape($db, $name);
241 $e_description = rg_sql_escape($db, $description);
242
243 $itime = time();
244
245 226 $ret = FALSE; $ret = FALSE;
246 $rollback = 0;
247 227 do { do {
248 if (rg_sql_begin($db) === FALSE) {
249 rg_repo_set_error("cannot begin transaction to add a repo"
250 . " (" . rg_sql_error() . ")");
228 if (rg_repo_ok($name) === FALSE)
229 break;
230
231 // First, test if it already exists
232 $rr = array("user" => $rg_ui['username'], "repo" => $name);
233 $ri = rg_repo_info($db, $rr);
234 if ($ri['ok'] != 1)
235 break;
236 if ($ri['exists'] == 1) {
237 rg_repo_set_error("Repository already exists.");
251 238 break; break;
252 239 } }
253 240
254 $rollback = 1;
241 $e_name = rg_sql_escape($db, $name);
242 $e_description = rg_sql_escape($db, $description);
243
244 $itime = time();
255 245
256 $sql = "INSERT INTO repos (uid, master, name, itime"
257 . ", max_commit_size, description, git_dir_done, default_rights"
258 . ", max_users)"
259 . " VALUES (" . $rg_ui['uid'] . ", $master, '$e_name', $itime"
260 . ", $max_commit_size, '$e_description', 0, '$rights', $max_users)"
246 $sql = "INSERT INTO repos (uid, master, name"
247 . ", itime, max_commit_size, description, git_dir_done"
248 . ", default_rights, max_users)"
249 . " VALUES (" . $rg_ui['uid'] . ", $master, '$e_name'"
250 . ", $itime, $max_commit_size, '$e_description', 0"
251 . ", '$rights', $max_users)"
261 252 . " RETURNING repo_id"; . " RETURNING repo_id";
262 253 $res = rg_sql_query($db, $sql); $res = rg_sql_query($db, $sql);
263 254 if ($res === FALSE) { if ($res === FALSE) {
 
... ... function rg_repo_create($db, $master, $rg_ui, $name, $max_commit_size,
265 256 break; break;
266 257 } }
267 258 $row = rg_sql_fetch_array($res); $row = rg_sql_fetch_array($res);
268 $repo_id = $row['repo_id'];
259 $ret = $row['repo_id'];
269 260 rg_sql_free_result($res); rg_sql_free_result($res);
270
271 if (rg_bug_add_max($db, $repo_id)) {
272 rg_repo_set_error(rg_bug_error());
273 break;
274 }
275
276 if (rg_sql_commit($db) === FALSE) {
277 rg_repo_set_error("Cannot commit (" . rg_sql_error() . ")");
278 break;
279 }
280
281 $rollback = 0;
282 $ret = TRUE;
283 261 } while (0); } while (0);
284 262
285 if ($rollback == 1)
286 rg_sql_rollback($db);
287
288 263 // git repo creation will be delayed for serialization reasons // git repo creation will be delayed for serialization reasons
289 264 // and for permission reasons (we are 'apache' user here) // and for permission reasons (we are 'apache' user here)
290 265
File inc/user/repo/bug/add/add.php changed (mode: 100644) (index 23dd3a0..0dc7471)
... ... if ($doit == 0) {
29 29 break; break;
30 30 } }
31 31
32 if (empty($_x['body'])) {
33 $bug_errmsg[] = "Description too short.";
34 break;
35 }
36
32 37 $bug_id = rg_bug_add($db, $ri['repo_id'], $ui['uid'], $_x); $bug_id = rg_bug_add($db, $ri['repo_id'], $ui['uid'], $_x);
33 38 if ($bug_id === FALSE) { if ($bug_id === FALSE) {
34 39 $bug_errmsg[] = "Cannot add bug (" . rg_bug_error() . ")."; $bug_errmsg[] = "Cannot add bug (" . rg_bug_error() . ").";
File inc/util.inc.php changed (mode: 100644) (index 3a1e284..3130ebd)
... ... function rg_re_post($op)
135 135 function rg_re_userpage($ui) function rg_re_userpage($ui)
136 136 { {
137 137 if (!isset($ui['organization'])) { if (!isset($ui['organization'])) {
138 rg_internal_error("rg_re_userpage called with wrong ui!");
138 rg_internal_error("rg_re_userpage called with wrong ui (no org)!");
139 139 exit(1); exit(1);
140 140 } }
141 141
 
... ... function rg_re_userpage($ui)
154 154 function rg_re_repopage($ui, $repo) function rg_re_repopage($ui, $repo)
155 155 { {
156 156 if (!isset($ui['organization'])) { if (!isset($ui['organization'])) {
157 rg_internal_error("rg_re_repopage called with wrong ui!");
157 rg_internal_error("rg_re_repopage called with wrong ui (no org)!");
158 158 exit(1); exit(1);
159 159 } }
160 160
 
... ... function rg_re_repopage($ui, $repo)
169 169 function rg_re_bugpage($ui, $ri, $bug_id) function rg_re_bugpage($ui, $ri, $bug_id)
170 170 { {
171 171 if (!isset($ui['organization'])) { if (!isset($ui['organization'])) {
172 rg_internal_error("rg_re_repopage called with wrong ui!");
172 rg_internal_error("rg_re_repopage called with wrong ui (no org)!");
173 173 exit(1); exit(1);
174 174 } }
175 175
File root/themes/default/repo/bug/bug_add.html changed (mode: 100644) (index 7da46ff..d74158f)
1 <pre>@@DUMP@@</pre>
2
3 1 <div class="formarea"> <div class="formarea">
4 2
5 3 @@bug_errmsg@@ @@bug_errmsg@@
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/rocketgit

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/rocketgit

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master