public static void main(String[] args) throws IOException, GitAPIException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { try (Git git = new Git(repository)) { Status status = git.status().call(); System.out.println("Added: " + status.getAdded()); System.out.println("Changed: " + status.getChanged()); System.out.println("Conflicting: " + status.getConflicting()); System.out.println("ConflictingStageState: " + status.getConflictingStageState()); System.out.println("IgnoredNotInIndex: " + status.getIgnoredNotInIndex()); System.out.println("Missing: " + status.getMissing()); System.out.println("Modified: " + status.getModified()); System.out.println("Removed: " + status.getRemoved()); System.out.println("Untracked: " + status.getUntracked()); System.out.println("UntrackedFolders: " + status.getUntrackedFolders()); } } } }
@Signature public Memory status(@Nullable Set<String> paths, ArrayMemory settings) throws GitAPIException { StatusCommand status = getWrappedObject().status(); if (paths != null) { for (String path : paths) { status.addPath(path); } } if (settings != null && settings.isNotNull()) { if (settings.containsKey("ignoreSubmoduleMode")) { status.setIgnoreSubmodules(SubmoduleWalk.IgnoreSubmoduleMode.valueOf(settings.valueOfIndex("ignoreSubmoduleMode").toString())); } } Status call = status.call(); return GitUtils.valueOf(call); }
/** * Return a command object to execute a {@code status} command * * @see <a href= * "http://www.kernel.org/pub/software/scm/git/docs/git-status.html" * >Git documentation about status</a> * @return a {@link org.eclipse.jgit.api.StatusCommand} used to collect all * optional parameters and to finally execute the {@code status} * command */ public StatusCommand status() { return new StatusCommand(repo); }
git = Git.open(gitRepository); File dst = new File(git.getRepository().getWorkTree(), "fabric/profiles"); git.add().addFilepattern(".").call(); for (String missing : git.status().call().getMissing()) { git.rm().addFilepattern(missing).call();
@Override public void flush(boolean success) { if (success) { try (Git git = gitRepository.getGit()) { Status status = git.status().call(); if (!status.isClean()) { String message = git.getRepository().parseCommit(git.getRepository().resolve(HEAD)).getFullMessage(); if(! status.getUntracked().isEmpty() || ! status.getUntrackedFolders().isEmpty()) { AddCommand addCommand = git.add(); for(String untracked : status.getUntrackedFolders()) { addCommand = addCommand.addFilepattern(untracked); } for(String untracked : status.getUntracked()) { addCommand = addCommand.addFilepattern(untracked); } addCommand.call(); } git.commit().setMessage(message).setAmend(true).setAll(true).setNoVerify(true).call(); } } catch (RevisionSyntaxException | IOException | GitAPIException ex) { throw new RuntimeException(ex); } } }
Git git = null; try { git = Git.open(repoDir); CheckoutCommand coCmd = git.checkout(); git.merge() .setStrategy(MergeStrategy.OURS) .include(ref) .addFilepattern(filename).call(); org.eclipse.jgit.api.Status status = git.status().call(); System.out.println("Added: " + status.getAdded()); System.out.println("Changed: " + status.getChanged()); System.out.println("Conflicting: " + status.getConflicting()); System.out.println("Missing: " + status.getMissing()); System.out.println("Modified: " + status.getModified());
private boolean isClean(Git git, String label) { StatusCommand status = git.status(); try { BranchTrackingStatus trackingStatus = BranchTrackingStatus.of(git.getRepository(), label); boolean isBranchAhead = trackingStatus != null && trackingStatus.getAheadCount() > 0; return status.call().isClean() && !isBranchAhead; } catch (Exception e) { String message = "Could not execute status command on local repository. Cause: (" + e.getClass().getSimpleName() + ") " + e.getMessage(); warn(message, e); return false; } }
public static boolean isRepositoryInDirtyState(Repository repo) throws GitAPIException { Git git = Git.wrap(repo); Status status = git.status().call(); // Git describe doesn't mind about untracked files when checking if // repo is dirty. JGit does this, so we cannot use the isClean method // to get the same behaviour. Instead check dirty state without // status.getUntracked().isEmpty() boolean isDirty = !(status.getAdded().isEmpty() && status.getChanged().isEmpty() && status.getRemoved().isEmpty() && status.getMissing().isEmpty() && status.getModified().isEmpty() && status.getConflicting().isEmpty()); return isDirty; } }
public synchronized void backup() throws Exception { repo.add().setUpdate(false).addFilepattern(".").call(); Status status = repo.status().setIgnoreSubmodules(SubmoduleWalk.IgnoreSubmoduleMode.ALL).call(); log.debug("status.getUncommittedChanges() = " + status.getUncommittedChanges()); if (!status.getUncommittedChanges().isEmpty()) { for (String missingPath : status.getMissing()) { repo.rm().addFilepattern(missingPath).call(); } log.info("Changes detected in the following files: " + status.getUncommittedChanges()); repo.commit() .setMessage("Backing up data dir") .setAuthor("AppRunner BackupService", "noemail@example.org") .call(); Iterable<PushResult> pushResults = repo.push().call(); for (PushResult pushResult : pushResults) { log.info("Result of pushing to remote: " + pushResult.getRemoteUpdates()); } } else { log.info("No changes to back up"); } }
/** * @param gitConfigFolder e.g. /your/project/root/.git * * @return Returns true if 'git status' has modified files inside the 'Changes to be committed' section */ public static boolean isCommitNecessary( String gitConfigFolder ) throws MojoExecutionException { try { Repository repo = new FileRepository( gitConfigFolder ); Git git = new Git( repo ); Status status = git.status().call(); Set<String> modified = status.getModified(); return ( modified.size() != 0 ); } catch ( Exception e ) { throw new MojoExecutionException( "Error trying to find out if git commit is needed", e ); } }
File wcDir = git.getRepository().getWorkTree(); Status status = git.status().call(); if (!status.isClean()) { boolean amend = false; if (status.getUntracked().size() == 0 && status.getMissing().size() == 0 && status.getModified().size() == 1) { if ("etc/io.fabric8.mq.fabric.server-broker.cfg".equals(status.getModified().iterator().next())) { git.add() .addFilepattern(".") .call();
Log.info("[{}] Writing commit", name); contents.write(); Git git = new Git(getJGitRepository()); Log.info("[{}] Getting missing files", name); Set<String> missingFiles = git.status().call().getMissing(); for (String missing : missingFiles) { Log.info("[{}] Git rm {}", name, missing); git.rm().setCached(true).addFilepattern(missing).call();
@Override public void addCommitPush() { try { Git git = gitOpen(); git.add().addFilepattern(".").call(); // status Status status=git.status().call(); // rm the deleted ones if(status.getMissing().size() >0){ for(String rm : status.getMissing()){ git.rm().addFilepattern(rm).call(); } } // commit and push if needed if(!status.hasUncommittedChanges()){ log.debug("not commiting git, because there are no changes"); return; } git.commit().setMessage("Automatic commit by releasetrain").call(); git.push().setCredentialsProvider(credentialsProvider()).call(); } catch (Exception e) { throw new GitException(e.getMessage(), e); } }
Status gitStatus = git.status().call(); if (!gitStatus.isClean()) { this.getLogger().debug("Commit repairnator files..."); filesChanged.addAll(gitStatus.getUncommittedChanges()); filesChanged.addAll(gitStatus.getUntracked()); filesChanged.addAll(gitStatus.getUntrackedFolders()); .directory(git.getRepository().getDirectory().getParentFile()).inheritIO(); git.commit() .setMessage("repairnator: add files created to push \n"+commitMsg) .setCommitter(this.getCommitterIdent())
protected boolean isClean(Git git) throws GitAPIException, IOException { if (git.getRepository().getBranch().equals(BASE_BRANCH)) { Optional<Ref> baseBranch = getOriginHead(git); if (baseBranch.isPresent()) { RevWalk walk = new RevWalk(git.getRepository()); RevCommit baseCommit = walk.parseCommit(getBaseObjectId(git)); RevCommit headCommit = walk.parseCommit(getHead(git).getObjectId()); log.info(String.format("origin/master sha: [%s], head: [%s]", baseCommit.getName(), headCommit.getName())); return baseCommit.equals(headCommit) && git.status().call().isClean(); } else { // there is no origin Status status = git.status().call(); boolean isClean = status.isClean(); if (!isClean) { log.info("Untracked Folders: " + Arrays.toString(status.getUntrackedFolders().toArray())); log.info("Untracked Files: " + Arrays.toString(status.getUntracked().toArray())); log.info("Changed Files: " + Arrays.toString(status.getChanged().toArray())); log.info("Added Files: " + Arrays.toString(status.getAdded().toArray())); log.info("Removed Files: " + Arrays.toString(status.getRemoved().toArray())); log.info("Uncommitted Files: " + Arrays.toString(status.getUncommittedChanges().toArray())); } return isClean; } } return false; }
private void pull() throws GitAPIException { Git git = recordGit(Git.wrap(repository)); PullCommand pull = git.pull() .setRebase(true); PullResult result = pull.call(); if (!result.isSuccessful()) { LOGGER.log(Level.WARNING, () -> String.format("Cannot pull from git '%s', branch '%s'", uri.toASCIIString(), branch)); if (LOGGER.isLoggable(Level.FINEST)) { Status status = git.status().call(); LOGGER.finest(() -> "git status cleanliness: " + status.isClean()); if (!status.isClean()) { LOGGER.finest(() -> "git status uncommitted changes: " + status.getUncommittedChanges()); LOGGER.finest(() -> "git status untracked: " + status.getUntracked()); } } } else { LOGGER.fine("Pull was successful."); } LOGGER.finest(() -> "git rebase result: " + result.getRebaseResult().getStatus().name()); LOGGER.finest(() -> "git fetch result: " + result.getFetchResult().getMessages()); }
public void addCommit(String[] patterns, String message) throws GitAPIException { if (!message.isEmpty()) { synchronized (COMMIT_LOCK) { AddCommand add = this.git.add(); Status status = this.git.status().call(); for (String pattern : patterns) { add.addFilepattern(pattern); } if (!status.getMissing().isEmpty() || !status.getRemoved().isEmpty()) { RmCommand remove = this.git.rm(); for (String file : Iterables.concat(status.getMissing(), status.getRemoved())) { remove.addFilepattern(file); } remove.call(); } add.call(); CommitCommand commit = this.git.commit(); commit.setMessage(message); commit.call(); } } postEventMap(); }
@Override public String snapshot(String name, String comment) throws ConfigurationPersistenceException { boolean noComment = (comment ==null || comment.isEmpty()); String message = noComment ? SNAPSHOT_PREFIX + FORMATTER.format(LocalDateTime.now()) : comment; String tagName = (name ==null || name.isEmpty()) ? SNAPSHOT_PREFIX + FORMATTER.format(LocalDateTime.now()) : name; try (Git git = gitRepository.getGit()) { Status status = git.status().call(); List<Ref> tags = git.tagList().call(); String refTagName = R_TAGS + tagName; for(Ref tag : tags) { if(refTagName.equals(tag.getName())) { throw MGMT_OP_LOGGER.snapshotAlreadyExistError(tagName); } } //if comment is not null if(status.hasUncommittedChanges() || !noComment) { git.commit().setMessage(message).setAll(true).setNoVerify(true).call(); } git.tag().setName(tagName).setMessage(message).call(); } catch (GitAPIException ex) { throw MGMT_OP_LOGGER.failedToPersistConfiguration(ex, message, ex.getMessage()); } return message; }