Example usage for org.eclipse.jgit.lib Constants CHARACTER_ENCODING

List of usage examples for org.eclipse.jgit.lib Constants CHARACTER_ENCODING

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib Constants CHARACTER_ENCODING.

Prototype

String CHARACTER_ENCODING

To view the source code for org.eclipse.jgit.lib Constants CHARACTER_ENCODING.

Click Source Link

Document

Native character encoding for commit messages, file names...

Usage

From source file:org.eclipse.egit.core.internal.CompareCoreUtils.java

License:Open Source License

/**
 * Determine the encoding used by eclipse for the resource.
 *
 * @param resource// w  w  w.  j  a v a2 s .  com
 *            must be an instance of IEncodedStorage
 * @return the encoding used in Eclipse for the resource if found or null
 */
public static String getResourceEncoding(IResource resource) {
    // Get the encoding for the current version. As a matter of
    // principle one might want to use the eclipse settings for the
    // version we are retrieving as that may be defined by the
    // project settings, but there is no historic API for this.
    String charset;
    IEncodedStorage encodedStorage = ((IEncodedStorage) resource);
    try {
        charset = encodedStorage.getCharset();
        if (charset == null)
            charset = resource.getParent().getDefaultCharset();
    } catch (CoreException e) {
        charset = Constants.CHARACTER_ENCODING;
    }
    return charset;
}

From source file:org.eclipse.egit.core.op.IgnoreOperation.java

License:Open Source License

private ByteArrayInputStream asStream(String entry) throws UnsupportedEncodingException {
    return new ByteArrayInputStream(entry.getBytes(Constants.CHARACTER_ENCODING));
}

From source file:org.eclipse.egit.ui.internal.actions.IgnoreAction.java

License:Open Source License

@SuppressWarnings("restriction")
@Override//from w  ww.  ja v a  2  s  .  c  o  m
public void execute(IAction action) {
    final IResource[] resources = getSelectedResources();
    if (resources.length == 0)
        return;

    WorkspaceJob job = new WorkspaceJob(UIText.IgnoreAction_jobName) {
        public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
            monitor.beginTask(UIText.IgnoreAction_taskName, resources.length);
            try {
                for (IResource resource : resources) {
                    // TODO This is pretty inefficient; multiple ignores in
                    // the same directory cause multiple writes.

                    // NB This does the same thing in
                    // DecoratableResourceAdapter, but neither currently
                    // consult .gitignore

                    if (!Team.isIgnoredHint(resource)) {
                        addIgnore(monitor, resource);
                    }
                    monitor.worked(1);
                }
                monitor.done();
            } catch (CoreException e) {
                throw e;
            } catch (Exception e) {
                throw new CoreException(
                        new Status(IStatus.ERROR, "org.eclipse.egit.ui", UIText.IgnoreAction_error, e)); //$NON-NLS-1$
            }
            return Status.OK_STATUS;
        }

        private void addIgnore(IProgressMonitor monitor, IResource resource)
                throws UnsupportedEncodingException, CoreException {
            IContainer container = resource.getParent();
            IFile gitignore = container.getFile(new Path(Constants.GITIGNORE_FILENAME));
            String entry = "/" + resource.getName() + "\n"; //$NON-NLS-1$  //$NON-NLS-2$
            ByteArrayInputStream entryBytes = asStream(entry);

            if (gitignore.exists())
                gitignore.appendContents(entryBytes, true, true, monitor);
            else
                gitignore.create(entryBytes, true, monitor);
        }

        private ByteArrayInputStream asStream(String entry) throws UnsupportedEncodingException {
            return new ByteArrayInputStream(entry.getBytes(Constants.CHARACTER_ENCODING));
        }
    };
    job.schedule();
}

From source file:org.gitective.tests.BlobUtilsTest.java

License:Open Source License

/**
 * Get raw HEAD content at path// w ww.  j  a v  a  2  s  .c om
 *
 * @throws Exception
 */
@Test
public void getRawHeadContent() throws Exception {
    add("test.txt", "content");
    assertArrayEquals("content".getBytes(Constants.CHARACTER_ENCODING),
            BlobUtils.getRawHeadContent(new FileRepository(testRepo), "test.txt"));
}

From source file:org.kie.commons.java.nio.fs.jgit.util.JGitUtil.java

License:Apache License

