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
|
# frozen_string_literal: true
require 'logger'
require 'gitlab/housekeeper/shell'
module Gitlab
module Housekeeper
class Git
def initialize(logger:, branch_from: 'master')
@logger = logger
@branch_from = branch_from
end
def commit_in_branch(change)
branch_name = branch_name(change.identifiers)
create_commit(branch_name, change)
branch_name
end
def with_branch_from_branch
stashed = false
current_branch = Shell.execute('git', 'branch', '--show-current').chomp
result = Shell.execute('git', 'stash')
stashed = !result.include?('No local changes to save')
Shell.execute("git", "checkout", @branch_from)
yield
ensure
Shell.execute("git", "checkout", current_branch)
Shell.execute('git', 'stash', 'pop') if stashed
end
def create_commit(branch_name, change)
current_branch = Shell.execute('git', 'branch', '--show-current').chomp
begin
Shell.execute("git", "branch", '-D', branch_name)
rescue Shell::Error # Might not exist yet
end
Shell.execute("git", "checkout", "-b", branch_name)
Shell.execute("git", "add", *change.changed_files)
commit_message = <<~MSG
#{change.title}
#{change.description}
This commit was generated by
[gitlab-housekeeper](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/139492).
Changelog: other
MSG
Shell.execute("git", "commit", "-m", commit_message)
ensure
Shell.execute("git", "checkout", current_branch)
end
def branch_name(identifiers)
# Hyphen-case each identifier then join together with hyphens.
branch_name = identifiers
.map { |i| i.gsub(/[[:upper:]]/) { |w| "-#{w.downcase}" } }
.join('-')
.delete_prefix("-")
# Truncate if it's too long and add a digest
if branch_name.length > 240
branch_name = branch_name[0...200] + OpenSSL::Digest::SHA256.hexdigest(branch_name)[0...15]
end
branch_name
end
end
end
end
|