List of usage examples for org.eclipse.jgit.api Git open
public static Git open(File dir) throws IOException
From source file:eu.mihosoft.vrl.io.VersionedFile.java
License:Open Source License
/** * Commit file changes. IF flushing for commits is enabled changes will be * flushed./*from w ww. j a v a 2 s. c o m*/ * * @param message commit message * @return this file * @throws IOException * @throws IllegalStateException if this file is currently not open */ public VersionedFile commit(String message) throws IOException { // file has to be opened if (!isOpened()) { throw new IllegalStateException("File\"" + getFile().getPath() + "\" not opened!"); } Git git = null; try { // this should NEVER happen if (hasConflicts()) { throw new IllegalStateException("File \"" + getFile().getPath() + "\" has conflicts!"); } // ensures that message is not null if (message == null || message.isEmpty()) { message = "no message"; } System.out.print(">> commit version "); // open the git repository git = Git.open(tmpFolder); // retrieve the current git status Status status = git.status().call(); // rm command to tell git to remove files RmCommand rm = git.rm(); boolean needsRM = false; // checks whether rm is necessary and adds relevant paths for (String removedFile : status.getMissing()) { rm.addFilepattern(removedFile); needsRM = true; } // calls the rm command if necessary if (needsRM) { rm.call(); } // adds all remaining files git.add().addFilepattern(".").call(); // perform the commit git.commit().setMessage(message).setAuthor(System.getProperty("user.name"), "?").call(); commits = null; // updates the current version number currentVersion = getNumberOfVersions() - 1; System.out.println(currentVersion + ": "); System.out.println(">>> commit-id (SHA-1): " + getVersions().get(currentVersion).getName()); if (isFlushCommits()) { flush(); } closeGit(git); return this; } catch (NoFilepatternException ex) { closeGit(git); throw new IOException("Git exception", ex); } catch (NoHeadException ex) { closeGit(git); throw new IOException("Git exception", ex); } catch (NoMessageException ex) { closeGit(git); throw new IOException("Git exception", ex); } catch (UnmergedPathException ex) { closeGit(git); throw new IOException("Git exception", ex); } catch (ConcurrentRefUpdateException ex) { closeGit(git); throw new IOException("Git exception", ex); } catch (JGitInternalException ex) { closeGit(git); throw new IOException("Git exception", ex); } catch (WrongRepositoryStateException ex) { closeGit(git); throw new IOException("Git exception", ex); } catch (IOException ex) { closeGit(git); throw new IOException("Git exception", ex); } }
From source file:eu.mihosoft.vrl.io.VersionedFile.java
License:Open Source License
/** * Returns a list containing the paths to all files in the specified * version.// ww w. j a va 2 s .c om * * @param c version identifier (commit) * @return a list containing the paths to all files in the specified version * @throws IllegalStateException if this file is currently not open */ private Collection<String> getFilesInVersion(RevCommit c) { Collection<String> result = new ArrayList<String>(); // file has to be opened if (!isOpened()) { throw new IllegalStateException("File\"" + getFile().getPath() + "\" not opened!"); } Git git = null; try { git = Git.open(tmpFolder); // create a tree walk to search for files TreeWalk walk = new TreeWalk(git.getRepository()); if (walk != null) { // recursively search fo files walk.setRecursive(true); // add the tree the specified commit belongs to walk.addTree(c.getTree()); // walk through the tree while (walk.next()) { // TODO: is it a problem if mode is treemode? final FileMode mode = walk.getFileMode(0); if (mode == FileMode.TREE) { System.out.print("VersionedFile." + "getFilesInVersion(): FileMode unexpected!"); } // retrieve the path name of the current element String fileName = walk.getPathString(); // we do not want to commit/checkout this file if (!fileName.equals(FILE_INFO_NAME)) { result.add(walk.getPathString()); } } } } catch (IOException ex) { closeGit(git); Logger.getLogger(VersionedFile.class.getName()).log(Level.SEVERE, null, ex); } closeGit(git); return result; }
From source file:eu.mihosoft.vrl.io.VersionedFile.java
License:Open Source License
/** * Checks out all files in the specified version. * * @param c version identifier (commit)/*from w ww . ja v a 2 s . c o m*/ * @throws IllegalStateException if this file is currently not open */ private void checkoutFilesInVersion(RevCommit c) { // file has to be opened if (!isOpened()) { throw new IllegalStateException("File\"" + getFile().getPath() + "\" not opened!"); } System.out.println(">>> commit-id (SHA-1): " + c.getName()); Git git = null; TreeWalk walk = null; try { git = Git.open(tmpFolder); // create a tree walk to search for files. walk = new TreeWalk(git.getRepository()); if (walk != null) { // recursively search fo files walk.setRecursive(true); // add the tree the specified commit belongs to walk.addTree(c.getTree()); while (walk.next()) { // TODO: is it a problem if mode is treemode? final FileMode mode = walk.getFileMode(0); if (mode == FileMode.TREE) { System.out.print('0'); } String fileName = walk.getPathString(); if (!fileName.equals(FILE_INFO_NAME)) { // checks out the current file checkoutFile(fileName, walk.getObjectId(0)); } } } } catch (IOException ex) { closeGit(git); Logger.getLogger(VersionedFile.class.getName()).log(Level.SEVERE, null, ex); } closeGit(git); }
From source file:eu.mihosoft.vrl.io.VersionedFile.java
License:Open Source License
/** * Checks out the specified file (it is necessary to specifiy the file name * and the object id)./*from w w w. ja v a 2 s. c o m*/ * * @param fileName file name (path relative to content dir) * @param id object id of this file * @throws IOException * @throws IllegalStateException if this file is currently not open */ private void checkoutFile(String fileName, ObjectId id) throws IOException { // file has to be opened if (!isOpened()) { throw new IllegalStateException("File\"" + getFile().getPath() + "\" not opened!"); } System.out.println(">>> checkout file: " + fileName); // file that shall be checked out File checkoutFile = new File(tmpFolder.getAbsolutePath() + "/" + fileName); // the parent directory of the file to be checked out File parentDirectory = checkoutFile.getParentFile(); // create the parent directory if it is not the content directory to // allow the output stream to save the file there if (parentDirectory != null && !parentDirectory.equals(tmpFolder)) { parentDirectory.mkdirs(); } BufferedOutputStream out = null; Git git = null; try { git = Git.open(tmpFolder); // checkout the file via an object loader ObjectLoader loader = git.getRepository().open(id); out = new BufferedOutputStream(new FileOutputStream(checkoutFile)); loader.copyTo(out); closeGit(git); } catch (IOException ex) { closeGit(git); Logger.getLogger(VersionedFile.class.getName()).log(Level.SEVERE, null, ex); } finally { closeGit(git); // we are responsible to close the stream if (out != null) { out.close(); } } }
From source file:eu.mihosoft.vrl.io.VersionedFile.java
License:Open Source License
/** * Returns a list containing commit objects of all versions. This method can * be used to show the version messages, e.g., for creating a ui that does * allow the selection of the version that shall be checked out. * * @return a list containing commit objects of all versions * @throws IOException// w w w. ja v a2 s . com * @throws IllegalStateException if this file is currently not open */ @Override public ArrayList<RevCommit> getVersions() throws IOException { // use cached results if possible if (commits != null) { return commits; } // file has to be opened if (!isOpened()) { throw new IllegalStateException("File\"" + getFile().getPath() + "\" not opened!"); } RevWalk walk = null; Git git = null; try { // open the git repository git = Git.open(tmpFolder); walk = new RevWalk(git.getRepository()); // retrieve the object id of the current HEAD version // (latest/youngest version) ObjectId headId = git.getRepository().resolve(Constants.HEAD); // tell the walk to start from HEAD walk.markStart(walk.parseCommit(headId)); // change sorting order walk.sort(RevSort.TOPO, true); walk.sort(RevSort.REVERSE, true); commits = new ArrayList<RevCommit>(); // walk through all versions and add them to the list for (RevCommit commit : walk) { commits.add(commit); } closeGit(git); } catch (IOException ex) { throw new IOException("Git exception", ex); } finally { closeGit(git); // we are responsible for disposing the walk object if (walk != null) { walk.dispose(); } } return commits; }
From source file:eu.mihosoft.vrl.io.VersionedFile.java
License:Open Source License
/** * Initializes git repository.//from ww w . j av a 2s. co m * * <p><b>Warning:</b> Be careful when calling this method. It will destroy * any existing repository!</p> * * @throws IOException */ private void initGit() throws IOException { File repoFile = new File(tmpFolder.getAbsolutePath() + "/.git"); // delete existing repository if (repoFile.exists()) { IOUtil.deleteDirectory(repoFile); } Git git = null; try { // initialize git repository Git.init().setDirectory(tmpFolder).call(); git = Git.open(tmpFolder); git.add().addFilepattern(".").call(); // perform initial commit git.commit().setMessage("initial commit").setAuthor("VRL-User", "").call(); git.getRepository().close(); } catch (NoHeadException ex) { throw new IOException("Git exception", ex); } catch (NoMessageException ex) { throw new IOException("Git exception", ex); } catch (UnmergedPathException ex) { throw new IOException("Git exception", ex); } catch (ConcurrentRefUpdateException ex) { throw new IOException("Git exception", ex); } catch (JGitInternalException ex) { throw new IOException("Git exception", ex); } catch (WrongRepositoryStateException ex) { throw new IOException("Git exception", ex); } catch (NoFilepatternException ex) { throw new IOException("Git exception", ex); } catch (IOException ex) { throw new IOException("Git exception", ex); } finally { if (git != null) { git.getRepository().close(); } } }
From source file:eu.seaclouds.platform.discoverer.crawler.PaasifyCrawler.java
License:Apache License
/** * Updates local Paasify repository/* w ww .jav a 2s. com*/ * * @throws GitAPIException */ private void updateLocalRepository() throws GitAPIException { try { Git.open(new File(paasifyRepositoryDirecory)).pull().call(); } catch (IOException e) { log.error("Cannot update local Git repository"); log.error(e.getMessage()); } }
From source file:eu.seaclouds.platform.discoverer.ws.PaasifySpider.java
License:Apache License
/** * Updates local Paasify repository//ww w . ja v a2 s. c o m * * @throws GitAPIException */ private void updateLocalRepository() throws GitAPIException { try { Git.open(new File(paasifyRepositoryDirecory)).pull().call(); } catch (IOException e) { e.printStackTrace(); } }
From source file:ezbake.deployer.publishers.openShift.RhcApplication.java
License:Apache License
/** * This will make sure that a directory is on disk for the git repository. It will clone the git repo to the directory * if needed, or git pull the new contents if required. * * @return git repository created/retrieved from OpenShift with the remote pointing to OpenShift * @throws DeploymentException - on any exception getting it from git *///from www .j a v a 2 s . c om public Git getOrCreateGitRepo() throws DeploymentException { String gitUrl = applicationInstance.getGitUrl(); Files.createDirectories(appTmpDir); File gitDir = new File(appTmpDir, ".git"); if (gitRepo != null || gitDir.exists() && gitDir.isDirectory()) { try { Git git = gitRepo != null ? gitRepo : Git.open(appTmpDir); // stash to get to a clean state of git dir so that we can pull without conflicts git.stashCreate(); git.stashDrop(); PullCommand pullCommand = git.pull(); if (gitCredentialsProvider != null) pullCommand.setCredentialsProvider(gitCredentialsProvider); PullResult result = pullCommand.call(); if (!result.isSuccessful()) throw new DeploymentException("Git pull was not successful: " + result.toString()); setGitRepo(git); return git; } catch (IOException | GitAPIException e) { log.error("Error opening existing cached git repo", e); throw new DeploymentException("Error opening existing cached git repo: " + e.getMessage()); } } else { try { log.info("Cloning to " + appTmpDir.toString()); CloneCommand cloneCommand = Git.cloneRepository().setURI(gitUrl).setTimeout(10000) .setDirectory(appTmpDir); if (gitCredentialsProvider != null) cloneCommand.setCredentialsProvider(gitCredentialsProvider); gitRepo = cloneCommand.call(); log.info("Cloned to directory: " + gitRepo.getRepository().getDirectory().getParentFile().getAbsolutePath()); return gitRepo; } catch (GitAPIException | JGitInternalException e) { log.error("Error cloning repository from OpenShift", e); throw new DeploymentException("Error cloning repository from OpenShift: " + e.getMessage()); } } }
From source file:facade.GitFacade.java
public static void findAllReposInDirectory() { repos.clear();//from w ww .j a v a2 s .c o m for (File file : selectedDirectory.listFiles()) { try { Git repo = Git.open(file); repos.put(getRepoName(repo), repo); } catch (IOException ex) { //System.out.println("Nie jestem repo!"); } } }