org.eclipse.jgit.lib.ObjectReader Java Examples

The following examples show how to use org.eclipse.jgit.lib.ObjectReader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BlameCalculator.java    From diff-check with GNU Lesser General Public License v2.1 6 votes vote down vote up
public List<BlameResult> calculate(
        File repoDir,
        List<String> filePathList,
        String startRev) throws Exception {
    try (Git git = Git.open(repoDir);
            ObjectReader reader = git.getRepository().newObjectReader();
            RevWalk rw = new RevWalk(git.getRepository())) {
        RevCommit startCommit = rw.parseCommit(git.getRepository().resolve(startRev));
        List<BlameResult> resultList = new ArrayList<>();
        for (String filePath : filePathList) {
            BlameResult result = calculateBlame(filePath, startCommit, git);
            resultList.add(result);
        }
        return resultList;
    }
}
 
Example #2
Source File: JGitDiscoveryTest.java    From multi-module-maven-release-plugin with MIT License 6 votes vote down vote up
@Test public void name() throws IOException, GitAPIException {
    ObjectId head = repo.resolve("HEAD^{tree}");
    ObjectId oldHead = repo.resolve("HEAD^^{tree}");


    ObjectReader reader = repo.newObjectReader();

    CanonicalTreeParser prevParser = new CanonicalTreeParser();
    prevParser.reset(reader, oldHead);

    CanonicalTreeParser headParser = new CanonicalTreeParser();
    headParser.reset(reader, head);

    List<DiffEntry> diffs = new Git(repo).diff()
            .setNewTree(headParser)
            .setOldTree(prevParser)
            .call();

    for (DiffEntry entry : diffs)
        System.out.println(entry);
}
 
Example #3
Source File: GitRepoMetaData.java    From GitFx with Apache License 2.0 6 votes vote down vote up
public String getDiffBetweenCommits(int commitIndex) throws IOException,GitAPIException{
    if(commitIndex+1==commitCount)
        return "Nothing to Diff. This is first commit";
    AbstractTreeIterator current = prepareTreeParser(repository,commitSHA.get(commitIndex));
    AbstractTreeIterator parent = prepareTreeParser(repository,commitSHA.get(++commitIndex));
    ObjectReader reader = repository.newObjectReader();
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    // finally get the list of changed files
    Git git = new Git(repository) ;
    List<DiffEntry> diff = git.diff().
            setOldTree(parent).
            setNewTree(current).
            //TODO Set the path filter to filter out the selected file
            //setPathFilter(PathFilter.create("README.md")).
            call();
    for (DiffEntry entry : diff) {
        System.out.println("Entry: " + entry + ", from: " + entry.getOldId() + ", to: " + entry.getNewId());
        DiffFormatter formatter = new DiffFormatter(byteStream) ;
            formatter.setRepository(repository);
            formatter.format(entry);
        }
   // System.out.println(byteStream.toString());
    String diffContent = byteStream.toString();
    return byteStream.toString();
}
 
Example #4
Source File: GitUtils.java    From onedev with MIT License 6 votes vote down vote up
@Nullable
public static List<String> readLines(Repository repository, RevCommit commit, String path, 
		WhitespaceOption whitespaceOption) {
	try {
		TreeWalk treeWalk = TreeWalk.forPath(repository, path, commit.getTree());
		if (treeWalk != null) {
			ObjectId blobId = treeWalk.getObjectId(0);
			ObjectReader objectReader = treeWalk.getObjectReader();
			BlobIdent blobIdent = new BlobIdent(commit.name(), path, FileMode.REGULAR_FILE.getBits()); 
			Blob blob = new Blob(blobIdent, blobId, objectReader);
			List<String> normalizedLines = new ArrayList<>();
			for (String line: Preconditions.checkNotNull(blob.getText()).getLines()) {
				normalizedLines.add(whitespaceOption.process(line));
			}
			return normalizedLines;
		} else {
			return null;
		}
	} catch (IOException e) {
		throw new RuntimeException(e);
	}
}
 
