Example usage for org.eclipse.jgit.revwalk RevCommit getEncoding

List of usage examples for org.eclipse.jgit.revwalk RevCommit getEncoding

Introduction

In this page you can find the example usage for org.eclipse.jgit.revwalk RevCommit getEncoding.

Prototype

public final Charset getEncoding() 

Source Link

Document

Determine the encoding of the commit message buffer.

Usage

From source file:com.chungkwong.jgitgui.CommitTreeItem.java

License:Open Source License

@Override
public Node getContentPage() {
    RevCommit rev = (RevCommit) getValue();
    Repository repository = ((Git) getParent().getParent().getValue()).getRepository();
    SplitPane page = new SplitPane();
    page.setOrientation(Orientation.VERTICAL);
    StringBuilder buf = new StringBuilder();
    buf.append(java.util.ResourceBundle.getBundle("com/chungkwong/jgitgui/text").getString("PARENTS:"));
    for (int i = 0; i < rev.getParentCount(); i++)
        buf.append(rev.getParent(i)).append('\n');
    buf.append(java.util.ResourceBundle.getBundle("com/chungkwong/jgitgui/text").getString("MESSAGE:"));
    buf.append(rev.getFullMessage());/*from  www  . j a  v a 2 s  .  com*/
    TextArea msg = new TextArea(buf.toString());
    msg.setEditable(false);
    page.getItems().add(msg);
    SplitPane fileViewer = new SplitPane();
    fileViewer.setOrientation(Orientation.HORIZONTAL);
    TreeView tree = new TreeView(new TreeItem());
    tree.setShowRoot(false);
    TextArea content = new TextArea();
    content.setEditable(false);
    try (TreeWalk walk = new TreeWalk(repository)) {
        walk.addTree(rev.getTree());
        walk.setRecursive(true);
        LinkedList<TreeItem> items = new LinkedList<>();
        items.add(tree.getRoot());
        while (walk.next()) {
            TreeItem item = new FileTreeItem(walk.getObjectId(0), walk.getPathString());
            /*while(walk.getDepth()<items.size()-1)
               items.removeLast();
            if(walk.getDepth()>items.size()-1)
               items.addLast(item);*/
            items.getLast().getChildren().add(item);
        }
    } catch (Exception ex) {
        Logger.getLogger(CommitTreeItem.class.getName()).log(Level.SEVERE, null, ex);
        Util.informUser(ex);
    }
    tree.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
        @Override
        public void changed(ObservableValue ov, Object t, Object t1) {
            if (t1 != null) {
                try {
                    ObjectLoader obj = repository.open(((FileTreeItem) t1).getId());
                    if (obj.getType() != Constants.OBJ_TREE) {
                        StringBuilder buf = new StringBuilder();
                        BufferedReader in = new BufferedReader(
                                new InputStreamReader(obj.openStream(), rev.getEncoding()));
                        content.setText(in.lines().collect(Collectors.joining("\n")));
                    }
                } catch (Exception ex) {
                    Logger.getLogger(CommitTreeItem.class.getName()).log(Level.SEVERE, null, ex);
                    Util.informUser(ex);
                }
            }
        }
    });
    fileViewer.getItems().add(tree);
    fileViewer.getItems().add(content);
    page.getItems().add(fileViewer);
    page.setDividerPositions(0.2, 0.8);
    return page;
}

From source file:com.google.gerrit.server.schema.Schema_146.java

License:Apache License

private void rewriteUserBranch(Repository repo, RevWalk rw, ObjectInserter oi, ObjectId emptyTree, Ref ref,
        Account account) throws IOException {
    ObjectId current = createInitialEmptyCommit(oi, emptyTree, account.getRegisteredOn());

    rw.reset();/*from ww w . j  av  a 2  s .  c  om*/
    rw.sort(RevSort.TOPO);
    rw.sort(RevSort.REVERSE, true);
    rw.markStart(rw.parseCommit(ref.getObjectId()));

    RevCommit c;
    while ((c = rw.next()) != null) {
        if (isInitialEmptyCommit(emptyTree, c)) {
            return;
        }

        CommitBuilder cb = new CommitBuilder();
        cb.setParentId(current);
        cb.setTreeId(c.getTree());
        cb.setAuthor(c.getAuthorIdent());
        cb.setCommitter(c.getCommitterIdent());
        cb.setMessage(c.getFullMessage());
        cb.setEncoding(c.getEncoding());
        current = oi.insert(cb);
    }

    oi.flush();

    RefUpdate ru = repo.updateRef(ref.getName());
    ru.setExpectedOldObjectId(ref.getObjectId());
    ru.setNewObjectId(current);
    ru.setForceUpdate(true);
    ru.setRefLogIdent(serverIdent);
    ru.setRefLogMessage(getClass().getSimpleName(), true);
    Result result = ru.update();
    if (result != Result.FORCED) {
        throw new IOException(String.format("Failed to update ref %s: %s", ref.getName(), result.name()));
    }
}

