use of org.eclipse.jgit.lib.RefUpdate in project gerrit by GerritCodeReview.
the class RepoSequence method store.
private RefUpdate.Result store(Repository repo, RevWalk rw, @Nullable ObjectId oldId, int val) throws IOException {
ObjectId newId;
try (ObjectInserter ins = repo.newObjectInserter()) {
newId = ins.insert(OBJ_BLOB, Integer.toString(val).getBytes(UTF_8));
ins.flush();
}
RefUpdate ru = repo.updateRef(refName);
if (oldId != null) {
ru.setExpectedOldObjectId(oldId);
}
ru.setNewObjectId(newId);
// Required for non-commitish updates.
ru.setForceUpdate(true);
return ru.update(rw);
}
use of org.eclipse.jgit.lib.RefUpdate in project gerrit by GerritCodeReview.
the class ChangeRebuilderIT method rebuildDeletesOldDraftRefs.
@Test
public void rebuildDeletesOldDraftRefs() throws Exception {
PushOneCommit.Result r = createChange();
Change.Id id = r.getPatchSetId().getParentKey();
putDraft(user, id, 1, "comment", null);
Account.Id otherAccountId = new Account.Id(user.getId().get() + 1234);
String otherDraftRef = refsDraftComments(id, otherAccountId);
try (Repository repo = repoManager.openRepository(allUsers);
ObjectInserter ins = repo.newObjectInserter()) {
ObjectId sha = ins.insert(OBJ_BLOB, "garbage data".getBytes(UTF_8));
ins.flush();
RefUpdate ru = repo.updateRef(otherDraftRef);
ru.setExpectedOldObjectId(ObjectId.zeroId());
ru.setNewObjectId(sha);
assertThat(ru.update()).isEqualTo(RefUpdate.Result.NEW);
}
checker.rebuildAndCheckChanges(id);
try (Repository repo = repoManager.openRepository(allUsers)) {
assertThat(repo.exactRef(otherDraftRef)).isNull();
}
}
use of org.eclipse.jgit.lib.RefUpdate in project gerrit by GerritCodeReview.
the class AccountsUpdate method deleteUserBranch.
public static void deleteUserBranch(Repository repo, PersonIdent refLogIdent, Account.Id accountId) throws IOException {
String refName = RefNames.refsUsers(accountId);
Ref ref = repo.exactRef(refName);
if (ref == null) {
return;
}
RefUpdate ru = repo.updateRef(refName);
ru.setExpectedOldObjectId(ref.getObjectId());
ru.setNewObjectId(ObjectId.zeroId());
ru.setForceUpdate(true);
ru.setRefLogIdent(refLogIdent);
ru.setRefLogMessage("Delete Account", true);
Result result = ru.delete();
if (result != Result.FORCED) {
throw new IOException(String.format("Failed to delete ref %s: %s", refName, result.name()));
}
}
use of org.eclipse.jgit.lib.RefUpdate in project gerrit by GerritCodeReview.
the class SetHead method apply.
@Override
public String apply(final ProjectResource rsrc, Input input) throws AuthException, ResourceNotFoundException, BadRequestException, UnprocessableEntityException, IOException {
if (!rsrc.getControl().isOwner()) {
throw new AuthException("restricted to project owner");
}
if (input == null || Strings.isNullOrEmpty(input.ref)) {
throw new BadRequestException("ref required");
}
String ref = RefNames.fullName(input.ref);
try (Repository repo = repoManager.openRepository(rsrc.getNameKey())) {
Map<String, Ref> cur = repo.getRefDatabase().exactRef(Constants.HEAD, ref);
if (!cur.containsKey(ref)) {
throw new UnprocessableEntityException(String.format("Ref Not Found: %s", ref));
}
final String oldHead = cur.get(Constants.HEAD).getTarget().getName();
final String newHead = ref;
if (!oldHead.equals(newHead)) {
final RefUpdate u = repo.updateRef(Constants.HEAD, true);
u.setRefLogIdent(identifiedUser.get().newRefLogIdent());
RefUpdate.Result res = u.link(newHead);
switch(res) {
case NO_CHANGE:
case RENAMED:
case FORCED:
case NEW:
break;
case FAST_FORWARD:
case IO_FAILURE:
case LOCK_FAILURE:
case NOT_ATTEMPTED:
case REJECTED:
case REJECTED_CURRENT_BRANCH:
default:
throw new IOException("Setting HEAD failed with " + res);
}
fire(rsrc.getNameKey(), oldHead, newHead);
}
return ref;
} catch (RepositoryNotFoundException e) {
throw new ResourceNotFoundException(rsrc.getName());
}
}
use of org.eclipse.jgit.lib.RefUpdate in project egit by eclipse.
the class TagOperation method updateRepo.
private void updateRepo(ObjectId tagId) throws TeamException {
String refName = Constants.R_TAGS + tag.getTag();
try {
RefUpdate tagRef = repo.updateRef(refName);
tagRef.setNewObjectId(tagId);
tagRef.setForceUpdate(shouldMoveTag);
Result updateResult = tagRef.update();
if (updateResult != Result.NEW && updateResult != Result.FORCED)
throw new TeamException(NLS.bind(CoreText.TagOperation_taggingFailure, tag.getTag(), updateResult));
} catch (IOException e) {
throw new TeamException(NLS.bind(CoreText.TagOperation_taggingFailure, tag.getTag(), e.getMessage()), e);
}
}
Aggregations