001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.maven.scm.provider.git.jgit.command.diff; 020 021import java.io.ByteArrayOutputStream; 022import java.io.IOException; 023import java.io.OutputStream; 024 025import org.apache.maven.scm.ScmException; 026import org.apache.maven.scm.ScmFileSet; 027import org.apache.maven.scm.ScmVersion; 028import org.apache.maven.scm.command.diff.AbstractDiffCommand; 029import org.apache.maven.scm.command.diff.DiffScmResult; 030import org.apache.maven.scm.provider.ScmProviderRepository; 031import org.apache.maven.scm.provider.git.command.GitCommand; 032import org.apache.maven.scm.provider.git.command.diff.GitDiffConsumer; 033import org.apache.maven.scm.provider.git.jgit.command.JGitUtils; 034import org.eclipse.jgit.api.Git; 035import org.eclipse.jgit.api.errors.GitAPIException; 036import org.eclipse.jgit.lib.ObjectId; 037import org.eclipse.jgit.lib.ObjectReader; 038import org.eclipse.jgit.lib.Repository; 039import org.eclipse.jgit.revwalk.RevWalk; 040import org.eclipse.jgit.treewalk.AbstractTreeIterator; 041import org.eclipse.jgit.treewalk.CanonicalTreeParser; 042 043/** 044 * @author Dominik Bartholdi (imod) 045 * @since 1.9 046 */ 047public class JGitDiffCommand extends AbstractDiffCommand implements GitCommand { 048 049 @Override 050 protected DiffScmResult executeDiffCommand( 051 ScmProviderRepository repository, ScmFileSet fileSet, ScmVersion startRevision, ScmVersion endRevision) 052 throws ScmException { 053 054 Git git = null; 055 try { 056 git = JGitUtils.openRepo(fileSet.getBasedir()); 057 DiffScmResult diff = callDiff(git, startRevision, endRevision); 058 git.getRepository().close(); 059 return diff; 060 } catch (Exception e) { 061 throw new ScmException("JGit diff failure!", e); 062 } finally { 063 JGitUtils.closeRepo(git); 064 } 065 } 066 067 public DiffScmResult callDiff(Git git, ScmVersion startRevision, ScmVersion endRevision) 068 throws IOException, GitAPIException, ScmException { 069 070 AbstractTreeIterator oldTree = null; 071 if (startRevision != null && !startRevision.getName().trim().isEmpty()) { 072 String startRev = startRevision.getName().trim(); 073 oldTree = getTreeIterator(git.getRepository(), startRev); 074 } 075 076 AbstractTreeIterator newTree = null; 077 if (endRevision != null && !endRevision.getName().trim().isEmpty()) { 078 String endRev = endRevision.getName().trim(); 079 newTree = getTreeIterator(git.getRepository(), endRev); 080 } 081 082 OutputStream out = new ByteArrayOutputStream(); 083 084 git.diff() 085 .setOutputStream(out) 086 .setOldTree(oldTree) 087 .setNewTree(newTree) 088 .setCached(false) 089 .call(); 090 git.diff() 091 .setOutputStream(out) 092 .setOldTree(oldTree) 093 .setNewTree(newTree) 094 .setCached(true) 095 .call(); 096 097 out.flush(); 098 099 GitDiffConsumer consumer = new GitDiffConsumer(null); 100 String fullDiff = out.toString(); 101 out.close(); 102 103 String[] lines = fullDiff.split("\n"); 104 for (String aLine : lines) { 105 consumer.consumeLine(aLine); 106 } 107 108 return new DiffScmResult( 109 "JGit diff", consumer.getChangedFiles(), consumer.getDifferences(), consumer.getPatch()); 110 } 111 112 private AbstractTreeIterator getTreeIterator(Repository repo, String name) throws IOException { 113 final ObjectId id = repo.resolve(name); 114 if (id == null) { 115 throw new IllegalArgumentException(name); 116 } 117 final CanonicalTreeParser p = new CanonicalTreeParser(); 118 119 try (ObjectReader or = repo.newObjectReader(); 120 RevWalk revWalk = new RevWalk(repo)) { 121 p.reset(or, revWalk.parseTree(id)); 122 return p; 123 } 124 } 125}