Search in sources :

Example 1 with ObjectLoader

use of org.eclipse.jgit.lib.ObjectLoader in project gerrit by GerritCodeReview.

the class RulesCache method read.

private String read(Project.NameKey project, ObjectId rulesId) throws CompileException {
    try (Repository git = gitMgr.openRepository(project)) {
        try {
            ObjectLoader ldr = git.open(rulesId, Constants.OBJ_BLOB);
            byte[] raw = ldr.getCachedBytes(maxSrcBytes);
            return RawParseUtils.decode(raw);
        } catch (LargeObjectException e) {
            throw new CompileException("rules of " + project + " are too large", e);
        } catch (RuntimeException | IOException e) {
            throw new CompileException("Cannot load rules of " + project, e);
        }
    } catch (IOException e) {
        throw new CompileException("Cannot open repository " + project, e);
    }
}
Also used : LargeObjectException(org.eclipse.jgit.errors.LargeObjectException) Repository(org.eclipse.jgit.lib.Repository) CompileException(com.googlecode.prolog_cafe.exceptions.CompileException) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) IOException(java.io.IOException)

Example 2 with ObjectLoader

use of org.eclipse.jgit.lib.ObjectLoader in project fabric8 by jboss-fuse.

the class GitHelpers method getContentOfObject.

/**
 * Fetches the content of a file from Git repository without checking out the branch
 * @param git
 * @param branch short branch name
 * @param fileName name of the file to fetch
 * @param onlyFromTheCommit if <code>true</code>, return content only if the file is different than in parent commit(s)
 * @return content of file or <code>null</code> if no such file exists
 */
public static byte[] getContentOfObject(Git git, String branch, String fileName, boolean onlyFromTheCommit) throws IOException {
    Ref ref = git.getRepository().getRef("refs/heads/" + branch);
    if (ref == null) {
        return null;
    }
    RevCommit rw = new RevWalk(git.getRepository()).parseCommit(ref.getObjectId());
    ObjectId objectId = objectIdOfResource(git, rw, fileName);
    if (objectId != null) {
        if (!onlyFromTheCommit) {
            ObjectLoader loader = git.getRepository().open(objectId);
            return loader.getBytes();
        } else {
            // if the objectId is the same as in *all* parent commits, than this resource is actually
            // not created in the branch itself - it comes from parent branch (version)
            RevCommit[] parents = rw.getParents();
            boolean change = false;
            if (parents != null && parents.length > 0) {
                for (RevCommit parent : parents) {
                    RevCommit prc = new RevWalk(git.getRepository()).parseCommit(parent.getId());
                    ObjectId parentObjectId = objectIdOfResource(git, prc, fileName);
                    if (parentObjectId == null || !parentObjectId.equals(objectId)) {
                        change = true;
                        break;
                    }
                }
            } else {
                change = true;
            }
            return change ? git.getRepository().open(objectId).getBytes() : null;
        }
    } else {
        return null;
    }
}
Also used : Ref(org.eclipse.jgit.lib.Ref) ObjectId(org.eclipse.jgit.lib.ObjectId) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 3 with ObjectLoader

use of org.eclipse.jgit.lib.ObjectLoader in project fabric8 by jboss-fuse.

the class GitIT method showContentOfFileFromNotCurrentlyCheckedOutBranch.

@Test
public void showContentOfFileFromNotCurrentlyCheckedOutBranch() throws Exception {
    String content = FileUtils.readFileToString(new File(dir, "version.attributes"));
    assertThat(content, equalTo("1.0"));
    RevCommit rw = new RevWalk(git.getRepository()).parseCommit(git.getRepository().getRef("refs/heads/master").getObjectId());
    TreeWalk tw = new TreeWalk(git.getRepository());
    tw.addTree(rw.getTree());
    tw.setRecursive(false);
    tw.setFilter(PathFilter.create("version.attributes"));
    assertThat(tw.next(), equalTo(true));
    ObjectId objectId = tw.getObjectId(0);
    ObjectLoader loader = git.getRepository().open(objectId);
    assertThat(new String(loader.getBytes()), equalTo("master"));
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) RevWalk(org.eclipse.jgit.revwalk.RevWalk) File(java.io.File) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

Example 4 with ObjectLoader

use of org.eclipse.jgit.lib.ObjectLoader in project fabric8 by jboss-fuse.

the class GitPatchManagementServiceImpl method handleConflict.

