Search in sources :

Example 1 with DiffException

use of com.github.difflib.algorithm.DiffException in project indy by Commonjava.

the class RepoChangeHandler method handleRepoChange.

private void handleRepoChange(DiffStoreFetcher fetcher, boolean isDelete) {
    if (!config.isEnabled()) {
        logger.info("Repository changelog module is not enabled. Will ignore all change logs for propagation.");
        return;
    }
    ChangeSummary changeSummary = fetcher.getSummary();
    Collection<ArtifactStore> stores = fetcher.getStores();
    String user = ChangeSummary.SYSTEM_USER;
    String summary = "";
    String version = "";
    Date timeStamp = new Date();
    if (changeSummary != null) {
        if (changeSummary.getUser() != null) {
            user = changeSummary.getUser();
        }
        if (changeSummary.getSummary() != null) {
            summary = changeSummary.getSummary();
        }
        if (changeSummary.getRevisionId() != null) {
            version = changeSummary.getRevisionId();
        } else {
            // FIXME: we need version not null here to let it be a key, not sure if timestamp is good here
            version = String.format("%s", timeStamp.getTime());
        }
    }
    for (ArtifactStore store : stores) {
        try {
            ArtifactStore origin = fetcher.getOriginal(store);
            ArtifactStore changed = fetcher.getChanged(store);
            String patchString = diffRepoChanges(changed, origin);
            ChangeEvent changeLog = new ChangeEvent();
            changeLog.setEventId(UUID.randomUUID().toString().replace("-", ""));
            changeLog.setStoreKey(store.getKey().toString());
            changeLog.setChangeTime(new Date());
            changeLog.setDiffContent(patchString);
            if (isDelete) {
                changeLog.setChangeType(ChangeType.DELETE);
            } else {
                changeLog.setChangeType(origin == null ? ChangeType.CREATE : ChangeType.UPDATE);
            }
            changeLog.setUser(user);
            changeLog.setSummary(summary);
            changeLog.setVersion(version);
            String key = changeLog.getStoreKey() + "_" + changeLog.getVersion();
            repoChangelogCache.put(key, changeLog);
        } catch (JsonProcessingException | DiffException e) {
            String error = String.format("Something wrong happened when doing repo change log generation for store %s", store.getKey());
            logger.error(error, e);
        }
    }
}
Also used : ChangeEvent(org.commonjava.auditquery.history.ChangeEvent) DiffException(com.github.difflib.algorithm.DiffException) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Date(java.util.Date)

Example 2 with DiffException

use of com.github.difflib.algorithm.DiffException in project page-factory-2 by sbtqa.

the class DiffUtils method diff.

public static String diff(String string1, String string2) {
    DiffRowGenerator generator = DiffRowGenerator.create().showInlineDiffs(true).mergeOriginalRevised(true).oldTag(f -> "|").newTag(f -> "|").build();
    List<DiffRow> rows = new ArrayList<>();
    try {
        rows = generator.generateDiffRows(Collections.singletonList(string1), Collections.singletonList(string2));
    } catch (DiffException e) {
        LOG.info("There is an error in string diff", e);
    }
    return !rows.isEmpty() ? rows.get(0).getOldLine() : "";
}
Also used : List(java.util.List) Logger(org.slf4j.Logger) CoreStepsImpl(ru.sbtqa.tag.pagefactory.junit.CoreStepsImpl) DiffException(com.github.difflib.algorithm.DiffException) LoggerFactory(org.slf4j.LoggerFactory) DiffRow(com.github.difflib.text.DiffRow) DiffRowGenerator(com.github.difflib.text.DiffRowGenerator) Collections(java.util.Collections) ArrayList(java.util.ArrayList) DiffRow(com.github.difflib.text.DiffRow) DiffException(com.github.difflib.algorithm.DiffException) ArrayList(java.util.ArrayList) DiffRowGenerator(com.github.difflib.text.DiffRowGenerator)

Example 3 with DiffException

use of com.github.difflib.algorithm.DiffException in project closure-compiler by google.

the class TextDiffFactsBuilder method build.

/**
 * Returns one or more Fact objects representing the difference between the expected and actual
 * text strings, which must be specified by calling their methods before calling this one.
 */
public ImmutableList<Fact> build() {
    try {
        // The diff algorithm expects to work on a list, so we need to split the text into
        // lines.
        final Splitter lineSplitter = Splitter.on('\n');
        final List<String> expectedLines = lineSplitter.splitToList(checkNotNull(expectedText));
        final List<String> actualLines = lineSplitter.splitToList(checkNotNull(actualText));
        final Patch<String> patch = DiffUtils.diff(expectedLines, actualLines);
        final List<String> unifiedDiff = UnifiedDiffUtils.generateUnifiedDiff("expected", "actual", expectedLines, patch, /* contextSize= */
        10);
        return ImmutableList.of(fact(title, unifiedDiff.stream().collect(joining("\n"))));
    } catch (DiffException e) {
        // It may indicate a bug in the diff library itself.
        throw new IllegalStateException(e);
    }
}
Also used : Splitter(com.google.common.base.Splitter) DiffException(com.github.difflib.algorithm.DiffException)

Aggregations

DiffException (com.github.difflib.algorithm.DiffException)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 DiffRow (com.github.difflib.text.DiffRow)1 DiffRowGenerator (com.github.difflib.text.DiffRowGenerator)1 Splitter (com.google.common.base.Splitter)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 Date (java.util.Date)1 List (java.util.List)1 ChangeEvent (org.commonjava.auditquery.history.ChangeEvent)1 ChangeSummary (org.commonjava.indy.audit.ChangeSummary)1 ArtifactStore (org.commonjava.indy.model.core.ArtifactStore)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 CoreStepsImpl (ru.sbtqa.tag.pagefactory.junit.CoreStepsImpl)1