diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..c5f3f6b --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "java.configuration.updateBuildConfiguration": "interactive" +} \ No newline at end of file diff --git a/RepositoryMinerForGit/pom.xml b/RepositoryMinerForGit/pom.xml index 6d451d6..125b127 100644 --- a/RepositoryMinerForGit/pom.xml +++ b/RepositoryMinerForGit/pom.xml @@ -10,5 +10,11 @@ org.eclipse.jgit 3.5.0.201409260305-r + + + org.hibernate + hibernate-core + 5.4.4.Final + \ No newline at end of file diff --git a/RepositoryMinerForGit/src/main/java/Commit.java b/RepositoryMinerForGit/src/main/java/Commit.java new file mode 100644 index 0000000..0f00ff7 --- /dev/null +++ b/RepositoryMinerForGit/src/main/java/Commit.java @@ -0,0 +1,153 @@ +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.ArrayList; +import java.util.HashSet; + +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 Commit { + + public void runCommit(String repoUrl) { + + int commitCount = 0; + List listAccount = new ArrayList(); + ArrayList afterListAccount = new ArrayList(); + + // 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( repoUrl ).setDirectory( project ).call(); + } + + PlotWalk revWalk = new PlotWalk(repo); + ObjectId rootId = repo.resolve("HEAD"); + RevCommit root = revWalk.parseCommit(rootId); + revWalk.markStart(root); + + // コミットコメント + PlotCommitList plotCommitList = new PlotCommitList(); + plotCommitList.source(revWalk); + plotCommitList.fillTo(Integer.MAX_VALUE); + + for (PlotCommit c: plotCommitList) { + System.out.println("\n=▼= 最新から" + (commitCount+1) + "コミット目 =▼=\n"); + + //詳細情報 + System.out.println("アカウント: "+ c.getAuthorIdent().getName()); + System.out.println("日付: "+ c.getAuthorIdent().getWhen()); + // コミットメッセージ + 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 entries = df.scan( oldTreeIter, newTreeIter ); + + for( DiffEntry entry : entries ) { + // ファイル毎の情報 + System.out.println("パス: " + entry.getNewPath()); + System.out.println("変更の種類: " + entry.getChangeType()); + // System.out.println("変更の内容: " + entry); + } + } + + // リストの重複を確認して追加 + String item = c.getAuthorIdent().getName(); + int i = 0; + for (; i < listAccount.size(); i++) { + if (item.equals(listAccount.get(i))) { + break; + } + } + if (i == listAccount.size()) { + listAccount.add(item); + } + + commitCount += 1; + } + } catch (IOException e) { + e.printStackTrace(); + } catch (InvalidRemoteException e) { + e.printStackTrace(); + } catch (TransportException e) { + e.printStackTrace(); + } catch (GitAPIException e) { + e.printStackTrace(); + } + + System.out.println("\n================"); + System.out.println("=== 解析結果 ==="); + System.out.println("================\n"); + System.out.println("コミット数:" + commitCount); + System.out.println("アカウントリスト: " + listAccount); + + System.out.println(""); + } + + public 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(); + } + +} diff --git a/RepositoryMinerForGit/src/main/java/Main.java b/RepositoryMinerForGit/src/main/java/Main.java new file mode 100644 index 0000000..4ba56bb --- /dev/null +++ b/RepositoryMinerForGit/src/main/java/Main.java @@ -0,0 +1,68 @@ +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + Integer[] selectNum = {-1, -1}; + Integer[] selectFlag = {0, 0}; + + String[] repo = {"acanthus_client", "acanthus_server", "amaryllis_server"}; + + // 解析するレポジトリを選択 + System.out.println("解析するレポジトリを選択してください。"); + System.out.println("0:" + repo[0] + ", 1:" + repo[1] + ", 2:" + repo[2]); + do { + selectNum[0] = Integer.parseInt(scanner.next()); + if (selectNum[0] == 0 || selectNum[0] == 1 || selectNum[0] == 2) { + selectFlag[0] = 1; + } else { + System.out.println("正しい値を入力してください。"); + } + } while (selectFlag[0] != 1); + System.out.println("\n--- " + repo[selectNum[0]] + "を選択 ---\n"); + String repoUrl = selectRepo(selectNum[0]); // レポジトリのURL + + // 解析するGitアクティビティを選択 + System.out.println("解析するGitアクティビティを選択してください。"); + System.out.println("0:Commit, 1:PullRequest, 2:Review"); + do { + selectNum[1] = Integer.parseInt(scanner.next()); + if (selectNum[1] == 0 || selectNum[1] == 1 || selectNum[1] == 2) { + selectFlag[1] = 1; + } else { + System.out.println("正しい値を入力してください。"); + } + } while (selectFlag[1] != 1); + selectGit(selectNum[1], repoUrl); + + scanner.close(); + } + + // レポジトリの選択処理 + public static String selectRepo(Integer selectNum) { + String repoUrl[] = { + "http://nitta-lab-www.is.konan-u.ac.jp/gitbucket/git/nitta-lab-2021/org.ntlab.acanthus_client.git", + "http://nitta-lab-www.is.konan-u.ac.jp/gitbucket/git/nitta-lab-2021/org.ntlab.acanthus_server.git", + "http://nitta-lab-www.is.konan-u.ac.jp/gitbucket/git/nitta-lab-2020/org.ntlab.amaryllis.server.git"}; + + return repoUrl[selectNum]; + } + + // Gitアクティビティの選択処理 + public static void selectGit(Integer selectNum, String repoUrl) { + switch (selectNum) { + case 0: // Commit + System.out.println("\n--- Commitを解析 ---\n"); + Commit commit = new Commit(); + commit.runCommit(repoUrl); + break; + case 1: // PullRequest + System.out.println("\n--- Pull Requestを解析 ---\n"); + break; + case 2: // Review + System.out.println("\n--- Reviewを解析 ---\n"); + break; + } + } +} diff --git a/RepositoryMinerForGit/src/main/java/Test.java b/RepositoryMinerForGit/src/main/java/Test.java deleted file mode 100644 index 792a953..0000000 --- a/RepositoryMinerForGit/src/main/java/Test.java +++ /dev/null @@ -1,131 +0,0 @@ -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 plotCommitList = new PlotCommitList(); - plotCommitList.source(revWalk); - plotCommitList.fillTo(Integer.MAX_VALUE); - - for (PlotCommit 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 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(); - } - -}