public static void commit(final Git git, final String branchName, final String name, final String email,
        final String message, final TimeZone timeZone, final Date when, final Map<String, File> content) {

    final PersonIdent author = buildPersonIdent(git, name, email, timeZone, when);

    try {/*from ww w  .ja  v  a  2s  .c o m*/
        final ObjectInserter odi = git.getRepository().newObjectInserter();
        try {
            // Create the in-memory index of the new/updated issue.
            final ObjectId headId = git.getRepository().resolve(branchName + "^{commit}");
            final DirCache index = createTemporaryIndex(git, headId, content);
            final ObjectId indexTreeId = index.writeTree(odi);

            // Create a commit object
            final CommitBuilder commit = new CommitBuilder();
            commit.setAuthor(author);
            commit.setCommitter(author);
            commit.setEncoding(Constants.CHARACTER_ENCODING);
            commit.setMessage(message);
            //headId can be null if the repository has no commit yet
            if (headId != null) {
                commit.setParentId(headId);
            }
            commit.setTreeId(indexTreeId);

            // Insert the commit into the repository
            final ObjectId commitId = odi.insert(commit);
            odi.flush();

            final RevWalk revWalk = new RevWalk(git.getRepository());
            try {
                final RevCommit revCommit = revWalk.parseCommit(commitId);
                final RefUpdate ru = git.getRepository().updateRef("refs/heads/" + branchName);
                if (headId == null) {
                    ru.setExpectedOldObjectId(ObjectId.zeroId());
                } else {
                    ru.setExpectedOldObjectId(headId);
                }
                ru.setNewObjectId(commitId);
                ru.setRefLogMessage("commit: " + revCommit.getShortMessage(), false);
                final RefUpdate.Result rc = ru.forceUpdate();
                switch (rc) {
                case NEW:
                case FORCED:
                case FAST_FORWARD:
                    break;
                case REJECTED:
                case LOCK_FAILURE:
                    throw new ConcurrentRefUpdateException(JGitText.get().couldNotLockHEAD, ru.getRef(), rc);
                default:
                    throw new JGitInternalException(MessageFormat.format(JGitText.get().updatingRefFailed,
                            Constants.HEAD, commitId.toString(), rc));
                }

            } finally {
                revWalk.release();
            }
        } finally {
            odi.release();
        }
    } catch (final Throwable t) {
        throw new RuntimeException(t);
    }
}

From source file:org.moxie.utils.JGitUtils.java

License:Apache License

/**
 * Create an orphaned branch in a repository.
 * /* w ww  . ja  va2s  .  c  o m*/
 * @param repository
 * @param branchName
 * @param author
 *            if unspecified, Moxie will be the author of this new branch
 * @return true if successful
 */
public static boolean createOrphanBranch(Repository repository, String branchName, PersonIdent author) {
    boolean success = false;
    String message = "Created branch " + branchName;
    if (author == null) {
        author = new PersonIdent("Moxie", "moxie@localhost");
    }
    try {
        ObjectInserter odi = repository.newObjectInserter();
        try {
            // Create a blob object to insert into a tree
            ObjectId blobId = odi.insert(Constants.OBJ_BLOB, message.getBytes(Constants.CHARACTER_ENCODING));

            // Create a tree object to reference from a commit
            TreeFormatter tree = new TreeFormatter();
            tree.append("NEWBRANCH", FileMode.REGULAR_FILE, blobId);
            ObjectId treeId = odi.insert(tree);

            // Create a commit object
            CommitBuilder commit = new CommitBuilder();
            commit.setAuthor(author);
            commit.setCommitter(author);
            commit.setEncoding(Constants.CHARACTER_ENCODING);
            commit.setMessage(message);
            commit.setTreeId(treeId);

            // Insert the commit into the repository
            ObjectId commitId = odi.insert(commit);
            odi.flush();

            RevWalk revWalk = new RevWalk(repository);
            try {
                RevCommit revCommit = revWalk.parseCommit(commitId);
                if (!branchName.startsWith("refs/")) {
                    branchName = "refs/heads/" + branchName;
                }
                RefUpdate ru = repository.updateRef(branchName);
                ru.setNewObjectId(commitId);
                ru.setRefLogMessage("commit: " + revCommit.getShortMessage(), false);
                Result rc = ru.forceUpdate();
                switch (rc) {
                case NEW:
                case FORCED:
                case FAST_FORWARD:
                    success = true;
                    break;
                default:
                    success = false;
                }
            } finally {
                revWalk.release();
            }
        } finally {
            odi.release();
        }
    } catch (Throwable t) {
        t.printStackTrace();
    }
    return success;
}

From source file:org.moxie.utils.JGitUtils.java

License:Apache License

