use of com.google.gerrit.server.patch.filediff.FileDiffOutput in project gerrit by GerritCodeReview.
the class DiffOperationsTest method diffAgainstAutoMergePersistsAutoMergeInRepo.
@Test
public void diffAgainstAutoMergePersistsAutoMergeInRepo() throws Exception {
ObjectId parent1 = createCommit(repo, null, ImmutableList.of(new FileEntity("file_1.txt", "file 1 content")));
ObjectId parent2 = createCommit(repo, null, ImmutableList.of(new FileEntity("file_2.txt", "file 2 content")));
ObjectId merge = createMergeCommit(repo, ImmutableList.of(new FileEntity("file_1.txt", "file 1 content"), new FileEntity("file_2.txt", "file 2 content"), new FileEntity("file_3.txt", "file 3 content")), parent1, parent2);
String autoMergeRef = RefNames.refsCacheAutomerge(merge.name());
assertThat(repo.getRefDatabase().exactRef(autoMergeRef)).isNull();
Map<String, FileDiffOutput> changedFiles = diffOperations.listModifiedFilesAgainstParent(testProjectName, merge, /* parentNum=*/
0, DiffOptions.DEFAULTS);
assertThat(changedFiles.keySet()).containsExactly("/COMMIT_MSG", "/MERGE_LIST", "file_3.txt");
// Requesting diff against auto-merge had the side effect of updating the auto-merge ref
assertThat(repo.getRefDatabase().exactRef(autoMergeRef)).isNotNull();
}
use of com.google.gerrit.server.patch.filediff.FileDiffOutput in project gerrit by GerritCodeReview.
the class EventFactory method asPatchSetAttribute.
/**
* Create a PatchSetAttribute for the given patchset suitable for serialization to JSON.
*/
public PatchSetAttribute asPatchSetAttribute(RevWalk revWalk, Change change, PatchSet patchSet) {
PatchSetAttribute p = new PatchSetAttribute();
p.revision = patchSet.commitId().name();
p.number = patchSet.number();
p.ref = patchSet.refName();
p.uploader = asAccountAttribute(patchSet.uploader());
p.createdOn = patchSet.createdOn().getEpochSecond();
PatchSet.Id pId = patchSet.id();
try {
p.parents = new ArrayList<>();
RevCommit c = revWalk.parseCommit(ObjectId.fromString(p.revision));
for (RevCommit parent : c.getParents()) {
p.parents.add(parent.name());
}
UserIdentity author = emails.toUserIdentity(c.getAuthorIdent());
if (author.getAccount() == null) {
p.author = new AccountAttribute();
p.author.email = author.getEmail();
p.author.name = author.getName();
p.author.username = "";
} else {
p.author = asAccountAttribute(author.getAccount());
}
Map<String, FileDiffOutput> modifiedFiles = diffOperations.listModifiedFilesAgainstParent(change.getProject(), patchSet.commitId(), /* parentNum= */
0, DiffOptions.DEFAULTS);
for (FileDiffOutput fileDiff : modifiedFiles.values()) {
p.sizeDeletions += fileDiff.deletions();
p.sizeInsertions += fileDiff.insertions();
}
p.kind = changeKindCache.getChangeKind(change, patchSet);
} catch (IOException | StorageException e) {
logger.atSevere().withCause(e).log("Cannot load patch set data for %s", patchSet.id());
} catch (DiffNotAvailableException e) {
logger.atSevere().withCause(e).log("Cannot get size information for %s.", pId);
}
return p;
}
use of com.google.gerrit.server.patch.filediff.FileDiffOutput in project gerrit by GerritCodeReview.
the class PRED_commit_edits_2 method exec.
@Override
public Operation exec(Prolog engine) throws PrologException {
engine.setB0();
Term a1 = arg1.dereference();
Term a2 = arg2.dereference();
Pattern fileRegex = getRegexParameter(a1);
Pattern editRegex = getRegexParameter(a2);
Map<String, FileDiffOutput> modifiedFiles = StoredValues.DIFF_LIST.get(engine);
FileDiffOutput firstDiff = Iterables.getFirst(modifiedFiles.values(), /* defaultValue= */
null);
if (firstDiff == null) {
// No available diffs. We cannot identify old and new commit IDs.
engine.fail();
}
Repository repo = StoredValues.REPOSITORY.get(engine);
try (ObjectReader reader = repo.newObjectReader();
RevWalk rw = new RevWalk(reader)) {
final RevTree aTree = firstDiff.oldCommitId().equals(ObjectId.zeroId()) ? null : rw.parseTree(firstDiff.oldCommitId());
final RevTree bTree = rw.parseCommit(firstDiff.newCommitId()).getTree();
for (FileDiffOutput entry : modifiedFiles.values()) {
String newName = FilePathAdapter.getNewPath(entry.oldPath(), entry.newPath(), entry.changeType());
String oldName = FilePathAdapter.getOldPath(entry.oldPath(), entry.changeType());
if (Patch.isMagic(newName)) {
continue;
}
if (fileRegex.matcher(newName).find() || (oldName != null && fileRegex.matcher(oldName).find())) {
List<Edit> edits = entry.edits().stream().map(TaggedEdit::jgitEdit).collect(Collectors.toList());
if (edits.isEmpty()) {
continue;
}
Text tA;
if (oldName != null) {
tA = load(aTree, oldName, reader);
} else {
tA = load(aTree, newName, reader);
}
Text tB = load(bTree, newName, reader);
for (Edit edit : edits) {
if (tA != Text.EMPTY) {
String aDiff = tA.getString(edit.getBeginA(), edit.getEndA(), true);
if (editRegex.matcher(aDiff).find()) {
return cont;
}
}
if (tB != Text.EMPTY) {
String bDiff = tB.getString(edit.getBeginB(), edit.getEndB(), true);
if (editRegex.matcher(bDiff).find()) {
return cont;
}
}
}
}
}
} catch (IOException err) {
throw new JavaException(this, 1, err);
}
return engine.fail();
}
use of com.google.gerrit.server.patch.filediff.FileDiffOutput in project gerrit by GerritCodeReview.
the class PRED_files_1 method getAllSubmodulePaths.
/**
* Returns the paths for all {@code GITLINK} files.
*/
private static Set<String> getAllSubmodulePaths(Repository repository, RevCommit commit, Collection<FileDiffOutput> modifiedFiles) throws PrologException, IOException {
Set<String> submodules = new HashSet<>();
try (TreeWalk treeWalk = new TreeWalk(repository)) {
treeWalk.addTree(commit.getTree());
Set<String> allPaths = modifiedFiles.stream().map(f -> FilePathAdapter.getNewPath(f.oldPath(), f.newPath(), f.changeType())).filter(f -> !Patch.isMagic(f)).collect(Collectors.toSet());
treeWalk.setFilter(PathFilterGroup.createFromStrings(allPaths));
while (treeWalk.next()) {
if (treeWalk.getFileMode() == FileMode.GITLINK) {
submodules.add(treeWalk.getPathString());
}
}
return submodules;
}
}
use of com.google.gerrit.server.patch.filediff.FileDiffOutput in project gerrit by GerritCodeReview.
the class PRED_commit_stats_3 method exec.
@Override
public Operation exec(Prolog engine) throws PrologException {
engine.setB0();
Term a1 = arg1.dereference();
Term a2 = arg2.dereference();
Term a3 = arg3.dereference();
Map<String, FileDiffOutput> modifiedFiles = StoredValues.DIFF_LIST.get(engine);
// Account for magic files
if (!a1.unify(new IntegerTerm(modifiedFiles.size() - countMagicFiles(modifiedFiles.values())), engine.trail)) {
return engine.fail();
}
Integer insertions = modifiedFiles.values().stream().map(FileDiffOutput::insertions).reduce(0, Integer::sum);
Integer deletions = modifiedFiles.values().stream().map(FileDiffOutput::deletions).reduce(0, Integer::sum);
if (!a2.unify(new IntegerTerm(insertions), engine.trail)) {
return engine.fail();
}
if (!a3.unify(new IntegerTerm(deletions), engine.trail)) {
return engine.fail();
}
return cont;
}
Aggregations