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
|
package Devscripts::Salsa::rename_branches;
use strict;
use Devscripts::Output;
use Moo::Role;
with 'Devscripts::Salsa::Repo';
# Helper function to add a mapping only if the source branch exists.
sub add_mapping_if_exists {
my ($branch_map_ref, $remote_branch_names_ref, $old, $new) = @_;
$branch_map_ref->{$old} //= $new
if exists $remote_branch_names_ref->{$old};
}
sub _add_dep14_mappings {
my ($self, $project, $branch_map_ref, $remote_branch_names_ref) = @_;
ds_verbose "Adding DEP-14 default mappings for existing branches.";
# Core DEP-14 and special branches
add_mapping_if_exists($branch_map_ref, $remote_branch_names_ref,
$project->{default_branch},
'debian/latest');
add_mapping_if_exists(
$branch_map_ref, $remote_branch_names_ref,
'upstream', 'upstream/latest'
);
add_mapping_if_exists(
$branch_map_ref, $remote_branch_names_ref,
'upstream/latest', 'upstream/latest'
);
add_mapping_if_exists(
$branch_map_ref, $remote_branch_names_ref,
'pristine-tar', 'pristine-tar'
);
add_mapping_if_exists(
$branch_map_ref, $remote_branch_names_ref,
'patch-queue/master', 'patch-queue/master'
);
add_mapping_if_exists(
$branch_map_ref, $remote_branch_names_ref,
'debian/experimental', 'debian/experimental'
);
# Debian suites
my @debian_suites
= qw(trixie bookworm bullseye buster stretch jessie wheezy);
for my $suite (@debian_suites) {
add_mapping_if_exists($branch_map_ref, $remote_branch_names_ref,
$suite, "debian/$suite");
add_mapping_if_exists(
$branch_map_ref, $remote_branch_names_ref,
"${suite}-backports", "debian/${suite}-backports"
);
add_mapping_if_exists($branch_map_ref, $remote_branch_names_ref,
"${suite}-backports-staging", "debian/${suite}-backports-staging");
add_mapping_if_exists(
$branch_map_ref, $remote_branch_names_ref,
"${suite}-fasttrack", "debian/${suite}-fasttrack"
);
add_mapping_if_exists(
$branch_map_ref, $remote_branch_names_ref,
"${suite}-security", "debian/${suite}-security"
);
# Map new DEP-14 names to themselves to avoid being flagged as unmapped.
add_mapping_if_exists(
$branch_map_ref, $remote_branch_names_ref,
"debian/$suite", "debian/$suite"
);
add_mapping_if_exists(
$branch_map_ref, $remote_branch_names_ref,
"debian/${suite}-backports", "debian/${suite}-backports"
);
add_mapping_if_exists(
$branch_map_ref, $remote_branch_names_ref,
"debian/${suite}-backports-staging",
"debian/${suite}-backports-staging"
);
add_mapping_if_exists(
$branch_map_ref, $remote_branch_names_ref,
"debian/${suite}-fasttrack", "debian/${suite}-fasttrack"
);
add_mapping_if_exists(
$branch_map_ref, $remote_branch_names_ref,
"debian/${suite}-security", "debian/${suite}-security"
);
}
}
sub rename_branches {
my ($self, $project_name, @branches) = @_;
# --- Project Information ---
# The first argument is the project name.
# The rest of the arguments are branch mappings in the format "old:new".
# Get the project ID and path.
my @repos = $self->get_repo(0, $project_name);
return 1 unless (ref $repos[0]);
my ($id, $str) = @{ $repos[0] };
if (!$id) {
ds_warn "Project not found: $project_name\n";
return 1;
}
# Get the full project object.
my $project = $self->api->project($id);
# --- Configuration ---
my $no_wait = $self->config->no_wait;
# --- Branch Mappings ---
# The rest of the arguments are branch mappings in the format "old:new".
my %branch_map;
for my $branch (@branches) {
my ($old, $new) = split /:/, $branch, 2;
$branch_map{$old} = $new;
}
# --- DEP-14 Default Mappings ---
if ($self->config->dep14) {
my @remote_branches = @{ $self->api->branches($id) };
my %remote_branch_names = map { $_->{name} => 1 } @remote_branches;
$self->_add_dep14_mappings($project, \%branch_map,
\%remote_branch_names);
}
# --- Rename Branches ---
my $default_branch = $project->{default_branch};
my %renamed_branches;
my @remote_branches_list = @{ $self->api->branches($id) };
my %remote_branch_names_map
= map { $_->{name} => 1 } @remote_branches_list;
for my $old (keys %branch_map) {
my $new = $branch_map{$old};
if ($old eq $new) {
ds_verbose "Branch '$old' is mapped to itself, skipping.";
next;
}
# --- Pre-flight checks ---
# Check if the source branch exists and the destination branch does not.
my $source_branch;
my $dest_branch_exists = 0;
if (exists $remote_branch_names_map{$old}) {
foreach my $b (@remote_branches_list) {
if ($b->{name} eq $old) {
$source_branch = $b;
last;
}
}
}
$dest_branch_exists = 1 if exists $remote_branch_names_map{$new};
if (not $source_branch) {
ds_warn "Source branch '$old' not found, skipping.\n";
next;
}
if ($dest_branch_exists) {
ds_warn "Destination branch '$new' already exists, skipping.\n";
next;
}
# --- Handle Protected Branches ---
# If the source branch is protected, we need to unprotect it first.
if ($source_branch->{protected}) {
$self->api->unprotect_branch($id, $old);
}
# --- Rename Logic ---
eval {
# If the new branch name is a "subdirectory" of the old one (e.g. master -> master/foo),
# we need to use a temporary branch to avoid the new branch being deleted with the old one.
if ($new =~ m/^$old\//) {
my $temp_branch = "temp-rename-" . time();
# 1. Create temporary branch from old branch.
$self->api->create_branch($id,
{ branch => $temp_branch, ref => $old });
# 2. If old branch is default, set temporary branch as default.
if ($old eq $default_branch) {
$self->api->edit_project($id,
{ default_branch => $temp_branch });
}
# 3. Delete old branch.
$self->api->delete_branch($id, $old);
# 4. Create new branch from temporary branch.
$self->api->create_branch($id,
{ branch => $new, ref => $temp_branch });
# 5. If temporary branch is default, set new branch as default.
if ($temp_branch eq $self->api->project($id)->{default_branch})
{
$self->api->edit_project($id, { default_branch => $new });
}
# 6. Delete temporary branch.
$self->api->delete_branch($id, $temp_branch);
} else {
# For simple renames:
# 1. Create new branch from old branch.
$self->api->create_branch($id,
{ branch => $new, ref => $old });
# 2. If old branch is default, set new branch as default.
if ($old eq $default_branch) {
$self->api->edit_project($id, { default_branch => $new });
}
# 3. Delete old branch.
$self->api->delete_branch($id, $old);
}
};
if ($@) {
ds_warn "Branch rename has failed for $str\n";
ds_verbose $@;
if (!$self->config->no_fail) {
ds_verbose "Use --no-fail to continue";
return 1;
}
next;
}
# Store successfully renamed branch for later verification.
$renamed_branches{$old} = $new;
}
# --- Verification ---
if (not $no_wait and keys %renamed_branches) {
ds_verbose
"Now polling Salsa to check if the changes are correctly reflected when listing branches...";
my $all_verified = 0;
my $retries = 30;
my $delay = 3;
for my $i (1 .. $retries) {
my @remote_branches = @{ $self->api->branches($id) };
my %remote_branch_names = map { $_->{name} => 1 } @remote_branches;
my $unverified_count = 0;
for my $old (keys %renamed_branches) {
my $new = $renamed_branches{$old};
if (exists $remote_branch_names{$old}
or not exists $remote_branch_names{$new}) {
$unverified_count++;
}
}
if ($unverified_count == 0) {
$all_verified = 1;
last;
}
if ($i == $retries) {
ds_die "Failed to verify all branch renames.";
}
sleep $delay;
}
ds_verbose "All branch renames verified successfully."
if $all_verified;
}
return 0;
}
1;
|