Newer
Older
RepositoryMinerForGit / RepositoryMinerForGit / src / main / java / Test.java
import java.io.File;
import java.nio.file.Path;
import java.io.IOException;
import java.util.List;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.InvalidRemoteException;
import org.eclipse.jgit.api.errors.TransportException;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.diff.DiffFormatter;
import org.eclipse.jgit.diff.RawTextComparator;
import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectLoader;
import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revplot.PlotCommit;
import org.eclipse.jgit.revplot.PlotCommitList;
import org.eclipse.jgit.revplot.PlotLane;
import org.eclipse.jgit.revplot.PlotWalk;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevObject;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.treewalk.CanonicalTreeParser;
import org.eclipse.jgit.util.io.DisabledOutputStream;

public class Test {

	public static void main(String[] args) {

		String amaryllis_server = "http://nitta-lab-www.is.konan-u.ac.jp/gitbucket/git/nitta-lab-2020/org.ntlab.amaryllis.server.git";
		String acanthus_client  = "http://nitta-lab-www.is.konan-u.ac.jp/gitbucket/git/nitta-lab-2021/org.ntlab.acanthus_client.git";
		String acanthus_server  = "http://nitta-lab-www.is.konan-u.ac.jp/gitbucket/git/nitta-lab-2021/org.ntlab.acanthus_server.git";

		// TODO Auto-generated method stub
		try {
			Repository repo = new FileRepository( "./project/.git" );
			Git git = new Git( repo );

			if( git != null ){
				//. git clone
				File project = new File( "./project" );

				String filePath = "./project";

				// フォルダの存在を確認する
				if (project.exists()) {
					System.out.println("projectフォルダが存在します");
					delete(filePath);
				} else {
					System.out.println("projectフォルダが存在しません");
				}

				git.cloneRepository().setURI( acanthus_client ).setDirectory( project ).call();
			}
			
			PlotWalk revWalk = new PlotWalk(repo);
			ObjectId rootId = repo.resolve("HEAD");
			RevCommit root = revWalk.parseCommit(rootId);
			revWalk.markStart(root);

			// コミットコメント
			PlotCommitList<PlotLane> plotCommitList = new PlotCommitList<PlotLane>();
			plotCommitList.source(revWalk);
			plotCommitList.fillTo(Integer.MAX_VALUE);

			for (PlotCommit<PlotLane> c: plotCommitList) {
				// コミットメッセージ
				System.out.println("コミットメッセージ: " + c.getFullMessage());
				ObjectId curId = c.getTree();
				if (c.getParentCount() == 0) {
					// 初期コミット
					System.out.println("id:" + c.getId());
				} else {
					// 親コミットとの差分
					ObjectId parentId = c.getParents()[0].getTree();
					System.out.println("id:" + c.getId() + " <== " + c.getParents()[0].getId());
					
					DiffFormatter df = new DiffFormatter(DisabledOutputStream.INSTANCE);
					df.setRepository(repo);
					df.setDiffComparator(RawTextComparator.DEFAULT);
					df.setDetectRenames(true);

					ObjectReader reader = repo.newObjectReader();
					CanonicalTreeParser newTreeIter = new CanonicalTreeParser();
					newTreeIter.reset(reader, curId);
					CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();
					oldTreeIter.reset(reader, parentId);
					List<DiffEntry> entries = df.scan( oldTreeIter, newTreeIter );

					for( DiffEntry entry : entries ) {
						// ファイル毎の情報
						System.out.println("パス: " + entry.getNewPath());
						System.out.println("変更の種類: " + entry.getChangeType());
						// System.out.println("変更の内容: " + entry);
					}
				}
	            System.out.println("=====");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InvalidRemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TransportException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (GitAPIException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void delete(String path) {
        File filePath = new File(path);
        String[] list = filePath.list();
        for(String file : list) {
            File f = new File(path + File.separator + file);
            if(f.isDirectory()) {
                delete(path + File.separator + file);
            }else {
                f.delete();
            }
        }
        filePath.delete();
    }

}