Skip to content

Commit bd6912b

Browse files
committed
add new option to generate a MD report in addition to logs
1 parent ddc080e commit bd6912b

File tree

7 files changed

+119
-29
lines changed

7 files changed

+119
-29
lines changed

sync-git-submodules-branches/.classpath

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,18 @@
2323
<attribute name="maven.pomderived" value="true"/>
2424
</attributes>
2525
</classpathentry>
26+
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
27+
<attributes>
28+
<attribute name="maven.pomderived" value="true"/>
29+
<attribute name="optional" value="true"/>
30+
</attributes>
31+
</classpathentry>
32+
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
33+
<attributes>
34+
<attribute name="maven.pomderived" value="true"/>
35+
<attribute name="test" value="true"/>
36+
<attribute name="optional" value="true"/>
37+
</attributes>
38+
</classpathentry>
2639
<classpathentry kind="output" path="target/classes"/>
2740
</classpath>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
eclipse.preferences.version=1
22
encoding//src/main/java=UTF-8
3+
encoding//src/main/resources=UTF-8
34
encoding/<project>=UTF-8

sync-git-submodules-branches/.settings/org.eclipse.jdt.core.prefs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ org.eclipse.jdt.core.compiler.debug.lineNumber=generate
88
org.eclipse.jdt.core.compiler.debug.localVariable=generate
99
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
1010
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
11+
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
1112
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
1213
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
14+
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
1315
org.eclipse.jdt.core.compiler.release=disabled
1416
org.eclipse.jdt.core.compiler.source=1.8

sync-git-submodules-branches/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>org.gemoc.git-sync-tools</groupId>
77
<artifactId>sync-git-submodules-branches-plugin</artifactId>
8-
<version>1.0.4</version>
8+
<version>1.1.0</version>
99
<packaging>maven-plugin</packaging>
1010

1111
<name>sync-git-submodules-branches Maven Plugin</name>
@@ -107,12 +107,12 @@
107107
<goal>descriptor</goal>
108108
</goals>
109109
</execution>
110-
<execution>
110+
<!--<execution>
111111
<id>help-goal</id>
112112
<goals>
113113
<goal>helpmojo</goal>
114114
</goals>
115-
</execution>
115+
</execution>-->
116116
</executions>
117117
</plugin>
118118
<plugin>

sync-git-submodules-branches/src/main/java/org/gemoc/sync_git_submodules_branches/SyncGitSubModulesBranchesCLI.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.gemoc.sync_git_submodules_branches;
22

33
import java.io.File;
4+
import java.io.IOException;
5+
import java.nio.charset.Charset;
46
import java.nio.file.Files;
57
import java.util.Set;
68

