forked from WhiteHouse/drushsubtree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drushsubtree.drush.inc
1521 lines (1354 loc) · 50.9 KB
/
drushsubtree.drush.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
/**
* @file
* drushsubtree drush command.
*
* You can copy this file to any of the following
* 1. A .drush folder in your HOME folder.
* 2. Anywhere in a folder tree below an active module on your site.
* 3. /usr/share/drush/commands (configurable)
* 4. In an arbitrary folder specified with the --include option.
* 5. Drupal's /drush or /sites/all/drush folders.
*/
require_once 'drushsubtree.subtreecommander.class.inc';
require_once 'drushsubtree.gitcommander.class.inc';
/**
* Implements hook_drush_command().
*/
function drushsubtree_drush_command() {
$items = array();
$standard_command_options = _drushsubtree_standard_command_options();
$items['subtree'] = array(
'description' => 'An interface for running drushsubtree commands with more git-like syntax. (All options for all commands are supported. See `drush subtree-<command>` --help for documentation about options for particular commands.)',
'arguments' => array(
'command' => '',
'project' => '',
),
'options' => $standard_command_options,
'examples' => array(
'drush subtree' => 'When args and options are missing, displays usage instructions.',
'drush subtree <command> <project>' => '',
'drush subtree add <project>' => '',
'drush subtree pull <project>' => '',
'drush subtree push <project>' => '',
'drush subtree merge <project> <id>' => '',
'drush subtree checkout <project> <tag>' => '',
),
// No bootstrap at all.
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
'drush dependencies' => 'buildmanager',
);
$items['subtree-add'] = array(
'description' => '',
'arguments' => array(
'project' => '(Optional) Subtree defined in buildmanager config.',
),
'options' => $standard_command_options,
'examples' => array(
'drush subtree-add' => '',
),
// No bootstrap at all.
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
'drush dependencies' => 'buildmanager',
);
$items['subtree-pull'] = array(
'description' => '',
'arguments' => array(
'project' => '(Optional) Subtree defined in buildmanager config.',
),
'options' => $standard_command_options,
'examples' => array(
'drush subtree-pull' => '',
),
// No bootstrap at all.
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
'drush dependencies' => 'buildmanager',
);
$items['subtree-merge'] = array(
'description' => '',
'arguments' => array(
'project' => '(Optional) Specify a subtree defined in buildmanager config.',
'commit ID' => '(Optional) Override ID/tag specified in make file(s).',
),
'options' => $standard_command_options,
'examples' => array(
'drush subtree-merge' => '',
'drush subtree-merge <my project>' => '',
'drush subtree-merge <my project> <commit id>' => '',
),
// No bootstrap at all.
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
'drush dependencies' => 'buildmanager',
);
$push_options = $standard_command_options;
unset($push_options['message']);
$items['subtree-push'] = array(
'description' => '',
'arguments' => array(
'project' => '',
),
'options' => $push_options,
'examples' => array(
'drush subtree-push' => '',
),
// No bootstrap at all.
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
'drush dependencies' => 'buildmanager',
);
$items['subtree-checkout'] = array(
'description' => 'Faux subtree command. "Checkout" a tagged version of a subtree project.',
'arguments' => array(
'project' => '',
'tag or commit ID' => 'Git tag or commit ID. (Check available tags with drushsubtree-get-tags. Commit IDs must be passed as full 40 character SHA1 hash. 40 character tags are not supported.)',
),
'options' => $standard_command_options,
'examples' => array(
'drush subtree-checkout <project> <id>' => '',
'drush subtree-checkout <project> <tag>' => '',
),
// No bootstrap at all.
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
'drush dependencies' => 'buildmanager',
);
$items['subtree-get-tags'] = array(
'description' => "View tagged releases for a subtree project.",
'arguments' => array(
'project_name' => '',
),
'examples' => array(
'drush subtree-get-tags <my-project>' => '',
'drush subtree-ls-tags <my-project>' => '',
),
'aliases' => array('subtree-ls-tags'),
// No bootstrap at all.
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
'drush dependencies' => 'buildmanager',
);
$items['subtree-get-head'] = array(
'description' => "Get HEAD commit id for subtree project (display corresponding tag, if there is one.",
'arguments' => array(
'project_name' => '',
),
'examples' => array(
'drush subtree-get-head <my-project>' => '',
'drush subtree-split <my-project>' => 'Same as `git subtree split --prefix=<my-project>`',
),
'aliases' => array('subtree-split'),
// No bootstrap at all.
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
'drush dependencies' => 'buildmanager',
);
$items['subtree-get-head-remote'] = array(
'description' => "Get HEAD commit id for subtree project's remote repo.",
'arguments' => array(
'project_name' => '',
),
'examples' => array(
'drush subtree-get-remote-head <my-project>' => '',
),
'aliases' => array('subtree-get-remote-head'),
// No bootstrap at all.
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
'drush dependencies' => 'buildmanager',
);
$items['subtree-get-subtrees'] = array(
'description' => "View a list of subtree projects specified in buildmanager config",
'examples' => array(
'drush subtree-show-subtrees' => '',
'drush sss' => '',
),
'aliases' => array('sss'),
// No bootstrap at all.
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
'drush dependencies' => 'buildmanager',
);
$items['subtree-get-info'] = array(
'description' => "Get info from config file for subtree.",
'examples' => array(
'drush subtree-get-info' => '',
'drush sgi' => '',
),
'aliases' => array('sgi'),
// No bootstrap at all.
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
'drush dependencies' => 'buildmanager',
);
$items['subtree-compare-heads'] = array(
'description' => "Compare local subtree HEAD with remote HEAD.",
'arguments' => array(
'project_name' => '',
),
'examples' => array(
'drush subtree-compare-heads' => 'Use prompt to select project',
'drush subtree-compare-heads <my-project>' => '',
'drush sch <my-project>' => '',
),
'aliases' => array('sch'),
// No bootstrap at all.
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
'drush dependencies' => 'buildmanager',
);
$items['subtree-compare-tag'] = array(
'description' => "Compare local subtree HEAD with tag in make file.",
'arguments' => array(
'project_name' => '',
),
'examples' => array(
'drush subtree-compare-tags' => 'Use prompt to select project',
'drush subtree-compare-tags <my-project>' => '',
'drush sct <my-project>' => '',
),
'aliases' => array('sct'),
// No bootstrap at all.
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
'drush dependencies' => 'buildmanager',
);
return $items;
}
/**
* Returns array of standard subtree options, used for multiple commands.
*
* @return array
* Options to be used in any command following pattern drush subtree <command>
*/
function _drushsubtree_standard_command_options() {
return array(
'config_file' => array(
'description' => 'path/to/buildmaster.config.yml',
),
'message' => array(
'description' => 'Message to include at the beginning of new commits. (Note: subtree add, merge, pull all generate commits in your parent repo.)',
),
'simulate' => array(
'description' => 'Output commands to be executed for examination, but do not actually execute them.',
),
);
}
/**
* Callback for drushsubtree-get-tags().
*/
function drush_drushsubtree_subtree_get_tags($subtree_name) {
// Get config.
$config = _drushsubtree_get_config();
if (!$details = $config['subtrees'][$subtree_name]) {
return drush_set_error(dt('No subtree config available for !subtree'),
array('!subtree' => $subtree_name)
);
}
// Look up tags from remote specified in $details.
$tags = drushsubtree_list_remote_tags($subtree_name, $details);
foreach ($tags as $tag => $id) {
drush_print("{$tag}\t{$id}");
}
}
/**
* Callback for drushsubtree-get-head().
*/
function drush_drushsubtree_subtree_get_head($subtree_name) {
// Get config.
$config = _drushsubtree_get_config();
if (!$details = $config['subtrees'][$subtree_name]) {
return drush_set_error(dt('No subtree config available for !subtree'),
array('!subtree' => $subtree_name)
);
}
$head = drushsubtree_subtree_get_head($subtree_name, $details);
$result = "{$subtree_name}\tHEAD\t{$head}";
// Check for matching tag.
if ($tag = _drushsubtree_match_tag($subtree_name, $head, $details)) {
$result .= "\t{$tag}";
}
// Print result.
drush_print($result);
}
/**
* Get HEAD for local subtree project.
*
* @param string $subtree_name
* Project name.
*
* @param array $details
* Project details from buildmanager config.
*
* @return string
* Commit ID for HEAD
*/
function drushsubtree_subtree_get_head($subtree_name, $details) {
// Instantiated subtree command generator.
$subtree = new SubtreeCommander($subtree_name, $details);
// Execute split command to get commit id.
exec($subtree->split(), $output, $exit_code);
if ($exit_code !== 0) {
drush_log(drush_print_r($output), 'error');
return drush_set_error(dt("Sorry. Git subtree split failed. Couldn't retrieve HEAD."));
}
// Retrieve HEAD.
$head = $output[0];
return $head;
}
/**
* Callback for drushsubtree-get-remote-head().
*/
function drush_drushsubtree_subtree_get_remote_head($subtree_name) {
// Get config.
$config = _drushsubtree_get_config();
if (!$details = $config['subtrees'][$subtree_name]) {
return drush_set_error(dt('No subtree config available for !subtree'),
array('!subtree' => $subtree_name)
);
}
// Get HEAD.
$uri = $details['uri'];
$branch = $details['branch'];
$head = drushsubtree_get_remote_head($uri, $branch);
// Print result.
drush_print("{$head}\t{$uri}\t{$branch}");
}
/**
* Get HEAD for branch on some remote repo.
*
* @param string $uri
* URI for remote repo.
*
* @param string $branch
* Branch on remote repo
*
* @return string
* Commit ID for remote HEAD
*/
function drushsubtree_get_remote_head($uri, $branch) {
// Instantiated git command generator.
$git = new GitCommander(array('uri' => $uri, 'branch' => $branch));
// Get remote HEAD. The command will look like this:
// git ls-remote --heads https://github.com/whitehouse/petitions 7.x-2.x
$command = $git->getRemoteHead();
exec($command, $output, $exit_code);
// Handle failure.
if ($exit_code !== 0) {
return drush_set_error('drushsubtree', dt("Request for remote head list failed: !command", array('!command' => $command)));
}
// Parse result. The output will look like this:
// 065f1f81709febfcb0d1429b100a1709d64c1c4d refs/heads/7.x-2.x
$parts = explode("\t", $output[0]);
$head = $parts[0];
return $head;
}
/**
* Find tag corresponding to commit ID (if one exists).
*
* @param string $name
* Subtree project name.
*
* @param string $id
* Git commit ID.
*
* @param array $details
* Subtree project details from buildmanager config file.
*
* @param bool $simplify
* TRUE, trim annotated tags so tags like this 7.x-1.4^{} returns 7.x-1.4.
* FALSE, return exact tag name matched.
*
* @return string|bool
* Tag name, false if not available.
*/
function _drushsubtree_match_tag($name, $id, $details, $simplify = TRUE) {
// Assume there's no match unless we find one.
$matching_tag = FALSE;
// Check remote tags for matching commit ID.
foreach (drushsubtree_list_remote_tags($name, $details) as $tag => $tag_id) {
if ($id == $tag_id) {
$matching_tag = $tag;
}
}
// If it's an annotated tag, trim the ^{} at the end.
if ($simplify) {
if (substr($matching_tag, -3) == '^{}') {
// It's an annotated tag.
$matching_tag = substr($matching_tag, 0, strpos($matching_tag, '^{}'));
}
}
return $matching_tag;
}
/**
* Get message to be included with subtree add/pull/merge commits.
*
* Site make config files can specify a message to be included
* project-by-project.
*
* @param string $subtree_message
* Defined in buildmanager YAML config file.
*
* @return string
* A commit message flag to a git subtree add, pull, or merge command.
* Empty string if no message should be added.
*/
function _drushsubtree_add_subtree_commit_message($subtree_message = '') {
$drush_message = drush_get_option('message', '');
if ($drush_message || $subtree_message) {
$custom_message = ($drush_message) ? "{$drush_message}\n\n\t{$subtree_message}" : $subtree_message;
}
$message = ($custom_message) ? " --message='{$custom_message}'" : '';
return $message;
}
/**
* Get prefix (path) to local subtree.
*
* @param string $directory
* Name of directory where subtree should go.
*
* @return string
* Path to subtree from top-level of repo to be used for --prefix.
*/
function _drushsubtree_get_prefix($directory) {
return "projects/{$directory}";
}
/**
* Get URI for remote git repo.
*
* @param array $details
* Relevant details for git subtree add command.
*
* @return string|bool
* Uri for git repo.
*/
function _drushsubtree_get_uri($details) {
if (!(isset($details['uri']) || !strlen($details['uri']) > 0)) {
return drush_set_error('drushsubtree', dt("uri is a required subtree parameter for any subtree included here. Please add this to your site's config file, then try again."), 'error');
}
else {
return $details['uri'];
}
}
/**
* Get subtree branch from drushsubtree project details included in config file.
*
* @param array $details
* Relevant details for git subtree add command:
*
* @return string|bool
* Branch name.
*/
function _drushsubtree_get_branch($details) {
if (!(isset($details['branch']) || !strlen($details['branch']) > 0)) {
return drush_set_error('drushsubtree', dt("branchis a required subtree parameter for any subtree included here. Please add this to your site's config file, then try again."), 'error');
}
return $details['branch'];
}
/**
* Figure out what version of a subtree project should be checked out.
*
* Return commit ID so "checkout" can be done as a subtree merge.
*
* @param string $subtree_name
* Name of subtree.
*
* @param array $project_info
* Info about a project from parsed make files.
*
* @param array $details
* Details about subtree project from buildmanager config.
*
* @param string $tag
* Tag, if available. Empty if not.
*
* @return string
* Commit ID for subtree to be merged to. Empty if unavailable.
*/
function _drushsubtree_get_project_id($subtree_name, $project_info, $details, $tag = '') {
$id = '';
// First, try getting ID ("revision") from make files.
$id = _drushsubtree_get_id_from_make_info($subtree_name, $project_info);
// If no commit ID was specified in make file(s), try looking up ID using
// remote list of tags.
if (!$id) {
// Get an array of commit IDs keyed by tags.
$tags = drushsubtree_list_remote_tags($subtree_name, $details);
if (!$tags) {
// Attempt to retrieved tags from remote failed.
drush_log(dt("Commit IDs for !name tags could not be retrieved from !remote.\nTo explicitly designate which commit ID to check out, you can add this to your makefile (replace abc1234 with a real commit ID):\n\tprojects[!name][download][revision] = abc1234",
array('!name' => $subtree_name, '!remote' => $details['uri'])
), 'warning');
}
elseif ($tags && !$tag) {
// Tags were retrieved successfully. No tag was passed in. See if we can
// get tag/version from make file(s).
$config = _drushsubtree_get_config();
$core = buildmanager_get_core_version($config);
$tag = _drushsubtree_get_project_tag($subtree_name, $project_info, $core);
}
}
// If we've got an array of IDs keyed by tags ($tags) and a tag to look up
// ($tags), we've got what we need now.
if ($tags && $tag) {
// Error handling. Make sure the tag specified in the make file(s) is
// available in the remote we're pulling commit IDs from.
if (!isset($tags[$tag])) {
$message = dt("The version/tag specified by your build.make file does not correspond with tags available at the remote we're pulling from.");
$message .= "\n" . dt("Review available tags like this: !command",
array('!command' => "\n\tdrush subtree-get-tags {$subtree_name}"));
// Display specs determined by make file and buildmanager config for user
// to review.
$specs = '';
$specs .= "\n\tproject:\t{$subtree_name}";
$specs .= "\n\ttag:\t\t{$tag}";
$specs .= "\n\tremote:\t\t{$details['uri']}";
$message .= "\n" . dt("Or see if you can spot a mistake in your make file or buildmanager config here: !specs",
array('!specs' => $specs));
return drush_set_error($message);
}
// Check for annotated tags. Use those when available. Otherwise users end
// up getting a confusing 'fatal: bad object' error message from git.
// Annotated tags look like this: 7.x-2.0^{} for 7.x-2.0.
$annotated_tag = "{$tag}^{}";
if (isset($tags[$annotated_tag])) {
$tag = $annotated_tag;
}
// Success!
$id = $tags[$tag];
}
return $id;
}
/**
* Get a commit ID ("revision") from a drush make file info.
*
* @param string $project_name
* Name of project (module, profile, theme, library).
*
* @param array $project_info
* Project info array from make files.
*
* @return string|bool
* Project revision (commit) id specified in make file. False if unavailable.
*/
function _drushsubtree_get_id_from_make_info($project_name, $project_info) {
$id = (isset($project_info['download']['revision'])) ? $project_info['download']['revision'] : FALSE;
return $id;
}
/**
* Retrieve list of tags and corresponding commit IDs from remote repository.
*
* @param string $project_name
* Name of project (module, profile, theme, library).
*
* @param array $details
* From drushsubtree config.
*
* @return array|bool
* Commit IDs keyed by tag name. False for failure.
*/
function drushsubtree_list_remote_tags($project_name, $details) {
// Instantiate git command generator.
$git = new GitCommander(array('uri' => $details['uri']));
// Get a list of commit IDs and corresponding tags. A successful request
// returns something like this:
// 516726690a5f3a15ccf8d7ffccd135db4375ad10 refs/tags/7.x-1.0-rc1
// e214ea5b94f345b66f302d0d4aae232b4128c1e3 refs/tags/7.x-1.0-rc2
// 8f61c66e5f55e72a35f4a1b8f4eee93a428a03ac refs/tags/7.x-1.0-rc3
$command = $git->listRemoteTags();
// Each line of output is stored in $output array.
// $exit_code is 0 for success, anything else for failure.
exec($command, $output, $exit_code);
// Handle failure.
if ($exit_code !== 0) {
return drush_set_error('drushsubtree', dt("Request for remote tag list failed: !command", array('!command' => $command)));
}
// Parse result.
$tags = array();
foreach ($output as $line) {
// Example line: 8f61c66e5f55e72a35f4a1b8f4eee93a428a03ac refs/tags/7.x-1.0
$parts = explode("\t", $line);
if (count($parts) < 2) {
// Skip empty lines that sneak in.
continue;
}
$pos = strlen('refs/tags/');
$tag = substr($parts[1], $pos);
$id = $parts[0];
$tags[$tag] = $id;
}
return $tags;
}
/**
* Get tag/version for a project specified in make file(s).
*
* @param string $project_name
* Name of project (module, profile, theme, library).
*
* @param array $project_info
* Project info array from make files.
*
* @param string $core
* Core version (e.g. 6.x, 7.x, 8.x). Required if tag is being looked up from
* a project's version number in a make file.
*
* @return string
* Project version number specified in make file. Empty if unavailable.
*/
function _drushsubtree_get_project_tag($project_name, $project_info, $core = '') {
$tag = '';
if (isset($project_info['version']) && $core) {
$version = $project_info['version'];
$tag = "{$core}-{$version}";
}
elseif (isset($project_info['tag'])) {
$tag = $project_info['tag'];
}
elseif (isset($project_info['download']['tag'])) {
$tag = $project_info['download']['tag'];
}
else {
// Make files do not specify any tag for this project.
drush_log(dt("No tag or commit ID available for !name.",
array('!name' => $project_name), 'warning'));
}
return $tag;
}
/**
* Return command to generate symlink to subtree.
*
* This command must be run from top-level of repo.
*
* @param string $name
* Name of project (module, theme, profile) to be replaced with a symlink to a
* subtree inside /projects.
*
* @param string $path
* Path to project in Drupal code base, relative to top-level of site repo.
*
* @return string
* Shell command for adding symlink.
*/
function _drushsubtree_subtree_add_symlink($name, $path) {
// Get path to subtree, relative to top-level directory.
$prefix = _drushsubtree_get_prefix($name);
// Get source, relative to parent directory inside docroot.
$count = count(explode('/', $path));
$source = '';
for ($i = 0; $i < $count - 1; $i++) {
$source .= '../';
};
// Add prefix to relative source path.
$source .= $prefix;
// Parent directory for symlink.
$pos = strrpos($path, $name);
$parent_directory = substr($path, 0, $pos);
// Remove the directory. Replace it with a symlink.
$command = "rm -rf {$path}; cd {$parent_directory}; ln -s {$source} {$name};";
return $command;
}
/**
* Implements hook_buildmanager_build().
*
* Check for subtrees in config file. Add/update subtrees in build.
*
* param array $info
* Info from drush make build file.
*
* param array $config
* Configuration defined in buildmanager.config.yml
*
* param obj $commands
* Commands to be executed by buildmanager.
* - precommands (array), commands to execute before `drush make` (re)build
* - postcommands (array), commands to execute after `drush make` (re)build
*/
function drushsubtree_buildmanager_build($info, $config, $commands) {
// Check for subtrees in $config.
if (!isset($config['subtrees'])) {
// This project has no subtrees. Our work here is done.
return;
}
drush_log(dt('Preparing to add/update git subtrees.'), 'ok');
// Prep projects directory. All subtrees will use --prefix=projects/$name
_drushsubtree_prep_projects_directory();
// Get subtree commands to be executed before running `drush make`.
$subtree_commands = array();
// Get symlink commands to be run after `drush make`.
$symlink_commands = array();
// Get drush options.
$fast_but_noisy = drush_get_option('fast-but-noisy', FALSE);
// Loop through all subtrees described in buildmanager config to add prebuild
// and postbuild commands.
// Prebuild:
// - Validation checks and warnings.
// - Subtree updates (add, pull, merge).
// Postbuild:
// - Symlinks (which will get blown away by drush make) are restored.
foreach ($config['subtrees'] as $subtree_name => $details) {
// Start with postbuild commands (adding symlinks) because prebuild commands
// are sometimes skipped (when no-subtree-updates flag is set).
// Remove files checked out by drush. Add symlink pointing to local subtree.
drush_log(dt('Preparing to symlink !subtree_name to path: !path', array('!subtree_name' => $subtree_name, '!path' => $details['path'])), 'ok');
$symlink_commands[] = _drushsubtree_subtree_add_symlink($subtree_name, $details['path']);
// Next add prebuild commands / subtree repo updates.
if (drush_get_option('no-subtree-updates', FALSE)) {
// If no-subtree-updates flag is set, skip the rest of the foreach loop.
drush_log(dt('Skipping git subtree updates.'), 'ok');
continue;
}
// Get project info.
$project_info = array();
if (isset($info['projects'][$subtree_name])) {
$project_info = $info['projects'][$subtree_name];
}
// If it's not a "project" it's a "library".
elseif (isset($info['libraries'][$subtree_name])) {
$project_info = $info['libraries'][$subtree_name];
}
// Can't find info in available make files.
else {
drush_log(dt("Could not find info in make file(s) about this project: !name", array(
'!name' => $subtree_name,
)), 'warning');
}
// Get tagged version of project from make file, if available.
$tag = _drushsubtree_get_project_tag($subtree_name, $project_info, $info['core']);
// Get git commit ID to merge, if enough info is available.
$id = _drushsubtree_get_project_id($subtree_name, $project_info,
$details, $tag);
// Run subtree developer checks?
// @todo Keep track of results, project-by-project. Now that we run checks
// by default to prevent adding noise to the commit history, it's a waste if
// we end up running the same time-consuming checks twice in a single run.
if (drush_get_option('subtree-dev-checks', FALSE)) {
$checks_pass = _drushsubtree_dev_checks($subtree_name, $tag, $id, $config);
if (!$checks_pass) {
// User canceled build.
drush_log(dt('Drush Subtree: Canceling build.'), 'error');
// Returning anything cancels the build.
return 'Drush Subtree: Canceling build.';
}
}
// Instantiate new commander to generate subtree commands.
$subtree = new SubtreeCommander($subtree_name, $details);
// Collect commands as $subtree_commands, then add them all to the
// beguinning of $commands->prebuild array.
// It's important these run before drush make for
// two reasons: (1) Incase a project's build file includes a make file in a
// subtree project. This will ensure the updated make file is included in
// the build. (2) This needs to run before anything else dirties the working
// tree (like drush make), otherwise the subtree add/pull/merge will fail.
// First, add git subtree if it doesn't already exist.
if ($subtree_add = $subtree->add(TRUE)) {
drush_log(dt('Preparing to add subtree: !subtree_name', array(
'!subtree_name' => $subtree_name,
)), 'ok');
$subtree_commands[] = $subtree_add;
}
// Check if subtree project already matches tag in make file.
// Only run this if we were able to find a tag for this project.
elseif ($tag && !$fast_but_noisy && drushsubtree_compare_tag($subtree_name, $config, TRUE)) {
// Skip this pull/merge. It's extraneous noise in the commit history.
drush_log(dt('Skipping subtree pull/merge on @name. Project HEAD already points to tag specified by @build.', array(
'@name' => $subtree_name,
'@build' => $config['build']['build_file'],
)), 'ok');
continue;
}
// Check if subtree project HEAD already matches revision in make file.
// Only run this if we were NOT able to find a tag for this project.
elseif (!$tag && !$fast_but_noisy && $id == drushsubtree_subtree_get_head($subtree_name, $details)) {
// Skip this pull/merge. It's extraneous noise in the commit history.
drush_log(dt('Skipping subtree pull/merge on @name. Project HEAD already points to revision specified by @build.', array(
'@name' => $subtree_name,
'@build' => $config['build']['build_file'],
)), 'ok');
continue;
}
// Otherwise, pull in any updates.
elseif ($subtree_pull = $subtree->pull()) {
$subtree_commands[] = $subtree_pull;
}
// Use git subtree merge to check out a particular version of a project.
if ($id) {
$subtree_commands[] = $subtree->merge($id, $tag);
}
else {
// @todo Detect latest recommended release on d.o when no version is
// specified. Consider using that to better match standard make file
// behavior?
drush_log(dt("No commit ID specified for !project (if a tag was specified in a make file, the corresponding commit ID could not be retrieved). Your install will run on the tip of whatever branch you checked out. (Subtrees do not fallback to the latest recommended release on drupal.org when no version is specified.)", array('!project' => $subtree_name)), 'warning');
}
// End loop thorugh subtrees.
}
// Add symlink commands after drush make but before other commands, incase any
// custom post-build commands are committing anything. This way, git history
// doesn't get polluted with projects being re-added and re-removed each time.
$commands->postbuild = array_merge($symlink_commands, $commands->postbuild);
// Now add all subtree_commands to begining of prebuild command list.
$commands->prebuild = array_merge($subtree_commands, $commands->prebuild);
// No return necessary. Commands added to $command object are available to
// buildmaster_build command now. Note: Returning anything actually triggers
// Build Manger abort.
}
/**
* Run helpful checks for developers during hook_buildmanager_build().
*
* Look for indications that projects may not been synced properly. Then prompt
* developer with information about what we found and tips on resolving the
* situation.
*
* @param string $name
* Name of subtree project.
*
* @param string $tag
* Tag specified by [download][tag], [tag], or [version] in make file.
*
* @param string $id
* Git commit ID specified by make file, or corresponding to tag in make file.
*
* @param string $config
* Build Manager config passed in by Build Manager at runtime.
*
* @return bool
* TRUE, checks pass, recommend proceeding with build. FALSE, checks failed.
*/
function _drushsubtree_dev_checks($name, $tag, $id, $config) {
// Assume we're proceeding with build unless checks prove otherwise.
$proceed_with_build = TRUE;
// Set vars for subtree check prompts.
$details = $config['subtrees'][$name];
$check_vars = array(
'@name' => $name,
'@tag' => $tag,
'@id' => $id,
'@uri' => $details['uri'],
'@branch' => $details['branch'],
);
// There's a tagged version of this project specified in the make file.
if ($tag) {
// Does local copy of subtree project match the version in the make file?
// If yes, proceed. If no, prompt user with warnings and suggestions.
if (!drushsubtree_compare_tag($name, $config, TRUE)) {
$message = dt("Your local copy of @name does not match @tag (specified in your make file). If you intend to pull in updates from outside your site repo with @tag, proceed. If your local version is ahead of @tag and you have been doing development inside your site repo, you should probably stop, push changes to @uri, tag a new version, update your make file, then rebuild. Otherwise, proceeding will be like downgrading to @tag. Do you want to proceed with this build? (To rebuild without interrupting local development on subtree projects, cancel and re-try with the --no-subtree-updates flag.)", $check_vars);
$proceed_with_build = drush_confirm($message);
}
}
// There's no tagged version specified in the make file, but a commit ID is
// specified.
elseif ($id && !$tag) {
// Do HEAD and the ID in the make file match? If yes, proceed. If no, notify
// user and confirm before proceeding.
$head = drushsubtree_subtree_get_head($name, $details);
drush_log("{$name}\t{$head}\tlocal HEAD", 'ok');
drush_log("{$name}\t{$id}\trevision specified by make file", 'ok');
if ($head == $id) {
drush_log("{$name}\tversions match", 'ok');
}
else {
$message = dt("Your local copy of @name does not match the download revision (commit ID) specified in your make file. If you intend to pull in updates from outside your site repo with @id, proceed. If your local version is ahead of @id and you have been doing development inside your site repo, you should probably stop, push changes to @uri, update your make file, then rebuild. Otherwise, proceeding will be like downgrading to @id. Do you want to proceed with this build? (To rebuild without interrupting local development on subtree projects, cancel and re-try with the --no-subtree-updates flag.)", $check_vars);
$proceed_with_build = drush_confirm($message);
}
}
// There's no tagged version for this project in the make file.
elseif (!$id && !$tag) {
// Do local and remote HEAD point at same commit? If yes, proceed. If no,
// prompt user with warnings and suggestions.
if (!drushsubtree_heads_match($name, $details, TRUE)) {
$message = dt("The version of @name in your local site repo does not match HEAD on @branch (@uri). Do you want to proceed with this build? (To rebuild without interrupting local development on subtree projects, cancel and re-try with the --no-subtree-updates flag.)", $check_vars);
$proceed_with_build = drush_confirm($message);
}
}
return $proceed_with_build;
}
/**
* Prep projects directory.
*/
function _drushsubtree_prep_projects_directory() {
// Before adding/pulling subtrees, make sure parent directory
// for --prefix exists.
if (!file_exists('projects')) {
$success = mkdir('projects', 0777, TRUE);
if (!$success) {
return drush_set_error('buildmanager', dt("Failed creating directory: projects"), 'error');
}
}
elseif (file_exists('projects') && !is_dir('projects')) {
return drush_set_error('buildmanager', dt("Please remove the 'projects' file. We need to create a directory with that name for your subtrees."), 'error');
}
if ($success) {
drush_log(dt('A projects directory has been created at the top of your git repo. Git subtrees will be added there.'), 'ok');
}
}
/**
* Implements hook_buildmanager_build_options().
*/
function drushsubtree_buildmanager_build_options() {
return array(
'no-subtree-updates' => array(
'description' => 'Do add/pull/merge subtrees in buildmanager config.',
),
'subtree-dev-checks' => array(
'description' => "Recommended for developers doing a rebuild after making changes to included contrib projects. Drush Subtree will run checks like: comparing included subtree projects with corresponding the versions in your make file, comparing local and remote HEAD. If things are out of sync, you will be notified, given suggestions about steps to take, and receive a prompt to optionally cancel the build. Checks run before any pre/postbuild commands. To run checks ONLY, run buildmanager-build with --simulate flag.",
),
'fast-but-noisy' => array(
'description' => "Don't check to see if subtree projects already match versions in make files. Simply run git subtree pull and merge on all subtree projects. This is faster, but it adds some extraneous noise to the commit history",
),
);
}
/**
* Implements hook_buildmanager_configure().
*
* Prompt for subtree info to be included in buildmanager config.
* Returns config with subtree properties added, to be included in
* buildmanager.config.yml.
*
* param array $config
* See hook_buildmanager_configure.
*
* pararm bool $prompt