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
|
# File 'lib/git_bpf/commands/recreate-branch.rb', line 43
def execute(opts, argv)
if argv.length != 1
run('recreate-branch', '--help')
terminate
end
source = argv.pop
opts.branch = source if opts.branch == nil
if not refExists? opts.base
terminate "Cannot find reference '#{opts.base}' to use as a base for new branch: #{opts.branch}."
end
if opts.discard
unless opts.remote
repo = Repository.new(Dir.getwd)
remote_name = repo.config(true, "--get", "gitbpf.remotename").chomp
opts.remote = remote_name.empty? ? 'origin' : remote_name
end
git('fetch', opts.remote)
if branchExists?(source, opts.remote)
opoo "This will delete your local '#{source}' branch if it exists and create it afresh from the #{opts.remote} remote."
if not promptYN "Continue?"
terminate "Aborting."
end
git('checkout', opts.base)
git('branch', '-D', source) if branchExists? source
git('checkout', source)
end
end
if not branchExists? source
terminate "Cannot recreate branch #{source} as it doesn't exist."
end
if opts.branch != source and branchExists? opts.branch
terminate "Cannot create branch #{opts.branch} as it already exists."
end
ohai "1. Processing branch '#{source}' for merge-commits..."
branches = getMergedBranches(opts.base, source)
if branches.empty?
terminate "No feature branches detected, '#{source}' matches '#{opts.base}'."
end
if opts.list
terminate "Branches to be merged:\n#{branches.shell_list}"
end
branches.reject! do |item|
stripped = item.gsub /^remotes\/\w+\/([\w\-\/]+)$/, '\1'
opts.exclude.include? stripped
end
opoo "The following branches will be merged when the new #{opts.branch} branch is created:\n#{branches.shell_list}"
puts
puts "If you see something unexpected check:"
puts "a) that your '#{source}' branch is up to date"
puts "b) if '#{opts.base}' is a branch, make sure it is also up to date."
opoo "If there are any non-merge commits in '#{source}', they will not be included in '#{opts.branch}'. You have been warned."
if not promptYN "Proceed with #{source} branch recreation?"
terminate "Aborting."
end
tmp_source = "#{@@prefix}-#{source}"
ohai "2. Creating backup of '#{source}', '#{tmp_source}'..."
if branchExists? tmp_source
terminate "Cannot create branch #{tmp_source} as one already exists. To continue, #{tmp_source} must be removed."
end
git('branch', '-m', source, tmp_source)
ohai "3. Creating new '#{opts.branch}' branch based on '#{opts.base}'..."
git('checkout', '-b', opts.branch, opts.base, '--quiet')
ohai "4. Merging in feature branches..."
branches.each do |branch|
begin
puts " - '#{branch}'"
git('merge', '--quiet', '--no-ff', '--no-edit', branch)
rescue
conflicts = git('rerere', 'status').chomp.split("\n")
if conflicts.length != 0
puts "\n"
puts "There is a merge conflict with branch #{branch} that has no rerere."
puts "Record a resoloution by resolving the conflict."
puts "Then run the following command to return your repository to its original state."
puts "\n"
puts "git checkout #{tmp_source} && git branch -D #{opts.branch} && git branch -m #{opts.branch}"
puts "\n"
puts "If you do not want to resolve the conflict, it is safe to just run the above command to restore your repository to the state it was in before executing this command."
terminate
else
git('commit', '-a', '--no-edit')
end
end
end
ohai "5. Cleaning up temporary branches ('#{tmp_source}')."
if source != opts.branch
git('branch', '-m', tmp_source, source)
else
git('branch', '-D', tmp_source)
end
end
|