@Test public void squash() throws Exception { InteractiveHandler messageHandler = new InteractiveHandler() { public void prepareSteps(List<RebaseTodoLine> steps) { // not used } public String modifyCommitMessage(String commit) { return "squashed"; } }; List<RevCommit> commits = Arrays.asList(commit1, commit2, commit3); SquashCommitsOperation op = new SquashCommitsOperation(testRepository.getRepository(), commits, messageHandler); op.execute(new NullProgressMonitor()); assertEquals(2, countCommitsInHead()); LogCommand log = new Git(testRepository.getRepository()).log(); Iterable<RevCommit> logCommits = log.call(); RevCommit latestCommit = logCommits.iterator().next(); assertEquals("squashed", latestCommit.getFullMessage()); }
private Iterable<RevCommit> getCommitsFromTag(String treeName) { try { List<Ref> call = git.tagList().call(); Iterable<RevCommit> logs = null; for (Ref ref : call) { if (ref.getName().equals(treeName)) { LogCommand log = git.log(); Ref peeledRef = repository.peel(ref); if (peeledRef.getPeeledObjectId() != null) { log.add(peeledRef.getPeeledObjectId()); } else { log.add(ref.getObjectId()); } logs = log.call(); return logs; } } return null; } catch (GitAPIException e) { throw new VisMinerAPIException(e.getMessage(), e); } catch (MissingObjectException e) { throw new VisMinerAPIException(e.getMessage(), e); } catch (IncorrectObjectTypeException e) { throw new VisMinerAPIException(e.getMessage(), e); } }
@Override protected IStatus performJob() { try { // list all tags File gitDir = GitUtils.getGitDir(path); Repository db = new FileRepository(gitDir); Git git = new Git(db); List<Ref> refs = git.tagList().call(); JSONObject result = new JSONObject(); List<Tag> tags = new ArrayList<Tag>(); for (Ref ref : refs) { Tag tag = new Tag(cloneLocation, db, ref); tags.add(tag); } Collections.sort(tags, Tag.COMPARATOR); JSONArray children = new JSONArray(); int firstTag = pageSize > 0 ? pageSize * (pageNo - 1) : 0; int lastTag = pageSize > 0 ? firstTag + pageSize - 1 : tags.size() - 1; lastTag = lastTag > tags.size() - 1 ? tags.size() - 1 : lastTag; if (pageNo > 1 && baseLocation != null) { String prev = baseLocation + "?page=" + (pageNo - 1) + "&pageSize=" + pageSize; if (commitsSize > 0) { prev += "&" + GitConstants.KEY_TAG_COMMITS + "=" + commitsSize; } result.put(ProtocolConstants.KEY_PREVIOUS_LOCATION, prev); } if (lastTag < tags.size() - 1) { String next = baseLocation + "?page=" + (pageNo + 1) + "&pageSize=" + pageSize; if (commitsSize > 0) { next += "&" + GitConstants.KEY_TAG_COMMITS + "=" + commitsSize; } result.put(ProtocolConstants.KEY_NEXT_LOCATION, next); } for (int i = firstTag; i <= lastTag; i++) { Tag tag = tags.get(i); if (this.commitsSize == 0) { children.put(tag.toJSON()); } else { // add info about commits if requested LogCommand lc = git.log(); String toCommitName = tag.getRevCommitName(); ObjectId toCommitId = db.resolve(toCommitName); Ref toCommitRef = db.getRef(toCommitName); toCommitId = getCommitObjectId(db, toCommitId); lc.add(toCommitId); lc.setMaxCount(this.commitsSize); Iterable<RevCommit> commits = lc.call(); Log log = new Log(cloneLocation, db, commits, null, null, toCommitRef); children.put(tag.toJSON(log.toJSON(1, commitsSize))); } } result.put(ProtocolConstants.KEY_CHILDREN, children); result.put(ProtocolConstants.KEY_TYPE, Tag.TYPE); return new ServerStatus(Status.OK_STATUS, HttpServletResponse.SC_OK, result); } catch (Exception e) { String msg = NLS.bind("An error occured when listing tags for {0}", path); return new Status(IStatus.ERROR, GitActivator.PI_GIT, msg, e); } }
@Override public Stream<Revision> log() { try { final LogCommand logCommand = git.log(); return stream(logCommand.call()).map(this::asRevision); } catch (GitAPIException e) { throw new SvcFailedException(e); } }
private int countCommitsInHead() throws GitAPIException { LogCommand log = new Git(testRepository.getRepository()).log(); Iterable<RevCommit> commits = log.call(); int result = 0; for (Iterator i = commits.iterator(); i.hasNext(); ) { i.next(); result++; } return result; }
/** Gets the commit log and stores it in a project property as configured. */ @TaskAction public void reset() { final LogCommand cmd = getGit().log(); Repository repo = getGit().getRepository(); if (includes != null) { for (String include : includes) { try { ObjectId commit = repo.resolve(include); if (commit == null) { throw new GradleException("No commit found for revision string: " + include); } else { cmd.add(commit); } } catch (AmbiguousObjectException e) { throw new GradleException("Revision string is ambiguous: " + include, e); } catch (MissingObjectException e) { throw new GradleException("Commit could not be found in repository: " + include, e); } catch (IncorrectObjectTypeException e) { throw new GradleException("Revision string did not point to a commit: " + include, e); } catch (IOException e) { throw new GradleException("Problem resolving revision string: " + include, e); } } } if (excludes != null) { for (String exclude : excludes) { try { ObjectId commit = repo.resolve(exclude); if (commit == null) { throw new GradleException("No commit found for revision string: " + exclude); } else { cmd.add(commit); } } catch (AmbiguousObjectException e) { throw new GradleException("Revision string is ambiguous: " + exclude, e); } catch (MissingObjectException e) { throw new GradleException("Commit could not be found in repository: " + exclude, e); } catch (IncorrectObjectTypeException e) { throw new GradleException("Revision string did not point to a commit: " + exclude, e); } catch (IOException e) { throw new GradleException("Problem resolving revision string: " + exclude, e); } } } cmd.setSkip(skipCommits); cmd.setMaxCount(maxCommits); try { Iterable<RevCommit> commits = cmd.call(); List<Commit> tempLog = new ArrayList<Commit>(); for (RevCommit commit : commits) { tempLog.add(GitUtil.revCommitToCommit(commit)); } this.log = Collections.unmodifiableList(tempLog); } catch (GitAPIException e) { throw new GradleException("Problem with log.", e); } // TODO add progress monitor to log progress to Gradle status bar }
@Override protected IStatus performJob() { Repository db = null; try { File gitDir = GitUtils.getGitDir(path); db = FileRepositoryBuilder.create(gitDir); Git git = Git.wrap(db); List<Ref> branchRefs = git.branchList().call(); List<Branch> branches = new ArrayList<Branch>(branchRefs.size()); for (Ref ref : branchRefs) { if (nameFilter != null && !nameFilter.equals("")) { String shortName = Repository.shortenRefName(ref.getName()); if (shortName.toLowerCase().contains(nameFilter.toLowerCase())) { branches.add(new Branch(cloneLocation, db, ref)); } } else { branches.add(new Branch(cloneLocation, db, ref)); } } Collections.sort(branches, Branch.COMPARATOR); JSONObject result = new JSONObject(); JSONArray children = new JSONArray(); int firstBranch = pageSize > 0 ? pageSize * (pageNo - 1) : 0; int lastBranch = pageSize > 0 ? firstBranch + pageSize - 1 : branches.size() - 1; lastBranch = lastBranch > branches.size() - 1 ? branches.size() - 1 : lastBranch; if (pageNo > 1 && baseLocation != null) { String prev = baseLocation + "?page=" + (pageNo - 1) + "&pageSize=" + pageSize; if (nameFilter != null && !nameFilter.equals("")) { prev += "&filter=" + GitUtils.encode(nameFilter); } if (commitsSize > 0) { prev += "&" + GitConstants.KEY_TAG_COMMITS + "=" + commitsSize; } result.put(ProtocolConstants.KEY_PREVIOUS_LOCATION, prev); } if (lastBranch < branches.size() - 1) { String next = baseLocation + "?page=" + (pageNo + 1) + "&pageSize=" + pageSize; if (nameFilter != null && !nameFilter.equals("")) { next += "&filter=" + GitUtils.encode(nameFilter); } if (commitsSize > 0) { next += "&" + GitConstants.KEY_TAG_COMMITS + "=" + commitsSize; } result.put(ProtocolConstants.KEY_NEXT_LOCATION, next); } for (int i = firstBranch; i <= lastBranch; i++) { Branch branch = branches.get(i); if (commitsSize == 0) { children.put(branch.toJSON()); } else { String branchName = branch.getName(true, false); ObjectId toObjectId = db.resolve(branchName); Ref toRefId = db.getRef(branchName); if (toObjectId == null) { String msg = NLS.bind("No ref or commit found: {0}", branchName); return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, msg, null); } toObjectId = getCommitObjectId(db, toObjectId); Log log = null; // single commit is requested and we already know it, no need for LogCommand if (commitsSize == 1 && toObjectId instanceof RevCommit) { log = new Log( cloneLocation, db, Collections.singleton((RevCommit) toObjectId), null, null, toRefId); } else { LogCommand lc = git.log(); // set the commit range lc.add(toObjectId); lc.setMaxCount(this.commitsSize); Iterable<RevCommit> commits = lc.call(); log = new Log(cloneLocation, db, commits, null, null, toRefId); } log.setPaging(1, commitsSize); children.put(branch.toJSON(log.toJSON())); } } result.put(ProtocolConstants.KEY_CHILDREN, children); result.put(ProtocolConstants.KEY_TYPE, Branch.TYPE); return new ServerStatus(Status.OK_STATUS, HttpServletResponse.SC_OK, result); } catch (Exception e) { String msg = NLS.bind("An error occured when listing branches for {0}", path); return new Status(IStatus.ERROR, GitActivator.PI_GIT, msg, e); } finally { if (db != null) { db.close(); } } }