Example #5
Source File: UpdaterGenerator.java    From neembuu-uploader with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Populate all the files to update, if the system should update.
 */
private void populateDiff() {
    try {
        git.fetch().call();
        Repository repo = git.getRepository();
        ObjectId fetchHead = repo.resolve("FETCH_HEAD^{tree}");
        ObjectId head = repo.resolve("HEAD^{tree}");
        
        ObjectReader reader = repo.newObjectReader();
        CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();
        oldTreeIter.reset(reader, head);
        CanonicalTreeParser newTreeIter = new CanonicalTreeParser();
        newTreeIter.reset(reader, fetchHead);
        List<DiffEntry> diffs = git.diff().setShowNameAndStatusOnly(true)
                .setNewTree(newTreeIter)
                .setOldTree(oldTreeIter)
                .call();
        
        pluginsToUpdate = new ArrayList<PluginToUpdate>();
        
        checkDiffEmpty(diffs);
        
    } catch (GitAPIException | IOException ex) {
        Logger.getLogger(UpdaterGenerator.class.getName()).log(Level.SEVERE, null, ex);
    }
}
 
Example #6
Source File: JGitTemplate.java    From piper with Apache License 2.0 6 votes vote down vote up
private List<IdentifiableResource> getHeadFiles (Repository aRepository, String... aSearchPaths) {
  List<String> searchPaths = Arrays.asList(aSearchPaths);
  List<IdentifiableResource> resources = new ArrayList<>();
  try (ObjectReader reader = aRepository.newObjectReader(); RevWalk walk = new RevWalk(reader); TreeWalk treeWalk = new TreeWalk(aRepository,reader);) {
    final ObjectId id = aRepository.resolve(Constants.HEAD);
    if(id == null) {
      return List.of();
    }
    RevCommit commit = walk.parseCommit(id);
    RevTree tree = commit.getTree();
    treeWalk.addTree(tree);
    treeWalk.setRecursive(true);
    while (treeWalk.next()) {
      String path = treeWalk.getPathString();        
      if(!path.startsWith(".") && (searchPaths == null || searchPaths.size() == 0 || searchPaths.stream().anyMatch((sp)->path.startsWith(sp)))) {
        ObjectId objectId = treeWalk.getObjectId(0);
        logger.debug("Loading {} [{}]",path,objectId.name());
        resources.add(readBlob(aRepository, path.substring(0, path.indexOf('.')), objectId.name()));
      }
    }
    return resources;
  }
  catch (Exception e) {
    throw Throwables.propagate(e);
  } 
}
 
Example #7
Source File: RewordActionHandler.java    From WebIDE-Backend with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public RebaseResponse extractMessage(Repository repository) throws IOException {
    List<RebaseTodoLine> rebaseTodoLines = repository.readRebaseTodo(getRebasePath(repository, DONE), false);
    // the last rebase_todo_line
    RebaseTodoLine line = rebaseTodoLines.get(rebaseTodoLines.size() - 1);

    try (RevWalk walk = new RevWalk(repository)) {
        ObjectReader or = repository.newObjectReader();
        RevCommit commitToPick = walk.parseCommit(or.resolve(line.getCommit()).iterator().next());

        String oldMessage = commitToPick.getFullMessage();

        RebaseResponse response = new RebaseResponse(false, RebaseResponse.Status.INTERACTIVE_EDIT);
        response.setMessage(oldMessage);

        return response;
    }
}
 