private void handleConflict(File patchDirectory, Git fork, boolean preferNew, String cpPrefix, boolean performBackup, String choose, String backup, boolean rollback) throws GitAPIException, IOException {
    Map<String, IndexDiff.StageState> conflicts = fork.status().call().getConflictingStageState();
    DirCache cache = fork.getRepository().readDirCache();
    // path -> [oursObjectId, baseObjectId, theirsObjectId]
    Map<String, ObjectId[]> threeWayMerge = new HashMap<>();
    // collect conflicts info
    for (int i = 0; i < cache.getEntryCount(); i++) {
        DirCacheEntry entry = cache.getEntry(i);
        if (entry.getStage() == DirCacheEntry.STAGE_0) {
            continue;
        }
        if (!threeWayMerge.containsKey(entry.getPathString())) {
            threeWayMerge.put(entry.getPathString(), new ObjectId[] { null, null, null });
        }
        if (entry.getStage() == DirCacheEntry.STAGE_1) {
            // base
            threeWayMerge.get(entry.getPathString())[1] = entry.getObjectId();
        }
        if (entry.getStage() == DirCacheEntry.STAGE_2) {
            // ours
            threeWayMerge.get(entry.getPathString())[0] = entry.getObjectId();
        }
        if (entry.getStage() == DirCacheEntry.STAGE_3) {
            // theirs
            threeWayMerge.get(entry.getPathString())[2] = entry.getObjectId();
        }
    }
    // resolve conflicts
    ObjectReader objectReader = fork.getRepository().newObjectReader();
    for (Map.Entry<String, ObjectId[]> entry : threeWayMerge.entrySet()) {
        if (entry.getKey().equals("patch-info.txt")) {
            fork.rm().addFilepattern(entry.getKey()).call();
            continue;
        }
        Resolver resolver = conflictResolver.getResolver(entry.getKey());
        // resolved version - either by custom resolved or using automatic algorithm
        String resolved = null;
        if (resolver != null && entry.getValue()[0] != null && entry.getValue()[2] != null) {
            // custom conflict resolution (don't expect DELETED_BY_X kind of conflict, only BOTH_MODIFIED)
            String message = String.format(" - %s (%s): %s", entry.getKey(), conflicts.get(entry.getKey()), "Using " + resolver.getClass().getName() + " to resolve the conflict");
            Activator.log2(LogService.LOG_INFO, message);
            // when doing custom resolution of conflict, we know that both user and patch has changed the file
            // in non-mergeable way.
            // If there was no resolver, we simply check what to choose by "preferNew" flag
            // But because we have custom resolver, we use "preferNew" flag to check which STAGE points to patch'
            // version and we select this patch' version of conflicting file as less important file inside
            // custom resolver
            File base = null, first = null, second = null;
            try {
                ObjectLoader loader = null;
                if (entry.getValue()[1] != null) {
                    base = new File(fork.getRepository().getWorkTree(), entry.getKey() + ".1");
                    loader = objectReader.open(entry.getValue()[1]);
                    try (FileOutputStream fos = new FileOutputStream(base)) {
                        loader.copyTo(fos);
                    }
                }
                // if preferNew == true (P patch) then "first" file (less important) will be file
                // provided by patch ("theirs", STAGE_3)
                first = new File(fork.getRepository().getWorkTree(), entry.getKey() + ".2");
                loader = objectReader.open(entry.getValue()[preferNew ? 2 : 0]);
                try (FileOutputStream fos = new FileOutputStream(first)) {
                    loader.copyTo(fos);
                }
                // "second", more important file will be user change
                second = new File(fork.getRepository().getWorkTree(), entry.getKey() + ".3");
                loader = objectReader.open(entry.getValue()[preferNew ? 0 : 2]);
                try (FileOutputStream fos = new FileOutputStream(second)) {
                    loader.copyTo(fos);
                }
                // resolvers treat patch change as less important - user lines overwrite patch lines
                if (resolver instanceof PropertiesFileResolver) {
                    // TODO: use options from patch:install / patch:fabric-install command
                    // by default we use a file that comes from patch and we may add property changes
                    // from user
                    // in R patch, preferNew == false, because patch comes first
                    // in P patch, preferNew == true, because patch comes last
                    // in R patch + fabric mode, preferNew == true, because we *merge* patch branch into version
                    // branch
                    boolean useFirstChangeAsBase = true;
                    if (entry.getKey().startsWith("etc/")) {
                        // as base
                        if (rollback) {
                            useFirstChangeAsBase = true;
                        } else {
                            useFirstChangeAsBase = false;
                        }
                    }
                    resolved = ((ResolverEx) resolver).resolve(first, base, second, useFirstChangeAsBase, rollback);
                } else {
                    resolved = resolver.resolve(first, base, second);
                }
                if (resolved != null) {
                    FileUtils.write(new File(fork.getRepository().getWorkTree(), entry.getKey()), resolved);
                    fork.add().addFilepattern(entry.getKey()).call();
                }
            } finally {
                if (base != null) {
                    base.delete();
                }
                if (first != null) {
                    first.delete();
                }
                if (second != null) {
                    second.delete();
                }
            }
        }
        if (resolved == null) {
            // automatic conflict resolution
            String message = String.format(" - %s (%s): Choosing %s", entry.getKey(), conflicts.get(entry.getKey()), choose);
            ObjectLoader loader = null;
            ObjectLoader loaderForBackup = null;
            // longer code, but more readable then series of elvis operators (?:)
            if (preferNew) {
                switch(conflicts.get(entry.getKey())) {
                    case BOTH_ADDED:
                    case BOTH_MODIFIED:
                        loader = objectReader.open(entry.getValue()[2]);
                        loaderForBackup = objectReader.open(entry.getValue()[0]);
                        break;
                    case BOTH_DELETED:
                        break;
                    case DELETED_BY_THEM:
                        // ENTESB-6003: special case: when R patch removes something and we've modified it
                        // let's preserve our version
                        message = String.format(" - %s (%s): Keeping custom change", entry.getKey(), conflicts.get(entry.getKey()));
                        loader = objectReader.open(entry.getValue()[0]);
                        break;
                    case DELETED_BY_US:
                        loader = objectReader.open(entry.getValue()[2]);
                        break;
                }
            } else {
                switch(conflicts.get(entry.getKey())) {
                    case BOTH_ADDED:
                    case BOTH_MODIFIED:
                        loader = objectReader.open(entry.getValue()[0]);
                        loaderForBackup = objectReader.open(entry.getValue()[2]);
                        break;
                    case DELETED_BY_THEM:
                        loader = objectReader.open(entry.getValue()[0]);
                        break;
                    case BOTH_DELETED:
                    case DELETED_BY_US:
                        break;
                }
            }
            Activator.log2(LogService.LOG_WARNING, message);
            if (loader != null) {
                try (FileOutputStream fos = new FileOutputStream(new File(fork.getRepository().getWorkTree(), entry.getKey()))) {
                    loader.copyTo(fos);
                }
                fork.add().addFilepattern(entry.getKey()).call();
            } else {
                fork.rm().addFilepattern(entry.getKey()).call();
            }
            if (performBackup) {
                // the other entry should be backed up
                if (loaderForBackup != null) {
                    File target = new File(patchDirectory.getParent(), patchDirectory.getName() + ".backup");
                    if (isStandaloneChild()) {
                        target = new File(patchDirectory.getParent(), patchDirectory.getName() + "." + System.getProperty("karaf.name") + ".backup");
                    }
                    if (cpPrefix != null) {
                        target = new File(target, cpPrefix);
                    }
                    File file = new File(target, entry.getKey());
                    message = String.format("Backing up %s to \"%s\"", backup, file.getCanonicalPath());
                    Activator.log2(LogService.LOG_DEBUG, message);
                    file.getParentFile().mkdirs();
                    try (FileOutputStream fos = new FileOutputStream(file)) {
                        loaderForBackup.copyTo(fos);
                    }
                }
            }
        }
    }
}
Also used : DirCacheEntry(org.eclipse.jgit.dircache.DirCacheEntry) ConflictResolver(io.fabric8.patch.management.conflicts.ConflictResolver) PropertiesFileResolver(io.fabric8.patch.management.conflicts.PropertiesFileResolver) Resolver(io.fabric8.patch.management.conflicts.Resolver) HashMap(java.util.HashMap) DirCache(org.eclipse.jgit.dircache.DirCache) PropertiesFileResolver(io.fabric8.patch.management.conflicts.PropertiesFileResolver) EOLFixingFileOutputStream(io.fabric8.patch.management.io.EOLFixingFileOutputStream) FileOutputStream(java.io.FileOutputStream) ObjectReader(org.eclipse.jgit.lib.ObjectReader) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) Map(java.util.Map) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File)

