git-multirepo 1.0.0.beta40 → 1.0.0.beta42

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/.gitattributes +2 -2
  3. data/.gitbugtraq +3 -3
  4. data/.gitignore +38 -38
  5. data/.multirepo.meta +2 -2
  6. data/.rspec +2 -2
  7. data/Gemfile +4 -4
  8. data/Gemfile.lock +42 -42
  9. data/LICENSE +22 -22
  10. data/README.md +143 -146
  11. data/Rakefile +2 -2
  12. data/bin/multi +10 -10
  13. data/docs/bug-repros/91565510-repro.sh +20 -20
  14. data/docs/git-multirepo-cheatsheet.docx +0 -0
  15. data/git-multirepo.gemspec +31 -31
  16. data/lib/commands.rb +13 -15
  17. data/lib/git-multirepo.rb +2 -2
  18. data/lib/info.rb +4 -4
  19. data/lib/multirepo/commands/add-command.rb +50 -53
  20. data/lib/multirepo/commands/branch-command.rb +81 -82
  21. data/lib/multirepo/commands/checkout-command.rb +119 -122
  22. data/lib/multirepo/commands/clone-command.rb +67 -70
  23. data/lib/multirepo/commands/command.rb +89 -75
  24. data/lib/multirepo/commands/do-command.rb +100 -75
  25. data/lib/multirepo/commands/graph-command.rb +42 -45
  26. data/lib/multirepo/commands/init-command.rb +119 -119
  27. data/lib/multirepo/commands/install-command.rb +106 -103
  28. data/lib/multirepo/commands/merge-command.rb +225 -167
  29. data/lib/multirepo/commands/open-command.rb +55 -57
  30. data/lib/multirepo/commands/remove-command.rb +47 -50
  31. data/lib/multirepo/commands/uninit-command.rb +17 -20
  32. data/lib/multirepo/commands/update-command.rb +55 -60
  33. data/lib/multirepo/config.rb +15 -15
  34. data/lib/multirepo/files/config-entry.rb +38 -38
  35. data/lib/multirepo/files/config-file.rb +45 -45
  36. data/lib/multirepo/files/lock-entry.rb +28 -24
  37. data/lib/multirepo/files/lock-file.rb +55 -38
  38. data/lib/multirepo/files/meta-file.rb +40 -40
  39. data/lib/multirepo/files/tracking-file.rb +8 -8
  40. data/lib/multirepo/files/tracking-files.rb +46 -46
  41. data/lib/multirepo/git/branch.rb +31 -30
  42. data/lib/multirepo/git/change.rb +10 -10
  43. data/lib/multirepo/git/commit.rb +6 -17
  44. data/lib/multirepo/git/git-runner.rb +46 -46
  45. data/lib/multirepo/git/git.rb +10 -0
  46. data/lib/multirepo/git/ref.rb +38 -0
  47. data/lib/multirepo/git/remote.rb +16 -16
  48. data/lib/multirepo/git/repo.rb +122 -77
  49. data/lib/multirepo/hooks/post-commit-hook.rb +22 -22
  50. data/lib/multirepo/hooks/pre-commit-hook.rb +34 -31
  51. data/lib/multirepo/logic/dependency.rb +6 -0
  52. data/lib/multirepo/logic/merge-descriptor.rb +94 -12
  53. data/lib/multirepo/logic/node.rb +71 -44
  54. data/lib/multirepo/logic/performer.rb +56 -62
  55. data/lib/multirepo/logic/revision-selector.rb +34 -34
  56. data/lib/multirepo/multirepo-exception.rb +5 -5
  57. data/lib/multirepo/utility/console.rb +51 -51
  58. data/lib/multirepo/utility/runner.rb +34 -34
  59. data/lib/multirepo/utility/utils.rb +94 -81
  60. data/resources/.gitconfig +2 -2
  61. data/resources/post-commit +5 -5
  62. data/resources/pre-commit +5 -5
  63. data/spec/integration/init_spec.rb +18 -18
  64. data/spec/spec_helper.rb +89 -89
  65. metadata +6 -5
  66. data/lib/multirepo/commands/clean-command.rb +0 -32
  67. data/lib/multirepo/commands/fetch-command.rb +0 -31
