From bd6912b4a7d2fdf534a455deb56c849285fd633c Mon Sep 17 00:00:00 2001 From: Didier Vojtisek Date: Fri, 13 Sep 2024 09:43:38 +0200 Subject: [PATCH 1/4] add new option to generate a MD report in addition to logs --- sync-git-submodules-branches/.classpath | 13 ++++ .../org.eclipse.core.resources.prefs | 1 + .../.settings/org.eclipse.jdt.core.prefs | 2 + sync-git-submodules-branches/pom.xml | 6 +- .../SyncGitSubModulesBranchesCLI.java | 21 +++++- .../SyncGitSubmodulesBranchesMojo.java | 33 ++++++++- .../gittool/GitModuleManager.java | 72 +++++++++++++------ 7 files changed, 119 insertions(+), 29 deletions(-) diff --git a/sync-git-submodules-branches/.classpath b/sync-git-submodules-branches/.classpath index 5e8a55f..6d619b3 100644 --- a/sync-git-submodules-branches/.classpath +++ b/sync-git-submodules-branches/.classpath @@ -23,5 +23,18 @@ + + + + + + + + + + + + + diff --git a/sync-git-submodules-branches/.settings/org.eclipse.core.resources.prefs b/sync-git-submodules-branches/.settings/org.eclipse.core.resources.prefs index e9441bb..abdea9a 100644 --- a/sync-git-submodules-branches/.settings/org.eclipse.core.resources.prefs +++ b/sync-git-submodules-branches/.settings/org.eclipse.core.resources.prefs @@ -1,3 +1,4 @@ eclipse.preferences.version=1 encoding//src/main/java=UTF-8 +encoding//src/main/resources=UTF-8 encoding/=UTF-8 diff --git a/sync-git-submodules-branches/.settings/org.eclipse.jdt.core.prefs b/sync-git-submodules-branches/.settings/org.eclipse.jdt.core.prefs index 91ca62e..8b5c4dc 100644 --- a/sync-git-submodules-branches/.settings/org.eclipse.jdt.core.prefs +++ b/sync-git-submodules-branches/.settings/org.eclipse.jdt.core.prefs @@ -8,7 +8,9 @@ org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore org.eclipse.jdt.core.compiler.release=disabled org.eclipse.jdt.core.compiler.source=1.8 diff --git a/sync-git-submodules-branches/pom.xml b/sync-git-submodules-branches/pom.xml index db90aac..d219fdc 100644 --- a/sync-git-submodules-branches/pom.xml +++ b/sync-git-submodules-branches/pom.xml @@ -5,7 +5,7 @@ org.gemoc.git-sync-tools sync-git-submodules-branches-plugin - 1.0.4 + 1.1.0 maven-plugin sync-git-submodules-branches Maven Plugin @@ -107,12 +107,12 @@ descriptor - + diff --git a/sync-git-submodules-branches/src/main/java/org/gemoc/sync_git_submodules_branches/SyncGitSubModulesBranchesCLI.java b/sync-git-submodules-branches/src/main/java/org/gemoc/sync_git_submodules_branches/SyncGitSubModulesBranchesCLI.java index 4d8647c..0b86744 100644 --- a/sync-git-submodules-branches/src/main/java/org/gemoc/sync_git_submodules_branches/SyncGitSubModulesBranchesCLI.java +++ b/sync-git-submodules-branches/src/main/java/org/gemoc/sync_git_submodules_branches/SyncGitSubModulesBranchesCLI.java @@ -1,6 +1,8 @@ package org.gemoc.sync_git_submodules_branches; import java.io.File; +import java.io.IOException; +import java.nio.charset.Charset; import java.nio.file.Files; import java.util.Set; @@ -25,6 +27,8 @@ public static void main(String[] args) throws Exception { .addOption("g", "gitURL", true, "git URL that will be cloned") .addOption("c", "committerName", true, "name of the committer who'll sign the commit") .addOption("e", "committerEmail", true, "email of the committer who'll sign the commit") + .addOption("d", "dryRun", false, "dryRun, do not commit and push the update") + .addOption("r", "reportFile", true, "file name tha will containt the markdown report") .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)"); @@ -41,6 +45,8 @@ public static void main(String[] args) throws Exception { String committerName = cmd.hasOption("c") ? cmd.getOptionValue("c") : ""; String committerEmail = cmd.hasOption("e") ? cmd.getOptionValue("e") : ""; String inactivityThreshold = cmd.hasOption("i") ? cmd.getOptionValue("i") : "90"; + String reportFilePath = cmd.hasOption("r") ? cmd.getOptionValue("r") : "syncReport.md"; + boolean dryRun = cmd.hasOption("d"); if(parentGitURL.isEmpty()) { HelpFormatter formatter = new HelpFormatter(); @@ -66,13 +72,24 @@ public static void main(String[] args) throws Exception { Set relevantBranches = gitManager.collectAllSubmodulesActiveRemoteBranches(Integer.parseInt(inactivityThreshold)); gitManager.deleteBranchesNotIn(relevantBranches); gitManager.createMissingParentBranches(relevantBranches); - gitManager.updateAllBranchesModules(); - + StringBuffer sb = new StringBuffer(); + gitManager.updateAllBranchesModules(sb, dryRun); + FileUtils.write(outputDirectory, sb.toString(), Charset.defaultCharset()); + writeReport(new File(reportFilePath), sb); if(directoryPath.isEmpty()) { // must delete the temp dir System.out.println("Deleting temp directory "+outputDirectory); FileUtils.deleteDirectory(outputDirectory); } } + + protected static void writeReport(File reportFile, StringBuffer content) throws IOException { + // Ensure the parent directory exists + File parentDir = reportFile.getParentFile(); + if (parentDir != null && !parentDir.exists()) { + parentDir.mkdirs(); + } + FileUtils.write(reportFile, content.toString(), Charset.defaultCharset()); + } } diff --git a/sync-git-submodules-branches/src/main/java/org/gemoc/sync_git_submodules_branches/SyncGitSubmodulesBranchesMojo.java b/sync-git-submodules-branches/src/main/java/org/gemoc/sync_git_submodules_branches/SyncGitSubmodulesBranchesMojo.java index 3c8d88b..850f4a4 100644 --- a/sync-git-submodules-branches/src/main/java/org/gemoc/sync_git_submodules_branches/SyncGitSubmodulesBranchesMojo.java +++ b/sync-git-submodules-branches/src/main/java/org/gemoc/sync_git_submodules_branches/SyncGitSubmodulesBranchesMojo.java @@ -1,8 +1,12 @@ package org.gemoc.sync_git_submodules_branches; import java.io.File; +import java.io.IOException; +import java.nio.charset.Charset; import java.util.Set; +import org.apache.commons.io.FileUtils; + /* * Copyright 2001-2005 The Apache Software Foundation. * @@ -38,11 +42,17 @@ public class SyncGitSubmodulesBranchesMojo extends AbstractMojo { /** - * Location of the file. + * Location of the git repository. */ @Parameter( defaultValue = "${project.build.directory}/syncgitsubmodules_repo", property = "outputDir", required = true ) private File outputDirectory; + /** + * Location of the report file. + */ + @Parameter( defaultValue = "${project.build.directory}/syncReport.md", property = "reportFile", required = true ) + private File reportFile; + @Parameter(property="parentGitURL", required = true) private String parentGitURL; @@ -59,6 +69,9 @@ public class SyncGitSubmodulesBranchesMojo @Parameter(defaultValue = "", property="committerName") private String committerName; + @Parameter(defaultValue = "false", property="dryRun") + private boolean dryRun; + /** * number of days since the last commit of a specific branch * The branch will be considered old/unmaintained /inactive @@ -91,7 +104,10 @@ public void execute() Set relevantBranches = gitManager.collectAllSubmodulesActiveRemoteBranches(inactivityThreshold); gitManager.deleteBranchesNotIn(relevantBranches); gitManager.createMissingParentBranches(relevantBranches); - gitManager.updateAllBranchesModules(); + StringBuffer sb = new StringBuffer(); + gitManager.updateAllBranchesModules(sb, dryRun); + writeReport(sb); + } catch (Exception e) { getLog().error( e); throw new MojoExecutionException(e.getMessage(), e); @@ -99,4 +115,17 @@ public void execute() } + + protected void writeReport(StringBuffer content) throws MojoExecutionException { + // Ensure the parent directory exists + File parentDir = reportFile.getParentFile(); + if (parentDir != null && !parentDir.exists()) { + parentDir.mkdirs(); + } + try { + FileUtils.write(reportFile, content.toString(), Charset.defaultCharset()); + } catch (IOException e) { + throw new MojoExecutionException("Error writing to file", e); + } + } } diff --git a/sync-git-submodules-branches/src/main/java/org/gemoc/sync_git_submodules_branches/gittool/GitModuleManager.java b/sync-git-submodules-branches/src/main/java/org/gemoc/sync_git_submodules_branches/gittool/GitModuleManager.java index 16b2d2a..e7b53b3 100644 --- a/sync-git-submodules-branches/src/main/java/org/gemoc/sync_git_submodules_branches/gittool/GitModuleManager.java +++ b/sync-git-submodules-branches/src/main/java/org/gemoc/sync_git_submodules_branches/gittool/GitModuleManager.java @@ -382,7 +382,15 @@ public void createBranchForModules(Git parentgit, String missingParentBranch) } } - public void updateAllBranchesModules() throws IOException, GitAPIException, GitSyncError, ConfigInvalidException { + /** + * + * @param dryRun report only, do not perform changes + * @throws IOException + * @throws GitAPIException + * @throws GitSyncError + * @throws ConfigInvalidException + */ + public void updateAllBranchesModules(StringBuffer reportBuffer, boolean dryRun) throws IOException, GitAPIException, GitSyncError, ConfigInvalidException { FileRepositoryBuilder builder = new FileRepositoryBuilder(); try (Repository parentRepository = builder.setMustExist(true).setGitDir(new File(localGitFolder + "/.git")) @@ -394,12 +402,15 @@ public void updateAllBranchesModules() throws IOException, GitAPIException, GitS for (Ref ref : call) { if (ref.getName().startsWith("refs/remotes/origin/")) { updateBranchesForModules(parentgit, - ref.getName().substring("refs/remotes/origin/".length())); + ref.getName().substring("refs/remotes/origin/".length()), + reportBuffer, + dryRun); } } } } } + /** * @@ -411,9 +422,13 @@ public void updateAllBranchesModules() throws IOException, GitAPIException, GitS * @throws IOException * @throws ConfigInvalidException */ - public void updateBranchesForModules(Git parentgit, String consideredBranch) + public void updateBranchesForModules(Git parentgit, String consideredBranch, StringBuffer reportBuffer, boolean dryRun) throws GitAPIException, GitSyncError, IOException, ConfigInvalidException { logger.info("updateBranchesForModules branch = " + consideredBranch); + reportBuffer.append(String.format("**Branch %s**\n", consideredBranch)); + reportBuffer.append("\n" + + "| Module | Branch |\n" + + "|:---------- |:---------- |\n"); // switch parentGit to branch checkoutBranch(parentgit, consideredBranch); @@ -442,6 +457,7 @@ public void updateBranchesForModules(Git parentgit, String consideredBranch) } } logger.info(String.format(" tracking module %-32s on branch "+trackedBranchName, walk.getModuleName())); + // Make sure the parent repo knows that its submodule now tracks a branch: FileBasedConfig modulesConfig = new FileBasedConfig(new File( @@ -475,6 +491,7 @@ public void updateBranchesForModules(Git parentgit, String consideredBranch) logger.debug("\t\tUntracked: " + status.getUntracked()); logger.debug("\t\tUntrackedFolders: " + status.getUntrackedFolders()); } + String branchModifier = ""; if(status.getAdded().size() + status.getChanged().size() +status.getRemoved().size() > 0) { String msg; PersonIdent committer; @@ -493,13 +510,19 @@ public void updateBranchesForModules(Git parentgit, String consideredBranch) msg = "Updating submodule "+walk.getModuleName()+" to track head of branch "+trackedBranchName; committer = defaultCommitter; } - logger.debug("\t\tgit commit -m \""+msg+"\""); - parentgit.commit() - .setMessage(msg) - .setAllowEmpty(false) - .setCommitter(committer) - .call(); + branchModifier = "🔄"; + if(! dryRun) { + logger.debug("\t\tgit commit -m \""+msg+"\""); + parentgit.commit() + .setMessage(msg) + .setAllowEmpty(false) + .setCommitter(committer) + .call(); + } else { + logger.info("\t\t[DRYRUN] git commit -m \""+msg+"\""); + } } + reportBuffer.append(String.format("| %-32s | %s %-16s |\n", walk.getModuleName(),branchModifier, trackedBranchName)); } } @@ -508,22 +531,27 @@ public void updateBranchesForModules(Git parentgit, String consideredBranch) logger.info( "\tupdating submodules: " + s); }*/ - Iterable pushResps = parentgit.push() - .setCredentialsProvider(credentialProvider) - .call(); - for (PushResult pushRes : pushResps) { - for (RemoteRefUpdate pushResult : pushRes.getRemoteUpdates()) { - if(pushResult.getStatus() == RemoteRefUpdate.Status.OK) { - logger.info("push branch "+consideredBranch+" => "+RemoteRefUpdate.Status.OK); - } else if(pushResult.getStatus() == RemoteRefUpdate.Status.UP_TO_DATE) { - logger.info("nothing to push for branch "+consideredBranch+" => "+RemoteRefUpdate.Status.UP_TO_DATE); - - } else { - logger.error("PB pushing branch "+consideredBranch+" => "+pushRes.getMessages()+"\" "+pushResult); + if(!dryRun) { + Iterable pushResps = parentgit.push() + .setCredentialsProvider(credentialProvider) + .call(); + for (PushResult pushRes : pushResps) { + for (RemoteRefUpdate pushResult : pushRes.getRemoteUpdates()) { + if(pushResult.getStatus() == RemoteRefUpdate.Status.OK) { + logger.info("push branch "+consideredBranch+" => "+RemoteRefUpdate.Status.OK); + } else if(pushResult.getStatus() == RemoteRefUpdate.Status.UP_TO_DATE) { + logger.info("nothing to push for branch "+consideredBranch+" => "+RemoteRefUpdate.Status.UP_TO_DATE); + + } else { + logger.error("PB pushing branch "+consideredBranch+" => "+pushRes.getMessages()+"\" "+pushResult); + } } + validateRemoteRefUpdates("push submodule tracking branch", pushRes.getRemoteUpdates()); } - validateRemoteRefUpdates("push submodule tracking branch", pushRes.getRemoteUpdates()); + } else { + logger.info("\t\t[DRYRUN] not pushing branch "+consideredBranch); } + reportBuffer.append("\n"); } } From d0900b9f8b5fda18470f73434c0937abbfb32bad Mon Sep 17 00:00:00 2001 From: Didier Vojtisek Date: Fri, 13 Sep 2024 09:49:49 +0200 Subject: [PATCH 2/4] trigger also on dev branches --- .github/workflows/maven.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index aff024d..4a9ed99 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -5,7 +5,7 @@ name: Maven Build on: push: - branches: [ master ] + # branches: [ master ] pull_request: branches: [ master ] From aa50b3a04066df71eaa76e4b0f71b6c23732034b Mon Sep 17 00:00:00 2001 From: Didier Vojtisek Date: Fri, 13 Sep 2024 09:54:14 +0200 Subject: [PATCH 3/4] update some github actions versions --- .github/workflows/maven-publish.yml | 2 +- .github/workflows/maven.yml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/maven-publish.yml b/.github/workflows/maven-publish.yml index 5256b1e..b260d49 100644 --- a/.github/workflows/maven-publish.yml +++ b/.github/workflows/maven-publish.yml @@ -16,7 +16,7 @@ jobs: packages: write steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Set up JDK 11 uses: actions/setup-java@v2 with: diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index 4a9ed99..aa7d767 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -15,7 +15,7 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Set up JDK 11 uses: actions/setup-java@v2 with: @@ -36,7 +36,7 @@ jobs: - name: Stage result files run: mkdir staging && cp sync-git-submodules-branches/target/*.jar staging - name: Upload Artifacts - uses: actions/upload-artifact@v2 + uses: actions/upload-artifact@v4 with: name: Package path: staging From ab7acd786c34d10e96a0c65a03bb80bf689c49bb Mon Sep 17 00:00:00 2001 From: Didier Vojtisek Date: Fri, 13 Sep 2024 10:02:26 +0200 Subject: [PATCH 4/4] improved presentation in case of change in submodule --- .../sync_git_submodules_branches/gittool/GitModuleManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sync-git-submodules-branches/src/main/java/org/gemoc/sync_git_submodules_branches/gittool/GitModuleManager.java b/sync-git-submodules-branches/src/main/java/org/gemoc/sync_git_submodules_branches/gittool/GitModuleManager.java index e7b53b3..60f5f5f 100644 --- a/sync-git-submodules-branches/src/main/java/org/gemoc/sync_git_submodules_branches/gittool/GitModuleManager.java +++ b/sync-git-submodules-branches/src/main/java/org/gemoc/sync_git_submodules_branches/gittool/GitModuleManager.java @@ -522,7 +522,7 @@ public void updateBranchesForModules(Git parentgit, String consideredBranch, Str logger.info("\t\t[DRYRUN] git commit -m \""+msg+"\""); } } - reportBuffer.append(String.format("| %-32s | %s %-16s |\n", walk.getModuleName(),branchModifier, trackedBranchName)); + reportBuffer.append(String.format("| %-32s | %-16s %s |\n", walk.getModuleName(), trackedBranchName, branchModifier)); } }