Example 5 with ObjectLoader

use of org.eclipse.jgit.lib.ObjectLoader in project fabric8 by jboss-fuse.

the class GitPatchRepositoryImpl method getFileContent.

@Override
public String getFileContent(Git fork, String sha1, String fileName) throws IOException {
    ObjectReader objectReader = fork.getRepository().newObjectReader();
    RevCommit commit = new RevWalk(fork.getRepository()).parseCommit(fork.getRepository().resolve(sha1));
    TreeWalk tw = new TreeWalk(fork.getRepository());
    tw.addTree(commit.getTree());
    tw.setRecursive(false);
    tw.setFilter(PathFilter.create(fileName));
    if (tw.next()) {
        ObjectId objectId = tw.getObjectId(0);
        ObjectLoader loader = fork.getRepository().open(objectId);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        loader.copyTo(out);
        return new String(out.toByteArray(), "UTF-8");
    }
    return null;
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) ObjectReader(org.eclipse.jgit.lib.ObjectReader) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) RevWalk(org.eclipse.jgit.revwalk.RevWalk) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

ObjectLoader (org.eclipse.jgit.lib.ObjectLoader)37 RevWalk (org.eclipse.jgit.revwalk.RevWalk)24 ObjectId (org.eclipse.jgit.lib.ObjectId)21 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)20 RevCommit (org.eclipse.jgit.revwalk.RevCommit)19 IOException (java.io.IOException)14 Repository (org.eclipse.jgit.lib.Repository)14 ObjectReader (org.eclipse.jgit.lib.ObjectReader)12 File (java.io.File)9 FileOutputStream (java.io.FileOutputStream)6 Ref (org.eclipse.jgit.lib.Ref)6 RevTree (org.eclipse.jgit.revwalk.RevTree)6 LargeObjectException (org.eclipse.jgit.errors.LargeObjectException)5 FileMode (org.eclipse.jgit.lib.FileMode)5 IArchimateModel (com.archimatetool.model.IArchimateModel)4 HashMap (java.util.HashMap)3 FilestoreModel (com.gitblit.models.FilestoreModel)2 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)2 CompileException (com.googlecode.prolog_cafe.exceptions.CompileException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2