@@ -1,104 +1,107 @@
1
- require "multirepo/utility/console"
2
- require "multirepo/utility/utils"
3
- require "multirepo/git/repo"
4
- require "multirepo/commands/checkout-command"
5
-
6
- module MultiRepo
7
- class InstallCommand < Command
8
- self.command = "install"
9
- self.summary = "Clones and checks out dependencies as defined in the version-controlled multirepo metadata files and installs git-multirepo's local git hooks."
10
-
11
- def self.options
12
- [['[--hooks]', 'Only install local git hooks.']].concat(super)
13
- end
14
-
15
- def initialize(argv)
16
- @hooks = argv.flag?("hooks")
17
- super
18
- end
19
-
20
- def run
21
- super
22
- ensure_in_work_tree
23
- ensure_multirepo_tracked
24
-
25
- if @hooks
26
- Console.log_step("Installing hooks in main repo and all dependencies...")
27
- install_hooks_step
28
- else
29
- Console.log_step("Cloning dependencies and installing hooks...")
30
- full_install
31
- end
32
-
33
- Console.log_step("Done!")
34
- rescue MultiRepoException => e
35
- Console.log_error(e.message)
36
- end
37
-
38
- def full_install
39
- install_dependencies_step
40
- install_hooks_step
41
- update_gitconfigs_step
42
- end
43
-
44
- def install_dependencies_step
45
- # Read config entries as-is on disk, without prior checkout
46
- config_entries = ConfigFile.new(".").load_entries
47
- Console.log_substep("Installing #{config_entries.count} dependencies...");
48
-
49
- # Clone or fetch all configured dependencies to make sure nothing is missing locally
50
- config_entries.each { |entry| clone_or_fetch(entry) }
51
-
52
- # Checkout the appropriate branches as specified in the lock file
53
- checkout_command = CheckoutCommand.new(CLAide::ARGV.new([]))
54
- checkout_command.dependencies_checkout_step(RevisionSelectionMode::LATEST)
55
- end
56
-
57
- def install_hooks_step
58
- perform_in_main_repo_and_dependencies("Installed git hooks") { |repo| install_hooks(repo) }
59
- end
60
-
61
- def update_gitconfigs_step
62
- perform_in_main_repo_and_dependencies("Updated .git/config file") { |repo| update_gitconfig(repo) }
63
- end
64
-
65
- def perform_in_main_repo_and_dependencies(message_prefix, &operation)
66
- operation.call(".")
67
- Console.log_substep("#{message_prefix} in main repo")
68
-
69
- multirepo_enabled_dependencies.each do |entry|
70
- operation.call(entry.repo.path)
71
- Console.log_substep("#{message_prefix} in multirepo-enabled dependency '#{entry.repo.path}'")
72
- end
73
- end
74
-
75
- def clone_or_fetch(entry)
76
- if entry.repo.exists?
77
- check_repo_validity(entry)
78
- fetch_repo(entry)
79
- else
80
- clone_repo(entry)
81
- end
82
- end
83
-
84
- # Repo operations
85
-
86
- def fetch_repo(entry)
87
- Console.log_substep("Working copy '#{entry.repo.path}' already exists, fetching instead...")
88
- raise MultiRepoException, "Could not fetch from remote #{entry.repo.remote('origin').url}" unless entry.repo.fetch
89
- end
90
-
91
- def clone_repo(entry)
92
- Console.log_substep("Cloning #{entry.url} into '#{entry.repo.path}'")
93
- raise MultiRepoException, "Could not clone remote #{entry.url}" unless entry.repo.clone(entry.url)
94
- end
95
-
96
- # Validation
97
-
98
- def check_repo_validity(entry)
99
- unless entry.repo.remote("origin").url == entry.url
100
- raise MultiRepoException, "'#{entry.path}' origin URL (#{entry.repo.remote('origin').url}) does not match entry (#{entry.url})!"
101
- end
102
- end
103
- end
1
+ require "multirepo/utility/console"
2
+ require "multirepo/utility/utils"
3
+ require "multirepo/git/repo"
4
+ require "multirepo/logic/performer"
5
+ require "multirepo/commands/checkout-command"
6
+
7
+ module MultiRepo
8
+ class InstallCommand < Command
9
+ self.command = "install"
10
+ self.summary = "Clones and checks out dependencies as defined in the version-controlled multirepo metadata files and installs git-multirepo's local git hooks."
11
+
12
+ def self.options
13
+ [['[--hooks]', 'Only install local git hooks.']].concat(super)
14
+ end
15
+
16
+ def initialize(argv)
17
+ @hooks = argv.flag?("hooks")
18
+ super
19
+ end
20
+
21
+ def run
22
+ ensure_in_work_tree
23
+ ensure_multirepo_tracked
24
+
25
+ if @hooks
26
+ Console.log_step("Installing hooks in main repo and all dependencies...")
27
+ install_hooks_step
28
+ else
29
+ Console.log_step("Cloning dependencies and installing hooks...")
30
+ full_install
31
+ end
32
+
33
+ Console.log_step("Done!")
34
+ end
35
+
36
+ def full_install
37
+ install_dependencies_step
38
+ install_hooks_step
39
+ update_gitconfigs_step
40
+ end
41
+
42
+ def install_dependencies_step
43
+ # Read config entries as-is on disk, without prior checkout
44
+ config_entries = ConfigFile.new(".").load_entries
45
+ Console.log_substep("Installing #{config_entries.count} dependencies...");
46
+
47
+ # Clone or fetch all configured dependencies to make sure nothing is missing locally
48
+ Performer.dependencies.each { |d| clone_or_fetch(d) }
49
+
50
+ # Checkout the appropriate branches as specified in the lock file
51
+ checkout_command = CheckoutCommand.new(CLAide::ARGV.new([]))
52
+ checkout_command.dependencies_checkout_step(RevisionSelectionMode::LATEST)
53
+ end
54
+
55
+ def install_hooks_step
56
+ perform_in_main_repo_and_dependencies("Installed git hooks") { |repo| install_hooks(repo) }
57
+ end
58
+
59
+ def update_gitconfigs_step
60
+ perform_in_main_repo_and_dependencies("Updated .git/config file") { |repo| update_gitconfig(repo) }
61
+ end
62
+
63
+ def perform_in_main_repo_and_dependencies(message_prefix, &operation)
64
+ operation.call(".")
65
+ Console.log_substep("#{message_prefix} in main repo")
66
+
67
+ multirepo_enabled_dependencies.each do |config_entry|
68
+ operation.call(config_entry.repo.path)
69
+ Console.log_substep("#{message_prefix} in multirepo-enabled dependency '#{config_entry.repo.path}'")
70
+ end
71
+ end
72
+
73
+ # Repo operations
74
+
75
+ def clone_or_fetch(dependency)
76
+ if dependency.config_entry.repo.exists?
77
+ check_repo_validity(dependency)
78
+
79
+ Console.log_substep("Working copy '#{dependency.config_entry.repo.path}' already exists, fetching instead...")
80
+ fetch_repo(dependency)
81
+ else
82
+ Console.log_substep("Cloning #{dependency.config_entry.url} into '#{dependency.config_entry.repo.path}'")
83
+ clone_repo(dependency)
84
+ end
85
+ end
86
+
87
+ def fetch_repo(dependency)
88
+ unless dependency.config_entry.repo.fetch
89
+ raise MultiRepoException, "Could not fetch from remote #{dependency.config_entry.repo.remote('origin').url}"
90
+ end
91
+ end
92
+
93
+ def clone_repo(dependency)
94
+ unless dependency.config_entry.repo.clone(dependency.config_entry.url, dependency.lock_entry.branch)
95
+ raise MultiRepoException, "Could not clone remote #{dependency.config_entry.url} with branch #{dependency.config_entry.branch}"
96
+ end
97
+ end
98
+
99
+ # Validation
100
+
101
+ def check_repo_validity(dependency)
102
+ unless dependency.config_entry.repo.remote("origin").url == dependency.config_entry.url
103
+ raise MultiRepoException, "'#{dependency.config_entry.path}' origin URL (#{dependency.config_entry.repo.remote('origin').url}) does not match entry (#{dependency.config_entry.url})!"
104
+ end
105
+ end
106
+ end
104
107
  end
