use of org.eclipse.jgit.lib.Repository in project gitblit by gitblit.
the class AddIndexedBranch method main.
public static void main(String... args) {
Params params = new Params();
CmdLineParser parser = new CmdLineParser(params);
try {
parser.parseArgument(args);
} catch (CmdLineException t) {
System.err.println(t.getMessage());
parser.printUsage(System.out);
return;
}
// create a lowercase set of excluded repositories
Set<String> exclusions = new TreeSet<String>();
for (String exclude : params.exclusions) {
exclusions.add(exclude.toLowerCase());
}
// determine available repositories
File folder = new File(params.folder);
List<String> repoList = JGitUtils.getRepositoryList(folder, false, true, -1, null);
int modCount = 0;
int skipCount = 0;
for (String repo : repoList) {
boolean skip = false;
for (String exclusion : exclusions) {
if (StringUtils.fuzzyMatch(repo, exclusion)) {
skip = true;
break;
}
}
if (skip) {
System.out.println("skipping " + repo);
skipCount++;
continue;
}
try {
// load repository config
File gitDir = FileKey.resolve(new File(folder, repo), FS.DETECTED);
Repository repository = new FileRepositoryBuilder().setGitDir(gitDir).build();
StoredConfig config = repository.getConfig();
config.load();
Set<String> indexedBranches = new LinkedHashSet<String>();
// add all local branches to index
if (params.addAllLocalBranches) {
List<RefModel> list = JGitUtils.getLocalBranches(repository, true, -1);
for (RefModel refModel : list) {
System.out.println(MessageFormat.format("adding [gitblit] indexBranch={0} for {1}", refModel.getName(), repo));
indexedBranches.add(refModel.getName());
}
} else {
// add only one branch to index ('default' if not specified)
System.out.println(MessageFormat.format("adding [gitblit] indexBranch={0} for {1}", params.branch, repo));
indexedBranches.add(params.branch);
}
String[] branches = config.getStringList("gitblit", null, "indexBranch");
if (!ArrayUtils.isEmpty(branches)) {
for (String branch : branches) {
indexedBranches.add(branch);
}
}
config.setStringList("gitblit", null, "indexBranch", new ArrayList<String>(indexedBranches));
config.save();
modCount++;
} catch (Exception e) {
System.err.println(repo);
e.printStackTrace();
}
}
System.out.println(MessageFormat.format("updated {0} repository configurations, skipped {1}", modCount, skipCount));
}
use of org.eclipse.jgit.lib.Repository in project gitblit by gitblit.
the class PushLogTest method testPushLog.
@Test
public void testPushLog() throws IOException {
String name = "~james/helloworld.git";
File gitDir = FileKey.resolve(new File(GitBlitSuite.REPOSITORIES, name), FS.DETECTED);
Repository repository = new FileRepositoryBuilder().setGitDir(gitDir).build();
List<RefLogEntry> pushes = RefLogUtils.getRefLog(name, repository);
GitBlitSuite.close(repository);
}
use of org.eclipse.jgit.lib.Repository in project gitblit by gitblit.
the class JGitUtils method cloneRepository.
/**
* Clone or Fetch a repository. If the local repository does not exist,
* clone is called. If the repository does exist, fetch is called. By
* default the clone/fetch retrieves the remote heads, tags, and notes.
*
* @param repositoriesFolder
* @param name
* @param fromUrl
* @param bare
* @param credentialsProvider
* @return CloneResult
* @throws Exception
*/
public static CloneResult cloneRepository(File repositoriesFolder, String name, String fromUrl, boolean bare, CredentialsProvider credentialsProvider) throws Exception {
CloneResult result = new CloneResult();
if (bare) {
// bare repository, ensure .git suffix
if (!name.toLowerCase().endsWith(Constants.DOT_GIT_EXT)) {
name += Constants.DOT_GIT_EXT;
}
} else {
// normal repository, strip .git suffix
if (name.toLowerCase().endsWith(Constants.DOT_GIT_EXT)) {
name = name.substring(0, name.indexOf(Constants.DOT_GIT_EXT));
}
}
result.name = name;
File folder = new File(repositoriesFolder, name);
if (folder.exists()) {
File gitDir = FileKey.resolve(new File(repositoriesFolder, name), FS.DETECTED);
Repository repository = new FileRepositoryBuilder().setGitDir(gitDir).build();
result.fetchResult = fetchRepository(credentialsProvider, repository);
repository.close();
} else {
CloneCommand clone = new CloneCommand();
clone.setBare(bare);
clone.setCloneAllBranches(true);
clone.setURI(fromUrl);
clone.setDirectory(folder);
if (credentialsProvider != null) {
clone.setCredentialsProvider(credentialsProvider);
}
Repository repository = clone.call().getRepository();
// Now we have to fetch because CloneCommand doesn't fetch
// refs/notes nor does it allow manual RefSpec.
result.createdRepository = true;
result.fetchResult = fetchRepository(credentialsProvider, repository);
repository.close();
}
return result;
}
use of org.eclipse.jgit.lib.Repository in project gitblit by gitblit.
the class ActivityUtils method getRecentActivity.
/**
* Gets the recent activity from the repositories for the last daysBack days
* on the specified branch.
*
* @param settings
* the runtime settings
* @param repositoryManager
* the repository manager
* @param models
* the list of repositories to query
* @param daysBack
* the number of days back from Now to collect
* @param objectId
* the branch to retrieve. If this value is null or empty all
* branches are queried.
* @param timezone
* the timezone for aggregating commits
* @return
*/
public static List<Activity> getRecentActivity(IStoredSettings settings, IRepositoryManager repositoryManager, List<RepositoryModel> models, int daysBack, String objectId, TimeZone timezone) {
// Activity panel shows last daysBack of activity across all
// repositories.
Date thresholdDate = new Date(System.currentTimeMillis() - daysBack * TimeUtils.ONEDAY);
// Build a map of DailyActivity from the available repositories for the
// specified threshold date.
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
df.setTimeZone(timezone);
Calendar cal = Calendar.getInstance();
cal.setTimeZone(timezone);
// aggregate author exclusions
Set<String> authorExclusions = new TreeSet<String>();
authorExclusions.addAll(settings.getStrings(Keys.web.metricAuthorExclusions));
for (RepositoryModel model : models) {
if (!ArrayUtils.isEmpty(model.metricAuthorExclusions)) {
authorExclusions.addAll(model.metricAuthorExclusions);
}
}
Map<String, Activity> activity = new HashMap<String, Activity>();
for (RepositoryModel model : models) {
if (!model.isShowActivity()) {
// skip this repository
continue;
}
if (model.hasCommits && model.lastChange.after(thresholdDate)) {
if (model.isCollectingGarbage) {
continue;
}
Repository repository = repositoryManager.getRepository(model.name);
List<String> branches = new ArrayList<String>();
if (StringUtils.isEmpty(objectId)) {
for (RefModel local : JGitUtils.getLocalBranches(repository, true, -1)) {
if (!local.getDate().after(thresholdDate)) {
// branch not recently updated
continue;
}
branches.add(local.getName());
}
} else {
branches.add(objectId);
}
for (String branch : branches) {
String shortName = branch;
if (shortName.startsWith(Constants.R_HEADS)) {
shortName = shortName.substring(Constants.R_HEADS.length());
}
List<RepositoryCommit> commits = CommitCache.instance().getCommits(model.name, repository, branch, thresholdDate);
if (model.maxActivityCommits > 0 && commits.size() > model.maxActivityCommits) {
// trim commits to maximum count
commits = commits.subList(0, model.maxActivityCommits);
}
for (RepositoryCommit commit : commits) {
Date date = commit.getCommitDate();
String dateStr = df.format(date);
if (!activity.containsKey(dateStr)) {
// Normalize the date to midnight
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Activity a = new Activity(cal.getTime());
a.excludeAuthors(authorExclusions);
activity.put(dateStr, a);
}
activity.get(dateStr).addCommit(commit);
}
}
// close the repository
repository.close();
}
}
List<Activity> recentActivity = new ArrayList<Activity>(activity.values());
return recentActivity;
}
use of org.eclipse.jgit.lib.Repository in project gitblit by gitblit.
the class GarbageCollectorService method run.
@Override
public void run() {
if (!isReady()) {
return;
}
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;
}
// 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);
}
Aggregations