public static void updateGhPages(File repositoryFolder, File sourceFolder, boolean obliterate) {
    String ghpages = "refs/heads/gh-pages";
    try {/*from w ww.j a  v a2s. c o m*/
        File gitDir = FileKey.resolve(repositoryFolder, FS.DETECTED);
        Repository repository = new FileRepository(gitDir);

        ObjectId objectId = repository.resolve(ghpages);
        if (objectId == null) {
            JGitUtils.createOrphanBranch(repository, "gh-pages", null);
        }

        System.out.println("Updating gh-pages branch...");
        ObjectId headId = repository.resolve(ghpages + "^{commit}");
        ObjectInserter odi = repository.newObjectInserter();
        try {
            // Create the in-memory index of the new/updated issue.
            DirCache index = createIndex(repository, headId, sourceFolder, obliterate);
            ObjectId indexTreeId = index.writeTree(odi);

            // Create a commit object
            PersonIdent author = new PersonIdent("Moxie", "moxie@localhost");
            CommitBuilder commit = new CommitBuilder();
            commit.setAuthor(author);
            commit.setCommitter(author);
            commit.setEncoding(Constants.CHARACTER_ENCODING);
            commit.setMessage("updated pages");
            commit.setParentId(headId);
            commit.setTreeId(indexTreeId);

            // Insert the commit into the repository
            ObjectId commitId = odi.insert(commit);
            odi.flush();

            RevWalk revWalk = new RevWalk(repository);
            try {
                RevCommit revCommit = revWalk.parseCommit(commitId);
                RefUpdate ru = repository.updateRef(ghpages);
                ru.setNewObjectId(commitId);
                ru.setExpectedOldObjectId(headId);
                ru.setRefLogMessage("commit: " + revCommit.getShortMessage(), false);
                Result rc = ru.forceUpdate();
                switch (rc) {
                case NEW:
                case FORCED:
                case FAST_FORWARD:
                    break;
                case REJECTED:
                case LOCK_FAILURE:
                    throw new ConcurrentRefUpdateException(JGitText.get().couldNotLockHEAD, ru.getRef(), rc);
                default:
                    throw new JGitInternalException(MessageFormat.format(JGitText.get().updatingRefFailed,
                            ghpages, commitId.toString(), rc));
                }
            } finally {
                revWalk.release();
            }
        } finally {
            odi.release();
        }
        System.out.println("gh-pages updated.");
    } catch (Throwable t) {
        t.printStackTrace();
    }
}

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

License:Apache License

public boolean execute() {
    boolean hadEffecitiveCommit = true;
    final PersonIdent author = buildPersonIdent(git, commitInfo.getName(), commitInfo.getEmail(),
            commitInfo.getTimeZone(), commitInfo.getWhen());

    try (final ObjectInserter odi = git.getRepository().newObjectInserter()) {
        final ObjectId headId = git.getRepository().resolve(branchName + "^{commit}");

        final Optional<ObjectId> tree;
        if (content instanceof DefaultCommitContent) {
            tree = new CreateDefaultCommitTree(git, originId, odi, (DefaultCommitContent) content).execute();
        } else if (content instanceof MoveCommitContent) {
            tree = new CreateMoveCommitTree(git, originId, odi, (MoveCommitContent) content).execute();
        } else if (content instanceof CopyCommitContent) {
            tree = new CreateCopyCommitTree(git, originId, odi, (CopyCommitContent) content).execute();
        } else if (content instanceof RevertCommitContent) {
            tree = new CreateRevertCommitTree(git, originId, odi, (RevertCommitContent) content).execute();
        } else {/*from  ww  w.j a v  a2s  .com*/
            tree = Optional.empty();
        }

        if (tree.isPresent()) {
            final CommitBuilder commit = new CommitBuilder();
            commit.setAuthor(author);
            commit.setCommitter(author);
            commit.setEncoding(Constants.CHARACTER_ENCODING);
            commit.setMessage(commitInfo.getMessage());
            if (headId != null) {
                if (amend) {
                    final RevCommit previousCommit = git.resolveRevCommit(headId);
                    final List<RevCommit> p = Arrays.asList(previousCommit.getParents());
                    reverse(p);
                    commit.setParentIds(p);
                } else {
                    commit.setParentId(headId);
                }
            }
            commit.setTreeId(tree.get());

            final ObjectId commitId = odi.insert(commit);
            odi.flush();

            git.refUpdate(branchName, git.resolveRevCommit(commitId));
        } else {
            hadEffecitiveCommit = false;
        }
    } catch (final Throwable t) {
        t.printStackTrace();
        throw new RuntimeException(t);
    }
    return hadEffecitiveCommit;
}

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