@@ -25,6 +27,8 @@ public static void main(String[] args) throws Exception {
2527
.addOption("g", "gitURL", true, "git URL that will be cloned")
2628
.addOption("c", "committerName", true, "name of the committer who'll sign the commit")
2729
.addOption("e", "committerEmail", true, "email of the committer who'll sign the commit")
30+
.addOption("d", "dryRun", false, "dryRun, do not commit and push the update")
31+
.addOption("r", "reportFile", true, "file name tha will containt the markdown report")
2832
.addOption("i", "inactivityThreshold", true, "number of days since the last commit of a specific branch before considering the branch as old/unmaintained/inactive (-1 for infinite duration)");
2933

3034

@@ -41,6 +45,8 @@ public static void main(String[] args) throws Exception {
4145
String committerName = cmd.hasOption("c") ? cmd.getOptionValue("c") : "";
4246
String committerEmail = cmd.hasOption("e") ? cmd.getOptionValue("e") : "";
4347
String inactivityThreshold = cmd.hasOption("i") ? cmd.getOptionValue("i") : "90";
48+
String reportFilePath = cmd.hasOption("r") ? cmd.getOptionValue("r") : "syncReport.md";
49+
boolean dryRun = cmd.hasOption("d");
4450

4551
if(parentGitURL.isEmpty()) {
4652
HelpFormatter formatter = new HelpFormatter();
@@ -66,13 +72,24 @@ public static void main(String[] args) throws Exception {
6672
Set<String> relevantBranches = gitManager.collectAllSubmodulesActiveRemoteBranches(Integer.parseInt(inactivityThreshold));
6773
gitManager.deleteBranchesNotIn(relevantBranches);
6874
gitManager.createMissingParentBranches(relevantBranches);
69-
gitManager.updateAllBranchesModules();
70-
75+
StringBuffer sb = new StringBuffer();
76+
gitManager.updateAllBranchesModules(sb, dryRun);
77+
FileUtils.write(outputDirectory, sb.toString(), Charset.defaultCharset());
78+
writeReport(new File(reportFilePath), sb);
7179
if(directoryPath.isEmpty()) {
7280
// must delete the temp dir
7381
System.out.println("Deleting temp directory "+outputDirectory);
7482
FileUtils.deleteDirectory(outputDirectory);
7583
}
7684
}
85+
86+
protected static void writeReport(File reportFile, StringBuffer content) throws IOException {
87+
// Ensure the parent directory exists
88+
File parentDir = reportFile.getParentFile();
89+
if (parentDir != null && !parentDir.exists()) {
90+
parentDir.mkdirs();
91+
}
92+
FileUtils.write(reportFile, content.toString(), Charset.defaultCharset());
93+
}
7794

7895
}

sync-git-submodules-branches/src/main/java/org/gemoc/sync_git_submodules_branches/SyncGitSubmodulesBranchesMojo.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package org.gemoc.sync_git_submodules_branches;
22

33
import java.io.File;
4+
import java.io.IOException;
5+
import java.nio.charset.Charset;
46
import java.util.Set;
57

8+
import org.apache.commons.io.FileUtils;
9+
610
/*
711
* Copyright 2001-2005 The Apache Software Foundation.
812
*
@@ -38,11 +42,17 @@ public class SyncGitSubmodulesBranchesMojo
3842
extends AbstractMojo
3943
{
4044
/**
41-
* Location of the file.
45+
* Location of the git repository.
4246
*/
4347
@Parameter( defaultValue = "${project.build.directory}/syncgitsubmodules_repo", property = "outputDir", required = true )
4448
private File outputDirectory;
4549

50+
/**
51+
* Location of the report file.
52+
*/
53+
@Parameter( defaultValue = "${project.build.directory}/syncReport.md", property = "reportFile", required = true )
54+
private File reportFile;
55+
4656
@Parameter(property="parentGitURL", required = true)
4757
private String parentGitURL;
4858

@@ -59,6 +69,9 @@ public class SyncGitSubmodulesBranchesMojo
5969
@Parameter(defaultValue = "", property="committerName")
6070
private String committerName;
6171

72+
@Parameter(defaultValue = "false", property="dryRun")
73+
private boolean dryRun;
74+
6275
/**
6376
* number of days since the last commit of a specific branch
6477
* The branch will be considered old/unmaintained /inactive
@@ -91,12 +104,28 @@ public void execute()
91104
Set<String> relevantBranches = gitManager.collectAllSubmodulesActiveRemoteBranches(inactivityThreshold);
92105
gitManager.deleteBranchesNotIn(relevantBranches);
93106
gitManager.createMissingParentBranches(relevantBranches);
94-
gitManager.updateAllBranchesModules();
107+
StringBuffer sb = new StringBuffer();
108+
gitManager.updateAllBranchesModules(sb, dryRun);
109+
writeReport(sb);
110+
95111
} catch (Exception e) {
96112
getLog().error( e);
97113
throw new MojoExecutionException(e.getMessage(), e);
98114
}
99115

100116

101117
}
118+
119+
protected void writeReport(StringBuffer content) throws MojoExecutionException {
120+
// Ensure the parent directory exists
121+
File parentDir = reportFile.getParentFile();
122+
if (parentDir != null && !parentDir.exists()) {
123+
parentDir.mkdirs();
124+
}
125+
try {
126+
FileUtils.write(reportFile, content.toString(), Charset.defaultCharset());
127+
} catch (IOException e) {
128+
throw new MojoExecutionException("Error writing to file", e);
129+
}
130+
}
102131
}

sync-git-submodules-branches/src/main/java/org/gemoc/sync_git_submodules_branches/gittool/GitModuleManager.java

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,15 @@ public void createBranchForModules(Git parentgit, String missingParentBranch)
382382
}
383383
}
384384

385-
public void updateAllBranchesModules() throws IOException, GitAPIException, GitSyncError, ConfigInvalidException {
385+
/**
386+
*
387+
* @param dryRun report only, do not perform changes
388+
* @throws IOException
389+
* @throws GitAPIException
390+
* @throws GitSyncError
391+
* @throws ConfigInvalidException
392+
*/
393+
public void updateAllBranchesModules(StringBuffer reportBuffer, boolean dryRun) throws IOException, GitAPIException, GitSyncError, ConfigInvalidException {
386394
FileRepositoryBuilder builder = new FileRepositoryBuilder();
387395

388396
try (Repository parentRepository = builder.setMustExist(true).setGitDir(new File(localGitFolder + "/.git"))
@@ -394,12 +402,15 @@ public void updateAllBranchesModules() throws IOException, GitAPIException, GitS
394402
for (Ref ref : call) {
395403
if (ref.getName().startsWith("refs/remotes/origin/")) {
396404
updateBranchesForModules(parentgit,
397-
ref.getName().substring("refs/remotes/origin/".length()));
405+
ref.getName().substring("refs/remotes/origin/".length()),
406+
reportBuffer,
407+
dryRun);
398408
}
399409
}
400410
}
401411
}
402412
}
413+
403414

