RepositoryCheckoutOperation extends TaskListenerOperation implements FileCallable<Boolean> { public Boolean invoke(File file, VirtualChannel channel) throws IOException, InterruptedException { CommitLogWriter writer = new CommitLogWriter(new OutputStreamWriter( log.write())); CommitLogWriterFilter filter = new CommitLogWriterFilter(writer); StreamProgressMonitor monitor = null; if (listener != null) monitor = new StreamProgressMonitor(listener.getLogger()); try { for (BuildRepository repo : repos) { Repository gitRepo = new FileRepositoryOperation(repo).invoke( file, channel); RevCommit current = null; if (gitRepo == null) gitRepo = new InitOperation(repo).invoke(file, channel); else current = CommitUtils.getLatest(gitRepo); RevCommit fetched = new FetchOperation(repo, gitRepo, monitor) .call(); if (fetched == null) return false; if (current != null) new CommitFinder(gitRepo).setFilter(filter).findBetween( fetched, current); else writer.write(new Commit(gitRepo, fetched)); new TreeCheckoutOperation(gitRepo, fetched).call(); RefUpdate refUpdate = gitRepo.updateRef(Constants.HEAD, true); refUpdate.setNewObjectId(fetched); Result result = refUpdate.forceUpdate(); if (result == null) throw new IOException("Null ref update result"); switch (result) { case NEW: case FORCED: case FAST_FORWARD: case NO_CHANGE: break; default: throw new IOException(result.name()); } } } finally { writer.close(); } return true; } RepositoryCheckoutOperation(
			Collection<BuildRepository> repositories, FilePath commitLog);  RepositoryCheckoutOperation(
			Collection<BuildRepository> repositories, FilePath commitLog,
			TaskListener listener); Boolean invoke(File file, VirtualChannel channel);  }
@Test public void checkoutThreeSequentialSingleCommits() throws Exception { File dir = git.tempDirectory(); File gitDir = new File(dir, Constants.DOT_GIT); File log = File.createTempFile("log", ".txt"); BuildRepository repo = new BuildRepository(git.repo().getDirectory() .toURI().toString(), BuildRepository.BRANCH_DEFAULT, null); RevCommit commit1 = git.add("file.txt", "content"); RepositoryCheckoutOperation op = new RepositoryCheckoutOperation( Collections.singletonList(repo), new FilePath(log)); assertTrue(op.invoke(dir, null)); CommitLogReader reader = new CommitLogReader(); CommitLog cl = reader.parse(null, log); assertNotNull(cl); assertFalse(cl.isEmptySet()); assertEquals(1, cl.toArray().length); assertEquals(commit1.name(), cl.iterator().next().getCommitId()); assertTrue(log.delete()); assertTrue(log.createNewFile()); Repository gitRepo = new FileRepository(gitDir); assertFalse(git.repo().getDirectory().equals(gitRepo.getDirectory())); assertEquals(commit1, CommitUtils.getLatest(gitRepo)); RevCommit commit2 = git.add("file.txt", "new content"); assertTrue(op.invoke(dir, null)); assertEquals(commit2, CommitUtils.getLatest(gitRepo)); cl = reader.parse(null, log); assertNotNull(cl); assertFalse(cl.isEmptySet()); assertEquals(1, cl.toArray().length); assertEquals(commit2.name(), cl.iterator().next().getCommitId()); RevCommit commit3 = git.add("file.txt", "less content"); assertTrue(op.invoke(dir, null)); assertEquals(commit3, CommitUtils.getLatest(gitRepo)); cl = reader.parse(null, log); assertNotNull(cl); assertFalse(cl.isEmptySet()); assertEquals(1, cl.toArray().length); assertEquals(commit3.name(), cl.iterator().next().getCommitId()); }