License:Apache License

public static boolean commit(final Git git, final String branchName, final CommitInfo commitInfo,
        final boolean amend, final ObjectId _originId, final CommitContent content) {
    boolean hadEffecitiveCommit = true;
    final PersonIdent author = buildPersonIdent(git, commitInfo.getName(), commitInfo.getEmail(),
            commitInfo.getTimeZone(), commitInfo.getWhen());

    try {//  w ww .jav a2s. co m
        final ObjectInserter odi = git.getRepository().newObjectInserter();
        try {
            // Create the in-memory index of the new/updated issue.
            final ObjectId headId = git.getRepository().resolve(branchName + "^{commit}");

            final ObjectId originId;
            if (_originId == null) {
                originId = git.getRepository().resolve(branchName + "^{commit}");
            } else {
                originId = _originId;
            }

            final DirCache index;
            if (content instanceof DefaultCommitContent) {
                index = createTemporaryIndex(git, originId, (DefaultCommitContent) content);
            } else if (content instanceof MoveCommitContent) {
                index = createTemporaryIndex(git, originId, (MoveCommitContent) content);
            } else if (content instanceof CopyCommitContent) {
                index = createTemporaryIndex(git, originId, (CopyCommitContent) content);
            } else if (content instanceof RevertCommitContent) {
                index = createTemporaryIndex(git, originId);
            } else {
                index = null;
            }

            if (index != null) {
                final ObjectId indexTreeId = index.writeTree(odi);

                // Create a commit object
                final CommitBuilder commit = new CommitBuilder();
                commit.setAuthor(author);
                commit.setCommitter(author);
                commit.setEncoding(Constants.CHARACTER_ENCODING);
                commit.setMessage(commitInfo.getMessage());
                //headId can be null if the repository has no commit yet
                if (headId != null) {
                    if (amend) {
                        final List<ObjectId> parents = new LinkedList<ObjectId>();
                        final RevCommit previousCommit = new RevWalk(git.getRepository()).parseCommit(headId);
                        final RevCommit[] p = previousCommit.getParents();
                        for (final RevCommit revCommit : p) {
                            parents.add(0, revCommit.getId());
                        }
                        commit.setParentIds(parents);
                    } else {
                        commit.setParentId(headId);
                    }
                }
                commit.setTreeId(indexTreeId);

                // Insert the commit into the repository
                final ObjectId commitId = odi.insert(commit);
                odi.flush();

                final RevWalk revWalk = new RevWalk(git.getRepository());
                try {
                    final RevCommit revCommit = revWalk.parseCommit(commitId);
                    final RefUpdate ru = git.getRepository().updateRef("refs/heads/" + branchName);
                    if (headId == null) {
                        ru.setExpectedOldObjectId(ObjectId.zeroId());
                    } else {
                        ru.setExpectedOldObjectId(headId);
                    }
                    ru.setNewObjectId(commitId);
                    ru.setRefLogMessage("commit: " + revCommit.getShortMessage(), false);
                    final RefUpdate.Result rc = ru.forceUpdate();
                    switch (rc) {
                    case NEW:
                    case FORCED:
                    case FAST_FORWARD:
                        break;
                    case REJECTED:
                    case LOCK_FAILURE:
                        throw new ConcurrentRefUpdateException(JGitText.get().couldNotLockHEAD, ru.getRef(),
                                rc);
                    default:
                        throw new JGitInternalException(MessageFormat.format(JGitText.get().updatingRefFailed,
                                Constants.HEAD, commitId.toString(), rc));
                    }

                } finally {
                    revWalk.release();
                }
            } else {
                hadEffecitiveCommit = false;
            }
        } finally {
            odi.release();
        }
    } catch (final Throwable t) {
        throw new RuntimeException(t);
    }
    return hadEffecitiveCommit;
}

From source file:org.webcat.core.http.RequestUtils.java

License:Open Source License

/**
 * Appends a plain text string to a response, compressing it with GZIP if
 * the request supports that.//from   w  ww  .  ja v a2 s. com
 *
 * @param content the content string
 * @param request the request
 * @param response the response
 * @throws IOException if an I/O error occurs
 */
public static void sendPlainText(String content, WORequest request, WOResponse response) throws IOException {
    byte[] raw = content.getBytes(Constants.CHARACTER_ENCODING);
    response.setHeader("text/plain;charset=" + Constants.CHARACTER_ENCODING, HttpSupport.HDR_CONTENT_TYPE);
    sendBytes(raw, request, response);
}