List of usage examples for org.eclipse.jgit.api GarbageCollectCommand call
@Override public Properties call() throws GitAPIException
From source file:com.chungkwong.jgitgui.GitTreeItem.java
License:Open Source License
private void gitGC() { ProgressDialog progressDialog = new ProgressDialog( java.util.ResourceBundle.getBundle("com/chungkwong/jgitgui/text").getString("GC")); GarbageCollectCommand command = ((Git) getValue()).gc().setProgressMonitor(progressDialog); new Thread(() -> { try {//from w ww . j a va 2s . c o m Properties stat = command.call(); System.out.println(stat); } catch (Exception ex) { Logger.getLogger(GitTreeItem.class.getName()).log(Level.SEVERE, null, ex); Platform.runLater(() -> { progressDialog.hide(); Util.informUser(ex); }); } }).start(); }
From source file:com.gitblit.GCExecutor.java
License:Apache License
@Override public void run() { if (!isReady()) { return;//from w w w. j av a 2 s . c o m } running.set(true); Date now = new Date(); for (String repositoryName : GitBlit.self().getRepositoryList()) { if (forceClose.get()) { break; } if (isCollectingGarbage(repositoryName)) { logger.warn(MessageFormat.format("Already collecting garbage from {0}?!?", repositoryName)); continue; } boolean garbageCollected = false; RepositoryModel model = null; Repository repository = null; try { model = GitBlit.self().getRepositoryModel(repositoryName); repository = GitBlit.self().getRepository(repositoryName); if (repository == null) { logger.warn(MessageFormat.format("GCExecutor is missing repository {0}?!?", repositoryName)); continue; } if (!isRepositoryIdle(repository)) { logger.debug(MessageFormat.format("GCExecutor is skipping {0} because it is not idle", repositoryName)); continue; } // By setting the GCStatus to COLLECTING we are // disabling *all* access to this repository from Gitblit. // Think of this as a clutch in a manual transmission vehicle. if (!setGCStatus(repositoryName, GCStatus.COLLECTING)) { logger.warn(MessageFormat.format("Can not acquire GC lock for {0}, skipping", repositoryName)); continue; } logger.debug(MessageFormat.format("GCExecutor locked idle repository {0}", repositoryName)); Git git = new Git(repository); GarbageCollectCommand gc = git.gc(); Properties stats = gc.getStatistics(); // determine if this is a scheduled GC Calendar cal = Calendar.getInstance(); cal.setTime(model.lastGC); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); cal.add(Calendar.DATE, model.gcPeriod); Date gcDate = cal.getTime(); boolean shouldCollectGarbage = now.after(gcDate); // determine if filesize triggered GC long gcThreshold = FileUtils.convertSizeToLong(model.gcThreshold, 500 * 1024L); long sizeOfLooseObjects = (Long) stats.get("sizeOfLooseObjects"); boolean hasEnoughGarbage = sizeOfLooseObjects >= gcThreshold; // if we satisfy one of the requirements, GC boolean hasGarbage = sizeOfLooseObjects > 0; if (hasGarbage && (hasEnoughGarbage || shouldCollectGarbage)) { long looseKB = sizeOfLooseObjects / 1024L; logger.info(MessageFormat.format("Collecting {1} KB of loose objects from {0}", repositoryName, looseKB)); // do the deed gc.call(); garbageCollected = true; } } catch (Exception e) { logger.error("Error collecting garbage in " + repositoryName, e); } finally { // cleanup if (repository != null) { if (garbageCollected) { // update the last GC date model.lastGC = new Date(); GitBlit.self().updateConfiguration(repository, model); } repository.close(); } // reset the GC lock releaseLock(repositoryName); logger.debug(MessageFormat.format("GCExecutor released GC lock for {0}", repositoryName)); } } running.set(false); }
From source file:com.gitblit.service.GarbageCollectorService.java
License:Apache License
@Override public void run() { if (!isReady()) { return;//from ww w . ja va2s .c o m } running.set(true); Date now = new Date(); for (String repositoryName : repositoryManager.getRepositoryList()) { if (forceClose.get()) { break; } if (isCollectingGarbage(repositoryName)) { logger.warn(MessageFormat.format("Already collecting garbage from {0}?!?", repositoryName)); continue; } boolean garbageCollected = false; RepositoryModel model = null; Repository repository = null; try { model = repositoryManager.getRepositoryModel(repositoryName); repository = repositoryManager.getRepository(repositoryName); if (repository == null) { logger.warn(MessageFormat.format("GCExecutor is missing repository {0}?!?", repositoryName)); continue; } if (!repositoryManager.isIdle(repository)) { logger.debug(MessageFormat.format("GCExecutor is skipping {0} because it is not idle", repositoryName)); continue; } // By setting the GCStatus to COLLECTING we are // disabling *all* access to this repository from Gitblit. // Think of this as a clutch in a manual transmission vehicle. if (!setGCStatus(repositoryName, GCStatus.COLLECTING)) { logger.warn(MessageFormat.format("Can not acquire GC lock for {0}, skipping", repositoryName)); continue; } logger.debug(MessageFormat.format("GCExecutor locked idle repository {0}", repositoryName)); Git git = new Git(repository); GarbageCollectCommand gc = git.gc(); Properties stats = gc.getStatistics(); // determine if this is a scheduled GC Calendar cal = Calendar.getInstance(); cal.setTime(model.lastGC); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); cal.add(Calendar.DATE, model.gcPeriod); Date gcDate = cal.getTime(); boolean shouldCollectGarbage = now.after(gcDate); // determine if filesize triggered GC long gcThreshold = FileUtils.convertSizeToLong(model.gcThreshold, 500 * 1024L); long sizeOfLooseObjects = (Long) stats.get("sizeOfLooseObjects"); boolean hasEnoughGarbage = sizeOfLooseObjects >= gcThreshold; // if we satisfy one of the requirements, GC boolean hasGarbage = sizeOfLooseObjects > 0; if (hasGarbage && (hasEnoughGarbage || shouldCollectGarbage)) { long looseKB = sizeOfLooseObjects / 1024L; logger.info(MessageFormat.format("Collecting {1} KB of loose objects from {0}", repositoryName, looseKB)); // do the deed gc.call(); garbageCollected = true; } } catch (Exception e) { logger.error("Error collecting garbage in " + repositoryName, e); } finally { // cleanup if (repository != null) { if (garbageCollected) { // update the last GC date model.lastGC = new Date(); repositoryManager.updateConfiguration(repository, model); } repository.close(); } // reset the GC lock releaseLock(repositoryName); logger.debug(MessageFormat.format("GCExecutor released GC lock for {0}", repositoryName)); } } running.set(false); }
From source file:com.gitblit.transport.ssh.git.GarbageCollectionCommand.java
License:Apache License
@Override protected void runImpl() throws IOException, Failure { try {/*w w w . j a va 2s. co m*/ GarbageCollectCommand gc = Git.wrap(repo).gc(); logGcInfo("before:", gc.getStatistics()); gc.setProgressMonitor(NullProgressMonitor.INSTANCE); Properties statistics = gc.call(); logGcInfo("after: ", statistics); } catch (Exception e) { throw new Failure(1, "fatal: Cannot run gc: ", e); } }
From source file:com.google.gerrit.server.git.GarbageCollection.java
License:Apache License
public GarbageCollectionResult run(List<Project.NameKey> projectNames, boolean aggressive, PrintWriter writer) { GarbageCollectionResult result = new GarbageCollectionResult(); Set<Project.NameKey> projectsToGc = gcQueue.addAll(projectNames); for (Project.NameKey projectName : Sets.difference(Sets.newHashSet(projectNames), projectsToGc)) { result.addError(new GarbageCollectionResult.Error( GarbageCollectionResult.Error.Type.GC_ALREADY_SCHEDULED, projectName)); }/* ww w. j a v a2 s . com*/ for (Project.NameKey p : projectsToGc) { try (Repository repo = repoManager.openRepository(p)) { logGcConfiguration(p, repo, aggressive); print(writer, "collecting garbage for \"" + p + "\":\n"); GarbageCollectCommand gc = Git.wrap(repo).gc(); gc.setAggressive(aggressive); logGcInfo(p, "before:", gc.getStatistics()); gc.setProgressMonitor( writer != null ? new TextProgressMonitor(writer) : NullProgressMonitor.INSTANCE); Properties statistics = gc.call(); logGcInfo(p, "after: ", statistics); print(writer, "done.\n\n"); fire(p, statistics); } catch (RepositoryNotFoundException e) { logGcError(writer, p, e); result.addError(new GarbageCollectionResult.Error( GarbageCollectionResult.Error.Type.REPOSITORY_NOT_FOUND, p)); } catch (Exception e) { logGcError(writer, p, e); result.addError(new GarbageCollectionResult.Error(GarbageCollectionResult.Error.Type.GC_FAILED, p)); } finally { gcQueue.gcFinished(p); } } return result; }
From source file:jetbrains.buildServer.buildTriggers.vcs.git.GitGcProcess.java
License:Apache License
public static void main(String... args) throws Exception { GitServerUtil.configureExternalProcessLogger(false); try {//from ww w . ja v a 2 s. co m String gitDir = args[0]; System.out.println("run gc in " + gitDir); Repository r = new RepositoryBuilder().setBare().setGitDir(new File(gitDir)).build(); Git git = new Git(r); GarbageCollectCommand gc = git.gc(); gc.setProgressMonitor(new TextProgressMonitor(new PrintWriter(System.out))); gc.call(); } catch (Throwable t) { if (isImportant(t)) { t.printStackTrace(System.err); } else { System.err.println(t.getMessage()); } System.exit(1); } }
From source file:org.kuali.student.git.importer.GitImporterParseOptions.java
License:Educational Community License
@Override public void onRevisionContentLength(long currentRevision, long contentLength, long propContentLength, ReadLineData lineData) {//from w ww. j a v a2 s . c o m // flush logs for the last revision blobLog.flush(); copyFromSkippedLog.flush(); vetoLog.flush(); // for any branch with a blob added create a git commit object pointing // at the git tree for the change. if (this.currentRevision != -1) flushPendingBranchCommits(); if (gcEnabled && this.currentRevision != 0 && this.currentRevision % 500 == 0) { // every five hundred revisions garbage collect the repository to // keep it fast log.info("Garbage collecting git repository"); if (externalGitCommandPath != null) { ExternalGitUtils.runGarbageCollection(externalGitCommandPath, repo, System.out); } else { try { GarbageCollectCommand gc = new Git(repo).gc(); // should not matter but anything loose can be collected. gc.setExpire(new Date()); gc.call(); } catch (GitAPIException e) { } } /* * Make sure JGit knows where the ref is located after loose refs are put into the pack file. */ repo.getRefDatabase().refresh(); } // if (gcEnabled && this.currentRevision != 0 && this.currentRevision % // 10000 == 0) { // // repack the revision map file every 10000 revs // try { // revisionMapper.repackMapFile(); // } catch (IOException e) { // throw new RuntimeException("failed to repack revision mapper", e); // } // } if (this.currentRevision == -1) { /* * In the initialization case check that we haven't already imported this revision. * * This will prevent clobbering an existing import. */ try { List<SvnRevisionMap> knownHeads = revisionMapper.getRevisionHeads(currentRevision); if (knownHeads != null && knownHeads.size() > 0) throw new RuntimeException( "Aborting: Target Git Repository(" + repo.getDirectory().getAbsolutePath() + ") already contains an export of revision: " + currentRevision); } catch (IOException e) { throw new RuntimeException( "failed to check existing revision heads for revision = " + currentRevision, e); } } this.currentRevision = currentRevision; log.info("starting on Revision: " + currentRevision); // at this point we should be able to read // propContentLength and parse out the try { Map<String, String> revisionProperties = org.kuali.student.common.io.IOUtils .extractRevisionProperties(inputStream, propContentLength, contentLength); // read out the author and commit message String author = revisionProperties.get("svn:author"); String commitDate = revisionProperties.get("svn:date"); String commitMessage = revisionProperties.get("svn:log"); String userName = author; if (userName == null) userName = "unknown"; String emailAddress = userName + "@kuali.org"; Date actualCommitDate = null; if (commitDate != null) { actualCommitDate = GitImporterDateUtils.convertDateString(commitDate); } else { log.warn("Missing commit date"); if (commitMessage == null) { commitMessage = MISSING_COMMIT_DATE_MESSAGE; } else { commitMessage = commitMessage + "\n" + MISSING_COMMIT_DATE_MESSAGE; } /* * Get the commit time of the previous commit and add 5 minutes * to it. */ List<SvnRevisionMap> heads = revisionMapper.getRevisionHeads(currentRevision - 1L); if (heads.size() == 0) { actualCommitDate = new DateTime(0L).toDate(); } else { SvnRevisionMap head = heads.get(0); RevWalk rw = new RevWalk(repo); RevCommit lastCommit = rw.parseCommit(ObjectId.fromString(head.getCommitId())); DateTime dt = new DateTime(lastCommit.getAuthorIdent().getWhen()).plusMinutes(5); actualCommitDate = dt.toDate(); } } TimeZone tz = GitImporterDateUtils.extractTimeZone(actualCommitDate); commitData = new GitCommitData(new PersonIdent(userName, emailAddress, actualCommitDate, tz), commitMessage); nodeProcessor.setCommitData(commitData); // also consider copyfrom or other details that // suggest a merge at this point. } catch (Exception e) { throw new RuntimeException("onRevisionContentLength failed to read revision properties.", e); } }