gerrit/gerrit-server/src/main/java/com/google/gerrit/server/ProjectUtil.java
Edwin Kempin f5a46a4109 Allow to create a change for the initial commit of refs/meta/config
Normally you can only push commits for review to branches that exist.
But there is an exception for the branch to which HEAD in the remote
repository points (usually master). The same is true on submit, changes
can only be submitted if the destination branch exists, except if the
destination branch is the branch to which HEAD points. Relax this
further and also allow to push an initial commit for review to the
refs/meta/config branch and also allow to submit such a change.

This is useful to setup an initial project configuration when the
refs/meta/config branch is missing.

Actually already today it is possible to create a change with an initial
commit for the refs/meta/config branch, but then this change is not
submittable because the destination branch is missing. The creation of
such a change is possible by editing the access rights in the WebUI and
then clicking on the 'Save for Review' button. This creates a change for
the refs/meta/config branch regardless of whether the branch exists.

Change-Id: I2e28fdd5384149d0ee32b10641652803c73842ab
Signed-off-by: Edwin Kempin <ekempin@google.com>
2017-08-18 15:56:11 +02:00

48 lines
1.9 KiB
Java

// Copyright (C) 2012 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server;
import com.google.gerrit.reviewdb.client.Branch;
import com.google.gerrit.reviewdb.client.RefNames;
import com.google.gerrit.server.git.GitRepositoryManager;
import java.io.IOException;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.lib.Repository;
public class ProjectUtil {
/**
* Checks whether the specified branch exists.
*
* @param repoManager Git repository manager to open the git repository
* @param branch the branch for which it should be checked if it exists
* @return {@code true} if the specified branch exists or if {@code HEAD} points to this branch,
* otherwise {@code false}
* @throws RepositoryNotFoundException the repository of the branch's project does not exist.
* @throws IOException error while retrieving the branch from the repository.
*/
public static boolean branchExists(final GitRepositoryManager repoManager, Branch.NameKey branch)
throws RepositoryNotFoundException, IOException {
try (Repository repo = repoManager.openRepository(branch.getParentKey())) {
boolean exists = repo.getRefDatabase().exactRef(branch.get()) != null;
if (!exists) {
exists =
repo.getFullBranch().equals(branch.get()) || RefNames.REFS_CONFIG.equals(branch.get());
}
return exists;
}
}
}