private void fillLastModifiedCommits(ObjectId start, String basePath) throws IOException, GitAPIException { Map<String, ObjectId> objects = lastModifiedCommits.get(start); if (objects == null) { objects = new TreeMap<>(); } DiffFormatter diffFmt = new DiffFormatter(NullOutputStream.INSTANCE); diffFmt.setRepository(getGitRepository()); LogCommand log = new Git(getGitRepository()).log(); if (!basePath.isEmpty()) { log.addPath(basePath); } for(RevCommit c: log.call()) { final RevTree a = c.getParentCount() > 0 ? c.getParent(0).getTree() : null; final RevTree b = c.getTree(); for(DiffEntry diff: diffFmt.scan(a, b)) { objects.put(diff.getNewPath(), c.getId()); } } lastModifiedCommits.put(start, objects); }
public void testCommitNoRoots () throws Exception { File toCommit = new File(workDir, "testnotadd.txt"); write(toCommit, "blablabla"); GitClient client = getClient(workDir); Map<File, GitStatus> statuses = client.getStatus(new File[] { toCommit }, NULL_PROGRESS_MONITOR); assertStatus(statuses, workDir, toCommit, false, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_ADDED, GitStatus.Status.STATUS_ADDED, false); client.add(new File[] { toCommit }, NULL_PROGRESS_MONITOR); statuses = client.getStatus(new File[] { toCommit }, NULL_PROGRESS_MONITOR); assertStatus(statuses, workDir, toCommit, true, GitStatus.Status.STATUS_ADDED, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_ADDED, false); GitRevisionInfo info = client.commit(new File[0], "initial commit", null, null, NULL_PROGRESS_MONITOR); statuses = client.getStatus(new File[] { toCommit }, NULL_PROGRESS_MONITOR); assertStatus(statuses, workDir, toCommit, true, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, false); Git git = new Git(repository); LogCommand log = git.log(); RevCommit com = log.call().iterator().next(); assertEquals("initial commit", info.getFullMessage()); assertEquals("initial commit", com.getFullMessage()); assertEquals(ObjectId.toString(com.getId()), info.getRevision()); Map<File, GitFileInfo> modifiedFiles = info.getModifiedFiles(); assertTrue(modifiedFiles.get(toCommit).getStatus().equals(Status.ADDED)); }
@Override public List<GitUser> getCommiters() throws GitException { List<GitUser> gitUsers = new ArrayList<>(); try { LogCommand logCommand = getGit().log(); for (RevCommit commit : logCommand.call()) { PersonIdent committerIdentity = commit.getCommitterIdent(); GitUser gitUser = newDto(GitUser.class) .withName(committerIdentity.getName()) .withEmail(committerIdentity.getEmailAddress()); if (!gitUsers.contains(gitUser)) { gitUsers.add(gitUser); } } } catch (GitAPIException exception) { throw new GitException(exception.getMessage(), exception); } return gitUsers; }
@Override public WikiPage toWikiPage(WikiPage root) { FileBasedWikiPage fsPage = (FileBasedWikiPage) root; String content; try { content = convertToWikiText(history(fsPage.getFileSystemPath(), new LogCommandSpec() { @Override public LogCommand specify(LogCommand log, Repository repository) { return log.setMaxCount(RECENT_CHANGES_DEPTH); } })); } catch (GitAPIException e) { content = "Unable to read history: " + e.getMessage(); } return new GitRecentChangesPage(RecentChanges.RECENT_CHANGES, root, new PageData(content, new WikiPageProperties())); }
private List<RevCommit> getFirstCommits(Git git, ObjectId objectId, String path, int length) throws MissingObjectException, IncorrectObjectTypeException, NoHeadException, GitAPIException { LogCommand command = git.log(); if (objectId != null) { command.add(objectId); } if (StringUtils.isNotBlank(path)) { command.addPath(path); } Iterator<RevCommit> iterator = command.setMaxCount(length).call().iterator(); List<RevCommit> list = new ArrayList<RevCommit>(); for (int i = 0; i < length; i++) { if (iterator.hasNext()) { list.add(iterator.next()); } else { break; } } return list; }
private Iterable<RevCommit> getCommitsFromTag(String refName) { try { List<Ref> call = git.tagList().call(); for (Ref ref : call) { if (ref.getName().endsWith(refName)) { LogCommand log = git.log(); Ref peeledRef = git.getRepository().peel(ref); if (peeledRef.getPeeledObjectId() != null) { return log.add(peeledRef.getPeeledObjectId()).call(); } else { return log.add(ref.getObjectId()).call(); } } } return null; } catch (GitAPIException | IncorrectObjectTypeException | MissingObjectException e) { close(); throw new RepositoryMinerException(e); } }
@Test public void testRange () throws IOException { Git git = mock(Git.class); Repository repository = mock(Repository.class); LogCommand logCommand = mock(LogCommand.class); ObjectId a1 = mock(ObjectId.class); ObjectId a2 = mock(ObjectId.class); when(git.getRepository()).thenReturn(repository); when(git.log()).thenReturn(logCommand); when(repository.resolve("HEAD^^")).thenReturn(a1); when(repository.resolve("4a877e")).thenReturn(a2); JGitScanner.getLogWithOrWithOutRange(git, "HEAD^^..4a877e"); verify(logCommand).addRange(a1, a2); }
@Test public void testRangeNoUntil () throws IOException { Git git = mock(Git.class); Repository repository = mock(Repository.class); LogCommand logCommand = mock(LogCommand.class); ObjectId a1 = mock(ObjectId.class); ObjectId a2 = mock(ObjectId.class); when(git.getRepository()).thenReturn(repository); when(git.log()).thenReturn(logCommand); when(repository.resolve("HEAD^^")).thenReturn(a1); when(repository.resolve("HEAD")).thenReturn(a2); JGitScanner.getLogWithOrWithOutRange(git, "HEAD^^.."); verify(logCommand).addRange(a1, a2); }
@Override public List<Revision> getHistory(final String test, final String revision, final int start, final int limit) throws StoreException { try { final ObjectId commitId = ObjectId.fromString(revision); final LogCommand logCommand = git.log() // TODO: create path to definition.json file, sanitize test name for invalid / relative characters .addPath(getTestDefinitionsDirectory() + File.separator + test + File.separator + FileBasedProctorStore.TEST_DEFINITION_FILENAME) .add(commitId) .setSkip(start) .setMaxCount(limit); return getHistoryFromLogCommand(logCommand); } catch (IOException e) { throw new StoreException("Could not get history for " + test + " starting at " + getGitCore().getRefName(), e); } }
private List<Revision> getHistoryFromLogCommand(final LogCommand command) throws StoreException { final List<Revision> versions = Lists.newArrayList(); final Iterable<RevCommit> commits; try { commits = command.call(); } catch (GitAPIException e) { throw new StoreException("Could not get history", e); } for( RevCommit commit : commits) { versions.add(new Revision( commit.getName(), commit.getAuthorIdent().toExternalString(), new Date(Long.valueOf(commit.getCommitTime()) * 1000 /* convert seconds to milliseconds */), commit.getFullMessage() )); } return versions; }
public static void main(String[] args) throws IOException, GitAPIException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { try (Git git = new Git(repository)) { List<Ref> call = git.tagList().call(); for (Ref ref : call) { System.out.println("Tag: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName()); // fetch all commits for this tag LogCommand log = git.log(); Ref peeledRef = repository.peel(ref); if(peeledRef.getPeeledObjectId() != null) { log.add(peeledRef.getPeeledObjectId()); } else { log.add(ref.getObjectId()); } Iterable<RevCommit> logs = log.call(); for (RevCommit rev : logs) { System.out.println("Commit: " + rev /* + ", name: " + rev.getName() + ", id: " + rev.getId().getName() */); } } } } }
private static TagVersionAndCount fixCommitCount(TagVersionAndCount resolved, Repository repo) throws RefNotFoundException, GitAPIException { Git git = new Git(repo); ObjectId target, head; LogCommand logCommand; try { target = repo.getRef(resolved.getVersion()).getPeeledObjectId(); logCommand = git.log(); logCommand.add(target); } catch (IOException e) { throw new SemverGitflowPlugin.VersionApplicationException(e); } int count = 0; for (RevCommit commit : logCommand.call()) { count ++; } return new TagVersionAndCount(resolved.getVersion(), count); }
public void testSingleTreeCommit () throws Exception { File folder = new File(workDir, "folder"); File subfolder1 = new File(folder, "subfolder"); File subfolder11 = new File(subfolder1, "subfolder1"); File subfolder12 = new File(subfolder1, "subfolder2"); subfolder11.mkdirs(); subfolder12.mkdirs(); File file1 = new File(subfolder11, "file1"); File file2 = new File(subfolder12, "file2"); write(file1, "file1 content"); write(file2, "file2 content"); File[] files = new File[] { folder }; GitClient client = getClient(workDir); client.add(files, NULL_PROGRESS_MONITOR); GitRevisionInfo info = client.commit(files, "initial commit", null, null, NULL_PROGRESS_MONITOR); Map<File, GitFileInfo> modifiedFiles = info.getModifiedFiles(); assertEquals(2, modifiedFiles.size()); assertTrue(modifiedFiles.get(file1).getStatus().equals(Status.ADDED)); assertTrue(modifiedFiles.get(file2).getStatus().equals(Status.ADDED)); Map<File, GitStatus> statuses = client.getStatus(files, NULL_PROGRESS_MONITOR); assertStatus(statuses, workDir, file1, true, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, false); assertStatus(statuses, workDir, file2, true, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, false); Git git = new Git(repository); LogCommand log = git.log(); RevCommit com = log.call().iterator().next(); assertEquals("initial commit", com.getFullMessage()); }
/** @see org.eclipse.che.api.git.GitConnection#log(LogParams) */ @Override public LogPage log(LogParams params) throws GitException { LogCommand logCommand = getGit().log(); try { setRevisionRange(logCommand, params); logCommand.setSkip(params.getSkip()); logCommand.setMaxCount(params.getMaxCount()); List<String> fileFilter = params.getFileFilter(); if (fileFilter != null) { fileFilter.forEach(logCommand::addPath); } String filePath = params.getFilePath(); if (!isNullOrEmpty(filePath)) { logCommand.addPath(filePath); } Iterator<RevCommit> revIterator = logCommand.call().iterator(); List<Revision> commits = new ArrayList<>(); while (revIterator.hasNext()) { RevCommit commit = revIterator.next(); Revision revision = getRevision(commit, filePath); commits.add(revision); } return new LogPage(commits); } catch (GitAPIException | IOException exception) { String errorMessage = exception.getMessage(); if (ERROR_LOG_NO_HEAD_EXISTS.equals(errorMessage)) { throw new GitException(errorMessage, ErrorCodes.INIT_COMMIT_WAS_NOT_PERFORMED); } else { LOG.error("Failed to retrieve log. ", exception); throw new GitException(exception); } } }
private void setRevisionRange(LogCommand logCommand, LogParams params) throws IOException { if (params != null && logCommand != null) { String revisionRangeSince = params.getRevisionRangeSince(); String revisionRangeUntil = params.getRevisionRangeUntil(); if (revisionRangeSince != null && revisionRangeUntil != null) { ObjectId since = repository.resolve(revisionRangeSince); ObjectId until = repository.resolve(revisionRangeUntil); logCommand.addRange(since, until); } } }
private RevWalk buildWalk() throws GitAPIException, IncorrectObjectTypeException, MissingObjectException { if (ref != null) { final LogCommand logCommand = git._log().add(ref.getObjectId()); if (path != null && !path.isEmpty()) { logCommand.addPath(path); } return (RevWalk) logCommand.call(); } return new RevWalk(git.getRepository()); }
/** * git log ref1 */ public List<CommitInfo> log(RepositoryPK repositoryPK, String ref, Integer num) { try (RepositoryReader reader = new RepositoryReader(repositoryPK)) { Git git = reader.getGit(); Repository repository = reader.getRepository(); ObjectId objectId = repository.resolve(ref); LogCommand command = git.log(); if (objectId != null) { command.add(objectId); } if (num != null) { command.setMaxCount(num); } Iterable<RevCommit> commits = command.call(); List<CommitInfo> commitInfos = new ArrayList<>(); for (RevCommit commit : commits) { commitInfos.add(new CommitInfo(commit)); } return commitInfos; } catch (IOException | GitAPIException e) { throw new RuntimeException(e); } }
static LogCommand getLogWithOrWithOutRange(Git git, String range) throws IOException { LogCommand result = git.log(); if (null == range) { result = result.all(); } else { int firstDot = range.indexOf('.'); if (firstDot <= 0) { throw new IllegalArgumentException ("Git range must start like '<rev specification>..'"); } int lastDot = range.lastIndexOf("."); if (lastDot - firstDot != 1) { throw new IllegalArgumentException ("Git range specials ('three dot notation' etc.) are not supported!"); } String sinceString = range.substring(0, firstDot); String untilString = lastDot + 1 < range.length() ? range.substring(lastDot + 1) : "HEAD"; logger.debug ("Using range from '{}' to '{}'", sinceString, untilString); AnyObjectId since = git.getRepository().resolve(sinceString); if (null == since) { throw new IllegalArgumentException("Could not retrieve 'since' Range part '" + sinceString + "'"); } AnyObjectId until = git.getRepository().resolve(untilString); if (null == until) { throw new IllegalArgumentException("Could not retrieve 'until' Range part '" + untilString + "'"); } result = result.addRange(since, until); } return result; }
private RevCommit resolveFirstCommitForTag (Git git, Ref tagRef) throws IOException, GitAPIException { LogCommand log = git.log(); Ref peeledRef = git.getRepository().peel(tagRef); if(peeledRef.getPeeledObjectId() != null) { log.add(peeledRef.getPeeledObjectId()); } else { log.add(tagRef.getObjectId()); } Iterable<RevCommit> logs = log.call(); return logs.iterator().next(); }
@Test(expected = IllegalArgumentException.class) public void testRangeThreeDots () throws IOException { Git git = mock(Git.class); Repository repository = mock(Repository.class); LogCommand logCommand = mock(LogCommand.class); when(git.getRepository()).thenReturn(repository); when(git.log()).thenReturn(logCommand); JGitScanner.getLogWithOrWithOutRange(git, "HEAD^^...master"); }
@Test(expected = IllegalArgumentException.class) public void testRangeSingleDot () throws IOException { Git git = mock(Git.class); Repository repository = mock(Repository.class); LogCommand logCommand = mock(LogCommand.class); when(git.getRepository()).thenReturn(repository); when(git.log()).thenReturn(logCommand); JGitScanner.getLogWithOrWithOutRange(git, "HEAD^^.master"); }
@Test(expected = IllegalArgumentException.class) public void testRangeSinceDoesNotExist () throws IOException { Git git = mock(Git.class); Repository repository = mock(Repository.class); LogCommand logCommand = mock(LogCommand.class); when(git.getRepository()).thenReturn(repository); when(git.log()).thenReturn(logCommand); when(repository.resolve("NonExistingRev")).thenReturn(null); JGitScanner.getLogWithOrWithOutRange(git, "NonExistingRev..master"); }
@Test(expected = IllegalArgumentException.class) public void testRangeUntilDoesNotExist () throws IOException { Git git = mock(Git.class); Repository repository = mock(Repository.class); LogCommand logCommand = mock(LogCommand.class); ObjectId a1 = mock(ObjectId.class); when(git.getRepository()).thenReturn(repository); when(git.log()).thenReturn(logCommand); when(repository.resolve("HEAD")).thenReturn(a1); when(repository.resolve("NonExistingRev")).thenReturn(null); JGitScanner.getLogWithOrWithOutRange(git, "HEAD..NonExistingRev"); }
public void testMultipleTreesCommit () throws Exception { File folder1 = new File(workDir, "folder1"); File subfolder11 = new File(folder1, "subfolder1"); File subfolder12 = new File(folder1, "subfolder2"); subfolder11.mkdirs(); subfolder12.mkdirs(); File file11 = new File(subfolder11, "file1"); File file12 = new File(subfolder12, "file2"); write(file11, "file1 content"); write(file12, "file2 content"); File folder2 = new File(workDir, "folder2"); File subfolder21 = new File(folder2, "subfolder1"); File subfolder22 = new File(folder2, "subfolder2"); subfolder21.mkdirs(); subfolder22.mkdirs(); File file21 = new File(subfolder21, "file1"); File file22 = new File(subfolder22, "file2"); write(file21, "file1 content"); write(file22, "file2 content"); File[] files = new File[] { folder1, folder2 }; GitClient client = getClient(workDir); client.add(files, NULL_PROGRESS_MONITOR); GitRevisionInfo info = client.commit(files, "initial commit", null, null, NULL_PROGRESS_MONITOR); Map<File, GitFileInfo> modifiedFiles = info.getModifiedFiles(); assertEquals(4, modifiedFiles.size()); Map<File, GitStatus> statuses = client.getStatus(files, NULL_PROGRESS_MONITOR); assertStatus(statuses, workDir, file11, true, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, false); assertStatus(statuses, workDir, file12, true, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, false); assertStatus(statuses, workDir, file21, true, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, false); assertStatus(statuses, workDir, file22, true, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, false); Git git = new Git(repository); LogCommand log = git.log(); RevCommit com = log.call().iterator().next(); assertEquals("initial commit", com.getFullMessage()); write(file11, "!modification!"); write(file12, "another modification!"); write(file21, "!modification!"); write(file22, "another modification!"); client.add(files, NULL_PROGRESS_MONITOR); statuses = client.getStatus(files, NULL_PROGRESS_MONITOR); assertStatus(statuses, workDir, file11, true, GitStatus.Status.STATUS_MODIFIED, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_MODIFIED, false); assertStatus(statuses, workDir, file12, true, GitStatus.Status.STATUS_MODIFIED, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_MODIFIED, false); assertStatus(statuses, workDir, file21, true, GitStatus.Status.STATUS_MODIFIED, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_MODIFIED, false); assertStatus(statuses, workDir, file22, true, GitStatus.Status.STATUS_MODIFIED, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_MODIFIED, false); client.commit(files, "second commit", null, null, NULL_PROGRESS_MONITOR); statuses = client.getStatus(files, NULL_PROGRESS_MONITOR); assertStatus(statuses, workDir, file11, true, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, false); assertStatus(statuses, workDir, file12, true, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, false); assertStatus(statuses, workDir, file21, true, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, false); assertStatus(statuses, workDir, file22, true, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, false); log = git.log(); com = log.call().iterator().next(); assertEquals("second commit", com.getFullMessage()); }
public void testCommitOnlySomeOfAllFilesFromMultipleTrees () throws Exception { File folder1 = new File(workDir, "folder1"); File subfolder11 = new File(folder1, "subfolder1"); File subfolder12 = new File(folder1, "subfolder2"); subfolder11.mkdirs(); subfolder12.mkdirs(); File file11 = new File(subfolder11, "file1"); File file12 = new File(subfolder12, "file2"); write(file11, "file1 content"); write(file12, "file2 content"); File folder2 = new File(workDir, "folder2"); File subfolder21 = new File(folder2, "subfolder1"); File subfolder22 = new File(folder2, "subfolder2"); subfolder21.mkdirs(); subfolder22.mkdirs(); File file21 = new File(subfolder21, "file1"); File file22 = new File(subfolder22, "file2"); write(file21, "file1 content"); write(file22, "file2 content"); File[] trees = new File[] { folder1, folder2 }; File[] filesToCommit = new File[] { folder1, subfolder21 }; File[] filesSingleFolder = new File[] { subfolder21 }; GitClient client = getClient(workDir); client.add(trees, NULL_PROGRESS_MONITOR); // COMMIT SOME GitRevisionInfo info = client.commit(filesSingleFolder, "initial commit", null, null, NULL_PROGRESS_MONITOR); Map<File, GitFileInfo> modifiedFiles = info.getModifiedFiles(); assertEquals(1, modifiedFiles.size()); Map<File, GitStatus> statuses = client.getStatus(trees, NULL_PROGRESS_MONITOR); assertStatus(statuses, workDir, file11, true, GitStatus.Status.STATUS_ADDED, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_ADDED, false); assertStatus(statuses, workDir, file12, true, GitStatus.Status.STATUS_ADDED, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_ADDED, false); assertStatus(statuses, workDir, file21, true, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, false); assertStatus(statuses, workDir, file22, true, GitStatus.Status.STATUS_ADDED, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_ADDED, false); Git git = new Git(repository); LogCommand log = git.log(); RevCommit com = log.call().iterator().next(); assertEquals("initial commit", com.getFullMessage()); // COMMIT ALL info = client.commit(trees, "commit all", null, null, NULL_PROGRESS_MONITOR); modifiedFiles = info.getModifiedFiles(); assertEquals(3, modifiedFiles.size()); statuses = client.getStatus(trees, NULL_PROGRESS_MONITOR); assertStatus(statuses, workDir, file11, true, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, false); assertStatus(statuses, workDir, file12, true, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, false); assertStatus(statuses, workDir, file21, true, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, false); assertStatus(statuses, workDir, file22, true, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, false); write(file11, "!modification!"); write(file12, "another modification!"); write(file21, "!modification!"); write(file22, "another modification!"); client.add(trees, NULL_PROGRESS_MONITOR); statuses = client.getStatus(trees, NULL_PROGRESS_MONITOR); assertStatus(statuses, workDir, file11, true, GitStatus.Status.STATUS_MODIFIED, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_MODIFIED, false); assertStatus(statuses, workDir, file12, true, GitStatus.Status.STATUS_MODIFIED, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_MODIFIED, false); assertStatus(statuses, workDir, file21, true, GitStatus.Status.STATUS_MODIFIED, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_MODIFIED, false); assertStatus(statuses, workDir, file22, true, GitStatus.Status.STATUS_MODIFIED, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_MODIFIED, false); info = client.commit(filesToCommit, "second commit", null, null, NULL_PROGRESS_MONITOR); modifiedFiles = info.getModifiedFiles(); assertEquals(3, modifiedFiles.size()); statuses = client.getStatus(trees, NULL_PROGRESS_MONITOR); assertStatus(statuses, workDir, file11, true, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, false); assertStatus(statuses, workDir, file12, true, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, false); assertStatus(statuses, workDir, file21, true, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, false); assertStatus(statuses, workDir, file22, true, GitStatus.Status.STATUS_MODIFIED, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_MODIFIED, false); log = git.log(); com = log.call().iterator().next(); assertEquals("second commit", com.getFullMessage()); // COMMIT ALL client.commit(trees, "commit all", null, null, NULL_PROGRESS_MONITOR); statuses = client.getStatus(trees, NULL_PROGRESS_MONITOR); assertStatus(statuses, workDir, file11, true, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, false); assertStatus(statuses, workDir, file12, true, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, false); assertStatus(statuses, workDir, file21, true, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, false); assertStatus(statuses, workDir, file22, true, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, GitStatus.Status.STATUS_NORMAL, false); }
@Test public void testInit_gitLogApiException() throws RevisionGeneratorException, GitAPIException, IOException { String branch = "master"; String hash = UUID.randomUUID().toString().replaceAll("-", ""); String abbreviatedHash = hash.substring(0, 5); AbbreviatedObjectId abbreviatedObjectId = AbbreviatedObjectId.fromString(abbreviatedHash); Repository repo = mock(Repository.class); Ref headRef = mock(Ref.class); ObjectId headObjectId = mock(ObjectId.class); LogCommand logCmd = mock(LogCommand.class); GitAPIException exception = new NoHeadException("Dummy Git API Exception"); when(git.getRepository()).thenReturn(repo); when(repo.isBare()).thenReturn(Boolean.FALSE); when(repo.getBranch()).thenReturn(branch); when(repo.getRef(eq("HEAD"))).thenReturn(headRef); when(headRef.getObjectId()).thenReturn(headObjectId); when(headObjectId.abbreviate(eq(5))).thenReturn(abbreviatedObjectId); when(git.log()).thenReturn(logCmd); when(logCmd.setMaxCount(1)).thenReturn(logCmd); when(logCmd.call()).thenThrow(exception); exceptions.expect(RevisionGeneratorException.class); exceptions.expectMessage("Issue getting Git CommitTime"); exceptions.expectCause(IsInstanceOf.any(GitAPIException.class)); try { item.init(git, logger); } finally { verify(git).getRepository(); verify(git).log(); verify(repo).isBare(); verify(repo).getRef(eq("HEAD")); verify(repo).getBranch(); verify(headRef, times(2)).getObjectId(); verify(headObjectId).abbreviate(eq(5)); verify(logCmd).setMaxCount(eq(1)); verify(logCmd).call(); verifyNoMoreInteractions(git); verifyNoMoreInteractions(repo); verifyNoMoreInteractions(headRef); verifyNoMoreInteractions(headObjectId); verifyNoMoreInteractions(logCmd); verifyZeroInteractions(logger); } }
@Test public void testInit_gitStatusApiException() throws RevisionGeneratorException, GitAPIException, IOException { String branch = "master"; String hash = UUID.randomUUID().toString().replaceAll("-", "")+UUID.randomUUID().toString().replaceAll("-", "").substring(0,8); String abbreviatedHash = hash.substring(0, 5); AbbreviatedObjectId abbreviatedObjectId = AbbreviatedObjectId.fromString(abbreviatedHash); int commitTime = (int) (System.currentTimeMillis()/1000); RevCommit headCommit = createRevCommit(hash, commitTime); Repository repo = mock(Repository.class); Ref headRef = mock(Ref.class); ObjectId headObjectId = mock(ObjectId.class); LogCommand logCmd = mock(LogCommand.class); StatusCommand statusCmd = mock(StatusCommand.class); GitAPIException exception = new NoHeadException("Dummy Git API Exception"); when(git.getRepository()).thenReturn(repo); when(repo.isBare()).thenReturn(Boolean.FALSE); when(repo.getBranch()).thenReturn(branch); when(repo.getRef(eq("HEAD"))).thenReturn(headRef); when(headRef.getObjectId()).thenReturn(headObjectId); when(headObjectId.abbreviate(eq(5))).thenReturn(abbreviatedObjectId); when(git.log()).thenReturn(logCmd); when(logCmd.setMaxCount(1)).thenReturn(logCmd); when(logCmd.call()).thenReturn(Arrays.asList(headCommit)); when(git.status()).thenReturn(statusCmd); when(statusCmd.call()).thenThrow(exception); exceptions.expect(RevisionGeneratorException.class); exceptions.expectMessage("Issue getting Git Status"); exceptions.expectCause(IsInstanceOf.any(GitAPIException.class)); try { item.init(git, logger); } finally { verify(git).getRepository(); verify(git).log(); verify(repo).isBare(); verify(repo).getRef(eq("HEAD")); verify(repo).getBranch(); verify(headRef, times(2)).getObjectId(); verify(headObjectId).abbreviate(eq(5)); verify(logCmd).setMaxCount(eq(1)); verify(logCmd).call(); verify(git).status(); verify(statusCmd).call(); verifyNoMoreInteractions(git); verifyNoMoreInteractions(repo); verifyNoMoreInteractions(headRef); verifyNoMoreInteractions(headObjectId); verifyNoMoreInteractions(logCmd); verifyNoMoreInteractions(statusCmd); verifyZeroInteractions(logger); } }
public LogCommand _log() { return git.log(); }
public List<String[]> call(final GitInfoStep gitInfoStep, Git git, CredentialsProvider cp, String gitRepoUrl, File gitRepoFolder) throws InvalidRemoteException, TransportException, GitAPIException, IllegalArgumentException, IOException { final RevWalk walk = new RevWalk(git.getRepository()); LogCommand lc = git.log(); if (!Const.isEmpty(this.path)) { lc = lc.addPath(gitInfoStep.environmentSubstitute(this.path)); } if (this.maxCount != null) { lc = lc.setMaxCount(Integer.parseInt(gitInfoStep .environmentSubstitute(this.maxCount))); } if (this.skip != null) { lc = lc.setSkip(Integer.parseInt(gitInfoStep .environmentSubstitute(this.skip))); } final Iterable<RevCommit> logs = lc.call(); final Iterator<RevCommit> i = logs.iterator(); final List<String[]> commits = new ArrayList<String[]>(); while (i.hasNext()) { final String[] commitRow = new String[] {null, null, null, null, null, null, null, null, null, null}; final RevCommit commit = walk.parseCommit(i.next()); commitRow[0] = commit.getId().getName(); commitRow[1] = commit.getName(); commitRow[2] = commit.getFullMessage(); commitRow[3] = commit.getShortMessage(); commitRow[4] = dt.format(commit.getAuthorIdent().getWhen()); commitRow[5] = commit.getAuthorIdent().getName(); commitRow[6] = commit.getAuthorIdent().getEmailAddress(); commitRow[7] = dt.format(commit.getCommitterIdent().getWhen()); commitRow[8] = commit.getCommitterIdent().getName(); commitRow[9] = commit.getCommitterIdent().getEmailAddress(); commits.add(commitRow); } return commits; }
List<GitCommit> findCommits() throws IOException { Repository repository = getRepository(); List<GitCommit> result = new LinkedList<>(); ObjectId head = repository.resolve("HEAD"); logger.debug("Found head: {}", head); RevWalk rw = new RevWalk(repository); try (Git git = new Git(repository)) { LogCommand logCommand = getLogWithOrWithOutRange(git, range); Iterable<RevCommit> commits = logCommand.call(); DiffFormatter df = new DiffFormatter(DisabledOutputStream.INSTANCE); df.setRepository(repository); df.setDiffComparator(RawTextComparator.DEFAULT); df.setDetectRenames(true); for (RevCommit commit : commits) { logger.debug("Commit-Message: '{}'", commit.getShortMessage()); final Date date = new Date(1000 * (long) commit.getCommitTime()); final GitCommit gitCommit = retrieveCommit(ObjectId.toString(commit.getId())); gitCommit.setAuthor(makeStringOfIdent(commit.getAuthorIdent())); gitCommit.setCommitter(makeStringOfIdent(commit.getCommitterIdent())); gitCommit.setDate(date); gitCommit.setMessage(commit.getFullMessage()); gitCommit.setShortMessage(commit.getShortMessage()); gitCommit.setEncoding(commit.getEncodingName()); addCommitParents(rw, df, commit, gitCommit); result.add(gitCommit); } } catch (GitAPIException e) { throw new IllegalStateException("Could not read logs from Git repository '" + path + "'", e); } finally { rw.close(); repository.close(); } logger.debug("Found #{} commits", result.size()); return result; }
LogCommand specify(LogCommand log, Repository repository);