Example usage for org.eclipse.jgit.api RmCommand RmCommand

List of usage examples for org.eclipse.jgit.api RmCommand RmCommand

Introduction

In this page you can find the example usage for org.eclipse.jgit.api RmCommand RmCommand.

Prototype

public RmCommand(Repository repo) 

Source Link

Document

Constructor for RmCommand.

Usage

From source file:org.eclipse.ptp.internal.rdt.sync.git.core.GitSyncFileFilter.java

License:Open Source License

@Override
public void saveFilter() throws IOException {
    Repository repo = jgitRepo.getRepository();
    File exclude = repo.getFS().resolve(repo.getDirectory(), Constants.INFO_EXCLUDE);
    exclude.getParentFile().mkdirs();/*from  www.  j a v a 2s.  c o  m*/
    FileOutputStream file = new FileOutputStream(exclude);
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(file, Constants.CHARSET));
    try {
        for (AbstractIgnoreRule rule : rules) {
            out.write(rule.toString());
            out.newLine();
        }
    } finally {
        out.close();
    }
    final RmCommand rmCommand = new RmCommand(repo);
    rmCommand.setCached(true);
    for (String fileName : getIgnoredFiles(null)) {
        rmCommand.addFilepattern(fileName);
    }
    try {
        rmCommand.call();
    } catch (NoFilepatternException e) {
        new IOException(e); // TODO: a bit ugly to wrap it into IOExcpetion
    } catch (GitAPIException e) {
        new IOException(e);
    }
}

From source file:org.eclipse.ptp.internal.rdt.sync.git.core.JGitRepo.java

License:Open Source License

/**
 * Commit files in working directory.// w ww .  j  ava2 s .com
 * 
 * assumes that no files are in conflict (do not call during merge)
 *
 * @param monitor
 *
 * @return whether any changes were committed
 * @throws GitAPIException
 *          on JGit-specific problems
 * @throws IOException
 *          on file system problems
 */
public boolean commit(IProgressMonitor monitor) throws GitAPIException, IOException {
    RecursiveSubMonitor subMon = RecursiveSubMonitor.convert(monitor, 10);

    assert (!inUnresolvedMergeState());
    try {
        DiffFiles diffFiles = fileFilter.getDiffFiles();

        // Create and add an empty file to all synchronized directories to force sync'ing of empty directories
        for (String dirName : diffFiles.dirSet) {
            IPath emptyFilePath = new Path(this.getRepository().getWorkTree().getAbsolutePath());
            emptyFilePath = emptyFilePath.append(dirName);
            // Bug 439609 - directory may have been deleted
            File emptyFileDir = new File(emptyFilePath.toOSString());
            if (!emptyFileDir.exists()) {
                continue;
            }
            emptyFilePath = emptyFilePath.append(EMPTY_FILE_NAME);
            File emptyFile = new File(emptyFilePath.toOSString());
            boolean fileWasCreated = emptyFile.createNewFile();
            if (fileWasCreated) {
                diffFiles.added.add(emptyFilePath.toString());
            }
        }

        subMon.subTask(Messages.JGitRepo_2);
        if (!diffFiles.added.isEmpty()) {
            final AddCommand addCommand = git.add();
            //Bug 401161 doesn't matter here because files are already filtered anyhow. It would be OK
            //if the tree iterator would always return false in isEntryIgnored
            addCommand.setWorkingTreeIterator(new SyncFileTreeIterator(git.getRepository(), fileFilter));
            for (String fileName : diffFiles.added) {
                addCommand.addFilepattern(fileName);
            }
            addCommand.call();
        }
        subMon.worked(3);

        subMon.subTask(Messages.JGitRepo_3);
        if (!diffFiles.removed.isEmpty()) {
            final RmCommand rmCommand = new RmCommand(git.getRepository());
            rmCommand.setCached(true);
            for (String fileName : diffFiles.removed) {
                rmCommand.addFilepattern(fileName);
            }
            rmCommand.call();
        }
        subMon.worked(3);

        // Check if a commit is required.
        subMon.subTask(Messages.JGitRepo_4);
        if (anyDiffInIndex() || inMergeState()) {
            final CommitCommand commitCommand = git.commit();
            commitCommand.setMessage(GitSyncService.commitMessage);
            commitCommand.call();
            return true;
        } else {
            return false;
        }
    } finally {
        if (monitor != null) {
            monitor.done();
        }
    }
}

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.//w ww .j a v  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);

    }

}