public boolean applyPatches() throws Exception { git.reset().setMode(ResetCommand.ResetType.HARD).setRef(getSrcCommit().getName()).call(); File[] patchFiles = patchDirectory.listFiles(((dir, name) -> name.endsWith(".patch")) ) != null ? patchDirectory.listFiles(((dir, name) -> name.endsWith(".patch"))) : new File[0]; boolean allPatchesApplied = true; for (File patchFile : patchFiles) { try { git.apply().setPatch(new FileInputStream(patchFile)).call(); }catch(PatchApplyException e) { allPatchesApplied = false; System.out.println("Failed to apply patch "+patchFile.getName()+". " + e.getMessage()); } } return allPatchesApplied; }
/** * Reset all the specified files from the staging area. * * @param fileNames * - the list of file to be removed */ public void resetAll(List<FileStatus> files) { try { if (!files.isEmpty()) { ResetCommand reset = git.reset(); for (FileStatus file : files) { reset.addPath(file.getFileLocation()); } reset.call(); } fireFileStateChanged(new ChangeEvent(GitCommand.UNSTAGE, getPaths(files))); } catch (GitAPIException e) { if (logger.isDebugEnabled()) { logger.debug(e, e); } } }
private void update(Git git) throws GitAPIException { PullResult pullResult = git.pull().setRebase(true).call(); RebaseResult rebaseResult = pullResult.getRebaseResult(); if(!pullResult.isSuccessful()) { if(rebaseResult.getStatus() == RebaseResult.Status.CONFLICTS) { logger.warn("Git `pull` reported conflicts - will reset and try again next pass!"); git.reset().setMode(ResetCommand.ResetType.HARD).call(); return; } logger.warn("Git `pull` was unsuccessful :("); return; } if(rebaseResult.getStatus() == RebaseResult.Status.UP_TO_DATE) { logger.debug("Git `pull` reported that repository is already up-to-date"); return; } logger.debug("Git repo is now at commit '{}'", rebaseResult.getCurrentCommit()); }
public static void pull(Git repo, String ref) throws Exception { System.out.println( "Pulling updates for " + repo.getRepository().getDirectory() ); repo.reset().setRef( "origin/master" ).setMode( ResetCommand.ResetType.HARD ).call(); repo.fetch().call(); System.out.println( "Successfully fetched updates!" ); if ( ref == null || ref.isEmpty()) ref = "master"; repo.reset().setRef( ref ).setMode( ResetCommand.ResetType.HARD ).call(); if ( ref.equals( "master" ) ) { repo.reset().setRef( "origin/master" ).setMode( ResetCommand.ResetType.HARD ).call(); } System.out.println( "Checked out: " + ref ); }
private Menu getAdvancedResetMenu(CommitHelper commit) { Menu resetMenu = new Menu("Advanced"); MenuItem hardItem = new MenuItem("reset --hard"); MenuItem mixedItem = new MenuItem("reset --mixed"); MenuItem softItem = new MenuItem("reset --soft"); hardItem.setOnAction(event -> CommitTreeController.sessionController.handleAdvancedResetButton(commit, ResetCommand.ResetType.HARD)); mixedItem.setOnAction(event -> CommitTreeController.sessionController.handleAdvancedResetButton(commit, ResetCommand.ResetType.MIXED)); softItem.setOnAction(event -> CommitTreeController.sessionController.handleAdvancedResetButton(commit, ResetCommand.ResetType.SOFT)); resetMenu.getItems().setAll(hardItem, mixedItem, softItem); return resetMenu; }
public static void pullRepository() { try { logger.info("Scanning devices folder for changes."); git.add().addFilepattern(".").call(); Status status = git.status().call(); if (status.getChanged().size()>0 || status.getAdded().size()>0 || status.getModified().size()>0) { logger.info("Changes have been found. Doing a hard reset (removing user modifications)."); ResetCommand reset = git.reset(); reset.setMode(ResetType.HARD); reset.setRef(Constants.HEAD); reset.call(); } logger.info("Pulling changes from github."); git.pull().call(); } catch (NoHeadException e) { logger.info("Pull failed. Trying to clone repository instead"); closeRepository(); cloneRepository(); } catch (Exception e1) { closeRepository(); } }
private Ref resetHard(Git git, String label, String ref) { ResetCommand reset = git.reset(); reset.setRef(ref); reset.setMode(ResetType.HARD); try { Ref resetRef = reset.call(); if (resetRef != null) { this.logger.info( "Reset label " + label + " to version " + resetRef.getObjectId()); } return resetRef; } catch (Exception ex) { String message = "Could not reset to remote for " + label + " (current ref=" + ref + "), remote: " + git.getRepository().getConfig() .getString("remote", "origin", "url"); warn(message, ex); return null; } }
@Override public void refreshRepo() throws RotationLoadException, IOException { try { if (git == null) { if (new File(getPath() + File.separator + ".git").exists()) this.git = Git.open(new File(getPath())); else this.git = ((CloneCommand) addCredentials( Git.cloneRepository().setURI(gitUrl.toString()).setDirectory(new File(getPath())))).call(); } git.clean().call(); addCredentials(git.fetch()).call(); git.reset().setRef("@{upstream}").setMode(ResetCommand.ResetType.HARD).call(); } catch (GitAPIException e) { e.printStackTrace(); throw new RotationLoadException("Could not load git repository: " + gitUrl); } super.refreshRepo(); }
public static Stream<FileVersion> getAllVersions(final String dirName, final String fileNameStartWith, final Optional<String> realm) throws IOException, GitAPIException { final File dir = new File(GitHubPublisher.localPath + dirName + File.separator); final Git git = GitHubPublisher.getRepo(); logger.info("git fetch"); git.fetch().call(); logger.info("git fetch finished"); logger.info("git reset"); git.reset().setMode(ResetCommand.ResetType.HARD).call(); logger.info("git reset finished"); logger.trace("dir = {}", dir.getAbsoluteFile()); if (dir.listFiles() == null) { return Stream.empty(); } final Stream<FileVersion> stream = Stream.of(dir.listFiles()) .filter(File::isDirectory) .filter(realmDir -> !realm.isPresent() || realmDir.getName().equals(realm.get())) .map(File::listFiles) .flatMap(Stream::of) .filter(File::isFile) .filter(f -> f.getName().startsWith(fileNameStartWith)) .map(file -> { try { return GitHubPublisher.getAllVersions(git, dirName + "/" + realm.orElse(new File(file.getParent()).getName()) + "/" + file.getName()); } catch (final Exception e) { logger.error(e.getLocalizedMessage(), e); return null; } }) .filter(Objects::nonNull) .flatMap(Collection::stream) .parallel(); logger.info("getAllVersions done"); return stream; }
/** Removes any staged changes that are not yet committed from a git repository. */ public Git reset(Path repositoryRoot) { try { FileRepositoryBuilder builder = new FileRepositoryBuilder(); Repository repository = builder.setWorkTree(repositoryRoot.toFile()).build(); Git git = new Git(repository); git.reset().setMode(ResetCommand.ResetType.HARD).call(); return git; } catch (Exception e) { throw new IllegalStateException( String.format("error resetting local GIT repository at %s", repositoryRoot), e); } }
@Override public void reset(ResetParams params) throws GitException { try { ResetCommand resetCommand = getGit().reset(); resetCommand.setRef(params.getCommit()); List<String> patterns = params.getFilePattern(); patterns.forEach(resetCommand::addPath); if (params.getType() != null && patterns.isEmpty()) { switch (params.getType()) { case HARD: resetCommand.setMode(ResetType.HARD); break; case KEEP: resetCommand.setMode(ResetType.KEEP); break; case MERGE: resetCommand.setMode(ResetType.MERGE); break; case MIXED: resetCommand.setMode(ResetType.MIXED); break; case SOFT: resetCommand.setMode(ResetType.SOFT); break; } } resetCommand.call(); } catch (GitAPIException exception) { throw new GitException(exception.getMessage(), exception); } }
public void reset(String refToResetTo) { ResetCommand command = _git.reset(); command.setRef(refToResetTo); command.setMode(ResetType.HARD); try { command.call(); } catch (Throwable e) { throw new RuntimeException(String.format("Failed to reset to [%s]", refToResetTo), e); } }
/** * * @throws GitAPIException * @throws IOException */ private List<DiffEntry> getChanges() throws GitAPIException, IOException { // get tree for last processed commit ObjectId oldHeadTree = git.getRepository().resolve(this.lastProcessedGitHash + "^{tree}"); // refresh to latest and reset hard to handle forced pushes this.git.fetch().setRemote(REMOTE_NAME).call(); // reset hard to get a clean working set since pull --rebase may leave files around this.git.reset().setMode(ResetCommand.ResetType.HARD).setRef(REMOTE_NAME + "/" + this.branchName).call(); ObjectId head = this.git.getRepository().resolve("HEAD"); ObjectId headTree = this.git.getRepository().resolve("HEAD^{tree}"); // remember the hash for the current HEAD. This will be checkpointed after the diff is processed. latestGitHash = head.getName(); // diff old and new heads to find changes ObjectReader reader = this.git.getRepository().newObjectReader(); CanonicalTreeParser oldTreeIter = new CanonicalTreeParser(); oldTreeIter.reset(reader, oldHeadTree); CanonicalTreeParser newTreeIter = new CanonicalTreeParser(); newTreeIter.reset(reader, headTree); return this.git.diff() .setNewTree(newTreeIter) .setOldTree(oldTreeIter) .setShowNameAndStatusOnly(true) .call(); }
Ref resetHard() { try { ResetCommand resetCmd = git.reset().setMode(ResetType.HARD); return resetCmd.call(); } catch (GitAPIException ex) { throw new IllegalStateException("Cannot reset workspace", ex); } }
public void resetHard() throws IOException { Git git = new Git(getJGitRepository()); try { git.reset().setMode(ResetCommand.ResetType.HARD).call(); } catch (GitAPIException e) { throw new IOException(e); } }
public static void reset(final Repository repo) { try { new Git(repo).reset().setMode(ResetCommand.ResetType.HARD).setRef("HEAD").call(); } catch (GitAPIException e) { throw new RuntimeException("Failed to issue git reset HEAD!", e); } }
@Override public void doExecute() { try { ResetCommand resetCommand = git.reset(); resetCommand.setMode(mode).call(); } catch (Exception e) { throw new GitBuildException("Unexpected exception: " + e.getMessage(), e); } }
private void gitUpdateFromOrigin() throws GitAPIException { git.fetch().setRemote("origin").call(); git.reset().setMode(ResetCommand.ResetType.HARD).setRef("origin/master").call(); this.contributors = getContributorsFromRepo(); }
/** * Resets the repository to the given ref * @param git the git repository * @param mode the reset type to use * @param ref the ref to which to reset the given files * @throws GitAPIException */ public static void reset(Git git, ResetCommand.ResetType mode, String ref) throws GitAPIException { git.reset() .setMode(mode) .setRef(ref) .call(); }
/** * Resets the repository to HEAD * @param git the git repository * @param mode the reset type to use * @throws GitAPIException */ public static void reset(Git git, ResetCommand.ResetType mode) throws GitAPIException { git.reset() .setMode(mode) .call(); }
/** * Resets the tree to a given commit with default settings * * @param commit the commit to reset to */ public void handleResetButton(CommitHelper commit) { handleAdvancedResetButton(commit, ResetCommand.ResetType.MIXED); }