@@ -1,168 +1,226 @@
1
- require "terminal-table"
2
-
3
- require "multirepo/utility/console"
4
- require "multirepo/logic/node"
5
- require "multirepo/logic/revision-selector"
6
- require "multirepo/logic/performer"
7
- require "multirepo/logic/merge-descriptor"
8
-
9
- module MultiRepo
10
- class MergeCommand < Command
11
- self.command = "merge"
12
- self.summary = "Performs a git merge on all dependencies and the main repo, in the proper order."
13
-
14
- def self.options
15
- [
16
- ['<ref>', 'The main repo tag, branch or commit id to merge.'],
17
- ['[--latest]', 'Merge the HEAD of each stored dependency branch instead of the commits recorded in the lock file.'],
18
- ['[--exact]', 'Merge the exact specified ref for each repo, regardless of what\'s stored in the lock file.']
19
- ].concat(super)
20
- end
21
-
22
- def initialize(argv)
23
- @ref = argv.shift_argument
24
- @checkout_latest = argv.flag?("latest")
25
- @checkout_exact = argv.flag?("exact")
26
- super
27
- end
28
-
29
- def validate!
30
- super
31
- help! "You must specify a ref to merge" unless @ref
32
- unless validate_only_one_flag(@checkout_latest, @checkout_exact)
33
- help! "You can't provide more than one operation modifier (--latest, --exact, etc.)"
34
- end
35
- end
36
-
37
- def run
38
- super
39
- ensure_in_work_tree
40
- ensure_multirepo_enabled
41
-
42
- # Find out the checkout mode based on command-line options
43
- mode = RevisionSelector.mode_for_args(@checkout_latest, @checkout_exact)
44
-
45
- strategy_name = RevisionSelectionMode.name_for_mode(mode)
46
- Console.log_step("Merging #{@ref} with '#{strategy_name}' strategy...")
47
-
48
- main_repo = Repo.new(".")
49
-
50
- # Keep the initial revision because we're going to need to come back to it later
51
- initial_revision = main_repo.current_branch || main_repo.head_hash
52
-
53
- begin
54
- merge_core(main_repo, initial_revision, mode)
55
- rescue MultiRepoException => e
56
- # Revert to the initial revision only if necessary
57
- unless main_repo.current_branch == initial_revision || main_repo.head_hash == initial_revision
58
- Console.log_substep("Restoring working copy to #{initial_revision}")
59
- main_repo.checkout(initial_revision)
60
- end
61
- raise e
62
- end
63
-
64
- Console.log_step("Done!")
65
- rescue MultiRepoException => e
66
- Console.log_error(e.message)
67
- end
68
-
69
- def merge_core(main_repo, initial_revision, mode)
70
- config_file = ConfigFile.new(".")
71
-
72
- # Load entries prior to checkout so that we can compare them later
73
- pre_checkout_config_entries = config_file.load_entries
74
-
75
- # Ensure the main repo is clean
76
- raise MultiRepoException, "Main repo is not clean; merge aborted" unless main_repo.clean?
77
-
78
- # Ensure dependencies are clean
79
- unless Utils.ensure_dependencies_clean(pre_checkout_config_entries)
80
- raise MultiRepoException, "Dependencies are not clean; merge aborted"
81
- end
82
-
83
- # Fetch repos to make sure we have the latest history in each.
84
- # Fetching pre-checkout dependency repositories is sufficient because
85
- # we make sure that the same dependencies are configured post-checkout.
86
- Console.log_substep("Fetching repositories before proceeding with merge...")
87
- main_repo.fetch
88
- pre_checkout_config_entries.each { |e| e.repo.fetch }
89
-
90
- # Checkout the specified main repo ref to find out which dependency refs to merge
91
- Performer.perform_main_repo_checkout(main_repo, @ref)
92
-
93
- # Load config entries for the ref we're going to merge
94
- post_checkout_config_entries = config_file.load_entries
95
-
96
- # Auto-merge would be too complex to implement (due to lots of edge cases)
97
- # if the specified ref does not have the same dependencies
98
- ensure_dependencies_match(pre_checkout_config_entries, post_checkout_config_entries)
99
-
100
- # Create a merge descriptor for each would-be merge
101
- descriptors = []
102
- Performer.perform_on_dependencies do |config_entry, lock_entry|
103
- revision = RevisionSelector.revision_for_mode(mode, @ref, lock_entry)
104
- descriptors.push(MergeDescriptor.new(config_entry.name, config_entry.path, revision))
105
- end
106
- descriptors.push(MergeDescriptor.new("Main Repo", main_repo.path, @ref))
107
-
108
- # Log merge operations to the console before the fact
109
- Console.log_warning("Merging would #{message_for_mode(mode, @ref)}:")
110
- log_merges(descriptors)
111
-
112
- raise MultiRepoException, "Merge aborted" unless Console.ask_yes_no("Proceed?")
113
-
114
- Console.log_step("Performing merge...")
115
-
116
- # Checkout the initial revision to perform the merge in it
117
- Performer.perform_main_repo_checkout(main_repo, initial_revision)
118
-
119
- # Merge dependencies and the main repo
120
- perform_merges(descriptors)
121
- end
122
-
123
- def ensure_dependencies_match(pre_checkout_config_entries, post_checkout_config_entries)
124
- perfect_match = true
125
- pre_checkout_config_entries.each do |pre_entry|
126
- found = post_checkout_config_entries.find { |post_entry| post_entry.id = pre_entry.id }
127
- perfect_match &= found
128
- Console.log_warning("Dependency '#{pre_entry.repo.path}' was not found in the target ref") unless found
129
- end
130
-
131
- unless perfect_match
132
- raise MultiRepoException, "Dependencies differ, please merge manually"
133
- end
134
-
135
- if post_checkout_config_entries.count > pre_checkout_config_entries.count
136
- raise MultiRepoException, "There are more dependencies in the specified ref, please merge manually"
137
- end
138
- end
139
-
140
- def log_merges(descriptors)
141
- table = Terminal::Table.new do |t|
142
- descriptors.reverse.each_with_index do |descriptor, index|
143
- t.add_row [descriptor.name, "Merge '#{descriptor.revision}'"]
144
- t.add_separator unless index == descriptors.count - 1
145
- end
146
- end
147
- puts table
148
- end
149
-
150
- def message_for_mode(mode, ref)
151
- case mode
152
- when RevisionSelectionMode::AS_LOCK
153
- "merge specific commits as stored in the lock file for main repo revision #{ref}"
154
- when RevisionSelectionMode::LATEST
155
- "merge each branch as stored in the lock file of main repo revision #{ref}"
156
- when RevisionSelectionMode::EXACT
157
- "merge #{ref} for each repository, ignoring the contents of the lock file"
158
- end
159
- end
160
-
161
- def perform_merges(descriptors)
162
- descriptors.each do |descriptor|
163
- Console.log_substep("#{descriptor.name} : Merging #{descriptor.revision} into current branch...")
164
- GitRunner.run_in_working_dir(descriptor.path, "merge #{descriptor.revision}", Runner::Verbosity::OUTPUT_ALWAYS)
165
- end
166
- end
167
- end
1
+ require "terminal-table"
2
+
3
+ require "multirepo/utility/console"
4
+ require "multirepo/logic/node"
5
+ require "multirepo/logic/revision-selector"
6
+ require "multirepo/logic/performer"
7
+ require "multirepo/logic/merge-descriptor"
8
+
9
+ module MultiRepo
10
+ class MergeValidationResult
11
+ ABORT = 0
12
+ PROCEED = 1
13
+ MERGE_UPSTREAM = 2
14
+
15
+ attr_accessor :outcome
16
+ attr_accessor :message
17
+ end
18
+
19
+ class MergeCommand < Command
20
+ self.command = "merge"
21
+ self.summary = "Performs a git merge on all dependencies and the main repo, in the proper order."
22
+
23
+ def self.options
24
+ [
25
+ ['<refname>', 'The main repo tag, branch or commit id to merge.'],
26
+ ['[--latest]', 'Merge the HEAD of each stored dependency branch instead of the commits recorded in the lock file.'],
27
+ ['[--exact]', 'Merge the exact specified ref for each repo, regardless of what\'s stored in the lock file.']
28
+ ].concat(super)
29
+ end
30
+
31
+ def initialize(argv)
32
+ @ref_name = argv.shift_argument
33
+ @checkout_latest = argv.flag?("latest")
34
+ @checkout_exact = argv.flag?("exact")
35
+ super
36
+ end
37
+
38
+ def validate!
39
+ super
40
+ help! "You must specify a ref to merge" unless @ref_name
41
+ unless validate_only_one_flag(@checkout_latest, @checkout_exact)
42
+ help! "You can't provide more than one operation modifier (--latest, --exact, etc.)"
43
+ end
44
+ end
45
+
46
+ def run
47
+ ensure_in_work_tree
48
+ ensure_multirepo_enabled
49
+
50
+ # Find out the checkout mode based on command-line options
51
+ mode = RevisionSelector.mode_for_args(@checkout_latest, @checkout_exact)
52
+
53
+ strategy_name = RevisionSelectionMode.name_for_mode(mode)
54
+ Console.log_step("Merging #{@ref_name} with '#{strategy_name}' strategy...")
55
+
56
+ main_repo = Repo.new(".")
57
+
58
+ # Keep the initial revision because we're going to need to come back to it later
59
+ initial_revision = main_repo.current_revision
60
+
61
+ begin
62
+ merge_core(main_repo, initial_revision, mode)
63
+ rescue MultiRepoException => e
64
+ # Revert to the initial revision only if necessary
65
+ unless main_repo.current_revision == initial_revision
66
+ Console.log_warning("Restoring working copy to #{initial_revision}")
67
+ main_repo.checkout(initial_revision)
68
+ end
69
+ raise e
70
+ end
71
+
72
+ Console.log_step("Done!")
73
+ end
74
+
75
+ def merge_core(main_repo, initial_revision, mode)
76
+ config_file = ConfigFile.new(".")
77
+ lock_file = LockFile.new(".")
78
+
79
+ # Ensure the main repo is clean
80
+ raise MultiRepoException, "Main repo is not clean; merge aborted" unless main_repo.clean?
81
+
82
+ # Ensure dependencies are clean
83
+ unless Utils.dependencies_clean?(config_file.load_entries)
84
+ raise MultiRepoException, "Dependencies are not clean; merge aborted"
85
+ end
86
+
87
+ ref_name = @ref_name
88
+ descriptors = nil
89
+ loop do
90
+ # Gather information about the merges that would occur
91
+ descriptors = build_merge(main_repo, initial_revision, ref_name, mode)
92
+
93
+ # Preview merge operations in the console
94
+ preview_merge(descriptors, mode, ref_name)
95
+
96
+ # Validate merge operations
97
+ result = ensure_merge_valid(descriptors)
98
+
99
+ case result.outcome
100
+ when MergeValidationResult::ABORT
101
+ raise MultiRepoException, result.message
102
+ when MergeValidationResult::PROCEED
103
+ raise MultiRepoException, "Merge aborted" unless Console.ask_yes_no("Proceed?")
104
+ Console.log_warning(result.message) if result.message
105
+ break
106
+ when MergeValidationResult::MERGE_UPSTREAM
107
+ Console.log_warning(result.message)
108
+ raise MultiRepoException, "Merge aborted" unless Console.ask_yes_no("Merge upstream instead of local branches?")
109
+ # TODO: Modify operations!
110
+ raise MultiRepoException, "Fallback behavior not implemented. Please merge manually."
111
+ next
112
+ end
113
+
114
+ raise MultiRepoException, "Merge aborted" unless Console.ask_yes_no("Proceed?")
115
+ end
116
+
117
+ Console.log_step("Performing merge...")
118
+
119
+ # Merge dependencies and the main repo
120
+ perform_merges(descriptors)
121
+ end
122
+
123
+ def build_merge(main_repo, initial_revision, ref_name, mode)
124
+ # List dependencies prior to checkout so that we can compare them later
125
+ our_dependencies = Performer.dependencies
126
+
127
+ # Checkout the specified main repo ref to find out which dependency refs to merge
128
+ Performer.perform_main_repo_checkout(main_repo, ref_name, "Checked out main repo '#{ref_name}' to inspect to-merge dependencies")
129
+
130
+ # List dependencies for the ref we're trying to merge
131
+ their_dependencies = Performer.dependencies
132
+
133
+ # Checkout the initial revision ASAP
134
+ Performer.perform_main_repo_checkout(main_repo, initial_revision, "Checked out initial main repo revision '#{initial_revision}'")
135
+
136
+ # Auto-merge would be too complex to implement (due to lots of edge cases)
137
+ # if the specified ref does not have the same dependencies. Better perform a manual merge.
138
+ ensure_dependencies_match(our_dependencies, their_dependencies)
139
+
140
+ # Create a merge descriptor for each would-be merge as well as the main repo.
141
+ # This step MUST be performed in OUR revision for the merge descriptors to be correct!
142
+ descriptors = build_dependency_merge_descriptors(our_dependencies, their_dependencies, ref_name, mode)
143
+ descriptors.push(MergeDescriptor.new("Main Repo", main_repo, initial_revision, ref_name))
144
+
145
+ return descriptors
146
+ end
147
+
148
+ def build_dependency_merge_descriptors(our_dependencies, their_dependencies, ref_name, mode)
149
+ descriptors = []
150
+ our_dependencies.zip(their_dependencies).each do |our_dependency, their_dependency|
151
+ our_revision = our_dependency.config_entry.repo.current_revision
152
+
153
+ their_revision = RevisionSelector.revision_for_mode(mode, ref_name, their_dependency.lock_entry)
154
+ their_name = their_dependency.config_entry.name
155
+ their_repo = their_dependency.config_entry.repo
156
+
157
+ descriptor = MergeDescriptor.new(their_name, their_repo, our_revision, their_revision)
158
+
159
+ descriptors.push(descriptor)
160
+ end
161
+ return descriptors
162
+ end
163
+
164
+ def ensure_dependencies_match(our_dependencies, their_dependencies)
165
+ our_dependencies.zip(their_dependencies).each do |our_dependency, their_dependency|
166
+ if their_dependency == nil || their_dependency.config_entry.id != our_dependency.config_entry.id
167
+ raise MultiRepoException, "Dependencies differ, please merge manually"
168
+ end
169
+ end
170
+
171
+ if their_dependencies.count > our_dependencies.count
172
+ raise MultiRepoException, "There are more dependencies in the specified ref, please merge manually"
173
+ end
174
+ end
175
+
176
+ def preview_merge(descriptors, mode, ref_name)
177
+ Console.log_info("Merging would #{message_for_mode(mode, ref_name)}:")
178
+
179
+ table = Terminal::Table.new do |t|
180
+ descriptors.reverse.each_with_index do |descriptor, index|
181
+ t.add_row [descriptor.name.bold, descriptor.merge_description, descriptor.upstream_description]
182
+ t.add_separator unless index == descriptors.count - 1
183
+ end
184
+ end
185
+ puts table
186
+ end
187
+
188
+ def ensure_merge_valid(descriptors)
189
+ outcome = MergeValidationResult.new
190
+ outcome.outcome = MergeValidationResult::PROCEED
191
+
192
+ if descriptors.any? { |d| d.state == TheirState::LOCAL_NO_UPSTREAM }
193
+ outcome.message = "Some branches are not remote-tracking! Please review the merge operations above."
194
+ elsif descriptors.any? { |d| d.state == TheirState::LOCAL_UPSTREAM_DIVERGED }
195
+ outcome.outcome = MergeValidationResult::ABORT
196
+ outcome.message = "Some upstream branches have diverged. This warrants a manual merge!"
197
+ elsif descriptors.any? { |d| d.state == TheirState::LOCAL_OUTDATED }
198
+ outcome.outcome = MergeValidationResult::MERGE_UPSTREAM
199
+ outcome.message = "Some local branches are outdated."
200
+ end
201
+
202
+ return outcome
203
+ end
204
+
205
+ def perform_merges(descriptors)
206
+ success = true
207
+ descriptors.each do |descriptor|
208
+ Console.log_substep("#{descriptor.name} : Merging #{descriptor.their_revision} into #{descriptor.our_revision}...")
209
+ GitRunner.run_in_working_dir(descriptor.repo.path, "merge #{descriptor.their_revision}", Runner::Verbosity::OUTPUT_ALWAYS)
210
+ success &= GitRunner.last_command_succeeded
211
+ end
212
+ Console.log_warning("Some merge operations failed. Please review the above results.") unless success
213
+ end
214
+
215
+ def message_for_mode(mode, ref_name)
216
+ case mode
217
+ when RevisionSelectionMode::AS_LOCK
218
+ "merge specific commits as stored in the lock file for main repo revision #{ref_name}"
219
+ when RevisionSelectionMode::LATEST
220
+ "merge each branch as stored in the lock file of main repo revision #{ref_name}"
221
+ when RevisionSelectionMode::EXACT
222
+ "merge #{ref_name} for each repository, ignoring the contents of the lock file"
223
+ end
224
+ end
225
+ end
168
226
  end