Example usage for org.eclipse.jgit.api Git open

List of usage examples for org.eclipse.jgit.api Git open

Introduction

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

Prototype

public static Git open(File dir) throws IOException 

Source Link

Document

Open repository

Usage

From source file:org.ms123.common.git.GitServiceImpl.java

License:Open Source License

public void pull(@PName("name") String name) throws RpcException {
    try {// w  ww  .  j  av  a  2s . c  o m
        String gitSpace = System.getProperty("git.repos");
        File dir = new File(gitSpace, name);
        if (!dir.exists()) {
            throw new RpcException(ERROR_FROM_METHOD, 100, "GitService.pull:Repo(" + name + ") not exists");
        }
        Git git = Git.open(dir);
        PullCommand pull = git.pull();
        PullResult result = pull.call();
        debug(result.toString());
        return;
    } catch (Exception e) {
        throw new RpcException(ERROR_FROM_METHOD, INTERNAL_SERVER_ERROR, "GitService.pull:", e);
    } finally {
    }
}

From source file:org.ms123.common.git.GitServiceImpl.java

License:Open Source License

public void add(@PName("name") String name, @PName("pattern") String pattern) throws RpcException {
    try {/*  ww w. j a v a2 s . com*/
        String gitSpace = System.getProperty("git.repos");
        File dir = new File(gitSpace, name);
        if (!dir.exists()) {
            throw new RpcException(ERROR_FROM_METHOD, 100, "GitService.add:Repo(" + name + ") not exists");
        }
        Git git = Git.open(dir);
        AddCommand add = git.add();
        add.addFilepattern(pattern);
        add.call();
        return;
    } catch (Exception e) {
        throw new RpcException(ERROR_FROM_METHOD, INTERNAL_SERVER_ERROR, "GitService.add:", e);
    } finally {
    }
}

From source file:org.ms123.common.git.GitServiceImpl.java

License:Open Source License

public void rm(@PName("name") String name, @PName("pattern") String pattern) throws RpcException {
    try {/*from w  ww.j  a  va  2  s .c  om*/
        String gitSpace = System.getProperty("git.repos");
        File dir = new File(gitSpace, name);
        if (!dir.exists()) {
            throw new RpcException(ERROR_FROM_METHOD, 100, "GitService.rm:Repo(" + name + ") not exists");
        }
        Git git = Git.open(dir);
        RmCommand rm = git.rm();
        rm.addFilepattern(pattern);
        rm.call();
        return;
    } catch (Exception e) {
        throw new RpcException(ERROR_FROM_METHOD, INTERNAL_SERVER_ERROR, "GitService.rm:", e);
    } finally {
    }
}

From source file:org.ms123.common.git.GitServiceImpl.java

License:Open Source License

public void addRemoteOrigin(@PName("name") String name, @PName("url") String url) {
    try {// ww w  .ja  v  a  2s . c o m
        String gitSpace = System.getProperty("git.repos");
        File dir = new File(gitSpace, name);
        if (!dir.exists()) {
            throw new RpcException(ERROR_FROM_METHOD, 100,
                    "GitService.setAddRemoteOrigin:Repo(" + name + ") not exists");
        }
        Git git = Git.open(dir);
        StoredConfig config = git.getRepository().getConfig();
        config.setString("remote", "origin", "url", url);
        config.save();
    } catch (Exception e) {
        throw new RpcException(ERROR_FROM_METHOD, INTERNAL_SERVER_ERROR, "GitService.addRemoteOrigin:", e);
    } finally {
    }
}

From source file:org.n52.wps.repository.git.GitAlgorithmRepositoryTest.java

License:Open Source License