Example #8
Source File: GitUtils.java    From blueocean-plugin with MIT License 6 votes vote down vote up
@SuppressFBWarnings(value={"RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE"}, justification="JDK11 produces different bytecode - https://github.com/spotbugs/spotbugs/issues/756")
static byte[] readFile(Repository repository, String ref, String filePath) {
    try (ObjectReader reader = repository.newObjectReader()) {
        ObjectId branchRef = repository.resolve(ref); // repository.exactRef(ref);
        if (branchRef != null) { // for empty repositories, branchRef may be null
            RevWalk revWalk = new RevWalk(repository);
            RevCommit commit = revWalk.parseCommit(branchRef);
            // and using commit's tree find the path
            RevTree tree = commit.getTree();
            TreeWalk treewalk = TreeWalk.forPath(reader, filePath, tree);
            if (treewalk != null) {
                // use the blob id to read the file's data
                return reader.open(treewalk.getObjectId(0)).getBytes();
            }
        }
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
    return null;
}
 
Example #9
Source File: UIGit.java    From hop with Apache License 2.0 6 votes vote down vote up
private AbstractTreeIterator getTreeIterator( String commitId ) throws Exception {
  if ( commitId == null ) {
    return new EmptyTreeIterator();
  }
  if ( commitId.equals( WORKINGTREE ) ) {
    return new FileTreeIterator( git.getRepository() );
  } else if ( commitId.equals( INDEX ) ) {
    return new DirCacheIterator( git.getRepository().readDirCache() );
  } else {
    ObjectId id = git.getRepository().resolve( commitId );
    if ( id == null ) { // commitId does not exist
      return new EmptyTreeIterator();
    } else {
      CanonicalTreeParser treeIterator = new CanonicalTreeParser();
      try ( RevWalk rw = new RevWalk( git.getRepository() ) ) {
        RevTree tree = rw.parseTree( id );
        try ( ObjectReader reader = git.getRepository().newObjectReader() ) {
          treeIterator.reset( reader, tree.getId() );
        }
      }
      return treeIterator;
    }
  }
}
 
Example #10
Source File: JGitBugManualTest.java    From tutorials with MIT License 6 votes vote down vote up
/**
 * This test case expects one git repository to be present in local file system.
 * Currently this test uses the Baeldung repository i.e. the current checkout repository.
 * It finds the repository by tracking back and scan file system to find .git folder in
 * the file system.
 * 
 * Before running the test case ensure that the .git folder is present.
 * 
 * @throws IOException
 */
@Test
public void testRevWalkDisposeClosesReader() throws IOException {
    try (Repository repo = Helper.openJGitRepository()) {
        try (ObjectReader reader = repo.newObjectReader()) {
            try (RevWalk walk = new RevWalk(reader)) {
                walk.dispose();

                Ref head = repo.exactRef("refs/heads/master");
                System.out.println("Found head: " + head);

                ObjectLoader loader = reader.open(head.getObjectId());
                assertNotNull(loader);
            }
        }
    }
}
 
Example #11
Source File: JGitAPIImpl.java    From git-client-plugin with MIT License 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void addNote(String note, String namespace) throws GitException {
    try (Repository repo = getRepository()) {
        ObjectId head = repo.resolve(HEAD); // commit to put a note on

        AddNoteCommand cmd = git(repo).notesAdd();
        cmd.setMessage(normalizeNote(note));
        cmd.setNotesRef(qualifyNotesNamespace(namespace));
        try (ObjectReader or = repo.newObjectReader();
             RevWalk walk = new RevWalk(or)) {
            cmd.setObjectId(walk.parseAny(head));
            cmd.call();
        }
    } catch (GitAPIException | IOException e) {
        throw new GitException(e);
    }
}
 
Example #12
Source File: AddTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testAddMissingSymlink () throws Exception {
    if (isWindows()) {
        return;
    }
    String path = "folder/file";
    File f = new File(workDir, path);
    
    // try with commandline client
    File link = new File(workDir, "link");
    Files.createSymbolicLink(Paths.get(link.getAbsolutePath()), Paths.get(path));
    getClient(workDir).add(new File[] { link }, NULL_PROGRESS_MONITOR);
    DirCacheEntry e = repository.readDirCache().getEntry(link.getName());
    assertEquals(FileMode.SYMLINK, e.getFileMode());
    assertEquals(0, e.getLength());
    try (ObjectReader reader = repository.getObjectDatabase().newReader()) {
        assertTrue(reader.has(e.getObjectId()));
        byte[] bytes = reader.open(e.getObjectId()).getBytes();
        assertEquals(path, RawParseUtils.decode(bytes));
    }
}
 
Example #13
Source File: JGitAPIImpl.java    From git-client-plugin with MIT License 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public List<IndexEntry> getSubmodules(String treeIsh) throws GitException {
    try (Repository repo = getRepository();
         ObjectReader or = repo.newObjectReader();
         RevWalk w = new RevWalk(or)) {
        List<IndexEntry> r = new ArrayList<>();

        RevTree t = w.parseTree(repo.resolve(treeIsh));
        SubmoduleWalk walk = new SubmoduleWalk(repo);
        walk.setTree(t);
        walk.setRootTree(t);
        while (walk.next()) {
            r.add(new IndexEntry(walk));
        }

        return r;
    } catch (IOException e) {
        throw new GitException(e);
    }
}
 
Example #14
Source File: MergeBaseCalculator.java    From diff-check with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * calculate the merge base between two refs
 *
 * @param repoDir           the git directory
 * @param ref1              the ref
 * @param ref2              the other ref
 * @return                  the merge base
 * @throws Exception        throw Exception when error happens
 */
public String calculateMergeBase(File repoDir, String ref1, String ref2) throws Exception {
    try (Git git = Git.open(repoDir);
            ObjectReader reader = git.getRepository().newObjectReader();
            RevWalk rw = new RevWalk(git.getRepository())) {

        RevCommit commit1 = rw.parseCommit(git.getRepository().resolve(ref1));
        RevCommit commit2 = rw.parseCommit(git.getRepository().resolve(ref2));

        rw.setRevFilter(RevFilter.MERGE_BASE);
        rw.markStart(commit1);
        rw.markStart(commit2);

        RevCommit mergeBase = rw.next();

        return mergeBase != null ? mergeBase.name() : "";

    }
}
 
Example #15
Source File: JGitAPIImpl.java    From git-client-plugin with MIT License 6 votes vote down vote up
/** {@inheritDoc} */
@Deprecated
@Override
public ObjectId mergeBase(ObjectId id1, ObjectId id2) throws InterruptedException {
    try (Repository repo = getRepository();
         ObjectReader or = repo.newObjectReader();
         RevWalk walk = new RevWalk(or)) {
        walk.setRetainBody(false);  // we don't need the body for this computation
        walk.setRevFilter(RevFilter.MERGE_BASE);

        walk.markStart(walk.parseCommit(id1));
        walk.markStart(walk.parseCommit(id2));

        RevCommit base = walk.next();
        if (base==null)     return null;    // no common base
        return base.getId();
    } catch (IOException e) {
        throw new GitException(e);
    }
}
 
Example #16
Source File: JGitAPIImpl.java    From git-client-plugin with MIT License 6 votes vote down vote up
/** {@inheritDoc} */
@Deprecated
@Override
public String getAllLogEntries(String branch) throws InterruptedException {
    try (Repository repo = getRepository();
         ObjectReader or = repo.newObjectReader();
         RevWalk walk = new RevWalk(or)) {
        StringBuilder w = new StringBuilder();
        markAllRefs(walk);
        walk.setRetainBody(false);

        for (RevCommit c : walk) {
            w.append('\'').append(c.name()).append('#').append(c.getCommitTime()).append("'\n");
        }
        return w.toString().trim();
    } catch (IOException e) {
        throw new GitException(e);
    }
}
 
Example #17
Source File: JGitAPIImpl.java    From git-client-plugin with MIT License 6 votes vote down vote up
/** {@inheritDoc} */
@Deprecated
@Override
public List<IndexEntry> lsTree(String treeIsh, boolean recursive) throws GitException, InterruptedException {
    try (Repository repo = getRepository();
         ObjectReader or = repo.newObjectReader();
         RevWalk w = new RevWalk(or)) {
        TreeWalk tree = new TreeWalk(or);
        tree.addTree(w.parseTree(repo.resolve(treeIsh)));
        tree.setRecursive(recursive);

        List<IndexEntry> r = new ArrayList<>();
        while (tree.next()) {
            RevObject rev = w.parseAny(tree.getObjectId(0));
            r.add(new IndexEntry(
                    String.format("%06o", tree.getRawMode(0)),
                    typeString(rev.getType()),
                    tree.getObjectId(0).name(),
                    tree.getNameString()));
        }
        return r;
    } catch (IOException e) {
        throw new GitException(e);
    }
}
 
Example #18
Source File: UpdaterGenerator.java    From neembuu-uploader with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Populate all the files to update, if the system should update.
 */
private boolean populateDiff() {
    try {
        git.fetch().call();
        Repository repo = git.getRepository();
        ObjectId fetchHead = repo.resolve("FETCH_HEAD^{tree}");
        ObjectId head = repo.resolve("HEAD^{tree}");
        
        ObjectReader reader = repo.newObjectReader();
        CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();
        oldTreeIter.reset(reader, head);
        CanonicalTreeParser newTreeIter = new CanonicalTreeParser();
        newTreeIter.reset(reader, fetchHead);
        List<DiffEntry> diffs = git.diff().setShowNameAndStatusOnly(true)
                .setNewTree(newTreeIter)
                .setOldTree(oldTreeIter)
                .call();
        
        if (diffs.isEmpty()) {
            System.out.println("No diff");
            return false;
        }else{
            return true;
        }
    } catch (GitAPIException | IOException ex) {
        Logger.getLogger(UpdaterGenerator.class.getName()).log(Level.SEVERE, null, ex);
    }return true;//assume true
}
 
Example #19
Source File: SMAGit.java    From salesforce-migration-assistant with MIT License 5 votes vote down vote up
/**
 * Replicates ls-tree for the current commit.
 *
 * @return Map containing the full path and the data for all items in the repository.
 * @throws IOException
 */
public Map<String, byte[]> getAllMetadata() throws Exception
{
    Map<String, byte[]> contents = new HashMap<String, byte[]>();
    ObjectReader reader = repository.newObjectReader();
    ObjectId commitId = repository.resolve(curCommit);
    RevWalk revWalk = new RevWalk(reader);
    RevCommit commit = revWalk.parseCommit(commitId);
    RevTree tree = commit.getTree();
    TreeWalk treeWalk = new TreeWalk(reader);
    treeWalk.addTree(tree);
    treeWalk.setRecursive(false);

    while (treeWalk.next())
    {
        if (treeWalk.isSubtree())
        {
            treeWalk.enterSubtree();
        }
        else
        {
            String member = treeWalk.getPathString();
            if (member.contains(SOURCEDIR))
            {
                byte[] data = getBlob(member, curCommit);
                contents.put(member, data);
            }
        }
    }

    reader.release();

    return contents;
}
 
Example #20
Source File: SMAGit.java    From salesforce-migration-assistant with MIT License 5 votes vote down vote up
/**
 * Returns the Canonical Tree Parser representation of a commit.
 *
 * @param commit Commit in the repository.
 * @return CanonicalTreeParser representing the tree for the commit.
 * @throws IOException
 */
private CanonicalTreeParser getTree(String commit) throws IOException
{
    CanonicalTreeParser tree = new CanonicalTreeParser();
    ObjectReader reader = repository.newObjectReader();
    ObjectId head = repository.resolve(commit + "^{tree}");
    tree.reset(reader, head);
    return tree;
}
 
Example #21
Source File: JGitAPIImpl.java    From git-client-plugin with MIT License 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void appendNote(String note, String namespace) throws GitException {
    try (Repository repo = getRepository()) {
        ObjectId head = repo.resolve(HEAD); // commit to put a note on

        ShowNoteCommand cmd = git(repo).notesShow();
        cmd.setNotesRef(qualifyNotesNamespace(namespace));
        try (ObjectReader or = repo.newObjectReader();
             RevWalk walk = new RevWalk(or)) {
            cmd.setObjectId(walk.parseAny(head));
            Note n = cmd.call();

            if (n==null) {
                addNote(note,namespace);
            } else {
                ObjectLoader ol = or.open(n.getData());
                StringWriter sw = new StringWriter();
                IOUtils.copy(new InputStreamReader(ol.openStream(), UTF_8),sw);
                sw.write("\n");
                addNote(sw.toString() + normalizeNote(note), namespace);
            }
        }
    } catch (GitAPIException | IOException e) {
        throw new GitException(e);
    }
}
 
Example #22
Source File: JGitAPIImpl.java    From git-client-plugin with MIT License 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public String getTagMessage(String tagName) throws GitException {
    try (Repository repo = getRepository();
        ObjectReader or = repo.newObjectReader();
        RevWalk walk = new RevWalk(or)) {
        return walk.parseTag(repo.resolve(tagName)).getFullMessage().trim();
    } catch (IOException e) {
        throw new GitException(e);
    }
}
 
Example #23
Source File: JGitAPIImpl.java    From git-client-plugin with MIT License 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public List<String> showRevision(ObjectId from, ObjectId to, Boolean useRawOutput) throws GitException {
    try (Repository repo = getRepository();
         ObjectReader or = repo.newObjectReader();
         RevWalk w = new RevWalk(or)) {
        w.markStart(w.parseCommit(to));
        if (from!=null)
            w.markUninteresting(w.parseCommit(from));
        else
            w.setRevFilter(MaxCountRevFilter.create(1));

        List<String> r = new ArrayList<>();
        StringWriter sw = new StringWriter();
        RawFormatter f = new RawFormatter();
        try (PrintWriter pw = new PrintWriter(sw)) {
            for (RevCommit c : w) {
                // do not duplicate merge commits unless using raw output
                if (c.getParentCount()<=1 || !useRawOutput) {
                    f.format(c,null,pw,useRawOutput);
                } else {
                    // the effect of the -m option, which makes the diff produce for each parent of a merge commit
                    for (RevCommit p : c.getParents()) {
                        f.format(c,p,pw,useRawOutput);
                    }
                }

                r.addAll(Arrays.asList(sw.toString().split("\n")));
                sw.getBuffer().setLength(0);
            }
        }
        return r;
    } catch (IOException e) {
        throw new GitException(e);
    }
}
 
Example #24
Source File: SMAGit.java    From salesforce-migration-assistant with MIT License 5 votes vote down vote up
/**
 * Returns the blob information for the file at the specified path and commit
 *
 * @param repoItem
 * @param commit
 * @return
 * @throws Exception
 */
public byte[] getBlob(String repoItem, String commit) throws Exception
{
    byte[] data;

    String parentPath = repository.getDirectory().getParent();

    ObjectId commitId = repository.resolve(commit);

    ObjectReader reader = repository.newObjectReader();
    RevWalk revWalk = new RevWalk(reader);
    RevCommit revCommit = revWalk.parseCommit(commitId);
    RevTree tree = revCommit.getTree();
    TreeWalk treeWalk = TreeWalk.forPath(reader, repoItem, tree);

    if (treeWalk != null)
    {
        data = reader.open(treeWalk.getObjectId(0)).getBytes();
    }
    else
    {
        throw new IllegalStateException("Did not find expected file '" + repoItem + "'");
    }

    reader.release();

    return data;
}
 
Example #25
Source File: GitDiffHandlerV1.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
private AbstractTreeIterator getTreeIterator(Repository db, String name) throws IOException {
	final ObjectId id = db.resolve(name);
	if (id == null)
		throw new IllegalArgumentException(name);
	final CanonicalTreeParser p = new CanonicalTreeParser();
	final ObjectReader or = db.newObjectReader();
	try {
		p.reset(or, new RevWalk(db).parseTree(id));
		return p;
	} finally {
		or.close();
	}
}
 
Example #26
Source File: JGitTemplate.java    From piper with Apache License 2.0 5 votes vote down vote up
private IdentifiableResource readBlob (Repository aRepo, String aPath, String aBlobId) throws Exception {
  try (ObjectReader reader = aRepo.newObjectReader()) {
    if(aBlobId.equals(LATEST)) {
      List<IdentifiableResource> headFiles = getHeadFiles(aRepo, aPath);
      Assert.notEmpty(headFiles,"could not find: " + aPath + ":" + aBlobId);
      return headFiles.get(0);
    }
    ObjectId objectId = aRepo.resolve(aBlobId);
    Assert.notNull(objectId,"could not find: " + aPath + ":" + aBlobId);
    byte[] data = reader.open(objectId).getBytes();
    AbbreviatedObjectId abbreviated = reader.abbreviate(objectId);
    return new IdentifiableResource(aPath+":"+abbreviated.name(), new ByteArrayResource(data));
  }
}
 
Example #27
Source File: DiffCalculator.java    From diff-check with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * calculate the diff between the old revision and the new revision
 *
 * @param repoDir               the git directory
 * @param oldRev                the old revision
 * @param newRev                the new revision
 * @param includeStagedCodes    include the staged codes
 * @return
 * @throws Exception            throw Exception when error happens
 */
public List<DiffEntryWrapper> calculateDiff(
        File repoDir,
        String oldRev,
        String newRev,
        boolean includeStagedCodes) throws Exception {

    try (Git git = Git.open(repoDir);
            ObjectReader reader = git.getRepository().newObjectReader();
            RevWalk rw = new RevWalk(git.getRepository())) {

        RevCommit oldCommit = rw.parseCommit(git.getRepository().resolve(oldRev));
        RevCommit newCommit = rw.parseCommit(git.getRepository().resolve(newRev));

        List<DiffEntryWrapper> wrappers = new ArrayList<>();

        if (includeStagedCodes) {
            wrappers.addAll(doCalculateIndexedDiff(oldCommit, reader, git, repoDir));
        }

        Set<String> indexedPathSet = wrappers.stream()
                .map(wrapper -> wrapper.getNewPath())
                .collect(Collectors.toSet());

        wrappers.addAll(doCalculateCommitDiff(oldCommit, newCommit, reader, git, repoDir, indexedPathSet));

        return wrappers;
    }
}
 
Example #28
Source File: CurioGenericCiPlugin.java    From curiostack with MIT License 5 votes vote down vote up
private static CanonicalTreeParser parserForBranch(Git git, Ref branch) throws IOException {
  try (RevWalk walk = new RevWalk(git.getRepository())) {
    RevCommit commit = walk.parseCommit(branch.getObjectId());
    RevTree tree = walk.parseTree(commit.getTree().getId());

    final CanonicalTreeParser parser;
    try (ObjectReader reader = git.getRepository().newObjectReader()) {
      parser = parser(reader, tree.getId());
    }

    walk.dispose();

    return parser;
  }
}
 
Example #29
Source File: CurioGenericCiPlugin.java    From curiostack with MIT License 5 votes vote down vote up
private static Set<String> computeAffectedFilesForMaster(Git git, Project rootProject)
    throws IOException {
  ObjectId oldTreeId = git.getRepository().resolve("HEAD^{tree}");
  ObjectId newTreeId = git.getRepository().resolve("HEAD^^{tree}");

  final CanonicalTreeParser oldTreeParser;
  final CanonicalTreeParser newTreeParser;
  try (ObjectReader reader = git.getRepository().newObjectReader()) {
    oldTreeParser = parser(reader, oldTreeId);
    newTreeParser = parser(reader, newTreeId);
  }

  return computeAffectedFiles(git, oldTreeParser, newTreeParser, rootProject);
}
 
Example #30
Source File: StashApplyCommand.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
private void checkoutPath(DirCacheEntry entry, ObjectReader reader) {
	try {
		DirCacheCheckout.checkoutEntry(repo, entry, reader);
	} catch (IOException e) {
		throw new JGitInternalException(MessageFormat.format(JGitText.get().checkoutConflictWithFile, entry.getPathString()), e);
	}
}