Use exactRef() when possible

This avoids ambiguous lookups when refs/heads/refs/meta/config exists.
Instead only refs/meta/config or nothing is returned.

exactRef() can also be optimized by JGit implementations to lookup a
ref, potentially saving time by avoiding unnecessary reads of the
storage system.

Change-Id: I1fd6750e7dce935597cf46331fe7cb6dc21473bb
This commit is contained in:
Shawn Pearce 2015-06-20 18:36:45 -07:00
parent 56e12f2277
commit 9f1d70c7c7
26 changed files with 86 additions and 73 deletions

View File

@ -47,7 +47,6 @@ public class ListBranchesIT extends AbstractDaemonTest {
@TestProjectInput(createEmptyCommit = false) @TestProjectInput(createEmptyCommit = false)
public void listBranchesOfEmptyProject() throws Exception { public void listBranchesOfEmptyProject() throws Exception {
assertBranches(ImmutableList.of( assertBranches(ImmutableList.of(
branch("HEAD", null, false),
branch("refs/meta/config", null, false)), branch("refs/meta/config", null, false)),
list().get()); list().get());
} }

View File

@ -58,7 +58,7 @@ public class RefNames {
public static final String EDIT_PREFIX = "edit-"; public static final String EDIT_PREFIX = "edit-";
public static String fullName(String ref) { public static String fullName(String ref) {
return (ref.startsWith(REFS) ? "" : REFS_HEADS) + ref; return ref.startsWith(REFS) ? ref : REFS_HEADS + ref;
} }
public static final String shortName(String ref) { public static final String shortName(String ref) {

View File

@ -41,7 +41,7 @@ public class ProjectUtil {
IOException { IOException {
final Repository repo = repoManager.openRepository(branch.getParentKey()); final Repository repo = repoManager.openRepository(branch.getParentKey());
try { try {
boolean exists = repo.getRef(branch.get()) != null; boolean exists = repo.getRefDatabase().exactRef(branch.get()) != null;
if (!exists) { if (!exists) {
exists = repo.getFullBranch().equals(branch.get()); exists = repo.getFullBranch().equals(branch.get());
} }

View File

@ -24,6 +24,7 @@ import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.RestModifyView; import com.google.gerrit.extensions.restapi.RestModifyView;
import com.google.gerrit.extensions.webui.UiAction; import com.google.gerrit.extensions.webui.UiAction;
import com.google.gerrit.reviewdb.client.Change; import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.RefNames;
import com.google.gerrit.reviewdb.server.ReviewDb; import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.git.MergeException; import com.google.gerrit.server.git.MergeException;
import com.google.gerrit.server.project.ChangeControl; import com.google.gerrit.server.project.ChangeControl;
@ -70,11 +71,7 @@ public class CherryPick implements RestModifyView<RevisionResource, CherryPickIn
throw new AuthException("Cherry pick not permitted"); throw new AuthException("Cherry pick not permitted");
} }
String refName = input.destination; String refName = RefNames.fullName(input.destination);
if (!refName.startsWith("refs/")) {
refName = "refs/heads/" + input.destination;
}
RefControl refControl = control.getProjectControl().controlForRef(refName); RefControl refControl = control.getProjectControl().controlForRef(refName);
if (!refControl.canUpload()) { if (!refControl.canUpload()) {
throw new AuthException("Not allowed to cherry pick " throw new AuthException("Not allowed to cherry pick "
@ -85,7 +82,7 @@ public class CherryPick implements RestModifyView<RevisionResource, CherryPickIn
try { try {
Change.Id cherryPickedChangeId = Change.Id cherryPickedChangeId =
cherryPickChange.cherryPick(revision.getChange(), cherryPickChange.cherryPick(revision.getChange(),
revision.getPatchSet(), input.message, input.destination, revision.getPatchSet(), input.message, refName,
refControl); refControl);
return json.format(cherryPickedChangeId); return json.format(cherryPickedChangeId);
} catch (InvalidChangeOperationException e) { } catch (InvalidChangeOperationException e) {

View File

@ -22,6 +22,7 @@ import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.ChangeMessage; import com.google.gerrit.reviewdb.client.ChangeMessage;
import com.google.gerrit.reviewdb.client.PatchSet; import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gerrit.reviewdb.client.Project; import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.reviewdb.client.RefNames;
import com.google.gerrit.reviewdb.server.ReviewDb; import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.ChangeMessagesUtil; import com.google.gerrit.server.ChangeMessagesUtil;
import com.google.gerrit.server.ChangeUtil; import com.google.gerrit.server.ChangeUtil;
@ -109,25 +110,26 @@ public class CherryPickChange {
} }
public Change.Id cherryPick(Change change, PatchSet patch, public Change.Id cherryPick(Change change, PatchSet patch,
final String message, final String destinationBranch, final String message, final String ref,
final RefControl refControl) throws NoSuchChangeException, final RefControl refControl) throws NoSuchChangeException,
OrmException, MissingObjectException, OrmException, MissingObjectException,
IncorrectObjectTypeException, IOException, IncorrectObjectTypeException, IOException,
InvalidChangeOperationException, MergeException { InvalidChangeOperationException, MergeException {
if (destinationBranch == null || destinationBranch.length() == 0) { if (Strings.isNullOrEmpty(ref)) {
throw new InvalidChangeOperationException( throw new InvalidChangeOperationException(
"Cherry Pick: Destination branch cannot be null or empty"); "Cherry Pick: Destination branch cannot be null or empty");
} }
Project.NameKey project = change.getProject(); Project.NameKey project = change.getProject();
String destinationBranch = RefNames.shortName(ref);
IdentifiedUser identifiedUser = (IdentifiedUser) currentUser.get(); IdentifiedUser identifiedUser = (IdentifiedUser) currentUser.get();
try (Repository git = gitManager.openRepository(project); try (Repository git = gitManager.openRepository(project);
RevWalk revWalk = new RevWalk(git)) { RevWalk revWalk = new RevWalk(git)) {
Ref destRef = git.getRef(destinationBranch); Ref destRef = git.getRefDatabase().exactRef(ref);
if (destRef == null) { if (destRef == null) {
throw new InvalidChangeOperationException("Branch " throw new InvalidChangeOperationException(String.format(
+ destinationBranch + " does not exist."); "Branch %s does not exist.", destinationBranch));
} }
final RevCommit mergeTip = revWalk.parseCommit(destRef.getObjectId()); final RevCommit mergeTip = revWalk.parseCommit(destRef.getObjectId());

View File

@ -18,6 +18,7 @@ import static com.google.gerrit.server.ChangeUtil.PS_ID_ORDER;
import static com.google.gerrit.server.ChangeUtil.TO_PS_ID; import static com.google.gerrit.server.ChangeUtil.TO_PS_ID;
import com.google.auto.value.AutoValue; import com.google.auto.value.AutoValue;
import com.google.common.base.Function;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.collect.Collections2; import com.google.common.collect.Collections2;
import com.google.common.collect.FluentIterable; import com.google.common.collect.FluentIterable;
@ -243,6 +244,21 @@ public class ConsistencyChecker {
patchSetsBySha = MultimapBuilder.hashKeys(all.size()) patchSetsBySha = MultimapBuilder.hashKeys(all.size())
.treeSetValues(PS_ID_ORDER) .treeSetValues(PS_ID_ORDER)
.build(); .build();
Map<String, Ref> refs;
try {
refs = repo.getRefDatabase().exactRef(
Lists.transform(all, new Function<PatchSet, String>() {
@Override
public String apply(PatchSet ps) {
return ps.getId().toRefName();
}
}).toArray(new String[all.size()]));
} catch (IOException e) {
error("error reading refs", e);
refs = Collections.emptyMap();
}
for (PatchSet ps : all) { for (PatchSet ps : all) {
// Check revision format. // Check revision format.
int psNum = ps.getId().get(); int psNum = ps.getId().get();
@ -256,21 +272,16 @@ public class ConsistencyChecker {
// Check ref existence. // Check ref existence.
ProblemInfo refProblem = null; ProblemInfo refProblem = null;
try { Ref ref = refs.get(refName);
Ref ref = repo.getRef(refName); if (ref == null) {
if (ref == null) { refProblem = problem("Ref missing: " + refName);
refProblem = problem("Ref missing: " + refName); } else if (!objId.equals(ref.getObjectId())) {
} else if (!objId.equals(ref.getObjectId())) { String actual = ref.getObjectId() != null
String actual = ref.getObjectId() != null ? ref.getObjectId().name()
? ref.getObjectId().name() : "null";
: "null"; refProblem = problem(String.format(
refProblem = problem(String.format( "Expected %s to point to %s, found %s",
"Expected %s to point to %s, found %s", ref.getName(), objId.name(), actual));
ref.getName(), objId.name(), actual));
}
} catch (IOException e) {
error("Error reading ref: " + refName, e);
refProblem = lastProblem();
} }
// Check object existence. // Check object existence.
@ -306,7 +317,7 @@ public class ConsistencyChecker {
String refName = change.getDest().get(); String refName = change.getDest().get();
Ref dest; Ref dest;
try { try {
dest = repo.getRef(refName); dest = repo.getRefDatabase().exactRef(refName);
} catch (IOException e) { } catch (IOException e) {
problem("Failed to look up destination ref: " + refName); problem("Failed to look up destination ref: " + refName);
return; return;

View File

@ -175,7 +175,7 @@ public class CreateChange implements
parentCommit = ObjectId.fromString(ps.getRevision().get()); parentCommit = ObjectId.fromString(ps.getRevision().get());
groups = ps.getGroups(); groups = ps.getGroups();
} else { } else {
Ref destRef = git.getRef(refName); Ref destRef = git.getRefDatabase().exactRef(refName);
if (destRef == null) { if (destRef == null) {
throw new UnprocessableEntityException(String.format( throw new UnprocessableEntityException(String.format(
"Branch %s does not exist.", refName)); "Branch %s does not exist.", refName));

View File

@ -76,7 +76,7 @@ class GetRelatedByAncestors {
throws RepositoryNotFoundException, IOException, OrmException { throws RepositoryNotFoundException, IOException, OrmException {
try (Repository git = gitMgr.openRepository(rsrc.getChange().getProject()); try (Repository git = gitMgr.openRepository(rsrc.getChange().getProject());
RevWalk rw = new RevWalk(git)) { RevWalk rw = new RevWalk(git)) {
Ref ref = git.getRef(rsrc.getChange().getDest().get()); Ref ref = git.getRefDatabase().exactRef(rsrc.getChange().getDest().get());
RelatedInfo info = new RelatedInfo(); RelatedInfo info = new RelatedInfo();
info.changes = walk(rsrc, rw, ref); info.changes = walk(rsrc, rw, ref);
return info; return info;

View File

@ -47,6 +47,7 @@ import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
public class Mergeable implements RestReadView<RevisionResource> { public class Mergeable implements RestReadView<RevisionResource> {
@ -116,7 +117,7 @@ public class Mergeable implements RestReadView<RevisionResource> {
return result; return result;
} }
Ref ref = git.getRef(change.getDest().get()); Ref ref = git.getRefDatabase().exactRef(change.getDest().get());
ProjectState projectState = projectCache.get(change.getProject()); ProjectState projectState = projectCache.get(change.getProject());
String strategy = mergeUtilFactory.create(projectState) String strategy = mergeUtilFactory.create(projectState)
.mergeStrategyName(); .mergeStrategyName();
@ -135,8 +136,10 @@ public class Mergeable implements RestReadView<RevisionResource> {
BranchOrderSection branchOrder = projectState.getBranchOrderSection(); BranchOrderSection branchOrder = projectState.getBranchOrderSection();
if (branchOrder != null) { if (branchOrder != null) {
int prefixLen = Constants.R_HEADS.length(); int prefixLen = Constants.R_HEADS.length();
for (String n : branchOrder.getMoreStable(ref.getName())) { String[] names = branchOrder.getMoreStable(ref.getName());
Ref other = git.getRef(n); Map<String, Ref> refs = git.getRefDatabase().exactRef(names);
for (String n : names) {
Ref other = refs.get(n);
if (other == null) { if (other == null) {
continue; continue;
} }

View File

@ -221,7 +221,7 @@ public class RebaseChange {
if (baseRev == null) { if (baseRev == null) {
// We are dependent on a merged PatchSet or have no PatchSet // We are dependent on a merged PatchSet or have no PatchSet
// dependencies at all. // dependencies at all.
Ref destRef = git.getRef(destBranch.get()); Ref destRef = git.getRefDatabase().exactRef(destBranch.get());
if (destRef == null) { if (destRef == null) {
throw new InvalidChangeOperationException( throw new InvalidChangeOperationException(
"The destination branch does not exist: " + destBranch.get()); "The destination branch does not exist: " + destBranch.get());

View File

@ -59,7 +59,7 @@ public class BanCommit {
public static NoteMap loadRejectCommitsMap(Repository repo, RevWalk walk) public static NoteMap loadRejectCommitsMap(Repository repo, RevWalk walk)
throws IOException { throws IOException {
try { try {
Ref ref = repo.getRef(RefNames.REFS_REJECT_COMMITS); Ref ref = repo.getRefDatabase().exactRef(RefNames.REFS_REJECT_COMMITS);
if (ref == null) { if (ref == null) {
return NoteMap.newEmptyMap(); return NoteMap.newEmptyMap();
} }

View File

@ -40,12 +40,12 @@ public class BranchOrderSection {
} }
} }
public List<String> getMoreStable(String branch) { public String[] getMoreStable(String branch) {
int i = order.indexOf(RefNames.fullName(branch)); int i = order.indexOf(RefNames.fullName(branch));
if (0 <= i) { if (0 <= i) {
return order.subList(i + 1, order.size()); List<String> r = order.subList(i + 1, order.size());
} else { return r.toArray(new String[r.size()]);
return ImmutableList.of();
} }
return new String[] {};
} }
} }

View File

@ -189,7 +189,7 @@ public class NotesBranchUtil {
} }
private void loadBase(String notesBranch) throws IOException { private void loadBase(String notesBranch) throws IOException {
Ref branch = db.getRef(notesBranch); Ref branch = db.getRefDatabase().exactRef(notesBranch);
if (branch != null) { if (branch != null) {
baseCommit = revWalk.parseCommit(branch.getObjectId()); baseCommit = revWalk.parseCommit(branch.getObjectId());
base = NoteMap.read(revWalk.getObjectReader(), baseCommit); base = NoteMap.read(revWalk.getObjectReader(), baseCommit);
@ -240,7 +240,7 @@ public class NotesBranchUtil {
} }
} else { } else {
throw new ConcurrentRefUpdateException("Failed to lock the ref: " throw new ConcurrentRefUpdateException("Failed to lock the ref: "
+ notesBranch, db.getRef(notesBranch), result); + notesBranch, refUpdate.getRef(), result);
} }
} else if (result == Result.REJECTED) { } else if (result == Result.REJECTED) {

View File

@ -1203,10 +1203,8 @@ public class ReceiveCommits {
String parse(CmdLineParser clp, Repository repo, Set<String> refs) String parse(CmdLineParser clp, Repository repo, Set<String> refs)
throws CmdLineException { throws CmdLineException {
String ref = MagicBranch.getDestBranchName(cmd.getRefName()); String ref = RefNames.fullName(
if (!ref.startsWith(Constants.R_REFS)) { MagicBranch.getDestBranchName(cmd.getRefName()));
ref = Constants.R_HEADS + ref;
}
int optionStart = ref.indexOf('%'); int optionStart = ref.indexOf('%');
if (0 < optionStart) { if (0 < optionStart) {
@ -1364,7 +1362,13 @@ public class ReceiveCommits {
} else if (newChangeForAllNotInTarget) { } else if (newChangeForAllNotInTarget) {
String destBranch = magicBranch.dest.get(); String destBranch = magicBranch.dest.get();
try { try {
ObjectId baseHead = repo.getRef(destBranch).getObjectId(); Ref r = repo.getRefDatabase().exactRef(destBranch);
if (r == null) {
reject(cmd, destBranch + " not found");
return;
}
ObjectId baseHead = r.getObjectId();
magicBranch.baseCommit = magicBranch.baseCommit =
Collections.singletonList(walk.parseCommit(baseHead)); Collections.singletonList(walk.parseCommit(baseHead));
} catch (IOException ex) { } catch (IOException ex) {

View File

@ -96,7 +96,7 @@ public abstract class VersionedMetaData {
* @throws ConfigInvalidException * @throws ConfigInvalidException
*/ */
public void load(Repository db) throws IOException, ConfigInvalidException { public void load(Repository db) throws IOException, ConfigInvalidException {
Ref ref = db.getRef(getRefName()); Ref ref = db.getRefDatabase().exactRef(getRefName());
load(db, ref != null ? ref.getObjectId() : null); load(db, ref != null ? ref.getObjectId() : null);
} }

View File

@ -81,7 +81,7 @@ public abstract class AbstractChangeNotes<T> extends VersionedMetaData {
try { try {
repo = repoManager.openMetadataRepository(getProjectName()); repo = repoManager.openMetadataRepository(getProjectName());
try { try {
Ref ref = repo.getRef(getRefName()); Ref ref = repo.getRefDatabase().exactRef(getRefName());
return ref != null ? ref.getObjectId() : null; return ref != null ? ref.getObjectId() : null;
} finally { } finally {
repo.close(); repo.close();

View File

@ -93,7 +93,7 @@ public class CommentsInNotesUtil {
Multimap<RevId, PatchLineComment> comments, Multimap<RevId, PatchLineComment> comments,
Status status) Status status)
throws IOException, ConfigInvalidException { throws IOException, ConfigInvalidException {
Ref ref = repo.getRef(refName); Ref ref = repo.getRefDatabase().exactRef(refName);
if (ref == null) { if (ref == null) {
return null; return null;
} }

View File

@ -318,7 +318,7 @@ public class PatchListLoader extends CacheLoader<PatchListKey, PatchList> {
+ hash.substring(0, 2) + hash.substring(0, 2)
+ "/" + "/"
+ hash.substring(2); + hash.substring(2);
Ref ref = repo.getRef(refName); Ref ref = repo.getRefDatabase().exactRef(refName);
if (ref != null && ref.getObjectId() != null) { if (ref != null && ref.getObjectId() != null) {
return rw.parseTree(ref.getObjectId()); return rw.parseTree(ref.getObjectId());
} }

View File

@ -154,13 +154,13 @@ public class CreateBranch implements RestModifyView<ProjectResource, Input> {
hooks.doRefUpdatedHook(name, u, identifiedUser.get().getAccount()); hooks.doRefUpdatedHook(name, u, identifiedUser.get().getAccount());
break; break;
case LOCK_FAILURE: case LOCK_FAILURE:
if (repo.getRef(ref) != null) { if (repo.getRefDatabase().exactRef(ref) != null) {
throw new ResourceConflictException("branch \"" + ref throw new ResourceConflictException("branch \"" + ref
+ "\" already exists"); + "\" already exists");
} }
String refPrefix = getRefPrefix(ref); String refPrefix = getRefPrefix(ref);
while (!Constants.R_HEADS.equals(refPrefix)) { while (!Constants.R_HEADS.equals(refPrefix)) {
if (repo.getRef(refPrefix) != null) { if (repo.getRefDatabase().exactRef(refPrefix) != null) {
throw new ResourceConflictException("Cannot create branch \"" throw new ResourceConflictException("Cannot create branch \""
+ ref + "\" since it conflicts with branch \"" + refPrefix + ref + "\" since it conflicts with branch \"" + refPrefix
+ "\"."); + "\".");
@ -229,7 +229,7 @@ public class CreateBranch implements RestModifyView<ProjectResource, Input> {
Iterable<Ref> refs = Iterables.concat( Iterable<Ref> refs = Iterables.concat(
refDb.getRefs(Constants.R_HEADS).values(), refDb.getRefs(Constants.R_HEADS).values(),
refDb.getRefs(Constants.R_TAGS).values()); refDb.getRefs(Constants.R_TAGS).values());
Ref rc = refDb.getRef(RefNames.REFS_CONFIG); Ref rc = refDb.exactRef(RefNames.REFS_CONFIG);
if (rc != null) { if (rc != null) {
refs = Iterables.concat(refs, Collections.singleton(rc)); refs = Iterables.concat(refs, Collections.singleton(rc));
} }

View File

@ -50,7 +50,7 @@ public class GetHead implements RestReadView<ProjectResource> {
public String apply(ProjectResource rsrc) throws AuthException, public String apply(ProjectResource rsrc) throws AuthException,
ResourceNotFoundException, IOException { ResourceNotFoundException, IOException {
try (Repository repo = repoManager.openRepository(rsrc.getNameKey())) { try (Repository repo = repoManager.openRepository(rsrc.getNameKey())) {
Ref head = repo.getRef(Constants.HEAD); Ref head = repo.getRefDatabase().exactRef(Constants.HEAD);
if (head == null) { if (head == null) {
throw new ResourceNotFoundException(Constants.HEAD); throw new ResourceNotFoundException(Constants.HEAD);
} else if (head.isSymbolic()) { } else if (head.isSymbolic()) {

View File

@ -115,9 +115,10 @@ public class ListBranches implements RestReadView<ProjectResource> {
db.getRefDatabase().getRefs(Constants.R_HEADS).values(); db.getRefDatabase().getRefs(Constants.R_HEADS).values();
refs = new ArrayList<>(heads.size() + 3); refs = new ArrayList<>(heads.size() + 3);
refs.addAll(heads); refs.addAll(heads);
addRef(db, refs, Constants.HEAD); refs.addAll(db.getRefDatabase().exactRef(
addRef(db, refs, RefNames.REFS_CONFIG); Constants.HEAD,
addRef(db, refs, RefNames.REFS_USERS_DEFAULT); RefNames.REFS_CONFIG,
RefNames.REFS_USERS_DEFAULT).values());
} catch (RepositoryNotFoundException noGitRepository) { } catch (RepositoryNotFoundException noGitRepository) {
throw new ResourceNotFoundException(); throw new ResourceNotFoundException();
} }
@ -183,14 +184,6 @@ public class ListBranches implements RestReadView<ProjectResource> {
} }
} }
private static void addRef(Repository db, List<Ref> refs, String name)
throws IOException {
Ref ref = db.getRef(name);
if (ref != null) {
refs.add(ref);
}
}
private FluentIterable<BranchInfo> filterBranches( private FluentIterable<BranchInfo> filterBranches(
FluentIterable<BranchInfo> branches) throws BadRequestException { FluentIterable<BranchInfo> branches) throws BadRequestException {
if (!Strings.isNullOrEmpty(matchSubstring)) { if (!Strings.isNullOrEmpty(matchSubstring)) {

View File

@ -91,7 +91,7 @@ public class ListProjects implements RestReadView<TopLevelResource> {
PERMISSIONS { PERMISSIONS {
@Override @Override
boolean matches(Repository git) throws IOException { boolean matches(Repository git) throws IOException {
Ref head = git.getRef(Constants.HEAD); Ref head = git.getRefDatabase().exactRef(Constants.HEAD);
return head != null return head != null
&& head.isSymbolic() && head.isSymbolic()
&& RefNames.REFS_CONFIG.equals(head.getLeaf().getName()); && RefNames.REFS_CONFIG.equals(head.getLeaf().getName());

View File

@ -104,7 +104,7 @@ public class ListTags implements RestReadView<ProjectResource> {
if (!tagName.startsWith(Constants.R_TAGS)) { if (!tagName.startsWith(Constants.R_TAGS)) {
tagName = Constants.R_TAGS + tagName; tagName = Constants.R_TAGS + tagName;
} }
Ref ref = repo.getRefDatabase().getRef(tagName); Ref ref = repo.getRefDatabase().exactRef(tagName);
if (ref != null && !visibleTags(resource.getControl(), repo, if (ref != null && !visibleTags(resource.getControl(), repo,
ImmutableMap.of(ref.getName(), ref)).isEmpty()) { ImmutableMap.of(ref.getName(), ref)).isEmpty()) {
return createTagInfo(ref, rw); return createTagInfo(ref, rw);

View File

@ -166,7 +166,7 @@ public class ProjectState {
try { try {
Repository git = gitMgr.openRepository(getProject().getNameKey()); Repository git = gitMgr.openRepository(getProject().getNameKey());
try { try {
Ref ref = git.getRef(RefNames.REFS_CONFIG); Ref ref = git.getRefDatabase().exactRef(RefNames.REFS_CONFIG);
if (ref == null || ref.getObjectId() == null) { if (ref == null || ref.getObjectId() == null) {
return true; return true;
} }

View File

@ -33,12 +33,14 @@ import com.google.inject.Singleton;
import org.eclipse.jgit.errors.RepositoryNotFoundException; import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Ref;
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.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.util.Map;
@Singleton @Singleton
public class SetHead implements RestModifyView<ProjectResource, Input> { public class SetHead implements RestModifyView<ProjectResource, Input> {
@ -77,12 +79,14 @@ public class SetHead implements RestModifyView<ProjectResource, Input> {
Repository repo = null; Repository repo = null;
try { try {
repo = repoManager.openRepository(rsrc.getNameKey()); repo = repoManager.openRepository(rsrc.getNameKey());
if (repo.getRef(ref) == null) { Map<String, Ref> cur =
repo.getRefDatabase().exactRef(Constants.HEAD, ref);
if (!cur.containsKey(ref)) {
throw new UnprocessableEntityException(String.format( throw new UnprocessableEntityException(String.format(
"Ref Not Found: %s", ref)); "Ref Not Found: %s", ref));
} }
final String oldHead = repo.getRef(Constants.HEAD).getTarget().getName(); final String oldHead = cur.get(Constants.HEAD).getTarget().getName();
final String newHead = ref; final String newHead = ref;
if (!oldHead.equals(newHead)) { if (!oldHead.equals(newHead)) {
final RefUpdate u = repo.updateRef(Constants.HEAD, true); final RefUpdate u = repo.updateRef(Constants.HEAD, true);

View File

@ -730,7 +730,7 @@ public class ChangeData {
return null; return null;
} }
try (Repository repo = repoManager.openRepository(c.getProject())) { try (Repository repo = repoManager.openRepository(c.getProject())) {
Ref ref = repo.getRef(c.getDest().get()); Ref ref = repo.getRefDatabase().exactRef(c.getDest().get());
SubmitTypeRecord rec = new SubmitRuleEvaluator(this) SubmitTypeRecord rec = new SubmitRuleEvaluator(this)
.getSubmitType(); .getSubmitType();
if (rec.status != SubmitTypeRecord.Status.OK) { if (rec.status != SubmitTypeRecord.Status.OK) {