use of org.eclipse.jgit.lib.ObjectInserter in project gerrit by GerritCodeReview.
the class ReceiveCommits method autoCloseChanges.
private void autoCloseChanges(final ReceiveCommand cmd) {
logDebug("Starting auto-closing of changes");
String refName = cmd.getRefName();
checkState(!MagicBranch.isMagicBranch(refName), "shouldn't be auto-closing changes on magic branch %s", refName);
// insertChangesAndPatchSets.
try (BatchUpdate bu = batchUpdateFactory.create(db, projectControl.getProject().getNameKey(), user, TimeUtil.nowTs());
ObjectInserter ins = repo.newObjectInserter();
ObjectReader reader = ins.newReader();
RevWalk rw = new RevWalk(reader)) {
bu.setRepository(repo, rw, ins).updateChangesInParallel();
bu.setRequestId(receiveId);
// TODO(dborowitz): Teach BatchUpdate to ignore missing changes.
RevCommit newTip = rw.parseCommit(cmd.getNewId());
Branch.NameKey branch = new Branch.NameKey(project.getNameKey(), refName);
rw.reset();
rw.markStart(newTip);
if (!ObjectId.zeroId().equals(cmd.getOldId())) {
rw.markUninteresting(rw.parseCommit(cmd.getOldId()));
}
ListMultimap<ObjectId, Ref> byCommit = changeRefsById();
Map<Change.Key, ChangeNotes> byKey = null;
List<ReplaceRequest> replaceAndClose = new ArrayList<>();
int existingPatchSets = 0;
int newPatchSets = 0;
COMMIT: for (RevCommit c; (c = rw.next()) != null; ) {
rw.parseBody(c);
for (Ref ref : byCommit.get(c.copy())) {
existingPatchSets++;
PatchSet.Id psId = PatchSet.Id.fromRef(ref.getName());
bu.addOp(psId.getParentKey(), mergedByPushOpFactory.create(requestScopePropagator, psId, refName));
continue COMMIT;
}
for (String changeId : c.getFooterLines(CHANGE_ID)) {
if (byKey == null) {
byKey = openChangesByBranch(branch);
}
ChangeNotes onto = byKey.get(new Change.Key(changeId.trim()));
if (onto != null) {
newPatchSets++;
// Hold onto this until we're done with the walk, as the call to
// req.validate below calls isMergedInto which resets the walk.
ReplaceRequest req = new ReplaceRequest(onto.getChangeId(), c, cmd, false);
req.notes = onto;
replaceAndClose.add(req);
continue COMMIT;
}
}
}
for (final ReplaceRequest req : replaceAndClose) {
Change.Id id = req.notes.getChangeId();
if (!req.validate(true)) {
logDebug("Not closing {} because validation failed", id);
continue;
}
req.addOps(bu, null);
bu.addOp(id, mergedByPushOpFactory.create(requestScopePropagator, req.psId, refName).setPatchSetProvider(new Provider<PatchSet>() {
@Override
public PatchSet get() {
return req.replaceOp.getPatchSet();
}
}));
bu.addOp(id, new ChangeProgressOp(closeProgress));
}
logDebug("Auto-closing {} changes with existing patch sets and {} with new patch sets", existingPatchSets, newPatchSets);
bu.execute();
} catch (RestApiException e) {
logError("Can't insert patchset", e);
} catch (IOException | OrmException | UpdateException | PermissionBackendException e) {
logError("Can't scan for changes to close", e);
}
}
use of org.eclipse.jgit.lib.ObjectInserter in project gerrit by GerritCodeReview.
the class MergeUtil method newMerger.
public static Merger newMerger(ObjectInserter inserter, Config repoConfig, String strategyName) {
MergeStrategy strategy = MergeStrategy.get(strategyName);
checkArgument(strategy != null, "invalid merge strategy: %s", strategyName);
return strategy.newMerger(new ObjectInserter.Filter() {
@Override
protected ObjectInserter delegate() {
return inserter;
}
@Override
public void flush() {
}
@Override
public void close() {
}
}, repoConfig);
}
use of org.eclipse.jgit.lib.ObjectInserter in project blueocean-plugin by jenkinsci.
the class GitUtils method commit.
// TODO - remove once https://github.com/spotbugs/spotbugs/issues/756 is resolved
@SuppressFBWarnings(value = { "RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE" }, justification = "JDK11 produces different bytecode - https://github.com/spotbugs/spotbugs/issues/756")
public static void commit(final Repository repo, final String refName, final String path, final byte[] contents, final String name, final String email, final String message, final TimeZone timeZone, final Date when) {
final PersonIdent author = buildPersonIdent(repo, name, email, timeZone, when);
try (final ObjectInserter odi = repo.newObjectInserter()) {
// Create the in-memory index of the new/updated issue.
final ObjectId headId = repo.resolve(refName + "^{commit}");
final DirCache index = createTemporaryIndex(repo, headId, path, contents);
final ObjectId indexTreeId = index.writeTree(odi);
// Create a commit object
final CommitBuilder commit = new CommitBuilder();
commit.setAuthor(author);
commit.setCommitter(author);
commit.setEncoding(Constants.CHARACTER_ENCODING);
commit.setMessage(message);
// headId can be null if the repository has no commit yet
if (headId != null) {
commit.setParentId(headId);
}
commit.setTreeId(indexTreeId);
// Insert the commit into the repository
final ObjectId commitId = odi.insert(commit);
odi.flush();
try (RevWalk revWalk = new RevWalk(repo)) {
final RevCommit revCommit = revWalk.parseCommit(commitId);
final RefUpdate ru = repo.updateRef(refName);
if (headId == null) {
ru.setExpectedOldObjectId(ObjectId.zeroId());
} else {
ru.setExpectedOldObjectId(headId);
}
ru.setNewObjectId(commitId);
ru.setRefLogMessage("commit: " + revCommit.getShortMessage(), false);
final RefUpdate.Result rc = ru.forceUpdate();
switch(rc) {
case NEW:
case FORCED:
case FAST_FORWARD:
break;
case REJECTED:
case LOCK_FAILURE:
throw new ConcurrentRefUpdateException(JGitText.get().couldNotLockHEAD, ru.getRef(), rc);
default:
throw new JGitInternalException(MessageFormat.format(JGitText.get().updatingRefFailed, Constants.HEAD, commitId.toString(), rc));
}
}
} catch (ConcurrentRefUpdateException | IOException | JGitInternalException ex) {
throw new RuntimeException(ex);
}
}
use of org.eclipse.jgit.lib.ObjectInserter in project egit by eclipse.
the class IndexFileRevisionTest method entry.
private DirCacheEntry entry(String path, int stage, String data) throws IOException {
DirCacheEntry entry = new DirCacheEntry(path, stage);
entry.setFileMode(FileMode.REGULAR_FILE);
try (ObjectInserter inserter = repository.newObjectInserter()) {
ObjectId blob = inserter.insert(Constants.OBJ_BLOB, data.getBytes("UTF-8"));
entry.setObjectId(blob);
inserter.flush();
}
return entry;
}
use of org.eclipse.jgit.lib.ObjectInserter in project egit by eclipse.
the class TagOperation method updateTagObject.
private ObjectId updateTagObject() throws TeamException {
ObjectId startPointRef = tag.getObjectId();
try {
ObjectId tagId;
repo.open(startPointRef);
try (ObjectInserter inserter = repo.newObjectInserter()) {
tagId = inserter.insert(tag);
inserter.flush();
}
return tagId;
} catch (IOException e) {
throw new TeamException(NLS.bind(CoreText.TagOperation_objectIdNotFound, tag.getTag(), e.getMessage()), e);
}
}
Aggregations