@@ -48,61 +48,11 @@ public void close() throws Exception {
4848 git .close ();
4949 }
5050
51- // Based on
52- // https://github.com/Cosium/git-code-format-maven-plugin/blob/master/src/main/java/com/cosium/code/format/AbstractMavenGitCodeFormatMojo.java
53- // MIT License
54- // Move to a provider?
55- public Repository gitRepository (File basedir ) throws IOException {
56- Repository gitRepository ;
57- FileRepositoryBuilder repositoryBuilder = new FileRepositoryBuilder ().findGitDir (basedir );
58- String gitIndexFileEnvVariable = System .getenv ("GIT_INDEX_FILE" );
59- if (Objects .nonNull (gitIndexFileEnvVariable )
60- && !gitIndexFileEnvVariable .trim ().isEmpty ()) {
61- log .debug ("Setting Index File based on Env Variable GIT_INDEX_FILE {}" , gitIndexFileEnvVariable );
62- repositoryBuilder = repositoryBuilder .setIndexFile (new File (gitIndexFileEnvVariable ));
63- }
64- gitRepository = repositoryBuilder .build ();
65-
66- return gitRepository ;
67- }
68-
6951 public File getGitDir (File basedir ) {
7052 FileRepositoryBuilder repositoryBuilder = new FileRepositoryBuilder ().findGitDir (basedir );
7153 return repositoryBuilder .getGitDir ();
7254 }
7355
74- // https://stackoverflow.com/a/19950970/346247
75- // and
76- // https://github.com/centic9/jgit-cookbook/blob/master/src/main/java/org/dstadler/jgit/api/ReadFileFromCommit.java
77- public Map <String , ByteArrayOutputStream > listRepositoryContentsAtHEAD (Repository repository ) throws IOException {
78- Ref head = repository .exactRef ("HEAD" );
79- // a RevWalk allows us to walk over commits based on some filtering that is defined
80- RevWalk walk = new RevWalk (repository );
81- RevCommit commit = walk .parseCommit (head .getObjectId ());
82- RevTree tree = commit .getTree ();
83-
84- TreeWalk treeWalk = new TreeWalk (repository );
85- treeWalk .addTree (tree );
86- treeWalk .setRecursive (false );
87-
88- // TODO: extract rest of this method to test it
89- Map <String , ByteArrayOutputStream > fileContentsCollection = new HashMap <>();
90- while (treeWalk .next ()) {
91- if (treeWalk .isSubtree ()) {
92- treeWalk .enterSubtree ();
93- } else {
94- if (treeWalk .getPathString ().endsWith (JAVA_FILE_TYPE )) {
95- ObjectId objectId = treeWalk .getObjectId (0 );
96- ObjectLoader loader = repository .open (objectId );
97- ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
98- loader .copyTo (outputStream );
99- fileContentsCollection .put (treeWalk .getPathString (), outputStream );
100- }
101- }
102- }
103- return fileContentsCollection ;
104- }
105-
10656 // log --follow implementation may be worth adopting in the future
10757 // https://github.com/spearce/jgit/blob/master/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/RevWalkTextBuiltin.java
10858
0 commit comments