List of usage examples for org.eclipse.jgit.api Git status
public StatusCommand status()
From source file:de.ks.blogging.grav.pages.GravPages.java
License:Apache License
public void addCommit(String msg) throws RuntimeException { Git git = getGit(); if (git != null) { try {/* ww w.j ava 2 s . co m*/ Status status = git.status().call(); Set<String> modified = status.getModified(); Set<String> untracked = status.getUntracked(); AddCommand add = git.add(); modified.forEach(s -> add.addFilepattern(s)); untracked.forEach(s -> add.addFilepattern(s)); add.call(); CommitCommand commit = git.commit(); if (msg == null || msg.isEmpty()) { commit.setAmend(true); } else { commit.setMessage(msg); } RevCommit rev = commit.call(); log.info("Commited change {} with new rev {}", msg, rev); } catch (Exception e) { log.error("Could not add and commit ", e); throw new RuntimeException(e); } } }
From source file:edu.nju.cs.inform.jgit.unfinished.UpdateIndex.java
License:Apache License
private static Set<String> getModifiedFiles(Git git) throws NoWorkTreeException, GitAPIException { Status status = git.status().call(); return status.getModified(); }
From source file:eu.mihosoft.vrl.io.VersionedFile.java
License:Open Source License
/** * Returns names of all files that contain uncommitted changes. * * @return names of all files that contain uncommitted changes *//*from w ww. j ava 2 s . c om*/ public Set<String> getUncommittedChanges() { // file has to be opened if (!isOpened()) { throw new IllegalStateException("File \"" + getFile().getPath() + "\" not opened!"); } Set<String> result = new HashSet<String>(); Git git = null; try { git = Git.open(tmpFolder); Status status = git.status().call(); for (String s : status.getAdded()) { result.add(s); } for (String s : status.getChanged()) { result.add(s); } for (String s : status.getMissing()) { result.add(s); } for (String s : status.getModified()) { result.add(s); } for (String s : status.getRemoved()) { result.add(s); } for (String s : status.getUntracked()) { result.add(s); } } catch (UnmergedPathException ex) { ex.printStackTrace(System.err); closeGit(git); } catch (IOException ex) { ex.printStackTrace(System.err); closeGit(git); } return result; }
From source file:eu.mihosoft.vrl.io.VersionedFile.java
License:Open Source License
/** * Determines if this file has conflicts. * * @return <code>true</code> if conflicts exist; <code>false</code> * otherwise/*from w w w.j av a 2 s . c o m*/ * @throws IOException * @throws IllegalStateException if this file is currently not open */ private boolean hasConflicts() throws IOException { // file has to be opened if (!isOpened()) { throw new IllegalStateException("File \"" + getFile().getPath() + "\" not opened!"); } Git git = null; try { git = Git.open(tmpFolder); Status status = git.status().call(); closeGit(git); return !status.getConflicting().isEmpty(); } catch (UnmergedPathException 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
/** * Commit file changes. IF flushing for commits is enabled changes will be * flushed.// w w w .j a v a 2 s . co 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:fr.brouillard.oss.jgitver.impl.GitUtils.java
License:Apache License
/** * Checks that underlying repository is dirty (modified with uncommitted changes). * @return true if the underlying repository is dirty, false otherwise * @throws GitAPIException if a git eeror occured while computing status * @throws NoWorkTreeException if the underlying repsoitory directory is not git managed *//*from w w w . ja v a2 s. com*/ public static boolean isDirty(Git git) throws NoWorkTreeException, GitAPIException { Status status = git.status().call(); return !status.isClean(); }
From source file:gitrunner.GitRunner.java
/** * @param args the command line arguments *///from w ww .jav a 2s. c o m public static void main(String[] args) { try { // Run git status for fun using Process Process p = Runtime.getRuntime().exec("git status"); BufferedReader reader; BufferedReader error_reader; reader = new BufferedReader(new InputStreamReader(p.getInputStream())); error_reader = new BufferedReader(new InputStreamReader(p.getErrorStream())); String cmd_output = null; while ((cmd_output = reader.readLine()) != null) { System.out.println(cmd_output); } while ((cmd_output = error_reader.readLine()) != null) { System.out.println("Error: " + cmd_output); } // Now use Jgit to get "git status" System.out.println("\n\n ===== Using Jgit ====="); FileRepositoryBuilder builder = new FileRepositoryBuilder(); Repository repo = builder.readEnvironment().findGitDir().build(); if (repo == null) { System.err.println("No repo"); } else { System.out.println("Got repo"); } Git git = new Git(repo); Status status = null; try { status = git.status().call(); } catch (GitAPIException ex) { Logger.getLogger(GitRunner.class.getName()).log(Level.SEVERE, null, ex); System.exit(-1); } catch (NoWorkTreeException ex) { Logger.getLogger(GitRunner.class.getName()).log(Level.SEVERE, null, ex); System.exit(-1); } Set<String> result = new HashSet<>(); System.out.println("Untracked = " + status.getUntracked()); result.addAll(status.getModified()); result.addAll(status.getAdded()); result.addAll(status.getUntracked()); System.out.println(result.toString()); } catch (IOException ex) { Logger.getLogger(GitRunner.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:gov.va.isaac.sync.git.SyncServiceGIT.java
License:Apache License
/** * @throws AuthenticationException /* w w w . j a v a2 s .c o m*/ * @see gov.va.isaac.interfaces.sync.ProfileSyncI#linkAndFetchFromRemote(java.io.File, java.lang.String, java.lang.String, java.lang.String) */ @Override public void linkAndFetchFromRemote(String remoteAddress, String username, String password) throws IllegalArgumentException, IOException, AuthenticationException { log.info("linkAndFetchFromRemote called - folder: {}, remoteAddress: {}, username: {}", localFolder, remoteAddress, username); try { File gitFolder = new File(localFolder, ".git"); Repository r = new FileRepository(gitFolder); if (!gitFolder.isDirectory()) { log.debug("Root folder does not contain a .git subfolder. Creating new git repository."); r.create(); } relinkRemote(remoteAddress, username, password); Git git = new Git(r); CredentialsProvider cp = new UsernamePasswordCredentialsProvider(username, (password == null ? new char[] {} : password.toCharArray())); log.debug("Fetching"); FetchResult fr = git.fetch().setCheckFetchedObjects(true).setCredentialsProvider(cp).call(); log.debug("Fetch messages: {}", fr.getMessages()); boolean remoteHasMaster = false; Collection<Ref> refs = git.lsRemote().setCredentialsProvider(cp).call(); for (Ref ref : refs) { if ("refs/heads/master".equals(ref.getName())) { remoteHasMaster = true; log.debug("Remote already has 'heads/master'"); break; } } if (remoteHasMaster) { //we need to fetch and (maybe) merge - get onto origin/master. log.debug("Fetching from remote"); String fetchResult = git.fetch().setCredentialsProvider(cp).call().getMessages(); log.debug("Fetch Result: {}", fetchResult); log.debug("Resetting to origin/master"); git.reset().setMode(ResetType.MIXED).setRef("origin/master").call(); //Get the files from master that we didn't have in our working folder log.debug("Checking out missing files from origin/master"); for (String missing : git.status().call().getMissing()) { log.debug("Checkout {}", missing); git.checkout().addPath(missing).call(); } for (String newFile : makeInitialFilesAsNecessary(localFolder)) { log.debug("Adding and committing {}", newFile); git.add().addFilepattern(newFile).call(); git.commit().setMessage("Adding " + newFile).setAuthor(username, "42").call(); for (PushResult pr : git.push().setCredentialsProvider(cp).call()) { log.debug("Push Message: {}", pr.getMessages()); } } } else { //just push //make sure we have something to push for (String newFile : makeInitialFilesAsNecessary(localFolder)) { log.debug("Adding and committing {}", newFile); git.add().addFilepattern(newFile).call(); git.commit().setMessage("Adding readme file").setAuthor(username, "42").call(); } log.debug("Pushing repository"); for (PushResult pr : git.push().setCredentialsProvider(cp).call()) { log.debug("Push Result: {}", pr.getMessages()); } } log.info("linkAndFetchFromRemote Complete. Current status: " + statusToString(git.status().call())); } catch (TransportException te) { if (te.getMessage().contains("Auth fail")) { log.info("Auth fail", te); throw new AuthenticationException("Auth fail"); } else { log.error("Unexpected", te); throw new IOException("Internal error", te); } } catch (GitAPIException e) { log.error("Unexpected", e); throw new IOException("Internal error", e); } }
From source file:gov.va.isaac.sync.git.SyncServiceGIT.java
License:Apache License
/** * @see gov.va.isaac.interfaces.sync.ProfileSyncI#addFiles(java.io.File, java.util.Set) *//*from w w w . j a v a2 s . co m*/ @Override public void addFiles(String... files) throws IllegalArgumentException, IOException { try { log.info("Add Files called {}", Arrays.toString(files)); Git git = getGit(); if (files.length == 0) { log.debug("No files to add"); } else { AddCommand ac = git.add(); for (String file : files) { ac.addFilepattern(file); } ac.call(); } log.info("addFiles Complete. Current status: " + statusToString(git.status().call())); } catch (GitAPIException e) { log.error("Unexpected", e); throw new IOException("Internal error", e); } }
From source file:gov.va.isaac.sync.git.SyncServiceGIT.java
License:Apache License
/** * @see gov.va.isaac.interfaces.sync.ProfileSyncI#removeFiles(java.io.File, java.util.Set) *///from w w w .j a v a2 s . c o m @Override public void removeFiles(String... files) throws IllegalArgumentException, IOException { try { log.info("Remove Files called {}", Arrays.toString(files)); Git git = getGit(); if (files.length == 0) { log.debug("No files to remove"); } else { RmCommand rm = git.rm(); for (String file : files) { rm.addFilepattern(file); } rm.call(); } log.info("removeFiles Complete. Current status: " + statusToString(git.status().call())); } catch (GitAPIException e) { log.error("Unexpected", e); throw new IOException("Internal error", e); } }