From source file:jbenchmarker.trace.git.model.Commit.java

License:Open Source License

public Commit(String id, RevCommit co, Set<String> children) {
    this.id = id;
    this.author = new Person(co.getAuthorIdent());
    this.committer = new Person(co.getCommitterIdent());
    this.encoding = co.getEncoding().name();
    this.message = co.getFullMessage();
    this.parentsIds = new LinkedList<String>();
    for (RevCommit p : co.getParents()) {
        parentsIds.add(ObjectId.toString(p));
    }/*from ww w  .  j a  v a2  s  .  com*/
    this.childrenIds = (children == null) ? new LinkedList<String>() : new LinkedList<String>(children);
}

From source file:org.test.RewriteGitHistory.java

License:Apache License

/**
 * Here we convert the branch to a new branch. We first extract the ordered
 * list of commits.//from  ww  w. ja v a 2  s  . c om
 * 
 * Then we iterate over the commits and at each step we create a new commit.
 * 
 * The new commit references the same tree but has a different commit
 * message (strips out the git-svn-id tags) and the authors are converted
 * from subversion users to be the GitUsers.
 * @param targetBranch 
 * 
 * @param branchName
 * @param newBranchName
 * @throws JGitInternalException
 * @throws RefAlreadyExistsException
 * @throws RefNotFoundException
 * @throws InvalidRefNameException
 * @throws NoHeadException
 * @throws IOException
 */
public void convertBranch(String sourceBranchName, String targetBranch) throws JGitInternalException,
        RefAlreadyExistsException, RefNotFoundException, InvalidRefNameException, NoHeadException, IOException {

    // start on the source branch
    git.checkout().setName(sourceBranchName).call();

    // extract all history
    Iterable<RevCommit> revIter = git.log().call();

    List<RevCommit> oldestToNewestCommits = new ArrayList<RevCommit>();

    for (RevCommit commit : revIter) {

        // reverse so we can replay on the new branch
        oldestToNewestCommits.add(0, commit);

    }

    // first commit
    RevCommit firstCommit = oldestToNewestCommits.get(0);

    /*
     * Create the new branch pointed at the first commit from the sourceBranch
     * 
     */
    Ref ref = git.checkout().setCreateBranch(true).setName(targetBranch).setStartPoint(firstCommit).call();

    ObjectId newBaseId = ref.getObjectId();

    for (RevCommit revCommit : oldestToNewestCommits) {

        printCommit(revCommit);

        PersonIdent author = commitFilter.filterPersonIdent(revCommit.getAuthorIdent());

        PersonIdent committer = commitFilter.filterPersonIdent(revCommit.getAuthorIdent());

        CommitBuilder commitBuilder = new CommitBuilder();

        commitBuilder.setParentId(newBaseId);
        commitBuilder.setTreeId(revCommit.getTree().getId());
        commitBuilder.setAuthor(author);
        commitBuilder.setCommitter(committer);
        commitBuilder.setEncoding(revCommit.getEncoding());
        commitBuilder.setMessage(commitFilter.filterCommitMessage(revCommit.getFullMessage()));

        newBaseId = executeCommit(commitBuilder);

    }

}

From source file:org.test.RewriteGitHistory.java

License:Apache License

/**
 * In this case the svn repository has been cloned but there are no seperate
 * branches. Master points at the root with all the directories contained
 * beneath it./*from w w  w. j  av  a 2 s.c  o  m*/
 * 
 * What we want to accomplish is to create new commits for each 'branch'
 * that will contain only their contents.
 * 
 * We do not erase or modify the original commits.
 * 
 * We locate all of the commits that contain the includePath.
 * 
 * Then we locate the first commit that included it. We create new commits for each of the existing commits
 * but we modify the tree to only contain the matched tree of each commit.
 * @throws NoHeadException 
 * @throws WrongRepositoryStateException 
 * @throws ConcurrentRefUpdateException 
 * @throws NoMessageException 
 * @throws NoFilepatternException 
 * 
 * 
 */