404415
/**
405416
*
@@ -411,9 +422,13 @@ public void updateAllBranchesModules() throws IOException, GitAPIException, GitS
411422
* @throws IOException
412423
* @throws ConfigInvalidException
413424
*/
414-
public void updateBranchesForModules(Git parentgit, String consideredBranch)
425+
public void updateBranchesForModules(Git parentgit, String consideredBranch, StringBuffer reportBuffer, boolean dryRun)
415426
throws GitAPIException, GitSyncError, IOException, ConfigInvalidException {
416427
logger.info("updateBranchesForModules branch = " + consideredBranch);
428+
reportBuffer.append(String.format("**Branch %s**\n", consideredBranch));
429+
reportBuffer.append("\n"
430+
+ "| Module | Branch |\n"
431+
+ "|:---------- |:---------- |\n");
417432
// switch parentGit to branch
418433
checkoutBranch(parentgit, consideredBranch);
419434

@@ -442,6 +457,7 @@ public void updateBranchesForModules(Git parentgit, String consideredBranch)
442457
}
443458
}
444459
logger.info(String.format(" tracking module %-32s on branch "+trackedBranchName, walk.getModuleName()));
460+
445461

446462
// Make sure the parent repo knows that its submodule now tracks a branch:
447463
FileBasedConfig modulesConfig = new FileBasedConfig(new File(
@@ -475,6 +491,7 @@ public void updateBranchesForModules(Git parentgit, String consideredBranch)
475491
logger.debug("\t\tUntracked: " + status.getUntracked());
476492
logger.debug("\t\tUntrackedFolders: " + status.getUntrackedFolders());
477493
}
494+
String branchModifier = "";
478495
if(status.getAdded().size() + status.getChanged().size() +status.getRemoved().size() > 0) {
479496
String msg;
480497
PersonIdent committer;
@@ -493,13 +510,19 @@ public void updateBranchesForModules(Git parentgit, String consideredBranch)
493510
msg = "Updating submodule "+walk.getModuleName()+" to track head of branch "+trackedBranchName;
494511
committer = defaultCommitter;
495512
}
496-
logger.debug("\t\tgit commit -m \""+msg+"\"");
497-
parentgit.commit()
498-
.setMessage(msg)
499-
.setAllowEmpty(false)
500-
.setCommitter(committer)
501-
.call();
513+
branchModifier = "🔄";
514+
if(! dryRun) {
515+
logger.debug("\t\tgit commit -m \""+msg+"\"");
516+
parentgit.commit()
517+
.setMessage(msg)
518+
.setAllowEmpty(false)
519+
.setCommitter(committer)
520+
.call();
521+
} else {
522+
logger.info("\t\t[DRYRUN] git commit -m \""+msg+"\"");
523+
}
502524
}
525+
reportBuffer.append(String.format("| %-32s | %s %-16s |\n", walk.getModuleName(),branchModifier, trackedBranchName));
503526
}
504527
}
505528

@@ -508,22 +531,27 @@ public void updateBranchesForModules(Git parentgit, String consideredBranch)
508531
logger.info(
509532
"\tupdating submodules: " + s);
510533
}*/
511-
Iterable<PushResult> pushResps = parentgit.push()
512-
.setCredentialsProvider(credentialProvider)
513-
.call();
514-
for (PushResult pushRes : pushResps) {
515-
for (RemoteRefUpdate pushResult : pushRes.getRemoteUpdates()) {
516-
if(pushResult.getStatus() == RemoteRefUpdate.Status.OK) {
517-
logger.info("push branch "+consideredBranch+" => "+RemoteRefUpdate.Status.OK);
518-
} else if(pushResult.getStatus() == RemoteRefUpdate.Status.UP_TO_DATE) {
519-
logger.info("nothing to push for branch "+consideredBranch+" => "+RemoteRefUpdate.Status.UP_TO_DATE);
520-
521-
} else {
522-
logger.error("PB pushing branch "+consideredBranch+" => "+pushRes.getMessages()+"\" "+pushResult);
534+
if(!dryRun) {
535+
Iterable<PushResult> pushResps = parentgit.push()
536+
.setCredentialsProvider(credentialProvider)
537+
.call();
538+
for (PushResult pushRes : pushResps) {
539+
for (RemoteRefUpdate pushResult : pushRes.getRemoteUpdates()) {
540+
if(pushResult.getStatus() == RemoteRefUpdate.Status.OK) {
541+
logger.info("push branch "+consideredBranch+" => "+RemoteRefUpdate.Status.OK);
542+
} else if(pushResult.getStatus() == RemoteRefUpdate.Status.UP_TO_DATE) {
543+
logger.info("nothing to push for branch "+consideredBranch+" => "+RemoteRefUpdate.Status.UP_TO_DATE);
544+
545+
} else {
546+
logger.error("PB pushing branch "+consideredBranch+" => "+pushRes.getMessages()+"\" "+pushResult);
547+
}
523548
}
549+
validateRemoteRefUpdates("push submodule tracking branch", pushRes.getRemoteUpdates());
524550
}
525-
validateRemoteRefUpdates("push submodule tracking branch", pushRes.getRemoteUpdates());
551+
} else {
552+
logger.info("\t\t[DRYRUN] not pushing branch "+consideredBranch);
526553
}
554+
reportBuffer.append("\n");
527555
}
528556
}
529557

0 commit comments

Comments
 (0)