Example usage for org.eclipse.jgit.util FileUtils mkdirs

List of usage examples for org.eclipse.jgit.util FileUtils mkdirs

Introduction

In this page you can find the example usage for org.eclipse.jgit.util FileUtils mkdirs.

Prototype

public static void mkdirs(File d, boolean skipExisting) throws IOException 

Source Link

Document

Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories.

Usage

From source file:MySmartApply.java

License:Eclipse Distribution License

private File getFile(String path, boolean create) throws PatchApplyException {
    File f = new File(this.local_path + path);
    if (create)//from w  w w.j  a  v  a2s  . co  m
        try {
            File parent = f.getParentFile();
            FileUtils.mkdirs(parent, true);
            FileUtils.createNewFile(f);
        } catch (IOException e) {
            throw new PatchApplyException(MessageFormat.format(JGitText.get().createNewFileFailed, f), e);
        }
    return f;
}

From source file:bench.MainJgit.java

License:BSD License

/**
 *//*  w w  w.  j a v  a 2  s .c o  m*/
public static void main(final String[] args) throws Exception {

    final File baseDir = new File(".");

    final File folder = new File(baseDir, "target");

    final File workspace = new File(folder, "workspace");

    FileUtils.delete(workspace, FileUtils.IGNORE_ERRORS | FileUtils.RECURSIVE);

    FileUtils.mkdirs(workspace, true);

    final String remoteURI = "git@github.com:barchart/barchart-jenkins-tester.git";
    final String remoteName = "archon";
    final String remoteBranch = "master";

    final String localBranch = "cascade";

    {
        final Git git = PluginScmGit.doClone(workspace, remoteURI, remoteName);
        final Repository repo = git.getRepository();
        System.out.println("repo " + repo);
    }

    {
        final CheckoutResult result = PluginScmGit.doCheckout(workspace, localBranch, remoteName, remoteBranch);
        System.out.println("checkout " + result.getStatus());
    }

    {
        final CheckoutResult result = PluginScmGit.doCheckout(workspace, localBranch, remoteName, remoteBranch);
        System.out.println("checkout " + result.getStatus());
    }

    {
        final RefSpec fetchSpec = PluginScmGit.refFetch(remoteBranch, remoteName, remoteBranch);

        final FetchResult fetchResult = PluginScmGit.doFetch(workspace, remoteName, fetchSpec);
        System.out.println("fetch satus: " + fetchResult.getAdvertisedRefs());

        final String refHead = PluginScmGit.refHeads(remoteBranch);

        final Ref remoteHead = fetchResult.getAdvertisedRef(refHead);

        final ObjectId commit = remoteHead.getObjectId();

        final MergeResult mergeResult = PluginScmGit.doMerge(workspace, commit);

        final MergeStatus mergeStatus = mergeResult.getMergeStatus();
        System.out.println("merge status: " + mergeStatus);

    }

}

From source file:com.barchart.jenkins.cascade.PluginScmGit.java

License:BSD License

/**
 * Destroy workspace and clone from scratch.
 *///from w  w  w.  j ava  2  s  . c  o m
public static Git doClone(final File workspace, final String uri, final String remote) {
    try {
        FileUtils.delete(workspace, FileUtils.RECURSIVE | FileUtils.IGNORE_ERRORS);
        FileUtils.mkdirs(workspace, true);
        return Git.cloneRepository().setURI(uri).setRemote(remote).setNoCheckout(true).setDirectory(workspace)
                .call();
    } catch (final Throwable e) {
        throw new RuntimeException(e);
    }
}

From source file:de.hub.srcrepo.ProjectUtil.java

License:Open Source License

private static void closeMissingProject(IProject p, File projectFile, IProgressMonitor monitor)
        throws CoreException {
    // Create temporary .project file so it can be closed
    boolean closeFailed = false;
    File projectRoot = projectFile.getParentFile();
    if (!projectRoot.isFile()) {
        boolean hasRoot = projectRoot.exists();
        try {/*from w  w w. ja v  a  2  s .c o m*/
            if (!hasRoot)
                FileUtils.mkdirs(projectRoot, true);
            if (projectFile.createNewFile())
                p.close(new SubProgressMonitor(monitor, 1));
            else
                closeFailed = true;
        } catch (IOException e) {
            closeFailed = true;
        } finally {
            // Clean up created .project file
            try {
                FileUtils.delete(projectFile, FileUtils.RETRY | FileUtils.SKIP_MISSING);
            } catch (IOException e) {
                closeFailed = true;
            }
            // Clean up created folder
            if (!hasRoot)
                try {
                    FileUtils.delete(projectRoot,
                            FileUtils.RETRY | FileUtils.SKIP_MISSING | FileUtils.RECURSIVE);
                } catch (IOException e) {
                    closeFailed = true;
                }
        }
    } else
        closeFailed = true;
    // Delete projects that can't be closed
    if (closeFailed)
        p.delete(false, true, new SubProgressMonitor(monitor, 1));
}

From source file:org.eclipse.emf.compare.diagram.papyrus.tests.egit.fixture.GitTestRepository.java

License:Open Source License

/**
 * Create a file or get an existing one/*from  w  w  w  .  ja v  a2 s. co  m*/
 *
 * @param project
 *            instance of project inside with file will be created
 * @param name
 *            name of file
 * @return nearly created file
 * @throws IOException
 */
public File createFile(IProject project, String name) throws IOException {
    String path = project.getLocation().append(name).toOSString();
    int lastSeparator = path.lastIndexOf(File.separator);
    FileUtils.mkdirs(new File(path.substring(0, lastSeparator)), true);

    File file = new File(path);
    if (!file.exists()) {
        FileUtils.createNewFile(file);
    }

    return file;
}

