55# this script will ensure that branches from the same MR will be rebased correctly, keeping the chain order
66
77def compute_default_branch
8- system ( *%w[ git show-ref -q --verify refs/heads/main ] ) ? ' main' : ' master'
8+ system ( *%w[ git show-ref -q --verify refs/heads/main ] ) ? " main" : " master"
99end
1010
1111# compute_parent_branch will determine the closest parent branch,
@@ -15,19 +15,19 @@ def compute_default_branch
1515def compute_parent_branch ( branch_name = nil )
1616 branch_name ||= `git rev-parse --abbrev-ref HEAD`
1717 remote_names = `git remote` . lines . map ( &:chomp )
18- active_remote_name = `git rev-parse --abbrev-ref --symbolic-full-name @{u} 2>/dev/null` . split ( '/' ) . first
18+ active_remote_name = `git rev-parse --abbrev-ref --symbolic-full-name @{u} 2>/dev/null` . split ( "/" ) . first
1919 other_remote_names = remote_names - [ active_remote_name ]
2020
2121 parent_branch_line = `git log --decorate --simplify-by-decoration --oneline #{ branch_name } ` . lines [ 1 ] . rstrip
2222 parent_branch_line = parent_branch_line
23- . sub ( ' HEAD -> ' , '' )
24- . gsub ( /tag: [^,)]+(, )?/ , '' )
23+ . sub ( " HEAD -> " , "" )
24+ . gsub ( /tag: [^,)]+(, )?/ , "" )
2525 return compute_default_branch if parent_branch_line . match? ( %r{.* \( (origin|security)/(.*)\) .*} )
2626 return compute_default_branch unless parent_branch_line . match? ( /.* \( (.*)\) .*/ )
2727
2828 parent_branch_line
2929 . sub ( /.* \( (.*)\) .*/ , '\1' )
30- . split ( ', ' )
30+ . split ( ", " )
3131 . reject { |b | other_remote_names . any? { |remote_name | b . start_with? ( "#{ remote_name } /" ) } }
3232 . min_by ( &:length ) || compute_default_branch
3333end
@@ -72,26 +72,26 @@ def rebase_all_per_capture_info(local_branch_info_hash)
7272 current_branch = `git branch --show-current` . strip
7373
7474 local_branch_info_hash . each do |branch , parent_branch |
75- puts ' Rebasing ' . brown + branch . cyan + ' onto ' . brown + parent_branch . green + ' ...' . brown
75+ puts " Rebasing " . brown + branch . cyan + " onto " . brown + parent_branch . green + " ..." . brown
7676
7777 system ( *%W[ git rev-parse --abbrev-ref --symbolic-full-name #{ parent_branch } ] , out : File ::NULL , err : File ::NULL )
7878 unless Process . last_status . success?
79- puts ' Skipping since ' . red + parent_branch . green + ' does not exist.' . red
80- return
79+ puts " Skipping since " . red + parent_branch . green + " does not exist." . red
80+ next
8181 end
8282
83- system ( { ' LEFTHOOK' => '0' } , *%W[ git rebase --update-refs --autostash #{ parent_branch } #{ branch } ] )
83+ system ( { " LEFTHOOK" => "0" } , *%W[ git rebase --update-refs --autostash #{ parent_branch } #{ branch } ] )
8484 next if Process . last_status . success?
8585
8686 auto_generated_files_hash = {
87- ' app/workers/all_queues.yml' => %w[ bundle exec rake gitlab:sidekiq:all_queues_yml:generate ] ,
88- ' config/sidekiq_queues.yml' => %w[ bundle exec rake gitlab:sidekiq:sidekiq_queues_yml:generate ] ,
89- ' db/structure.sql' => %w[ bundle exec scripts/regenerate-schema ] ,
90- ' doc/api/graphql/reference/index.md' => %w[ bundle exec rake gitlab:graphql:compile_docs ] ,
91- ' doc/user/compliance/audit_event_types.md' => %w[ bundle exec rake gitlab:audit_event_types:compile_docs ] ,
92- ' doc/update/breaking_windows.md' => %w[ bundle exec rake gitlab:docs:compile_windows ] ,
93- ' doc/update/deprecations.md' => %w[ bundle exec rake gitlab:docs:compile_deprecations ] ,
94- ' doc/update/removals.md' => %w[ bundle exec rake gitlab:docs:compile_removals ]
87+ " app/workers/all_queues.yml" => %w[ bundle exec rake gitlab:sidekiq:all_queues_yml:generate ] ,
88+ " config/sidekiq_queues.yml" => %w[ bundle exec rake gitlab:sidekiq:sidekiq_queues_yml:generate ] ,
89+ " db/structure.sql" => %w[ bundle exec scripts/regenerate-schema ] ,
90+ " doc/api/graphql/reference/index.md" => %w[ bundle exec rake gitlab:graphql:compile_docs ] ,
91+ " doc/user/compliance/audit_event_types.md" => %w[ bundle exec rake gitlab:audit_event_types:compile_docs ] ,
92+ " doc/update/breaking_windows.md" => %w[ bundle exec rake gitlab:docs:compile_windows ] ,
93+ " doc/update/deprecations.md" => %w[ bundle exec rake gitlab:docs:compile_deprecations ] ,
94+ " doc/update/removals.md" => %w[ bundle exec rake gitlab:docs:compile_removals ]
9595 }
9696
9797 loop do
@@ -112,12 +112,12 @@ def rebase_all_per_capture_info(local_branch_info_hash)
112112 err = false
113113 end
114114
115- break unless err || system ( { ' GIT_EDITOR' => ' true' , ' LEFTHOOK' => '0' } , *%w[ git rebase --continue ] )
115+ break unless err || system ( { " GIT_EDITOR" => " true" , " LEFTHOOK" => "0" } , *%w[ git rebase --continue ] )
116116 end
117117
118118 # There is a merge conflict that needs to be resolved by the user, exit now
119119 system ( %(terminal-notifier -title 'rebase_all failed with merge conflict' -message 'Please resolve merge conflicts!' -sound boop -ignoreDnD) )
120- return
120+ return false
121121 end
122122
123123 system ( *%W[ git switch #{ current_branch } ] )
@@ -127,22 +127,22 @@ def rebase_mappings
127127 system ( *%w[ git diff-index --quiet HEAD -- . ':!bin/' ] )
128128 unless Process . last_status . success?
129129 system ( %(terminal-notifier -title 'rebase_all failed' -message 'Please stash the changes in the current branch before calling rebase_all!' -sound boop -ignoreDnD) )
130- abort ' Please stash the changes in the current branch before calling rebase_all!' . red
130+ abort " Please stash the changes in the current branch before calling rebase_all!" . red
131131 end
132132
133- user_name = ENV . fetch ( ' USER' , nil )
133+ user_name = ENV . fetch ( " USER" , nil )
134134 default_branch = compute_default_branch
135135
136- mr_pattern = %r{^(security[-/])?#{ user_name } /(?<mr_id>\d +)/[a-z0-9\- +_]+$}i
137- seq_mr_pattern = %r{^(security[-/])?#{ user_name } /(?<mr_id>\d +)/(?<mr_seq_nr>\d +)-[a-z0-9\- +_]+$}i
136+ mr_pattern = %r{^(security[-/])?#{ user_name } /(?<mr_id>\d +)/[a-z0-9\- +_]+$}i
137+ seq_mr_pattern = %r{^(security[-/])?#{ user_name } /(?<mr_id>\d +)/(?<mr_seq_nr>\d +)-[a-z0-9\- +_]+$}i
138138 backport_pattern = %r{^(security[-/])?#{ user_name } /(?<mr_id>\d +)/[a-z0-9\- +_]+-(?<milestone>\d +[-.]\d +)$}i
139139
140140 local_branches =
141141 `git branch --list`
142- . lines
143- . map { |line | line [ 2 ..] . rstrip }
144- . select { |branch | branch . start_with? ( "#{ user_name } /" , "security-#{ user_name } /" , "security/#{ user_name } /" ) }
145- . sort_by do |branch |
142+ . lines
143+ . map { |line | line [ 2 ..] . rstrip }
144+ . select { |branch | branch . start_with? ( "#{ user_name } /" , "security-#{ user_name } /" , "security/#{ user_name } /" ) }
145+ . sort_by do |branch |
146146 seq_mr_match_data = seq_mr_pattern . match ( branch )
147147 backport_match_data = backport_pattern . match ( branch )
148148 mr_match_data = mr_pattern . match ( branch )
@@ -161,7 +161,7 @@ def rebase_mappings
161161 branch_distance ( branch , default_branch ) ,
162162 backport_match_data [ :mr_id ] . to_i ,
163163 999 , # Put backport branches after sequenced branches
164- backport_match_data [ :milestone ] . tr ( '.' , '-' ) . to_f
164+ backport_match_data [ :milestone ] . tr ( "." , "-" ) . to_f
165165 ]
166166 elsif mr_match_data
167167 [
@@ -193,7 +193,7 @@ def rebase_mappings
193193 current_mr_seq_nr = nil
194194
195195 if seq_mr_match_data
196- current_mr_id = seq_mr_match_data [ :mr_id ]
196+ current_mr_id = seq_mr_match_data [ :mr_id ]
197197 current_mr_seq_nr = seq_mr_match_data [ :mr_seq_nr ] . to_i
198198 elsif mr_match_data
199199 current_mr_id = mr_match_data [ :mr_id ]
@@ -219,9 +219,9 @@ def rebase_mappings
219219 rebase_onto = parent_branch
220220 end
221221 elsif backport_match_data
222- remote = ' security'
222+ remote = " security"
223223
224- parent_branch = "#{ remote } /#{ backport_match_data [ :milestone ] . tr ( '.' , '-' ) } -stable-ee"
224+ parent_branch = "#{ remote } /#{ backport_match_data [ :milestone ] . tr ( "." , "-" ) } -stable-ee"
225225 rebase_onto = parent_branch
226226 else
227227 parent_branch = compute_parent_branch ( branch )
@@ -238,22 +238,22 @@ def rebase_mappings
238238end
239239
240240def branch_sort_key ( branch_info )
241- [ branch_info [ :chain_mr_id ] || '' , branch_info [ :chain_mr_seq_nr ] . nil? ? 0 : -branch_info [ :chain_mr_seq_nr ] ]
241+ [ branch_info [ :chain_mr_id ] || "" , branch_info [ :chain_mr_seq_nr ] . nil? ? 0 : -branch_info [ :chain_mr_seq_nr ] ]
242242end
243243
244244def branch_distance ( branch , parent_branch )
245245 `git rev-list --count #{ parent_branch } ..#{ branch } ` . strip . to_i
246246end
247247
248248def rebase_all
249- require ' json'
249+ require " json"
250250 default_branch = compute_default_branch
251251 mappings = rebase_mappings
252- . sort { |b1 , b2 | branch_sort_key ( b1 ) <=> branch_sort_key ( b2 ) }
253- . sort do |b1 , b2 |
254- branch_distance ( b1 [ :branch ] ,
255- default_branch ) <=> branch_distance ( b2 [ :branch ] , default_branch )
256- end
252+ . sort { |b1 , b2 | branch_sort_key ( b1 ) <=> branch_sort_key ( b2 ) }
253+ . sort do |b1 , b2 |
254+ branch_distance ( b1 [ :branch ] ,
255+ default_branch ) <=> branch_distance ( b2 [ :branch ] , default_branch )
256+ end
257257 . to_h { |b | [ b [ :branch ] , b [ :rebase_onto ] ] }
258258 rebase_all_per_capture_info ( mappings )
259259end
@@ -263,7 +263,7 @@ def git_push_issue(*args)
263263
264264 current_branch = `git branch --show-current` . strip
265265
266- user_name = ENV . fetch ( ' USER' , nil )
266+ user_name = ENV . fetch ( " USER" , nil )
267267 mr_pattern = %r{^(?<prefix>(security[-/])?#{ user_name } )/(?<mr_id>\d +)/[a-z0-9\- +_]+$}
268268 mr_match_data = mr_pattern . match ( current_branch )
269269
@@ -281,7 +281,7 @@ def git_push_issue(*args)
281281
282282 system ( *%W[ git rev-parse --abbrev-ref --symbolic-full-name #{ parent_branch } ] , out : File ::NULL , err : File ::NULL )
283283 unless Process . last_status . success?
284- puts ' Skipping since ' . red + parent_branch . green + ' does not exist.' . red
284+ puts " Skipping since " . red + parent_branch . green + " does not exist." . red
285285 next
286286 end
287287
@@ -290,9 +290,9 @@ def git_push_issue(*args)
290290
291291 return unless branches . any?
292292
293- active_remote_name = `git rev-parse --abbrev-ref --symbolic-full-name @{u} 2>/dev/null` . split ( '/' ) . first
293+ active_remote_name = `git rev-parse --abbrev-ref --symbolic-full-name @{u} 2>/dev/null` . split ( "/" ) . first
294294
295- puts ' Pushing ' . brown + branches . map ( &:cyan ) . join ( ', ' ) + ' to ' . brown + active_remote_name . green + ' ...' . brown
295+ puts " Pushing " . brown + branches . map ( &:cyan ) . join ( ", " ) + " to " . brown + active_remote_name . green + " ..." . brown
296296
297297 system ( *%W[ git push --force-with-lease #{ active_remote_name } ] , *branches , *args )
298298end
@@ -303,11 +303,11 @@ def changed_branch_files(format: nil)
303303
304304 change_log = `git diff --diff-filter=AM -U0 #{ parent_branch } ..#{ current_branch } | grep -E '^(---|\\ +\\ +\\ +|@@)'` . lines
305305 changed_files =
306- change_log . reject { |line | line . start_with? ( '@@' ) } # discard diff header
307- . map { |line | line . slice ( 4 ..-1 ) . chomp }
308- . reject { |file | file == '/dev/null' } # discard deletions
309- . map { |file | file . slice ( 2 ..-1 ) } # discard b/
310- . uniq
306+ change_log . reject { |line | line . start_with? ( "@@" ) } # discard diff header
307+ . map { |line | line . slice ( 4 ..-1 ) . chomp }
308+ . reject { |file | file == File :: NULL } # discard deletions
309+ . map { |file | file . slice ( 2 ..-1 ) } # discard b/
310+ . uniq
311311
312312 changed_files . map do |file |
313313 index = change_log . index { |line | line . include? ( "b/#{ file } " ) }
0 commit comments