Skip to content

Commit

Permalink
[JENKINS-13493] Automatic Git Cache Maintenance API (#862)
Browse files Browse the repository at this point in the history
* added maintenance to CLI Git
* added logs to maintenance command
* added execution time for maintenance command
* added execution time unit
* tested commit-graph
* tested loose objects
* tested incremental-repack
* tested gc
* init prefetch testing
* added javadoc to maintenance apis
* return boolean after executing git maintenance
* updated tests with boolean

Co-authored-by: Mark Waite <[email protected]>
  • Loading branch information
Hrushi20 and MarkEWaite authored Sep 14, 2022
1 parent 204b405 commit 9f64c6b
Show file tree
Hide file tree
Showing 5 changed files with 435 additions and 1 deletion.
46 changes: 45 additions & 1 deletion src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.EnvVars;
import hudson.FilePath;
import hudson.Functions;
import hudson.Launcher;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.Launcher.LocalLauncher;
Expand Down Expand Up @@ -3866,4 +3865,49 @@ Output shows SHA1 and tag with (optional) marker for annotated tags
return tags;
}

/** {@inheritDoc} */
@Override
public boolean maintenance(String task) throws InterruptedException {
boolean isExecuted = true;
try {
listener.getLogger().println("Git maintenance " + task + " started on " + workspace.getName());
long startTime = System.currentTimeMillis();
if (isAtLeastVersion(2, 30, 0, 0)) {
// For prefetch, the command will throw an error for private repo if it has no access.
launchCommand("maintenance", "run", "--task=" + task);
} else {
switch(task) {
case "gc":
launchCommand("gc", "--auto");
break;
case "commit-graph":
if (isAtLeastVersion(2, 19, 0, 0)) {
launchCommand("commit-graph", "write");
} else {
listener.getLogger().println("Error executing commit-graph maintenance task");
}
break;
case "incremental-repack":
if (isAtLeastVersion(2, 25, 0, 0)) {
launchCommand("multi-pack-index", "expire");
launchCommand("multi-pack-index", "repack");
} else {
listener.getLogger().println("Error executing incremental-repack maintenance task");
}
break;
default:
String message = "Invalid legacy git maintenance task " + task + ".";
listener.getLogger().println(message);
throw new GitException(message);
}
}
long endTime = System.currentTimeMillis();
listener.getLogger().println("Git maintenance task " + task + " finished on " + workspace.getName() + " in " + (endTime - startTime) + "ms.");
} catch (GitException e) {
isExecuted = false;
listener.getLogger().println("Error executing " + task + " maintenance task");
listener.getLogger().println("Mainteance task " + task + " error message: " + e.getMessage());
}
return isExecuted;
}
}
9 changes: 9 additions & 0 deletions src/main/java/org/jenkinsci/plugins/gitclient/GitClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -1002,4 +1002,13 @@ public interface GitClient {
* @throws java.lang.InterruptedException on thread interruption
*/
Set<GitObject> getTags() throws GitException, InterruptedException;

/**
* Executes git maintenance commands based on the git version.
*
* @param task a {@link java.lang.String} object. i.e (prefetch/gc/commit-graph/incremental-repack/loose-objects)
* @return Boolean if maintenance has been executed or not.
* @throws InterruptedException if underlying git operation fails.
*/
boolean maintenance(String task) throws InterruptedException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2882,6 +2882,11 @@ public Set<GitObject> getTags() throws GitException {
return peeledTags;
}

@Override
public boolean maintenance(String task) {
listener.getLogger().println("JGIT doesn't support git maintenance. Use CLIGIT to execute maintenance tasks.");
return false;
}
private static class FileRepositoryImpl extends FileRepository {

private final File tempDir;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -921,4 +921,9 @@ public List<Branch> getBranchesContaining(String revspec, boolean allBranches)
public Set<GitObject> getTags() throws GitException, InterruptedException {
return proxy.getTags();
}

@Override
public boolean maintenance(String task) {
return false;
}
}
Loading

0 comments on commit 9f64c6b

Please sign in to comment.