From source file:org.fedoraproject.eclipse.packager.api.LocalFedoraPackagerProjectCreator.java

License:Open Source License

/**
 * Creates project structure inside the base project
 *
 * @throws CoreException/*ww w .  j  av a  2s. c  o m*/
 * @throws IOException
 * @throws WrongRepositoryStateException
 * @throws JGitInternalException
 * @throws ConcurrentRefUpdateException
 * @throws NoMessageException
 * @throws NoHeadException
 * @throws NoFilepatternException
 *
 */
public void createProjectStructure()
        throws NoFilepatternException, NoHeadException, NoMessageException, ConcurrentRefUpdateException,
        JGitInternalException, WrongRepositoryStateException, IOException, CoreException {

    File directory = new File(project.getLocation().toString());
    FileUtils.mkdirs(directory, true);
    directory.getCanonicalFile();

    InitCommand command = new InitCommand();
    command.setDirectory(directory);
    command.setBare(false);
    repository = command.call().getRepository();

    git = new Git(repository);

    for (File file : directory.listFiles()) {
        String name = file.getName();

        if (name.contains(".spec")) { //$NON-NLS-1$
            git.add().addFilepattern(name).call();
        }

        if (name.equals(".gitignore")) { //$NON-NLS-1$
            git.add().addFilepattern(name).call();
        }
    }

    // do the first commit
    git.commit().setMessage(FedoraPackagerText.LocalFedoraPackagerProjectCreator_FirstCommit).call();

    // Add created repository to the list of Git repositories so that it
    // shows up in the Git repositories view.
    final RepositoryUtil config = org.eclipse.egit.core.Activator.getDefault().getRepositoryUtil();
    config.addConfiguredRepository(repository.getDirectory());
}

From source file:org.fedoraproject.eclipse.packager.local.api.LocalFedoraPackagerProjectCreator.java

License:Open Source License

/**
 * Initialize a local git repository in project location
 *
 * @throws IOException//w  w  w .  j  a  va  2  s.  c o m
 * @return File directory of the git repository
 */
private File createLocalGitRepo() throws IOException {
    File directory = new File(project.getLocation().toString());
    FileUtils.mkdirs(directory, true);
    directory.getCanonicalFile();

    InitCommand command = new InitCommand();
    command.setDirectory(directory);
    command.setBare(false);
    repository = command.call().getRepository();

    git = new Git(repository);
    return directory;
}

From source file:org.jboss.tools.openshift.egit.internal.test.util.TestRepository.java

License:Open Source License

/**
 * Create a file or get an existing one/*from   w w w.j  av  a2s  .  c  om*/
 * 
 * @param project
 *            instance of project inside with file will be created
 * @param name
 *            name of file
 * @return nearly created file
 * @throws IOException
 */
public File createFile(IProject project, String name) throws IOException {
    String path = project.getLocation().append(name).toOSString();
    int lastSeparator = path.lastIndexOf(File.separator);
    FileUtils.mkdirs(new File(path.substring(0, lastSeparator)), true);

    File file = new File(path);
    if (!file.exists())
        FileUtils.createNewFile(file);

    return file;
}

From source file:org.jboss.tools.openshift.egit.internal.test.util.TestRepository.java

License:Open Source License

private void write(final File file, final String data) throws IOException {
    FileUtils.mkdirs(file.getParentFile(), true);
    Writer w = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
    try {/*from w  ww  .j a va 2 s  .  co m*/
        w.write(data);
    } finally {
        w.close();
    }
}

From source file:playRepository.GitRepository.java

License:Apache License

/**
 * Clones a local repository.//w w w  .  ja  va2 s.  co m
 *
 * This doesn't copy Git objects but hardlink them to save disk space.
 *
 * @param originalProject
 * @param forkProject
 * @throws IOException
 */
protected static void cloneHardLinkedRepository(Project originalProject, Project forkProject)
        throws IOException {
    Repository origin = GitRepository.buildGitRepository(originalProject);
    Repository forked = GitRepository.buildGitRepository(forkProject);
    forked.create();

    final Path originObjectsPath = Paths.get(new File(origin.getDirectory(), "objects").getAbsolutePath());
    final Path forkedObjectsPath = Paths.get(new File(forked.getDirectory(), "objects").getAbsolutePath());

    // Hardlink files .git/objects/ directory to save disk space,
    // but copy .git/info/alternates because the file can be modified.
    SimpleFileVisitor<Path> visitor = new SimpleFileVisitor<Path>() {
        public FileVisitResult visitFile(Path file, BasicFileAttributes attr) throws IOException {
            Path newPath = forkedObjectsPath.resolve(originObjectsPath.relativize(file.toAbsolutePath()));
            if (file.equals(forkedObjectsPath.resolve("/info/alternates"))) {
                Files.copy(file, newPath);
            } else {
                FileUtils.mkdirs(newPath.getParent().toFile(), true);
                Files.createLink(newPath, file);
            }
            return java.nio.file.FileVisitResult.CONTINUE;
        }
    };
    Files.walkFileTree(originObjectsPath, visitor);

    // Import refs.
    for (Map.Entry<String, Ref> entry : origin.getAllRefs().entrySet()) {
        RefUpdate updateRef = forked.updateRef(entry.getKey());
        Ref ref = entry.getValue();
        if (ref.isSymbolic()) {
            updateRef.link(ref.getTarget().getName());
        } else {
            updateRef.setNewObjectId(ref.getObjectId());
            updateRef.update();
        }
    }
}