use of org.eclipse.jgit.lib.RefUpdate.Result in project gerrit by GerritCodeReview.
the class CommitMsgHookTest method setHEAD.
private void setHEAD() throws Exception {
try (ObjectInserter oi = repository.newObjectInserter()) {
final CommitBuilder commit = new CommitBuilder();
commit.setTreeId(oi.insert(Constants.OBJ_TREE, new byte[] {}));
commit.setAuthor(author);
commit.setCommitter(committer);
commit.setMessage("test\n");
ObjectId commitId = oi.insert(commit);
final RefUpdate ref = repository.updateRef(Constants.HEAD);
ref.setNewObjectId(commitId);
Result result = ref.forceUpdate();
assert_().withFailureMessage(Constants.HEAD + " did not change: " + ref.getResult()).that(result).isAnyOf(Result.FAST_FORWARD, Result.FORCED, Result.NEW, Result.NO_CHANGE);
}
}
use of org.eclipse.jgit.lib.RefUpdate.Result in project gerrit by GerritCodeReview.
the class Schema_146 method rewriteUserBranch.
private void rewriteUserBranch(Repository repo, RevWalk rw, ObjectInserter oi, ObjectId emptyTree, Ref ref, Account account) throws IOException {
ObjectId current = createInitialEmptyCommit(oi, emptyTree, account.getRegisteredOn());
rw.reset();
rw.sort(RevSort.TOPO);
rw.sort(RevSort.REVERSE, true);
rw.markStart(rw.parseCommit(ref.getObjectId()));
RevCommit c;
while ((c = rw.next()) != null) {
if (isInitialEmptyCommit(emptyTree, c)) {
return;
}
CommitBuilder cb = new CommitBuilder();
cb.setParentId(current);
cb.setTreeId(c.getTree());
cb.setAuthor(c.getAuthorIdent());
cb.setCommitter(c.getCommitterIdent());
cb.setMessage(c.getFullMessage());
cb.setEncoding(c.getEncoding());
current = oi.insert(cb);
}
oi.flush();
RefUpdate ru = repo.updateRef(ref.getName());
ru.setExpectedOldObjectId(ref.getObjectId());
ru.setNewObjectId(current);
ru.setForceUpdate(true);
ru.setRefLogIdent(serverIdent);
ru.setRefLogMessage(getClass().getSimpleName(), true);
Result result = ru.update();
if (result != Result.FORCED) {
throw new IOException(String.format("Failed to update ref %s: %s", ref.getName(), result.name()));
}
}
use of org.eclipse.jgit.lib.RefUpdate.Result in project gitblit by gitblit.
the class JGitUtils method commitIndex.
public static boolean commitIndex(Repository db, String branch, DirCache index, ObjectId parentId, boolean forceCommit, String author, String authorEmail, String message) throws IOException, ConcurrentRefUpdateException {
boolean success = false;
ObjectId headId = db.resolve(branch + "^{commit}");
ObjectId baseId = parentId;
if (baseId == null || headId == null) {
return false;
}
ObjectInserter odi = db.newObjectInserter();
try {
// Create the in-memory index of the new/updated ticket
ObjectId indexTreeId = index.writeTree(odi);
// Create a commit object
PersonIdent ident = new PersonIdent(author, authorEmail);
if (forceCommit == false) {
ThreeWayMerger merger = MergeStrategy.RECURSIVE.newMerger(db, true);
merger.setObjectInserter(odi);
merger.setBase(baseId);
boolean mergeSuccess = merger.merge(indexTreeId, headId);
if (mergeSuccess) {
indexTreeId = merger.getResultTreeId();
} else {
//Manual merge required
return false;
}
}
CommitBuilder commit = new CommitBuilder();
commit.setAuthor(ident);
commit.setCommitter(ident);
commit.setEncoding(com.gitblit.Constants.ENCODING);
commit.setMessage(message);
commit.setParentId(headId);
commit.setTreeId(indexTreeId);
// Insert the commit into the repository
ObjectId commitId = odi.insert(commit);
odi.flush();
RevWalk revWalk = new RevWalk(db);
try {
RevCommit revCommit = revWalk.parseCommit(commitId);
RefUpdate ru = db.updateRef(branch);
ru.setForceUpdate(forceCommit);
ru.setNewObjectId(commitId);
ru.setExpectedOldObjectId(headId);
ru.setRefLogMessage("commit: " + revCommit.getShortMessage(), false);
Result rc = ru.update();
switch(rc) {
case NEW:
case FORCED:
case FAST_FORWARD:
success = true;
break;
case REJECTED:
case LOCK_FAILURE:
throw new ConcurrentRefUpdateException(JGitText.get().couldNotLockHEAD, ru.getRef(), rc);
default:
throw new JGitInternalException(MessageFormat.format(JGitText.get().updatingRefFailed, branch, commitId.toString(), rc));
}
} finally {
revWalk.close();
}
} finally {
odi.close();
}
return success;
}
use of org.eclipse.jgit.lib.RefUpdate.Result in project gitblit by gitblit.
the class JGitUtils method merge.
/**
* Tries to merge a commit into a branch. If there are conflicts, the merge
* will fail.
*
* @param repository
* @param src
* @param toBranch
* @param mergeType
* Defines the integration strategy to use for merging.
* @param committer
* @param message
* @return the merge result
*/
public static MergeResult merge(Repository repository, String src, String toBranch, MergeType mergeType, PersonIdent committer, String message) {
if (!toBranch.startsWith(Constants.R_REFS)) {
// branch ref doesn't start with ref, assume this is a branch head
toBranch = Constants.R_HEADS + toBranch;
}
IntegrationStrategy strategy = IntegrationStrategyFactory.create(mergeType, repository, src, toBranch);
MergeResult mergeResult = strategy.merge(committer, message);
if (mergeResult.status != MergeStatus.MERGED) {
return mergeResult;
}
try {
// Update the integration branch ref
RefUpdate mergeRefUpdate = repository.updateRef(toBranch);
mergeRefUpdate.setNewObjectId(strategy.getMergeCommit());
mergeRefUpdate.setRefLogMessage(strategy.getRefLogMessage(), false);
mergeRefUpdate.setExpectedOldObjectId(strategy.branchTip);
RefUpdate.Result rc = mergeRefUpdate.update();
switch(rc) {
case FAST_FORWARD:
// successful, clean merge
break;
default:
mergeResult = new MergeResult(MergeStatus.FAILED, null);
throw new GitBlitException(MessageFormat.format("Unexpected result \"{0}\" when {1} in {2}", rc.name(), strategy.getOperationMessage(), repository.getDirectory()));
}
} catch (IOException e) {
LOGGER.error("Failed to merge", e);
}
return mergeResult;
}
use of org.eclipse.jgit.lib.RefUpdate.Result in project gitblit by gitblit.
the class RefLogUtils method getRefLogBranch.
/**
* Returns a RefModel for the reflog branch in the repository. If the
* branch can not be found, null is returned.
*
* @param repository
* @return a refmodel for the reflog branch or null
*/
public static RefModel getRefLogBranch(Repository repository) {
List<RefModel> refs = JGitUtils.getRefs(repository, "refs/");
Ref oldRef = null;
for (RefModel ref : refs) {
if (ref.reference.getName().equals(GB_REFLOG)) {
return ref;
} else if (ref.reference.getName().equals("refs/gitblit/reflog")) {
oldRef = ref.reference;
} else if (ref.reference.getName().equals("refs/gitblit/pushes")) {
oldRef = ref.reference;
}
}
if (oldRef != null) {
// rename old ref to refs/meta/gitblit/reflog
RefRename cmd;
try {
cmd = repository.renameRef(oldRef.getName(), GB_REFLOG);
cmd.setRefLogIdent(new PersonIdent("Gitblit", "gitblit@localhost"));
cmd.setRefLogMessage("renamed " + oldRef.getName() + " => " + GB_REFLOG);
Result res = cmd.rename();
switch(res) {
case RENAMED:
LOGGER.info(repository.getDirectory() + " " + cmd.getRefLogMessage());
return getRefLogBranch(repository);
default:
LOGGER.error("failed to rename " + oldRef.getName() + " => " + GB_REFLOG + " (" + res.name() + ")");
}
} catch (IOException e) {
LOGGER.error("failed to rename reflog", e);
}
}
return null;
}
Aggregations