Support multiple branches creation in 'create-project'
In case if a project has some kind of waterfall automerging a->b->c it is convenient to create all these branches at the project creation time. e.g. '.. gerrit create-project -b master -b foo -b bar ...' Change-Id: Iec418985caa89197825cd4fc292bede787bf9786
This commit is contained in:
@@ -19,7 +19,7 @@ SYNOPSIS
|
||||
[--use-signed-off-by | --so]
|
||||
[--use-content-merge]
|
||||
[--require-change-id | --id]
|
||||
[--branch <REF> | -b <REF>]
|
||||
[[--branch <REF> | -b <REF>] ...]
|
||||
[--empty-commit]
|
||||
{ <NAME> | --name <NAME> }
|
||||
|
||||
@@ -59,8 +59,11 @@ OPTIONS
|
||||
|
||||
--branch::
|
||||
-b::
|
||||
Name of the initial branch in the newly created project.
|
||||
Defaults to 'master'.
|
||||
Name of the initial branch(es) in the newly created project.
|
||||
Several branches can be specified on the command line.
|
||||
If several branches are specified then the first one becomes HEAD
|
||||
of the project. If none branches are specified then default value
|
||||
('master') is used.
|
||||
|
||||
--owner::
|
||||
-o::
|
||||
|
@@ -28,6 +28,8 @@ import com.google.inject.assistedinject.Assisted;
|
||||
|
||||
import org.eclipse.jgit.lib.Constants;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
public class CreateProjectHandler extends Handler<VoidResult> {
|
||||
|
||||
interface Factory {
|
||||
@@ -74,7 +76,7 @@ public class CreateProjectHandler extends Handler<VoidResult> {
|
||||
}
|
||||
args.projectDescription = "";
|
||||
args.submitType = SubmitType.MERGE_IF_NECESSARY;
|
||||
args.branch = Constants.MASTER;
|
||||
args.branch = Collections.emptyList();
|
||||
args.createEmptyCommit = emptyCommit;
|
||||
args.permissionsOnly = permissionsOnly;
|
||||
|
||||
|
@@ -51,6 +51,8 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
|
||||
@@ -101,7 +103,7 @@ public class CreateProject {
|
||||
try {
|
||||
final String head =
|
||||
createProjectArgs.permissionsOnly ? GitRepositoryManager.REF_CONFIG
|
||||
: createProjectArgs.branch;
|
||||
: createProjectArgs.branch.get(0);
|
||||
final Repository repo = repoManager.createRepository(nameKey);
|
||||
try {
|
||||
NewProjectCreatedListener.Event event = new NewProjectCreatedListener.Event() {
|
||||
@@ -127,7 +129,7 @@ public class CreateProject {
|
||||
|
||||
if (!createProjectArgs.permissionsOnly
|
||||
&& createProjectArgs.createEmptyCommit) {
|
||||
createEmptyCommit(repo, nameKey, createProjectArgs.branch);
|
||||
createEmptyCommits(repo, nameKey, createProjectArgs.branch);
|
||||
}
|
||||
} finally {
|
||||
repo.close();
|
||||
@@ -235,20 +237,32 @@ public class CreateProject {
|
||||
new ArrayList<AccountGroup.UUID>(projectOwnerGroups);
|
||||
}
|
||||
|
||||
while (createProjectArgs.branch.startsWith("/")) {
|
||||
createProjectArgs.branch = createProjectArgs.branch.substring(1);
|
||||
List<String> transformedBranches = new ArrayList<String>();
|
||||
if (createProjectArgs.branch == null ||
|
||||
createProjectArgs.branch.isEmpty()) {
|
||||
createProjectArgs.branch = Collections.singletonList(Constants.MASTER);
|
||||
}
|
||||
if (!createProjectArgs.branch.startsWith(Constants.R_HEADS)) {
|
||||
createProjectArgs.branch = Constants.R_HEADS + createProjectArgs.branch;
|
||||
}
|
||||
if (!Repository.isValidRefName(createProjectArgs.branch)) {
|
||||
throw new ProjectCreationFailedException(String.format(
|
||||
"Branch \"%s\" is not a valid name.", createProjectArgs.branch));
|
||||
for (String branch : createProjectArgs.branch) {
|
||||
while (branch.startsWith("/")) {
|
||||
branch = branch.substring(1);
|
||||
}
|
||||
if (!branch.startsWith(Constants.R_HEADS)) {
|
||||
branch = Constants.R_HEADS + branch;
|
||||
}
|
||||
if (!Repository.isValidRefName(branch)) {
|
||||
throw new ProjectCreationFailedException(String.format(
|
||||
"Branch \"%s\" is not a valid name.", branch));
|
||||
}
|
||||
if (!transformedBranches.contains(branch)) {
|
||||
transformedBranches.add(branch);
|
||||
}
|
||||
}
|
||||
createProjectArgs.branch = transformedBranches;
|
||||
}
|
||||
|
||||
private void createEmptyCommit(final Repository repo,
|
||||
final Project.NameKey project, final String ref) throws IOException {
|
||||
private void createEmptyCommits(final Repository repo,
|
||||
final Project.NameKey project, final List<String> refs)
|
||||
throws IOException {
|
||||
ObjectInserter oi = repo.newObjectInserter();
|
||||
try {
|
||||
CommitBuilder cb = new CommitBuilder();
|
||||
@@ -260,15 +274,18 @@ public class CreateProject {
|
||||
ObjectId id = oi.insert(cb);
|
||||
oi.flush();
|
||||
|
||||
RefUpdate ru = repo.updateRef(Constants.HEAD);
|
||||
ru.setNewObjectId(id);
|
||||
final Result result = ru.update();
|
||||
switch (result) {
|
||||
case NEW:
|
||||
referenceUpdated.fire(project, ref);
|
||||
break;
|
||||
default: {
|
||||
throw new IOException(result.name());
|
||||
for (String ref : refs) {
|
||||
RefUpdate ru = repo.updateRef(ref);
|
||||
ru.setNewObjectId(id);
|
||||
final Result result = ru.update();
|
||||
switch (result) {
|
||||
case NEW:
|
||||
referenceUpdated.fire(project, ref);
|
||||
break;
|
||||
default: {
|
||||
throw new IOException(String.format(
|
||||
"Failed to create ref \"%s\": %s", ref, result.name()));
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
|
@@ -30,7 +30,7 @@ public class CreateProjectArgs {
|
||||
public boolean contributorAgreements;
|
||||
public boolean signedOffBy;
|
||||
public boolean permissionsOnly;
|
||||
public String branch;
|
||||
public List<String> branch;
|
||||
public boolean contentMerge;
|
||||
public boolean changeIdRequired;
|
||||
public boolean createEmptyCommit;
|
||||
|
@@ -79,7 +79,7 @@ final class CreateProjectCommand extends SshCommand {
|
||||
|
||||
@Option(name = "--branch", aliases = {"-b"}, metaVar = "BRANCH", usage = "initial branch name\n"
|
||||
+ "(default: master)")
|
||||
private String branch = Constants.MASTER;
|
||||
private List<String> branch;
|
||||
|
||||
@Option(name = "--empty-commit", usage = "to create initial empty commit")
|
||||
private boolean createEmptyCommit;
|
||||
|
Reference in New Issue
Block a user