@Test public void shouldSwitchToMasterAndDeleteTempBranches() throws Exception, GitAPIException { configRepo.checkin(goConfigRevision("v1", "md5-1")); configRepo.createBranch(ConfigRepository.BRANCH_AT_HEAD, configRepo.getCurrentRevCommit()); configRepo.createBranch(ConfigRepository.BRANCH_AT_REVISION, configRepo.getCurrentRevCommit()); configRepo.git().checkout().setName(ConfigRepository.BRANCH_AT_REVISION).call(); assertThat(configRepo.git().getRepository().getBranch(), is(ConfigRepository.BRANCH_AT_REVISION)); assertThat(configRepo.git().branchList().call().size(), is(3)); configRepo.cleanAndResetToMaster(); assertThat(configRepo.git().getRepository().getBranch(), is("master")); assertThat(configRepo.git().branchList().call().size(), is(1)); }
private void addEdge(File edgeDir, File edgeFile, String fileContents) throws IOException, GitAPIException { createNewFile(edgeDir, edgeFile, fileContents); // add, commit, push edge this.gitForPush.add().addFilepattern(formEdgeFilePath(edgeDir.getParentFile().getName(), edgeDir.getName(), edgeFile.getName())).call(); this.gitForPush.commit().setMessage("Edge commit").call(); this.gitForPush.push().setRemote("origin").setRefSpecs(this.masterRefSpec).call(); }
private static RevCommitsPair resolveRevCommitsPair(Repository repo) { RevCommitsPair revCommitIteratorPair; try (RevWalk revWalk = new RevWalk(repo); Git git = new Git(repo)) { final Iterator<RevCommit> first; final Iterator<RevCommit> second; final ObjectId headId = repo.resolve(Constants.HEAD); final RevCommit headCommit = revWalk.parseCommit(headId); if (isMergeCommit(headCommit)) { final RevCommit firstParent = headCommit.getParent(0); final RevCommit secondParent = headCommit.getParent(1); first = git.log().add(firstParent).call().iterator(); second = git.log().add(secondParent).call().iterator(); } else { first = git.log().call().iterator(); second = Collections.emptyIterator(); } revCommitIteratorPair = new RevCommitsPair(new OmitMergeCommitsIterator(first), new OmitMergeCommitsIterator(second)); } catch (GitAPIException | IOException ignored) { revCommitIteratorPair = new RevCommitsPair(); } return revCommitIteratorPair; }
void cleanAndResetToMaster() throws IOException { try { git.reset().setMode(ResetCommand.ResetType.HARD).call(); checkout("master"); deleteBranch(BRANCH_AT_REVISION); deleteBranch(BRANCH_AT_HEAD); } catch (Exception e) { String currentBranch = git.getRepository().getBranch(); LOGGER.error("Error while trying to clean up config repository, CurrentBranch: {} \n : \n Message: {} \n StackTrace: {}", currentBranch, e.getMessage(), e.getStackTrace(), e); throw new RuntimeException(e); } }
@Signature public void __construct(File directory, boolean create) throws IOException, GitAPIException { try { __wrappedObject = Git.open(directory, FS.DETECTED); } catch (RepositoryNotFoundException e) { if (create) { Git.init().setBare(false).setDirectory(directory).call(); __wrappedObject = Git.open(directory, FS.DETECTED); } } }
/** * @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 ); } }
private static void createCommit(Repository repository, Git git, String fileName, String content) throws IOException, GitAPIException { // create the file File myFile = new File(repository.getDirectory().getParent(), fileName); FileUtils.writeStringToFile(myFile, content, "UTF-8"); // run the add git.add() .addFilepattern(fileName) .call(); // and then commit the changes RevCommit revCommit = git.commit() .setMessage("Added " + fileName) .call(); System.out.println("Committed file " + myFile + " as " + revCommit + " to repository at " + repository.getDirectory()); } }
public static void main(String[] args) throws IOException, GitAPIException { final File path; try (Repository repository = CookbookHelper.createNewRepository()) { try (Git git = new Git(repository)) { path = repository.getWorkTree(); System.out.println("Repository at " + path); git.add().addFilepattern("file1.txt").call(); RevCommit rev1 = git.commit().setAuthor("test", "test@test.com").setMessage("Commit Log 1").call(); System.out.println("Rev1: " + rev1); git.add().addFilepattern("file1.txt").call(); RevCommit rev2 = git.commit().setAll(true).setAuthor("test", "test@test.com").setMessage("Commit Log 2").call(); System.out.println("Rev2: " + rev2); git.add().addFilepattern("file1.txt").call(); RevCommit rev3 = git.commit().setAll(true).setAuthor("test", "test@test.com").setMessage("Commit Log 3").call(); System.out.println("Rev3: " + rev3); Iterable<RevCommit> gitLog = git.log().call(); for (RevCommit logMessage : gitLog) { System.out.println("Before revert: " + logMessage.getName() + " - " + logMessage.getFullMessage()); RevertCommand revertCommand = git.revert(); gitLog = git.log().call(); for (RevCommit logMessage : gitLog) { System.out.println("After revert: " + logMessage.getName() + " - " + logMessage.getFullMessage());
public static void main(String[] args) throws IOException, GitAPIException { final File localPath; try (Repository repository = CookbookHelper.createNewRepository()) { localPath = repository.getWorkTree(); try (Git git = new Git(repository)) { File tempFile = new File(repository.getDirectory().getParentFile(), fileName); if(!tempFile.createNewFile()) { throw new IOException("Could not create temporary file " + tempFile); git.add().addFilepattern(fileName).call(); git.commit().setMessage("Added untracked file " + fileName + "to repo").call(); git.checkout().addPath(fileName).call();
try (Git git = new Git(repository)) { git.add() .addFilepattern("testfile") .call(); git.add() .addFilepattern("testfile2") .call(); git.commit() .setMessage("Added testfiles") .call(); RevCommit stash = git.stashCreate() .call(); stash = git.stashCreate() .call(); Collection<RevCommit> stashes = git.stashList().call(); for(RevCommit rev : stashes) { System.out.println("Found stash: " + rev + ": " + rev.getFullMessage()); ObjectId call = git.stashDrop().setStashRef(0).call(); System.out.println("StashDrop returned: " + call); ObjectId applied = git.stashApply().setStashRef(stash.getName()).call(); System.out.println("Applied 2nd stash as: " + applied);
checkCallable(); try (RevWalk revWalk = new RevWalk(repo)) { newHead = revWalk.parseCommit(headRef.getObjectId()); if (srcObjectId == null) srcObjectId = src.getObjectId(); RevCommit srcCommit = revWalk.parseCommit(srcObjectId); String cherryPickName = srcCommit.getId().abbreviate(7).name() + " " + srcCommit.getShortMessage(); //$NON-NLS-1$ merger.setBase(srcParent.getTree()); merger.setCommitNames(new String[] { "BASE", ourName, //$NON-NLS-1$ cherryPickName }); dco.checkout(); if (!noCommit) newHead = new Git(getRepository()).commit() .setMessage(srcCommit.getFullMessage()) .setReflogComment(reflogPrefix + " " //$NON-NLS-1$ + srcCommit.getShortMessage()) .setAuthor(srcCommit.getAuthorIdent()) repo.writeCherryPickHead(srcCommit.getId());
fileKey.open(false).create(true); this.gitForPush = Git.cloneRepository().setURI(GIT_REMOTE_REPO_DIR).setDirectory(new File(GIT_CLONE_DIR)).call(); this.gitForPush.commit().setMessage("First commit").call(); this.gitForPush.push().setRemote("origin").setRefSpecs(new RefSpec("master")).call();
checkCallable(); try (RevWalk revWalk = new RevWalk(repo)) { if (srcObjectId == null) srcObjectId = src.getObjectId(); RevCommit srcCommit = revWalk.parseCommit(srcObjectId); continue; DirCacheCheckout dco = new DirCacheCheckout(repo, headCommit.getTree(), repo.lockDirCache(), merger.getResultTreeId()); dco.setFailOnConflict(true); dco.setProgressMonitor(monitor); dco.checkout(); try (Git git = new Git(getRepository())) { newHead = git.commit().setMessage(newMessage) .setReflogComment("revert: " + shortMessage) //$NON-NLS-1$ .call(); .formatWithConflicts(newMessage, merger.getUnmergedPaths()); repo.writeRevertHead(srcCommit.getId());
public Repository createCommit(File srcFolder, String applicationName) throws IOException, GitAPIException { Repository repository = repositoryService.createRepository(new Repository().setName(applicationName)); String cloneUrl = repository.getCloneUrl(); InitCommand init = new InitCommand(); init.setDirectory(srcFolder); init.setBare(false); Git git = init.call(); StoredConfig config = git.getRepository().getConfig(); config.setString("remote", "origin", "url", cloneUrl); config.save(); UsernamePasswordCredentialsProvider user = new UsernamePasswordCredentialsProvider(accessToken, ""); git.add().addFilepattern(".").call(); git.commit().setMessage(COMMIT_MESSAGE).call(); git.push().setCredentialsProvider(user).call(); return repository; } }
remoteRepo.create(true); Git gitForPush = Git.cloneRepository().setURI(remoteRepo.getDirectory().getAbsolutePath()).setDirectory(cloneDir).call(); gitForPush.commit().setMessage("First commit").call(); RefSpec masterRefSpec = new RefSpec("master"); gitForPush.push().setRemote("origin").setRefSpecs(masterRefSpec).call(); + ConfigurationKeys.GIT_MONITOR_REPO_URI, remoteRepo.getDirectory().getAbsolutePath()) .addPrimitive(GitFlowGraphMonitor.GIT_FLOWGRAPH_MONITOR_PREFIX + "." + ConfigurationKeys.GIT_MONITOR_REPO_DIR, TESTDIR + "/git-flowgraph") .addPrimitive(GitFlowGraphMonitor.GIT_FLOWGRAPH_MONITOR_PREFIX + "." + ConfigurationKeys.GIT_MONITOR_POLLING_INTERVAL, 5) gitForPush.add().addFilepattern(formNodeFilePath(flowGraphDir, nodeDir.getName(), nodeFile.getName())).call(); gitForPush.commit().setMessage("Node commit").call(); gitForPush.push().setRemote("origin").setRefSpecs(masterRefSpec).call();
public static void main(String[] args) throws IOException, GitAPIException { try (Git git = new Git(repository)) { git.tagDelete().setTags("tag_for_testing").call(); Ref tag = git.tag().setName("tag_for_testing").call(); System.out.println("Created/moved tag " + tag + " to repository at " + repository.getDirectory()); git.tagDelete().setTags("tag_for_testing").call(); ObjectId id = repository.resolve("HEAD^"); try (RevWalk walk = new RevWalk(repository)) { RevCommit commit = walk.parseCommit(id); tag = git.tag().setObjectId(commit).setName("tag_for_testing").call(); System.out.println("Created/moved tag " + tag + " to repository at " + repository.getDirectory()); git.tagDelete().setTags("tag_for_testing").call(); tag = git.tag().setName("tag_for_testing").setAnnotated(true).call(); System.out.println("Created/moved tag " + tag + " to repository at " + repository.getDirectory()); git.tagDelete().setTags("tag_for_testing").call(); walk.dispose();
public static void main(String[] args) throws IOException, GitAPIException { final File localPath; try (Repository repository = CookbookHelper.createNewRepository()) { localPath = repository.getWorkTree(); try (Git git = new Git(repository)) { Ref changes = git.branchCreate().setName("changes").call(); System.out.println("Result of creating the branch: " + changes); Ref checkout = git.checkout().setName("changes").call(); System.out.println("Result of checking out the branch: " + checkout); checkout = git.checkout().setName("master").call(); System.out.println("Result of checking out master: " + checkout); ObjectId mergeBase = repository.resolve("changes"); MergeResult merge = git.merge(). include(mergeBase). setCommit(true).
private void addSubmodule(String name, String url, String path, String revision, List<CopyFile> copyfiles, List<LinkFile> linkfiles, Git git) throws GitAPIException, IOException { assert (!repo.isBare()); assert (git != null); if (!linkfiles.isEmpty()) { throw new UnsupportedOperationException( JGitText.get().nonBareLinkFilesNotSupported); } SubmoduleAddCommand add = git.submoduleAdd().setName(name).setPath(path) .setURI(url); if (monitor != null) add.setProgressMonitor(monitor); Repository subRepo = add.call(); if (revision != null) { try (Git sub = new Git(subRepo)) { sub.checkout().setName(findRef(revision, subRepo)).call(); } subRepo.close(); git.add().addFilepattern(path).call(); } for (CopyFile copyfile : copyfiles) { copyfile.copy(); git.add().addFilepattern(copyfile.dest).call(); } }
try (Git git = new Git(repo)) { CreateBranchCommand command = git.branchCreate(); command.setName(name); if (startCommit != null) Ref headRef = repo.exactRef(Constants.HEAD); if (headRef == null) { if (orphan) { if (startPoint == null && startCommit == null) { Result r = repo.updateRef(Constants.HEAD).link( r.name())); this.status = CheckoutResult.NOT_TRIED_RESULT; return repo.exactRef(Constants.HEAD); try (RevWalk revWalk = new RevWalk(repo)) { AnyObjectId headId = headRef.getObjectId(); headCommit = headId == null ? null : revWalk.parseCommit(headId); newCommit = revWalk.parseCommit(branch); RevTree headTree = headCommit == null ? null : headCommit.getTree(); dco = new DirCacheCheckout(repo, headTree, dc, newCommit.getTree()); dco.setFailOnConflict(!force); dco.setProgressMonitor(monitor);
writeRewrittenHashes(); ObjectReader or = repo.newObjectReader(); throw new JGitInternalException( JGitText.get().cannotResolveUniquelyAbbrevObjectId); RevCommit commitToPick = walk.parseCommit(ids.iterator().next()); if (shouldPick) { if (monitor.isCancelled()) return null; // continue rebase process on pick command case REWORD: String oldMessage = commitToPick.getFullMessage(); String newMessage = interactiveHandler .modifyCommitMessage(oldMessage); try (Git git = new Git(repo)) { newHead = git.commit().setMessage(newMessage).setAmend(true) .setNoVerify(true).call(); rebaseState.createFile(AMEND, commitToPick.name()); return stop(commitToPick, Status.EDIT); case COMMENT: List<RebaseTodoLine> steps = repo.readRebaseTodo( rebaseState.getPath(GIT_REBASE_TODO), false); RebaseTodoLine nextStep = steps.size() > 0 ? steps.get(0) : null;