@Test
public void initLocalGitRepository() throws IOException, GitAPIException {
    File file = cleanRepository.resolve("hello_world.txt").toFile();
    file.createNewFile();//from   ww  w . j  av a  2 s .  c om

    Git repoGit = Git.open(cleanRepository.toFile());
    repoGit.add().addFilepattern(file.getPath());
    repoGit.commit().setMessage("initial commit").call();

    File wc = testRoot.newFolder("workingCopy");

    Git wcGit = Git.cloneRepository().setURI(cleanRepository.toUri().toString()).setDirectory(wc).call();

    int i = 0;
    Iterator<RevCommit> commits = wcGit.log().all().call().iterator();
    while (commits.hasNext()) {
        i++;
        commits.next();
    }
    MatcherAssert.assertThat(i, Is.is(1));
}

From source file:org.nmrfx.project.GUIProject.java

boolean gitCommit() {
    boolean didSomething = false;
    commitActive = true;//from   w ww .ja  v  a  2s.c  om
    try {
        if (git == null) {
            try {
                git = Git.open(projectDir.toFile());
            } catch (IOException ioE) {
                git = Git.init().setDirectory(projectDir.toFile()).call();
                writeIgnore();
                System.out.println("gitinited");
            }
        }

        DirCache index = git.add().addFilepattern(".").call();
        Status status = git.status().call();
        System.out.println("status " + status.isClean() + " " + status.hasUncommittedChanges());
        StringBuilder sBuilder = new StringBuilder();
        Set<String> actionMap = new HashSet<>();
        if (!status.isClean() || status.hasUncommittedChanges()) {
            Set<String> addedFiles = status.getAdded();
            for (String addedFile : addedFiles) {
                String action = "add:" + Paths.get(addedFile).getName(0);
                actionMap.add(action);
                System.out.println("added " + addedFile);
            }
            Set<String> changedFiles = status.getChanged();
            for (String changedFile : changedFiles) {
                String action = "change:" + Paths.get(changedFile).getName(0);
                actionMap.add(action);
                System.out.println("changed " + changedFile);
            }
            Set<String> removedFiles = status.getRemoved();
            for (String removedFile : removedFiles) {
                System.out.println("removed " + removedFile);
                String action = "remove:" + Paths.get(removedFile).getName(0);
                actionMap.add(action);
                git.rm().addFilepattern(removedFile).call();
            }
            Set<String> missingFiles = status.getMissing();
            for (String missingFile : missingFiles) {
                System.out.println("missing " + missingFile);
                String action = "missing:" + Paths.get(missingFile).getName(0);
                actionMap.add(action);
                git.rm().addFilepattern(missingFile).call();
            }
            actionMap.stream().forEach(action -> sBuilder.append(action).append(","));
            RevCommit commit = git.commit().setMessage(sBuilder.toString()).call();
            didSomething = true;
        }
    } catch (GitAPIException ex) {
        Logger.getLogger(GUIProject.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        // fixme, should we do this after each commit, or leave git open
        git.close();
        git = null;
        commitActive = false;
    }
    return didSomething;
}

From source file:org.nuxeo.lang.ext.LangExtAssistantRoot.java

License:Open Source License

@Override
protected void initialize(Object... args) {
    super.initialize(args);
    if (mergedProp.isEmpty()) {
        try {/*from w w  w .ja  v  a  2 s.  c  om*/
            File localGitRepo = new File(LOCAL_PATH);
            if (!localGitRepo.exists()) {
                Git.cloneRepository().setURI(GIT_REPO_REMOTE_PATH).setDirectory(localGitRepo).call();
            }
            git = Git.open(localGitRepo);
            localRepo = git.getRepository();
            Properties enProp = loadProperties("en", String.format(MESSAGES_FILENAME, "_en"));
            Properties defaultProp = loadProperties("default", String.format(MESSAGES_FILENAME, ""));
            mergedProp.putAll(defaultProp);
            mergedProp.putAll(enProp);
            Enumeration<Object> keys = mergedProp.keys();
            while (keys.hasMoreElements()) {
                sortedKeys.add((String) keys.nextElement());
            }
            Collections.sort(sortedKeys);
            URL classesUrl = getClass().getResource("/");
            File f = new File(classesUrl.toURI());
            String[] messagesFiles = f.list();
            for (String messagesFile : messagesFiles) {
                Matcher m = messagesPattern.matcher(messagesFile);
                if (m.matches()) {
                    String languageKey = m.group(2);
                    loadProperties(languageKey, messagesFile);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("could not initialize webengine module " + this, e);
        }
    }
}

From source file:org.ocpsoft.redoculous.model.impl.GitRepository.java

License:Open Source License

@Override
public Set<String> getRefs() {
    if (refs == null) {
        try {//from w  w w  .  j a v  a2  s. c om
            refs = new LinkedHashSet<String>();

            Git git = null;
            try {
                git = Git.open(getRepoDir());
                List<Ref> branches = git.branchList().setListMode(ListMode.ALL).call();
                refs.addAll(processRefs(branches));
                List<Ref> tags = git.tagList().call();
                refs.addAll(processRefs(tags));
            } finally {
                if (git != null) {
                    git.getRepository().close();
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(
                    "Failed to get list of active refs for repository [" + getUrl() + "] [" + getKey() + "]",
                    e);
        }
    }
    return refs;
}

From source file:org.ocpsoft.redoculous.model.impl.GitRepository.java

License:Open Source License

@Override
public void initRef(String ref) {
    File repoDir = getRepoDir();//from w ww.  j a  v  a 2s .  c  o  m

    File refDir = getRefDir(ref);
    File refCacheDir = getCachedRefDir(ref);

    ref = resolveRef(ref);

    if (!refDir.exists()) {
        log.info("Creating ref copy [" + getUrl() + "] [" + ref + "] [" + getKey() + "]");
        refDir.mkdirs();
        refCacheDir.mkdirs();
        Git git = null;
        try {
            git = Git.open(repoDir);
            git.reset().setMode(ResetType.HARD).call();
            git.clean().setCleanDirectories(true).call();
            git.checkout().setName(ref).call();

            log.info("Deleting cache for [" + getUrl() + "] [" + ref + "] [" + getKey() + "]");
            Files.delete(refDir, true);
            Files.delete(refCacheDir, true);
            refCacheDir.mkdirs();
            Files.copyDirectory(repoDir, refDir, new FileFilter() {
                @Override
                public boolean accept(File file) {
                    return !(file.getName().equals(".git") || file.getName().equals(".gitignore"));
                }
            });
        } catch (Exception e) {
            if (git != null) {
                git.getRepository().close();
                git = null;
            }
            Files.delete(refDir, true);
            Files.delete(refCacheDir, true);
            throw new RewriteException(
                    "Could checkout ref [" + ref + "] from repository [" + getUrl() + "] [" + getKey() + "].",
                    e);
        } finally {
            if (git != null) {
                git.getRepository().close();
            }
        }
    }
}

From source file:org.ocpsoft.redoculous.model.impl.GitRepository.java

License:Open Source License

@Override
public void update() {
    File repoDir = getRepoDir();/* ww  w . ja v a  2s.c  o m*/

    Git git = null;
    RedoculousProgressMonitor monitor = new RedoculousProgressMonitor();
    try {
        log.info("Handling update request for [" + getUrl() + "] [" + getKey() + "]");
        git = Git.open(repoDir);

        git.fetch().setTagOpt(TagOpt.FETCH_TAGS).setRemote("origin")
                .setRefSpecs(new RefSpec("+refs/heads/*:refs/remotes/origin/*")).setProgressMonitor(monitor)
                .call();

        git.fetch().setTagOpt(TagOpt.FETCH_TAGS).setRemote("origin")
                .setRefSpecs(new RefSpec("+refs/tags/*:refs/tags/*")).setProgressMonitor(monitor).call();

        git.reset().setMode(ResetType.HARD).setRef("refs/remotes/origin/" + git.getRepository().getBranch())
                .call();

        git.clean().setCleanDirectories(true).call();

        Files.delete(getRefsDir(), true);
        Files.delete(getCacheDir(), true);
    } catch (Exception e) {
        throw new RuntimeException("Could not update repository [" + getUrl() + "] [" + getKey() + "]", e);
    } finally {
        if (git != null)
            git.getRepository().close();
    }
}