Enable creation of initial empty commit for new projects
Add new option '--empty-commit' for the 'create-project' SSH command that creates an initial empty commit for the newly created project. The idea of introducing this option was discussed on the mailing list: http://groups.google.com/group/repo-discuss/browse_thread/thread/945f313be4f26167?pli=1 Signed-off-by: Edwin Kempin <edwin.kempin@gmail.com> Change-Id: I54cee094a4d465cd05cbc13ffcf90666b205ba72
This commit is contained in:

committed by
Edwin Kempin

parent
876953ea4d
commit
ec15511690
@@ -19,6 +19,7 @@ SYNOPSIS
|
|||||||
[\--use-content-merge] \
|
[\--use-content-merge] \
|
||||||
[\--use-contributor-agreements] \
|
[\--use-contributor-agreements] \
|
||||||
[\--use-signed-off-by]
|
[\--use-signed-off-by]
|
||||||
|
[\--empty-commit]
|
||||||
|
|
||||||
DESCRIPTION
|
DESCRIPTION
|
||||||
-----------
|
-----------
|
||||||
@@ -113,6 +114,10 @@ link:project-setup.html#submit_type[Change Submit Actions].
|
|||||||
from either the author or the uploader in the commit message.
|
from either the author or the uploader in the commit message.
|
||||||
Disabled by default.
|
Disabled by default.
|
||||||
|
|
||||||
|
\--empty-commit:
|
||||||
|
Creates an initial empty commit for the Git repository of the
|
||||||
|
project that is newly created.
|
||||||
|
|
||||||
|
|
||||||
EXAMPLES
|
EXAMPLES
|
||||||
--------
|
--------
|
||||||
|
@@ -21,6 +21,7 @@ import com.google.gerrit.reviewdb.Project;
|
|||||||
import com.google.gerrit.reviewdb.RefRight;
|
import com.google.gerrit.reviewdb.RefRight;
|
||||||
import com.google.gerrit.reviewdb.ReviewDb;
|
import com.google.gerrit.reviewdb.ReviewDb;
|
||||||
import com.google.gerrit.reviewdb.Project.SubmitType;
|
import com.google.gerrit.reviewdb.Project.SubmitType;
|
||||||
|
import com.google.gerrit.server.GerritPersonIdent;
|
||||||
import com.google.gerrit.server.IdentifiedUser;
|
import com.google.gerrit.server.IdentifiedUser;
|
||||||
import com.google.gerrit.server.config.ProjectCreatorGroups;
|
import com.google.gerrit.server.config.ProjectCreatorGroups;
|
||||||
import com.google.gerrit.server.config.ProjectOwnerGroups;
|
import com.google.gerrit.server.config.ProjectOwnerGroups;
|
||||||
@@ -32,11 +33,19 @@ import com.google.gwtorm.client.OrmException;
|
|||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
|
|
||||||
import org.apache.sshd.server.Environment;
|
import org.apache.sshd.server.Environment;
|
||||||
|
import org.eclipse.jgit.lib.CommitBuilder;
|
||||||
import org.eclipse.jgit.lib.Constants;
|
import org.eclipse.jgit.lib.Constants;
|
||||||
|
import org.eclipse.jgit.lib.ObjectId;
|
||||||
|
import org.eclipse.jgit.lib.ObjectInserter;
|
||||||
|
import org.eclipse.jgit.lib.PersonIdent;
|
||||||
import org.eclipse.jgit.lib.RefUpdate;
|
import org.eclipse.jgit.lib.RefUpdate;
|
||||||
import org.eclipse.jgit.lib.Repository;
|
import org.eclipse.jgit.lib.Repository;
|
||||||
|
import org.eclipse.jgit.lib.RefUpdate.Result;
|
||||||
import org.kohsuke.args4j.Option;
|
import org.kohsuke.args4j.Option;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
@@ -46,6 +55,8 @@ import java.util.Set;
|
|||||||
|
|
||||||
/** Create a new project. **/
|
/** Create a new project. **/
|
||||||
final class CreateProject extends BaseCommand {
|
final class CreateProject extends BaseCommand {
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(CreateProject.class);
|
||||||
|
|
||||||
@Option(name = "--name", required = true, aliases = {"-n"}, metaVar = "NAME", usage = "name of project to be created")
|
@Option(name = "--name", required = true, aliases = {"-n"}, metaVar = "NAME", usage = "name of project to be created")
|
||||||
private String projectName;
|
private String projectName;
|
||||||
|
|
||||||
@@ -81,6 +92,9 @@ final class CreateProject extends BaseCommand {
|
|||||||
+ "(default: master)")
|
+ "(default: master)")
|
||||||
private String branch = Constants.MASTER;
|
private String branch = Constants.MASTER;
|
||||||
|
|
||||||
|
@Option(name = "--empty-commit", usage = "to create initial empty commit")
|
||||||
|
private boolean createEmptyCommit;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private ReviewDb db;
|
private ReviewDb db;
|
||||||
|
|
||||||
@@ -101,6 +115,10 @@ final class CreateProject extends BaseCommand {
|
|||||||
@Inject
|
@Inject
|
||||||
private ReplicationQueue rq;
|
private ReplicationQueue rq;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
@GerritPersonIdent
|
||||||
|
private PersonIdent serverIdent;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start(final Environment env) {
|
public void start(final Environment env) {
|
||||||
startThread(new CommandRunnable() {
|
startThread(new CommandRunnable() {
|
||||||
@@ -114,7 +132,7 @@ final class CreateProject extends BaseCommand {
|
|||||||
validateParameters();
|
validateParameters();
|
||||||
|
|
||||||
if (!permissionsOnly) {
|
if (!permissionsOnly) {
|
||||||
Repository repo = repoManager.createRepository(projectName);
|
final Repository repo = repoManager.createRepository(projectName);
|
||||||
try {
|
try {
|
||||||
repo.create(true);
|
repo.create(true);
|
||||||
|
|
||||||
@@ -125,7 +143,12 @@ final class CreateProject extends BaseCommand {
|
|||||||
repoManager
|
repoManager
|
||||||
.setProjectDescription(projectName, projectDescription);
|
.setProjectDescription(projectName, projectDescription);
|
||||||
|
|
||||||
rq.replicateNewProject(new Project.NameKey(projectName), branch);
|
final Project.NameKey project = new Project.NameKey(projectName);
|
||||||
|
rq.replicateNewProject(project, branch);
|
||||||
|
|
||||||
|
if (createEmptyCommit) {
|
||||||
|
createEmptyCommit(repo, project, branch);
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
repo.close();
|
repo.close();
|
||||||
}
|
}
|
||||||
@@ -142,6 +165,38 @@ final class CreateProject extends BaseCommand {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void createEmptyCommit(final Repository repo,
|
||||||
|
final Project.NameKey project, final String ref) throws IOException {
|
||||||
|
ObjectInserter oi = repo.newObjectInserter();
|
||||||
|
try {
|
||||||
|
CommitBuilder cb = new CommitBuilder();
|
||||||
|
cb.setTreeId(oi.insert(Constants.OBJ_TREE, new byte[] {}));
|
||||||
|
cb.setCommitter(serverIdent);
|
||||||
|
cb.setAuthor(cb.getCommitter());
|
||||||
|
cb.setMessage("Initial empty repository");
|
||||||
|
|
||||||
|
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:
|
||||||
|
rq.scheduleUpdate(project, ref);
|
||||||
|
break;
|
||||||
|
default: {
|
||||||
|
throw new IOException(result.name());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("Cannot create empty commit for " + projectName, e);
|
||||||
|
throw e;
|
||||||
|
} finally {
|
||||||
|
oi.release();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void createProject() throws OrmException {
|
private void createProject() throws OrmException {
|
||||||
final Project.NameKey newProjectNameKey = new Project.NameKey(projectName);
|
final Project.NameKey newProjectNameKey = new Project.NameKey(projectName);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user