forked from jpetso/versioncontrol
-
Notifications
You must be signed in to change notification settings - Fork 1
/
versioncontrol-backend.inc
1118 lines (1016 loc) · 46.4 KB
/
versioncontrol-backend.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// $Id$
/**
* @file
* Version Control API - An interface to version control systems
* whose functionality is provided by pluggable back-end modules.
*
* This file contains the backend-only side of the Version Control API.
* It is public API, but not meant to be used by non-backend modules.
*
* Copyright 2007, 2008 by Jakob Petsovits ("jpetso", http://drupal.org/user/56020)
*/
/**
* Determine if a commit, branch or tag operation may be executed or not.
* Call this function inside a pre-commit hook.
*
* @param $operation
* A single operation array like the ones returned by
* versioncontrol_get_operations(), but leaving out on a few details that
* will instead be determined by this function. This array describes
* the operation that is about to happen. Here's the allowed elements:
*
* - 'type': The type of the operation - one of the
* VERSIONCONTROL_OPERATION_{COMMIT,BRANCH,TAG} constants.
* - 'repository': The repository where this operation occurs,
* given as a structured array, like the return value
* of versioncontrol_get_repository().
* You can either pass this or 'repo_id'.
* - 'repo_id': The repository where this operation occurs, given as a simple
* integer id. You can either pass this or 'repository'.
* - 'uid': The Drupal user id of the committer. Passing this is optional -
* if it isn't set, this function will determine the uid.
* - 'username': The system specific VCS username of the committer.
* - 'message': The log message for the commit, tag or branch operation.
* If a version control system doesn't support messages for the current
* operation type, this element must not be set. Operations with
* log messages that are set but empty will be denied access.
*
* - 'labels': An array of branches or tags that will be affected by this
* operation. Branch and tag operations are known to only affect one
* branch or tag, so for these there will be only one element (with 0
* as key) in 'labels'. Commits might affect any number of branches,
* including none. Commits that emulate branches and/or tags (like
* in Subversion, where they're not a native concept) can also include
* add/delete/move operations for labels, as detailed below.
* Mind that the main development branch - e.g. 'HEAD', 'trunk'
* or 'master' - is also considered a branch. Each element in 'labels'
* is a structured array with the following keys:
*
* - 'name': The branch or tag name (a string).
* - 'type': Whether this label is a branch (indicated by the
* VERSIONCONTROL_OPERATION_BRANCH constant) or a tag
* (VERSIONCONTROL_OPERATION_TAG).
* - 'action': Specifies what happened to this label in this operation.
* For plain commits, this is always VERSIONCONTROL_ACTION_MODIFIED.
* For branch or tag operations (or commits that emulate those),
* it can be either VERSIONCONTROL_ACTION_ADDED or
* VERSIONCONTROL_ACTION_DELETED.
*
* @param $operation_items
* A structured array containing the exact details of what is about to happen
* to each item in this commit. The structure of this array is the same as
* the return value of versioncontrol_get_operation_items() - that is,
* elements for 'type', 'path', 'revision', 'action', 'source_items' and
* 'replaced_item' - but doesn't include the 'item_revision_id' element as
* there's no relation to the database yet.
*
* The 'action', 'source_items', 'replaced_item' and 'revision' elements
* of each item are optional and may be left unset.
*
* @return
* TRUE if the operation may happen, or FALSE if not.
* If FALSE is returned, you can retrieve the concerning error messages
* by calling versioncontrol_get_access_errors().
*/
function versioncontrol_has_write_access($operation, $operation_items) {
$operation = _versioncontrol_fill_operation($operation);
// If we can't determine this operation's repository,
// we can't really allow the operation in the first place.
if (!isset($operation['repository'])) {
switch ($operation['type']) {
case VERSIONCONTROL_OPERATION_COMMIT:
$type = t('commit');
break;
case VERSIONCONTROL_OPERATION_BRANCH:
$type = t('branch');
break;
case VERSIONCONTROL_OPERATION_TAG:
$type = t('tag');
break;
}
_versioncontrol_access_errors(array(t(
'** ERROR: Version Control API cannot determine a repository
** for the !commit-branch-or-tag information given by the VCS backend.',
array('!commit-branch-or-tag' => $type)
)));
return FALSE;
}
// If the user doesn't have commit access at all, we can't allow this as well.
$repo_data = $operation['repository']['data']['versioncontrol'];
if (!$repo_data['allow_unauthorized_access']) {
if (!versioncontrol_is_account_authorized($operation['repository'], $operation['uid'])) {
_versioncontrol_access_errors(array(t(
'** ERROR: !user does not have commit access to this repository.',
array('!user' => $operation['username'])
)));
return FALSE;
}
}
// Don't let people do empty log messages, that's as evil as it gets.
if (isset($operation['message']) && empty($operation['message'])) {
_versioncontrol_access_errors(array(
t('** ERROR: You have to provide a log message.'),
));
return FALSE;
}
// Also see if other modules have any objections.
$error_messages = array();
foreach (module_implements('versioncontrol_write_access') as $module) {
$function = $module .'_versioncontrol_write_access';
// If at least one hook_versioncontrol_write_access returns TRUE,
// the commit goes through. (This is for admin or sandbox exceptions.)
$outcome = $function($operation, $operation_items);
if ($outcome === TRUE) {
return TRUE;
}
else { // if !TRUE, $outcome is required to be an array with error messages
$error_messages = array_merge($error_messages, $outcome);
}
}
// Let the operation fail if there's more than zero error messages.
if (!empty($error_messages)) {
_versioncontrol_access_errors($error_messages);
return FALSE;
}
return TRUE;
}
/**
* If versioncontrol_has_commit_access(), versioncontrol_has_branch_access()
* or versioncontrol_has_tag_access() returned FALSE, you can use this function
* to retrieve the list of error messages from the various access checks.
* The error messages do not include trailing linebreaks, it is expected that
* those are inserted by the caller.
*/
function versioncontrol_get_access_errors() {
return _versioncontrol_access_errors();
}
/**
* Retrieve or set the list of access errors.
*/
function _versioncontrol_access_errors($new_messages = NULL) {
static $error_messages = array();
if (!isset($error_messages)) {
$error_messages = array();
}
if (isset($new_messages)) {
$error_messages = $new_messages;
}
return $error_messages;
}
/**
/**
* Insert a commit, branch or tag operation into the database, and call the
* necessary module hooks. Only call this function after the operation has been
* successfully executed.
*
* @param $operation
* A single operation array like the ones returned by
* versioncontrol_get_operations(), but leaving out on a few details that
* will instead be determined by this function. Here's the allowed elements:
*
* - 'type': The type of the operation - one of the
* VERSIONCONTROL_OPERATION_{COMMIT,BRANCH,TAG} constants.
* - 'repository': The repository where this operation occurs,
* given as a structured array, like the return value
* of versioncontrol_get_repository().
* You can either pass this or 'repo_id'.
* - 'repo_id': The repository where this operation occurs, given as a simple
* integer id. You can either pass this or 'repository'.
* - 'date': The time when the operation was performed, given as
* Unix timestamp. (For commits, this is the time when the revision
* was committed, whereas for branch/tag operations it is the time
* when the files were branched or tagged.)
* - 'uid': The Drupal user id of the committer. Passing this is optional -
* if it isn't set, this function will determine the uid.
* - 'username': The system specific VCS username of the user who executed
* this operation. For distributed version control systems, this should
* be the author, not the committer.
* - 'message': The log message for the commit, tag or branch operation.
* If a version control system doesn't support messages for the current
* operation type, this element should be empty.
* - 'revision': The VCS specific repository-wide revision identifier,
* like '' in CVS, '27491' in Subversion or some SHA-1 key in various
* distributed version control systems. If there is no such revision
* (which may be the case for version control systems that don't support
* atomic commits) then the 'revision' element is an empty string.
* For branch and tag operations, this element indicates the
* (repository-wide) revision of the files that were branched or tagged.
*
* - 'labels': An array of branches or tags that were affected by this
* operation. Branch and tag operations are known to only affect one
* branch or tag, so for these there will be only one element (with 0
* as key) in 'labels'. Commits might affect any number of branches,
* including none. Commits that emulate branches and/or tags (like
* in Subversion, where they're not a native concept) can also include
* add/delete/move operations for labels, as detailed below.
* Mind that the main development branch - e.g. 'HEAD', 'trunk'
* or 'master' - is also considered a branch. Each element in 'labels'
* is a structured array with the following keys:
*
* - 'name': The branch or tag name (a string).
* - 'type': Whether this label is a branch (indicated by the
* VERSIONCONTROL_OPERATION_BRANCH constant) or a tag
* (VERSIONCONTROL_OPERATION_TAG).
* - 'action': Specifies what happened to this label in this operation.
* For plain commits, this is always VERSIONCONTROL_ACTION_MODIFIED.
* For branch or tag operations (or commits that emulate those),
* it can be either VERSIONCONTROL_ACTION_ADDED or
* VERSIONCONTROL_ACTION_DELETED.
*
* @param $operation_items
* A structured array containing the exact details of happened to each
* item in this operation. The structure of this array is the same as
* the return value of versioncontrol_get_operation_items() - that is,
* elements for 'type', 'path' and 'revision' - but doesn't include the
* 'item_revision_id' element, that one will be filled in by this function.
*
* For commit operations, you also have to fill in the 'action' and
* 'source_items' elements (and optionally 'replaced_item') that are also
* described in the versioncontrol_get_operation_items() API documentation.
* The 'line_changes' element, as in versioncontrol_get_operation_items(),
* is optional to provide.
*
* This parameter is passed by reference as the insert operation will
* check the validity of a few item properties and will also assign an
* 'item_revision_id' property to each of the given items. So when this
* function returns with a result other than NULL, the @p $operation_items
* array will also be up to snuff for further processing.
*
* @return
* The finalized operation array, with all of the 'vc_op_id', 'repository'
* and 'uid' properties filled in, and 'repo_id' removed if it existed before.
* Labels are now equipped with an additional 'label_id' property.
* (For more info on these labels, see the API documentation for
* versioncontrol_get_operations() and versioncontrol_get_operation_items().)
* In case of an error, NULL is returned instead of the operation array.
*/
function versioncontrol_insert_operation($operation, &$operation_items) {
$operation = _versioncontrol_fill_operation($operation, TRUE);
if (!isset($operation['repository'])) {
return NULL;
}
// Ok, everything's there, insert the operation into the database.
$operation['repo_id'] = $operation['repository']['repo_id']; // for drupal_write_record()
drupal_write_record('versioncontrol_operations', $operation);
unset($operation['repo_id']);
// drupal_write_record() has now added the 'vc_op_id' to the $operation array.
// Insert labels that are attached to the operation.
$operation['labels'] = _versioncontrol_set_operation_labels($operation, $operation['labels']);
$vcs = $operation['repository']['vcs'];
// So much for the operation itself, now the more verbose part: items.
ksort($operation_items); // similar paths should be next to each other
foreach ($operation_items as $path => $item) {
$item = _versioncontrol_sanitize_item($operation['repository'], $item);
$item = _versioncontrol_ensure_item_revision(
$operation['repository'], $item
);
_versioncontrol_insert_operation_item(
$operation, $item, VERSIONCONTROL_OPERATION_MEMBER_ITEM
);
$item['selected_label'] = new stdClass();
$item['selected_label']->get_from = 'operation';
$item['selected_label']->successor_item = &$operation;
// If we've got source items (which is the case for commit operations),
// add them to the item revisions and source revisions tables as well.
foreach ($item['source_items'] as $key => $source_item) {
$source_item = _versioncontrol_ensure_item_revision(
$operation['repository'], $source_item
);
_versioncontrol_insert_source_revision($item, $source_item, $item['action']);
// Cache other important items in the operations table for 'path' search
// queries, because joining the source revisions table is too expensive.
switch ($item['action']) {
case VERSIONCONTROL_ACTION_MOVED:
case VERSIONCONTROL_ACTION_COPIED:
case VERSIONCONTROL_ACTION_MERGED:
if ($item['path'] != $source_item['path']) {
_versioncontrol_insert_operation_item($operation,
$source_item, VERSIONCONTROL_OPERATION_CACHED_AFFECTED_ITEM);
}
break;
default: // No additional caching for added, modified or deleted items.
break;
}
$source_item['selected_label'] = new stdClass();
$source_item['selected_label']->get_from = 'other_item';
$source_item['selected_label']->other_item = &$item;
$source_item['selected_label']->other_item_tags = array('successor_item');
$item['source_items'][$key] = $source_item;
}
// Plus a special case for the "added" action, as it needs an entry in the
// source items table but contains no items in the 'source_items' property.
if ($item['action'] == VERSIONCONTROL_ACTION_ADDED) {
_versioncontrol_insert_source_revision($item, 0, $item['action']);
}
// If we've got a replaced item (might happen for copy/move commits),
// add it to the item revisions and source revisions table as well.
if (isset($item['replaced_item'])) {
$item['replaced_item'] = _versioncontrol_ensure_item_revision(
$operation['repository'], $item['replaced_item']
);
_versioncontrol_insert_source_revision(
$item, $item['replaced_item'], VERSIONCONTROL_ACTION_REPLACED
);
$item['replaced_item']['selected_label'] = new stdClass();
$item['replaced_item']['selected_label']->get_from = 'other_item';
$item['replaced_item']['selected_label']->other_item = &$item;
$item['replaced_item']['selected_label']->other_item_tags = array('successor_item');
}
$operation_items[$path] = $item;
}
// Notify the backend first.
if (versioncontrol_backend_implements($vcs, 'operation')) {
_versioncontrol_call_backend($vcs, 'operation', array(
'insert', $operation, $operation_items
));
}
// Everything's done, let the world know about it!
module_invoke_all('versioncontrol_operation',
'insert', $operation, $operation_items
);
// This one too, as there is also an update function & hook for it.
// Pretend that the labels didn't exist beforehand.
$labels = $operation['labels'];
$operation['labels'] = array();
module_invoke_all('versioncontrol_operation_labels',
'insert', $operation, $labels
);
$operation['labels'] = $labels;
// Rules integration, because we like to enable people to be flexible.
if (module_exists('rules')) {
rules_invoke_event('versioncontrol_operation_insert', array(
'operation' => $operation,
'items' => $operation_items,
));
}
return $operation;
}
/**
* Write @p $labels to the database as set of affected labels of the
* given @p $operation. Label ids are not required to exist yet.
*
* @return
* The set of labels, all of them with 'label_id' filled in.
*/
function _versioncontrol_set_operation_labels($operation, $labels) {
db_query("DELETE FROM {versioncontrol_operation_labels}
WHERE vc_op_id = %d", $operation['vc_op_id']);
foreach ($labels as &$label) {
$label = versioncontrol_ensure_label($operation['repository'], $label);
db_query("INSERT INTO {versioncontrol_operation_labels}
(vc_op_id, label_id, action) VALUES (%d, %d, %d)",
$operation['vc_op_id'], $label['label_id'], $label['action']);
}
return $labels;
}
/**
* Replace the set of affected labels of the given @p $operation with the one
* in @p $labels. If any of the given labels does not yet exist in the
* database, a database entry (including new 'label_id' array element) will
* be written as well.
*
* @return
* The updated operation, containing the new set of labels
* in $operation['labels'].
*/
function versioncontrol_update_operation_labels($operation, $labels) {
module_invoke_all('versioncontrol_operation_labels',
'update', $operation, $labels
);
$operation['labels'] = _versioncontrol_set_operation_labels($operation, $labels);
return $operation;
}
/**
* Check and if necessary correct item arrays so that item type and
* the number of source items correspond to specified actions.
*/
function _versioncontrol_sanitize_item($repository, $item) {
if (isset($item['action'])) {
// Make sure the number of source items corresponds with the action.
switch ($item['action']) {
// No source items for "added" actions.
case VERSIONCONTROL_ACTION_ADDED:
if (count($item['source_items']) > 0) {
_versioncontrol_bad_item_warning($repository, $item, 'At least one source item exists although the "added" action was set (which mandates an empty \'source_items\' array.');
$item['source_items'] = array(reset($item['source_items'])); // first item
$item['source_items'] = array();
}
break;
// Exactly one source item for actions other than "added", "merged" or "other".
case VERSIONCONTROL_ACTION_MODIFIED:
case VERSIONCONTROL_ACTION_MOVED:
case VERSIONCONTROL_ACTION_COPIED:
case VERSIONCONTROL_ACTION_DELETED:
if (count($item['source_items']) > 1) {
_versioncontrol_bad_item_warning($repository, $item, 'More than one source item exists although a "modified", "moved", "copied" or "deleted" action was set (which allows only one of those).');
$item['source_items'] = array(reset($item['source_items'])); // first item
}
// fall through
case VERSIONCONTROL_ACTION_MERGED:
if (empty($item['source_items'])) {
_versioncontrol_bad_item_warning($repository, $item, 'No source item exists although a "modified", "moved", "copied", "merged" or "deleted" action was set (which requires at least or exactly one of those).');
}
break;
default:
break;
}
// For a "delete" action, make sure the item type is also a "deleted" one.
// That's quite a minor error, so don't complain but rather fix it quietly.
if ($item['action'] == VERSIONCONTROL_ACTION_DELETED) {
if ($item['type'] == VERSIONCONTROL_ITEM_FILE) {
$item['type'] = VERSIONCONTROL_ITEM_FILE_DELETED;
}
else if ($item['type'] == VERSIONCONTROL_ITEM_DIRECTORY) {
$item['type'] = VERSIONCONTROL_ITEM_DIRECTORY_DELETED;
}
}
}
return $item;
}
/**
* Print out a "Bad item received from VCS backend" warning to watchdog.
*/
function _versioncontrol_bad_item_warning($repository, $item, $message) {
watchdog('special', "<p>Bad item received from VCS backend: !message</p>
<pre>Item array: !item\nRepository array: !repository</pre>", array(
'!message' => $message,
'!item' => print_r($item, TRUE),
'!repository' => print_r($repository, TRUE),
), WATCHDOG_ERROR
);
}
/**
* Insert an item entry into the {versioncontrol_item_revisions} table,
* or retrieve the same one that's already there.
*
* @return
* The @p $item variable, enhanced with the newly added property
* 'item_revision_id' specifying the database identifier for that revision.
* If the 'type' property of the passed item is different from the one in
* the database, then the new value will be written to the database.
*/
function _versioncontrol_ensure_item_revision($repository, $item) {
$result = db_query(
"SELECT item_revision_id, type
FROM {versioncontrol_item_revisions}
WHERE repo_id = %d AND path = '%s' AND revision = '%s'",
$repository['repo_id'], $item['path'], $item['revision']
);
while ($item_revision = db_fetch_object($result)) {
// Replace / fill in properties that were not in the WHERE condition.
$item['item_revision_id'] = $item_revision->item_revision_id;
if ($item['type'] == $item_revision->type) {
return $item; // no changes needed - otherwise, replace the existing item.
}
}
// The item doesn't yet exist in the database, so create it.
return _versioncontrol_insert_item_revision($repository, $item);
}
function _versioncontrol_insert_item_revision($repository, $item) {
$item['repo_id'] = $repository['repo_id']; // for drupal_write_record() only
if (isset($item['item_revision_id'])) {
// The item already exists in the database, update the record.
drupal_write_record('versioncontrol_item_revisions', $item, 'item_revision_id');
}
else {
// The label does not yet exist, create it.
// drupal_write_record() also adds the 'item_revision_id' to the $item array.
drupal_write_record('versioncontrol_item_revisions', $item);
}
unset($item['repo_id']);
return $item;
}
/**
* Insert an item entry into the {versioncontrol_operation_items} table.
* The item is expected to have an 'item_revision_id' property already.
*/
function _versioncontrol_insert_operation_item($operation, $item, $type) {
// Before inserting that item entry, make sure it doesn't exist already.
db_query("DELETE FROM {versioncontrol_operation_items}
WHERE vc_op_id = %d AND item_revision_id = %d",
$operation['vc_op_id'], $item['item_revision_id']);
db_query("INSERT INTO {versioncontrol_operation_items}
(vc_op_id, item_revision_id, type) VALUES (%d, %d, %d)",
$operation['vc_op_id'], $item['item_revision_id'], $type);
}
/**
* Insert an item entry into the {versioncontrol_source_items} table.
* Both target and source items are expected to have an 'item_revision_id'
* property already. For "added" actions, it's also possible to pass 0 as the
* @p $source_item parameter instead of a full item array.
*/
function _versioncontrol_insert_source_revision($item, $source_item, $action) {
if ($action == VERSIONCONTROL_ACTION_ADDED && $source_item === 0) {
$source_item = array('item_revision_id' => 0);
}
// Before inserting that item entry, make sure it doesn't exist already.
db_query("DELETE FROM {versioncontrol_source_items}
WHERE item_revision_id = %d AND source_item_revision_id = %d",
$item['item_revision_id'], $source_item['item_revision_id']);
$line_changes = !empty($item['line_changes']);
db_query("INSERT INTO {versioncontrol_source_items}
(item_revision_id, source_item_revision_id, action,
line_changes_recorded, line_changes_added, line_changes_removed)
VALUES (%d, %d, %d, %d, %d, %d)",
$item['item_revision_id'], $source_item['item_revision_id'],
$action, ($line_changes ? 1 : 0),
($line_changes ? $item['line_changes']['added'] : 0),
($line_changes ? $item['line_changes']['removed'] : 0));
}
/**
* Delete a commit, a branch operation or a tag operation from the database,
* and call the necessary hooks.
*
* @param $operation
* The commit, branch operation or tag operation array containing
* the operation that should be deleted.
*/
function versioncontrol_delete_operation($operation) {
$operation_items = versioncontrol_get_operation_items($operation);
// As versioncontrol_update_operation_labels() provides an update hook for
// operation labels, we should also have a delete hook for completeness.
module_invoke_all('versioncontrol_operation_labels',
'delete', $operation, array());
// Announce deletion of the operation before anything has happened.
// Calls hook_versioncontrol_commit(), hook_versioncontrol_branch_operation()
// or hook_versioncontrol_tag_operation().
module_invoke_all('versioncontrol_operation',
'delete', $operation, $operation_items);
$vcs = $operation['repository']['vcs'];
// Provide an opportunity for the backend to delete its own stuff.
if (versioncontrol_backend_implements($vcs, 'operation')) {
_versioncontrol_call_backend($vcs, 'operation', array(
'delete', $operation, $operation_items
));
}
db_query('DELETE FROM {versioncontrol_operation_labels}
WHERE vc_op_id = %d', $operation['vc_op_id']);
db_query('DELETE FROM {versioncontrol_operation_items}
WHERE vc_op_id = %d', $operation['vc_op_id']);
db_query('DELETE FROM {versioncontrol_operations}
WHERE vc_op_id = %d', $operation['vc_op_id']);
}
/**
* Fill in various operation properties into the given operation array
* (commit, branch op or tag op), in case those values are not given.
*
* @param $operation
* The plain operation array that might lack have some properties yet.
* @param $include_unauthorized
* If FALSE, the 'uid' property will receive a value of 0 for known
* but unauthorized users. If TRUE, all known users are mapped to their uid.
*
* @return
* The completed commit, branch operation or tag operation array.
* Check on isset($operation['repository']) before proceeding.
*/
function _versioncontrol_fill_operation(&$operation, $include_unauthorized = FALSE) {
// If not already there, retrieve the full repository object.
if (!isset($operation['repository']) && isset($operation['repo_id'])) {
$operation['repository'] = versioncontrol_get_repository($operation['repo_id']);
unset($operation['repo_id']);
}
// If not already there, retrieve the Drupal user id of the committer.
if (!isset($operation['uid'])) {
$uid = versioncontrol_get_account_uid_for_username(
$operation['repository']['repo_id'], $operation['username'], $include_unauthorized
);
// If no uid could be retrieved, blame the commit on user 0 (anonymous).
$operation['uid'] = isset($uid) ? $uid : 0;
}
// For insertions (which have 'date' set, as opposed to write access checks),
// fill in the log message if it's unset. We don't want to do this for
// write access checks because empty messages are denied access,
// which requires distinguishing between unset and empty.
if (isset($operation['date']) && !isset($operation['message'])) {
$operation['message'] = '';
}
return $operation;
}
/**
* Insert a VCS user account into the database,
* and call the necessary module hooks.
*
* @param $repository
* The repository where the user has its VCS account.
* @param $uid
* The Drupal user id corresponding to the VCS username.
* @param $username
* The VCS specific username (a string).
* @param $additional_data
* An array of additional author information. Modules can fill this array
* by implementing hook_versioncontrol_account_submit().
*/
function versioncontrol_insert_account($repository, $uid, $username, $additional_data = array()) {
db_query(
"INSERT INTO {versioncontrol_accounts} (uid, repo_id, username)
VALUES (%d, %d, '%s')", $uid, $repository['repo_id'], $username
);
// Provide an opportunity for the backend to add its own stuff.
if (versioncontrol_backend_implements($repository['vcs'], 'account')) {
_versioncontrol_call_backend(
$repository['vcs'], 'account',
array('insert', $uid, $username, $repository, $additional_data)
);
}
// Update the operations table.
db_query("UPDATE {versioncontrol_operations}
SET uid = %d
WHERE username = '%s' AND repo_id = %d",
$uid, $username, $repository['repo_id']);
// Everything's done, let the world know about it!
module_invoke_all('versioncontrol_account',
'insert', $uid, $username, $repository, $additional_data
);
watchdog('special',
'Version Control API: added @username account in repository @repository',
array('@username' => $username, '@repository' => $repository['name']),
WATCHDOG_NOTICE, l('view', 'admin/project/versioncontrol-accounts')
);
}
/**
* Update a VCS user account in the database, and call the necessary
* module hooks. The @p $repository and @p $uid parameters must stay the same
* values as the one given on account creation, whereas @p $username and
* @p $additional_data may change.
*
* @param $uid
* The Drupal user id corresponding to the VCS username.
* @param $username
* The VCS specific username (a string).
* @param $repository
* The repository where the user has its VCS account.
* @param $additional_data
* An array of additional author information. Modules can fill this array
* by implementing hook_versioncontrol_account_submit().
*/
function versioncontrol_update_account($repository, $uid, $username, $additional_data = array()) {
$old_username = versioncontrol_get_account_username_for_uid($repository['repo_id'], $uid, TRUE);
$username_changed = ($username != $old_username);
if ($username_changed) {
db_query("UPDATE {versioncontrol_accounts}
SET username = '%s'
WHERE uid = %d AND repo_id = %d",
$username, $uid, $repository['repo_id']
);
}
// Provide an opportunity for the backend to add its own stuff.
if (versioncontrol_backend_implements($repository['vcs'], 'account')) {
_versioncontrol_call_backend(
$repository['vcs'], 'account',
array('update', $uid, $username, $repository, $additional_data)
);
}
// Update the operations table.
if ($username_changed) {
db_query("UPDATE {versioncontrol_operations}
SET uid = 0
WHERE uid = %d AND repo_id = %d",
$uid, $repository['repo_id']);
db_query("UPDATE {versioncontrol_operations}
SET uid = %d
WHERE username = '%s' AND repo_id = %d",
$uid, $username, $repository['repo_id']);
}
// Everything's done, let the world know about it!
module_invoke_all('versioncontrol_account',
'update', $uid, $username, $repository, $additional_data
);
watchdog('special',
'Version Control API: updated @username account in repository @repository',
array('@username' => $username, '@repository' => $repository['name']),
WATCHDOG_NOTICE, l('view', 'admin/project/versioncontrol-accounts')
);
}
/**
* Delete a VCS user account from the database, set all commits with this
* account as author to user 0 (anonymous), and call the necessary hooks.
*
* @param $repository
* The repository where the user has its VCS account.
* @param $uid
* The Drupal user id corresponding to the VCS username.
* @param $username
* The VCS specific username (a string).
*/
function versioncontrol_delete_account($repository, $uid, $username) {
// Update the operations table.
db_query('UPDATE {versioncontrol_operations}
SET uid = 0
WHERE uid = %d AND repo_id = %d',
$uid, $repository['repo_id']);
// Announce deletion of the account before anything has happened.
module_invoke_all('versioncontrol_account',
'delete', $uid, $username, $repository, array()
);
// Provide an opportunity for the backend to delete its own stuff.
if (versioncontrol_backend_implements($repository['vcs'], 'account')) {
_versioncontrol_call_backend(
$repository['vcs'], 'account',
array('delete', $uid, $username, $repository, array())
);
}
db_query('DELETE FROM {versioncontrol_accounts}
WHERE uid = %d AND repo_id = %d',
$uid, $repository['repo_id']);
watchdog('special',
'Version Control API: deleted @username account in repository @repository',
array('@username' => $username, '@repository' => $repository['name']),
WATCHDOG_NOTICE, l('view', 'admin/project/versioncontrol-accounts')
);
}
/**
* Insert a repository into the database, and call the necessary hooks.
*
* @param $repository
* The repository array containing the new or existing repository.
* It's a single repository array like the one returned by
* versioncontrol_get_repository(), so it consists of the following elements:
*
* - 'name': The user-visible name of the repository.
* - 'vcs': The unique string identifier of the version control system
* that powers this repository.
* - 'root': The root directory of the repository. In most cases,
* this will be a local directory (e.g. '/var/repos/drupal'),
* but it may also be some specialized string for remote repository
* access. How this string may look like depends on the backend.
* - 'authorization_method': The string identifier of the repository's
* authorization method, that is, how users may register accounts
* in this repository. Modules can provide their own methods
* by implementing hook_versioncontrol_authorization_methods().
* - 'url_backend': The prefix (excluding the trailing underscore)
* for URL backend retrieval functions.
* - 'data': An array where modules can store additional information about
* the repository, for settings or other data.
* - '[xxx]_specific': An array of VCS specific additional repository
* information. How this array looks like is defined by the
* corresponding backend module (versioncontrol_[xxx]).
* If the backend has registered itself with the
* VERSIONCONTROL_FLAG_AUTOADD_REPOSITORIES option, all items of
* this array will automatically be inserted into the
* {versioncontrol_[xxx]_commits} table.
* (Deprecated, to be replaced by the more general 'data' property.)
*
* @param $repository_urls
* An array of repository viewer URLs. How this array looks like is
* defined by the corresponding URL backend.
*
* @return
* The finalized repository array, including the 'repo_id' element.
*/
function versioncontrol_insert_repository($repository, $repository_urls) {
if (isset($repository['repo_id'])) {
// This is a new repository, it's not supposed to have a repo_id yet.
unset($repository['repo_id']);
}
drupal_write_record('versioncontrol_repositories', $repository);
// drupal_write_record() has now added the 'repo_id' to the $repository array.
$repository_urls['repo_id'] = $repository['repo_id']; // for drupal_write_record()
drupal_write_record('versioncontrol_repository_urls', $repository_urls);
unset($repository_urls['repo_id']);
// Auto-add repository info from $repository['[xxx]_specific'] into the database.
$backends = versioncontrol_get_backends();
$vcs = $repository['vcs'];
$is_autoadd = in_array(VERSIONCONTROL_FLAG_AUTOADD_REPOSITORIES,
$backends[$vcs]['flags']);
if ($is_autoadd) {
$table_name = 'versioncontrol_'. $vcs .'_repositories';
$elements = $repository[$vcs .'_specific'];
$elements['repo_id'] = $repository['repo_id'];
_versioncontrol_db_insert_additions($table_name, $elements);
}
// Provide an opportunity for the backend to add its own stuff.
if (versioncontrol_backend_implements($vcs, 'repository')) {
_versioncontrol_call_backend($vcs, 'repository', array('insert', $repository));
}
// Everything's done, let the world know about it!
module_invoke_all('versioncontrol_repository', 'insert', $repository);
watchdog('special',
'Version Control API: added repository @repository',
array('@repository' => $repository['name']),
WATCHDOG_NOTICE, l('view', 'admin/project/versioncontrol-repositories')
);
return $repository;
}
/**
* Update a repository in the database, and call the necessary hooks.
* The 'repo_id' and 'vcs' properties of the repository array must stay
* the same as the ones given on repository creation,
* whereas all other values may change.
*
* @param $repository
* The repository array containing the new or existing repository.
* It's a single repository array like the one returned by
* versioncontrol_get_repository(), so it consists of the following elements:
*
* - 'repo_id': The unique repository id.
* - 'name': The user-visible name of the repository.
* - 'vcs': The unique string identifier of the version control system
* that powers this repository.
* - 'root': The root directory of the repository. In most cases,
* this will be a local directory (e.g. '/var/repos/drupal'),
* but it may also be some specialized string for remote repository
* access. How this string may look like depends on the backend.
* - 'authorization_method': The string identifier of the repository's
* authorization method, that is, how users may register accounts
* in this repository. Modules can provide their own methods
* by implementing hook_versioncontrol_authorization_methods().
* - 'url_backend': The prefix (excluding the trailing underscore)
* for URL backend retrieval functions.
* - 'data': An array where modules can store additional information about
* the repository, for settings or other data.
* - '[xxx]_specific': An array of VCS specific additional repository
* information. How this array looks like is defined by the
* corresponding backend module (versioncontrol_[xxx]).
* If the backend has registered itself with the
* VERSIONCONTROL_FLAG_AUTOADD_REPOSITORIES option, all items of
* this array will automatically be inserted into the
* {versioncontrol_[xxx]_commits} table.
* (Deprecated, to be replaced by the more general 'data' property.)
*
* @param $repository_urls
* An array of repository viewer URLs. How this array looks like is
* defined by the corresponding URL backend.
*/
function versioncontrol_update_repository($repository, $repository_urls) {
drupal_write_record('versioncontrol_repositories', $repository, 'repo_id');
$repository_urls['repo_id'] = $repository['repo_id']; // for drupal_write_record()
drupal_write_record('versioncontrol_repository_urls', $repository_urls, 'repo_id');
unset($repository_urls['repo_id']);
// Auto-add commit info from $commit['[xxx]_specific'] into the database.
$backends = versioncontrol_get_backends();
$vcs = $repository['vcs'];
$is_autoadd = in_array(VERSIONCONTROL_FLAG_AUTOADD_REPOSITORIES,
$backends[$vcs]['flags']);
if ($is_autoadd) {
$table_name = 'versioncontrol_'. $vcs .'_repositories';
$elements = $repository[$vcs .'_specific'];
$elements['repo_id'] = $repository['repo_id'];
_versioncontrol_db_update_additions($table_name, 'repo_id', $elements);
}
// Provide an opportunity for the backend to add its own stuff.
if (versioncontrol_backend_implements($vcs, 'repository')) {
_versioncontrol_call_backend($vcs, 'repository', array('update', $repository));
}
// Everything's done, let the world know about it!
module_invoke_all('versioncontrol_repository', 'update', $repository);
watchdog('special',
'Version Control API: updated repository @repository',
array('@repository' => $repository['name']),
WATCHDOG_NOTICE, l('view', 'admin/project/versioncontrol-repositories')
);
}
/**
* Delete a repository from the database, and call the necessary hooks.
* Together with the repository, all associated commits and accounts are
* deleted as well.
*
* @param $repository
* The repository array containing the repository that is to be deleted.
* It's a single repository array like the one returned by
* versioncontrol_get_repository().
*/
function versioncontrol_delete_repository($repository) {
// Delete operations.
$operations = versioncontrol_get_operations(array('repo_ids' => array($repository['repo_id'])));
foreach ($operations as $operation) {
versioncontrol_delete_operation($operation);
}
unset($operations); // conserve memory, this might get quite large
// Delete labels.
db_query('DELETE FROM {versioncontrol_labels}
WHERE repo_id = %d', $repository['repo_id']);
// Delete item revisions and related source item entries.
$result = db_query('SELECT item_revision_id
FROM {versioncontrol_item_revisions}
WHERE repo_id = %d', $repository['repo_id']);
$item_ids = array();
$placeholders = array();
while ($item_revision = db_fetch_object($result)) {
$item_ids[] = $item_revision->item_revision_id;
$placeholders[] = '%d';
}
if (!empty($item_ids)) {
$placeholders = '('. implode(',', $placeholders) .')';
db_query('DELETE FROM {versioncontrol_source_items}
WHERE item_revision_id IN '. $placeholders, $item_ids);
db_query('DELETE FROM {versioncontrol_source_items}
WHERE source_item_revision_id IN '. $placeholders, $item_ids);
db_query('DELETE FROM {versioncontrol_item_revisions}
WHERE repo_id = %d', $repository['repo_id']);
}
unset($item_ids); // conserve memory, this might get quite large
unset($placeholders); // ...likewise
// Delete accounts.
$accounts = versioncontrol_get_accounts(
array('repo_ids' => array($repository['repo_id'])), TRUE
);
foreach ($accounts as $uid => $usernames_by_repository) {
foreach ($usernames_by_repository as $repo_id => $username) {
versioncontrol_delete_account($repository, $uid, $username);
}
}
// Announce deletion of the repository before anything has happened.