public void extractBranch(String sourceBranchName, String newBranchName, String includePath)
        throws JGitInternalException, RefAlreadyExistsException, RefNotFoundException, InvalidRefNameException,
        AmbiguousObjectException, IOException, NoHeadException, NoMessageException,
        ConcurrentRefUpdateException, WrongRepositoryStateException, NoFilepatternException {

    Ref branchId = git.checkout().setName(sourceBranchName).call();

    RevWalk rwalk = new RevWalk(repository);

    RevCommit tip = rwalk.lookupCommit(branchId.getObjectId());

    rwalk.markStart(tip);

    rwalk.setTreeFilter(PathFilter.create(includePath));

    Iterator<RevCommit> commitIter = rwalk.iterator();

    List<CommitBuilder> newBranchCommits = new ArrayList<CommitBuilder>();

    RevCommit branchPoint = null;

    while (commitIter.hasNext()) {

        RevCommit commit = (RevCommit) commitIter.next();

        printCommit(commit);

        final TreeWalk walk = new TreeWalk(repository);
        walk.setRecursive(false); //only look at the top level entries in the commit tree
        walk.addTree(commit.getTree().getId());

        // match all of the paths on this commit
        List<ObjectId> matchedPaths = new ArrayList<ObjectId>();

        while (walk.next()) {
            final FileMode mode = walk.getFileMode(0);
            //            if (mode == FileMode.TREE)
            //               log.info("0");
            //            log.info(mode.toString());
            //            log.info(" ");

            if (mode == FileMode.TREE) {

                String path = walk.getPathString();

                if (path.startsWith(includePath)) {
                    log.info("matched " + Constants.typeString(mode.getObjectType()) + " sha1: "
                            + walk.getObjectId(0).name() + " path: " + walk.getPathString());

                    matchedPaths.add(walk.getObjectId(0));

                    branchPoint = commit;
                } else {
                    log.debug("excluded " + Constants.typeString(mode.getObjectType()) + " sha1: "
                            + walk.getObjectId(0).name() + " path: " + walk.getPathString());
                }
            } else {
                log.debug("excluded " + Constants.typeString(mode.getObjectType()) + " sha1: "
                        + walk.getObjectId(0).name() + " path: " + walk.getPathString());
            }

        }

        if (matchedPaths.size() > 1) {
            // create a tree to hold the tree
            log.warn("multiple matches found for path = " + includePath + " commit = " + commit.getId().name());
        } else if (matchedPaths.size() == 1) {

            PersonIdent author = commitFilter.filterPersonIdent(commit.getAuthorIdent());

            PersonIdent committer = commitFilter.filterPersonIdent(commit.getAuthorIdent());

            CommitBuilder commitBuilder = new CommitBuilder();

            // set parent id later once we have the full path
            //            commitBuilder.setParentId(newBaseId);
            commitBuilder.setTreeId(matchedPaths.get(0));
            commitBuilder.setAuthor(author);
            commitBuilder.setCommitter(committer);
            commitBuilder.setEncoding(commit.getEncoding());
            commitBuilder.setMessage(commitFilter.filterCommitMessage(commit.getFullMessage()));

            newBranchCommits.add(0, commitBuilder);
        } else {
            // do nothing
        }

    }

    // done with the commits.
    ObjectId parent = branchPoint;

    if (branchPoint.getParentCount() > 0)
        parent = branchPoint.getParent(0);

    Ref branch = git.checkout().setName(newBranchName).setCreateBranch(true).setStartPoint(parent.name())
            .call();

    // first step is that we need to delete all of the files that do not match the pattern
    RmCommand rmCommand = new RmCommand(repository);

    File workTree = git.getRepository().getWorkTree();

    // only get the top level files and directories

    File[] topLevelFiles = workTree.listFiles();

    for (File file : topLevelFiles) {

        if (!file.getName().startsWith(includePath)) {
            log.info("removing file = " + file.getAbsolutePath());
            rmCommand.addFilepattern(file.getAbsolutePath());
        }
    }

    rmCommand.call();

    RevCommit ref = git.commit().setAuthor(selfCommitIdent).setCommitter(selfCommitIdent)
            .setMessage("Delete uneeded content from newly extracted branch '" + newBranchName + "'").call();

    ObjectId parentId = ref.getId();

    for (CommitBuilder commitBuilder : newBranchCommits) {

        commitBuilder.setParentId(parentId);

        parentId = executeCommit(commitBuilder);

    }

}

From source file:org.uberfire.java.nio.fs.jgit.util.commands.SubdirectoryClone.java

License:Apache License

private CommitBuilder generateNewCommit(final Map<ObjectId, ObjectId> commitMap, final RevCommit commit,
        final ObjectId newCommitTree) {
    final CommitBuilder commitBuilder = new CommitBuilder();
    commitBuilder.setAuthor(commit.getAuthorIdent());
    commitBuilder.setCommitter(commit.getCommitterIdent());
    commitBuilder.setTreeId(newCommitTree);
    commitBuilder.setMessage(commit.getFullMessage());
    commitBuilder.setEncoding(commit.getEncoding());
    final ObjectId[] newParentIds = closestMappedAncestorOrSelf(commitMap, commit.getParents());
    if (newParentIds.length > 0) {
        commitBuilder.setParentIds(newParentIds);
    }//ww w.  j a  v a 2s  .c  om

    return commitBuilder;
}