/** * This will create or refresh the working copy. If the working copy cannot be pulled cleanly this * method will fail. * * @param gitRepositoryUri remote git repository URI string * @return git * @throws GitAPIException * @throws IOException * @throws URISyntaxException */ private Git getGit(final String gitRepositoryUri) throws GitAPIException, IOException, URISyntaxException { final Git cachedGit = gitCache.get(gitRepositoryUri); if (cachedGit != null) { return cachedGit; } final File gitDir = File.createTempFile( gitRepositoryUri.replaceAll("[^A-Za-z]", "_"), "wagon-git"); // $NON-NLS-1$ gitDir.delete(); gitDir.mkdir(); credentialsProvider = new UsernamePasswordCredentialsProvider( getAuthenticationInfo().getUserName(), getAuthenticationInfo().getPassword() == null ? "" //$NON-NLS-1$ : getAuthenticationInfo().getPassword()); final Git git = Git.cloneRepository() .setURI(gitRepositoryUri) .setCredentialsProvider(credentialsProvider) .setBranch(gitUri.getBranchName()) .setDirectory(gitDir) .call(); if (!gitUri.getBranchName().equals(git.getRepository().getBranch())) { LOG.log(Level.INFO, "missingbranch", gitUri.getBranchName()); final RefUpdate refUpdate = git.getRepository().getRefDatabase().newUpdate(Constants.HEAD, true); refUpdate.setForceUpdate(true); refUpdate.link("refs/heads/" + gitUri.getBranchName()); // $NON-NLS-1$ } gitCache.put(gitRepositoryUri, git); return git; }
private void doCheckout(final Ref branch) throws IOException { if (branch == null) throw die(CLIText.get().cannotChekoutNoHeadsAdvertisedByRemote); if (!Constants.HEAD.equals(branch.getName())) { RefUpdate u = db.updateRef(Constants.HEAD); u.disableRefLog(); u.link(branch.getName()); } final RevCommit commit = parseCommit(branch); final RefUpdate u = db.updateRef(Constants.HEAD); u.setNewObjectId(commit); u.forceUpdate(); DirCache dc = db.lockDirCache(); DirCacheCheckout co = new DirCacheCheckout(db, dc, commit.getTree()); co.checkout(); }
public void setUp() throws Exception { super.setUp(); git = new Git(db); // commit something writeTrashFile("Test.txt", "Hello world"); git.add().addFilepattern("Test.txt").call(); git.commit().setMessage("Initial commit").call(); // create a master branch and switch to it git.branchCreate().setName("test").call(); RefUpdate rup = db.updateRef(Constants.HEAD); rup.link("refs/heads/test"); // commit something on the test branch writeTrashFile("Test.txt", "Some change"); git.add().addFilepattern("Test.txt").call(); git.commit().setMessage("Second commit").call(); }