List of usage examples for org.eclipse.jgit.lib Repository isBare
public boolean isBare()
From source file:com.genuitec.eclipse.gerrit.tools.utils.RepositoryUtils.java
License:Open Source License
public static List<Repository> getAllRepositories() { List<Repository> result = new ArrayList<Repository>(); for (String repo : repositoryUtil.getConfiguredRepositories()) { File gitDir = new File(repo); if (gitDir.exists()) { try { Repository repository = repositoryCache.lookupRepository(gitDir); if (!repository.isBare()) { result.add(repository); }//w ww .j av a2 s . com } catch (IOException e) { //ignore } } } return result; }
From source file:com.genuitec.eclipse.gerrit.tools.utils.RepositoryUtils.java
License:Open Source License
public static Repository getRepositoryForName(String name) { for (String repo : repositoryUtil.getConfiguredRepositories()) { File gitDir = new File(repo); if (gitDir.exists()) { try { Repository repository = repositoryCache.lookupRepository(gitDir); if (!repository.isBare() && repository.getDirectory().getParentFile().getName().equals(name)) { return repository; }//from w w w . j a v a 2 s. com } catch (IOException e) { //ignore } } } return null; }
From source file:com.gitblit.GitBlit.java
License:Apache License
/** * Create a repository model from the configuration and repository data. * /* w w w.java2 s .c o m*/ * @param repositoryName * @return a repositoryModel or null if the repository does not exist */ private RepositoryModel loadRepositoryModel(String repositoryName) { Repository r = getRepository(repositoryName); if (r == null) { return null; } RepositoryModel model = new RepositoryModel(); model.name = repositoryName; model.hasCommits = JGitUtils.hasCommits(r); model.lastChange = JGitUtils.getLastChange(r); model.isBare = r.isBare(); if (repositoryName.indexOf('/') == -1) { model.projectPath = ""; } else { model.projectPath = repositoryName.substring(0, repositoryName.indexOf('/')); } StoredConfig config = r.getConfig(); boolean hasOrigin = !StringUtils.isEmpty(config.getString("remote", "origin", "url")); if (config != null) { model.description = getConfig(config, "description", ""); model.owner = getConfig(config, "owner", ""); model.useTickets = getConfig(config, "useTickets", false); model.useDocs = getConfig(config, "useDocs", false); model.allowForks = getConfig(config, "allowForks", true); model.accessRestriction = AccessRestrictionType.fromName(getConfig(config, "accessRestriction", settings.getString(Keys.git.defaultAccessRestriction, null))); model.authorizationControl = AuthorizationControl.fromName(getConfig(config, "authorizationControl", settings.getString(Keys.git.defaultAuthorizationControl, null))); model.showRemoteBranches = getConfig(config, "showRemoteBranches", hasOrigin); model.isFrozen = getConfig(config, "isFrozen", false); model.showReadme = getConfig(config, "showReadme", false); model.skipSizeCalculation = getConfig(config, "skipSizeCalculation", false); model.skipSummaryMetrics = getConfig(config, "skipSummaryMetrics", false); model.federationStrategy = FederationStrategy.fromName(getConfig(config, "federationStrategy", null)); model.federationSets = new ArrayList<String>( Arrays.asList(config.getStringList(Constants.CONFIG_GITBLIT, null, "federationSets"))); model.isFederated = getConfig(config, "isFederated", false); model.origin = config.getString("remote", "origin", "url"); model.preReceiveScripts = new ArrayList<String>( Arrays.asList(config.getStringList(Constants.CONFIG_GITBLIT, null, "preReceiveScript"))); model.postReceiveScripts = new ArrayList<String>( Arrays.asList(config.getStringList(Constants.CONFIG_GITBLIT, null, "postReceiveScript"))); model.mailingLists = new ArrayList<String>( Arrays.asList(config.getStringList(Constants.CONFIG_GITBLIT, null, "mailingList"))); model.indexedBranches = new ArrayList<String>( Arrays.asList(config.getStringList(Constants.CONFIG_GITBLIT, null, "indexBranch"))); // Custom defined properties model.customFields = new LinkedHashMap<String, String>(); for (String aProperty : config.getNames(Constants.CONFIG_GITBLIT, Constants.CONFIG_CUSTOM_FIELDS)) { model.customFields.put(aProperty, config.getString(Constants.CONFIG_GITBLIT, Constants.CONFIG_CUSTOM_FIELDS, aProperty)); } } model.HEAD = JGitUtils.getHEADRef(r); model.availableRefs = JGitUtils.getAvailableHeadTargets(r); r.close(); if (model.origin != null && model.origin.startsWith("file://")) { // repository was cloned locally... perhaps as a fork try { File folder = new File(new URI(model.origin)); String originRepo = com.gitblit.utils.FileUtils.getRelativePath(getRepositoriesFolder(), folder); if (!StringUtils.isEmpty(originRepo)) { // ensure origin still exists File repoFolder = new File(getRepositoriesFolder(), originRepo); if (repoFolder.exists()) { model.originRepository = originRepo; } } } catch (URISyntaxException e) { logger.error("Failed to determine fork for " + model, e); } } return model; }
From source file:com.gitblit.manager.RepositoryManager.java
License:Apache License
/** * Create a repository model from the configuration and repository data. * * @param repositoryName/*w w w . j a v a 2s.c om*/ * @return a repositoryModel or null if the repository does not exist */ private RepositoryModel loadRepositoryModel(String repositoryName) { Repository r = getRepository(repositoryName); if (r == null) { return null; } RepositoryModel model = new RepositoryModel(); model.isBare = r.isBare(); File basePath = getRepositoriesFolder(); if (model.isBare) { model.name = com.gitblit.utils.FileUtils.getRelativePath(basePath, r.getDirectory()); } else { model.name = com.gitblit.utils.FileUtils.getRelativePath(basePath, r.getDirectory().getParentFile()); } if (StringUtils.isEmpty(model.name)) { // Repository is NOT located relative to the base folder because it // is symlinked. Use the provided repository name. model.name = repositoryName; } model.projectPath = StringUtils.getFirstPathElement(repositoryName); StoredConfig config = r.getConfig(); boolean hasOrigin = false; if (config != null) { // Initialize description from description file hasOrigin = !StringUtils.isEmpty(config.getString("remote", "origin", "url")); if (getConfig(config, "description", null) == null) { File descFile = new File(r.getDirectory(), "description"); if (descFile.exists()) { String desc = com.gitblit.utils.FileUtils.readContent(descFile, System.getProperty("line.separator")); if (!desc.toLowerCase().startsWith("unnamed repository")) { config.setString(Constants.CONFIG_GITBLIT, null, "description", desc); } } } model.description = getConfig(config, "description", ""); model.originRepository = getConfig(config, "originRepository", null); model.addOwners(ArrayUtils.fromString(getConfig(config, "owner", ""))); model.acceptNewPatchsets = getConfig(config, "acceptNewPatchsets", true); model.acceptNewTickets = getConfig(config, "acceptNewTickets", true); model.requireApproval = getConfig(config, "requireApproval", settings.getBoolean(Keys.tickets.requireApproval, false)); model.mergeTo = getConfig(config, "mergeTo", null); model.mergeType = MergeType .fromName(getConfig(config, "mergeType", settings.getString(Keys.tickets.mergeType, null))); model.useIncrementalPushTags = getConfig(config, "useIncrementalPushTags", false); model.incrementalPushTagPrefix = getConfig(config, "incrementalPushTagPrefix", null); model.allowForks = getConfig(config, "allowForks", true); model.accessRestriction = AccessRestrictionType.fromName(getConfig(config, "accessRestriction", settings.getString(Keys.git.defaultAccessRestriction, "PUSH"))); model.authorizationControl = AuthorizationControl.fromName(getConfig(config, "authorizationControl", settings.getString(Keys.git.defaultAuthorizationControl, null))); model.verifyCommitter = getConfig(config, "verifyCommitter", false); model.showRemoteBranches = getConfig(config, "showRemoteBranches", hasOrigin); model.isFrozen = getConfig(config, "isFrozen", false); model.skipSizeCalculation = getConfig(config, "skipSizeCalculation", false); model.skipSummaryMetrics = getConfig(config, "skipSummaryMetrics", false); model.commitMessageRenderer = CommitMessageRenderer.fromName(getConfig(config, "commitMessageRenderer", settings.getString(Keys.web.commitMessageRenderer, null))); model.federationStrategy = FederationStrategy.fromName(getConfig(config, "federationStrategy", null)); model.federationSets = new ArrayList<String>( Arrays.asList(config.getStringList(Constants.CONFIG_GITBLIT, null, "federationSets"))); model.isFederated = getConfig(config, "isFederated", false); model.gcThreshold = getConfig(config, "gcThreshold", settings.getString(Keys.git.defaultGarbageCollectionThreshold, "500KB")); model.gcPeriod = getConfig(config, "gcPeriod", settings.getInteger(Keys.git.defaultGarbageCollectionPeriod, 7)); try { model.lastGC = new SimpleDateFormat(Constants.ISO8601) .parse(getConfig(config, "lastGC", "1970-01-01'T'00:00:00Z")); } catch (Exception e) { model.lastGC = new Date(0); } model.maxActivityCommits = getConfig(config, "maxActivityCommits", settings.getInteger(Keys.web.maxActivityCommits, 0)); model.origin = config.getString("remote", "origin", "url"); if (model.origin != null) { model.origin = model.origin.replace('\\', '/'); model.isMirror = config.getBoolean("remote", "origin", "mirror", false); } model.preReceiveScripts = new ArrayList<String>( Arrays.asList(config.getStringList(Constants.CONFIG_GITBLIT, null, "preReceiveScript"))); model.postReceiveScripts = new ArrayList<String>( Arrays.asList(config.getStringList(Constants.CONFIG_GITBLIT, null, "postReceiveScript"))); model.mailingLists = new ArrayList<String>( Arrays.asList(config.getStringList(Constants.CONFIG_GITBLIT, null, "mailingList"))); model.indexedBranches = new ArrayList<String>( Arrays.asList(config.getStringList(Constants.CONFIG_GITBLIT, null, "indexBranch"))); model.metricAuthorExclusions = new ArrayList<String>( Arrays.asList(config.getStringList(Constants.CONFIG_GITBLIT, null, "metricAuthorExclusions"))); // Custom defined properties model.customFields = new LinkedHashMap<String, String>(); for (String aProperty : config.getNames(Constants.CONFIG_GITBLIT, Constants.CONFIG_CUSTOM_FIELDS)) { model.customFields.put(aProperty, config.getString(Constants.CONFIG_GITBLIT, Constants.CONFIG_CUSTOM_FIELDS, aProperty)); } } model.HEAD = JGitUtils.getHEADRef(r); if (StringUtils.isEmpty(model.mergeTo)) { model.mergeTo = model.HEAD; } model.availableRefs = JGitUtils.getAvailableHeadTargets(r); model.sparkleshareId = JGitUtils.getSparkleshareId(r); model.hasCommits = JGitUtils.hasCommits(r); updateLastChangeFields(r, model); r.close(); if (StringUtils.isEmpty(model.originRepository) && model.origin != null && model.origin.startsWith("file://")) { // repository was cloned locally... perhaps as a fork try { File folder = new File(new URI(model.origin)); String originRepo = com.gitblit.utils.FileUtils.getRelativePath(getRepositoriesFolder(), folder); if (!StringUtils.isEmpty(originRepo)) { // ensure origin still exists File repoFolder = new File(getRepositoriesFolder(), originRepo); if (repoFolder.exists()) { model.originRepository = originRepo.toLowerCase(); // persist the fork origin updateConfiguration(r, model); } } } catch (URISyntaxException e) { logger.error("Failed to determine fork for " + model, e); } } return model; }
From source file:com.github.rwhogg.git_vcr.App.java
License:Open Source License
/** * main is the entry point for Git-VCR//from w w w . jav a 2 s.c om * @param args Command-line arguments */ public static void main(String[] args) { Options options = parseCommandLine(args); HierarchicalINIConfiguration configuration = null; try { configuration = getConfiguration(); } catch (ConfigurationException e) { Util.error("could not parse configuration file!"); } // verify we are in a git folder and then construct the repo final File currentFolder = new File("."); FileRepositoryBuilder builder = new FileRepositoryBuilder(); Repository localRepo = null; try { localRepo = builder.findGitDir().build(); } catch (IOException e) { Util.error("not in a Git folder!"); } // deal with submodules assert localRepo != null; if (localRepo.isBare()) { FileRepositoryBuilder parentBuilder = new FileRepositoryBuilder(); Repository parentRepo; try { parentRepo = parentBuilder.setGitDir(new File("..")).findGitDir().build(); localRepo = SubmoduleWalk.getSubmoduleRepository(parentRepo, currentFolder.getName()); } catch (IOException e) { Util.error("could not find parent of submodule!"); } } // if we need to retrieve the patch file, get it now URL patchUrl = options.getPatchUrl(); String patchPath = patchUrl.getFile(); File patchFile = null; HttpUrl httpUrl = HttpUrl.get(patchUrl); if (httpUrl != null) { try { patchFile = com.twitter.common.io.FileUtils.SYSTEM_TMP.createFile(".diff"); Request request = new Request.Builder().url(httpUrl).build(); OkHttpClient client = new OkHttpClient(); Call call = client.newCall(request); Response response = call.execute(); ResponseBody body = response.body(); if (!response.isSuccessful()) { Util.error("could not retrieve diff file from URL " + patchUrl); } String content = body.string(); org.apache.commons.io.FileUtils.write(patchFile, content, (Charset) null); } catch (IOException ie) { Util.error("could not retrieve diff file from URL " + patchUrl); } } else { patchFile = new File(patchPath); } // find the patch //noinspection ConstantConditions if (!patchFile.canRead()) { Util.error("patch file " + patchFile.getAbsolutePath() + " is not readable!"); } final Git git = new Git(localRepo); // handle the branch String branchName = options.getBranchName(); String theOldCommit = null; try { theOldCommit = localRepo.getBranch(); } catch (IOException e2) { Util.error("could not get reference to current branch!"); } final String oldCommit = theOldCommit; // needed to reference from shutdown hook if (branchName != null) { // switch to the branch try { git.checkout().setName(branchName).call(); } catch (RefAlreadyExistsException e) { // FIXME Auto-generated catch block e.printStackTrace(); } catch (RefNotFoundException e) { Util.error("the branch " + branchName + " was not found!"); } catch (InvalidRefNameException e) { Util.error("the branch name " + branchName + " is invalid!"); } catch (org.eclipse.jgit.api.errors.CheckoutConflictException e) { Util.error("there was a checkout conflict!"); } catch (GitAPIException e) { Util.error("there was an unspecified Git API failure!"); } } // ensure there are no changes before we apply the patch try { if (!git.status().call().isClean()) { Util.error("cannot run git-vcr while there are uncommitted changes!"); } } catch (NoWorkTreeException e1) { // won't happen assert false; } catch (GitAPIException e1) { Util.error("call to git status failed!"); } // list all the files changed String patchName = patchFile.getName(); Patch patch = new Patch(); try { patch.parse(new FileInputStream(patchFile)); } catch (FileNotFoundException e) { assert false; } catch (IOException e) { Util.error("could not parse the patch file!"); } ReviewResults oldResults = new ReviewResults(patchName, patch, configuration, false); try { oldResults.review(); } catch (InstantiationException e1) { Util.error("could not instantiate a review tool class!"); } catch (IllegalAccessException e1) { Util.error("illegal access to a class"); } catch (ClassNotFoundException e1) { Util.error("could not find a review tool class"); } catch (ReviewFailedException e1) { e1.printStackTrace(); Util.error("Review failed!"); } // we're about to change the repo, so register a shutdown hook to clean it up Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { cleanupGit(git, oldCommit); } }); // apply the patch try { git.apply().setPatch(new FileInputStream(patchFile)).call(); } catch (PatchFormatException e) { Util.error("patch file " + patchFile.getAbsolutePath() + " is malformatted!"); } catch (PatchApplyException e) { Util.error("patch file " + patchFile.getAbsolutePath() + " did not apply correctly!"); } catch (FileNotFoundException e) { assert false; } catch (GitAPIException e) { Util.error(e.getLocalizedMessage()); } ReviewResults newResults = new ReviewResults(patchName, patch, configuration, true); try { newResults.review(); } catch (InstantiationException e1) { Util.error("could not instantiate a review tool class!"); } catch (IllegalAccessException e1) { Util.error("illegal access to a class"); } catch (ClassNotFoundException e1) { Util.error("could not find a review tool class"); } catch (ReviewFailedException e1) { e1.printStackTrace(); Util.error("Review failed!"); } // generate and show the report VelocityReport report = new VelocityReport(patch, oldResults, newResults); File reportFile = null; try { reportFile = com.twitter.common.io.FileUtils.SYSTEM_TMP.createFile(".html"); org.apache.commons.io.FileUtils.write(reportFile, report.toString(), (String) null); } catch (IOException e) { Util.error("could not generate the results page!"); } try { assert reportFile != null; Desktop.getDesktop().open(reportFile); } catch (IOException e) { Util.error("could not open the results page!"); } }
From source file:com.google.gerrit.server.git.ReadOnlyRepository.java
License:Apache License
private static BaseRepositoryBuilder<?, ?> builder(Repository r) { checkNotNull(r);//from ww w . j av a 2 s .c om BaseRepositoryBuilder<?, ?> builder = new BaseRepositoryBuilder<>().setFS(r.getFS()) .setGitDir(r.getDirectory()); if (!r.isBare()) { builder.setWorkTree(r.getWorkTree()).setIndexFile(r.getIndexFile()); } return builder; }
From source file:com.google.gitiles.DefaultAccess.java
License:Open Source License
private String getRepositoryName(Repository repo) { String path = getRelativePath(repo); if (repo.isBare() && path.endsWith(".git")) { path = path.substring(0, path.length() - 4); }//from w w w .j a v a 2 s .com return path; }
From source file:com.google.gitiles.DefaultAccess.java
License:Open Source License
private String getRelativePath(Repository repo) { String path = repo.isBare() ? repo.getDirectory().getPath() : repo.getDirectory().getParent(); if (repo.isBare()) { path = repo.getDirectory().getPath(); if (path.endsWith(".git")) { path = path.substring(0, path.length() - 4); }//from w w w . jav a2 s.c om } else { path = repo.getDirectory().getParent(); } return getRelativePath(path); }
From source file:com.googlesource.gerrit.plugins.gitiles.FilteredRepository.java
License:Apache License
private static RepositoryBuilder toBuilder(Repository repo) { RepositoryBuilder b = new RepositoryBuilder().setGitDir(repo.getDirectory()).setFS(repo.getFS()); if (!repo.isBare()) { b.setWorkTree(repo.getWorkTree()).setIndexFile(repo.getIndexFile()); }//from w w w . ja v a 2 s . c o m return b; }
From source file:com.madgag.agit.git.Repos.java
License:Open Source License
public static File topDirectoryFor(Repository repo) { return repo.isBare() ? repo.getDirectory() : repo.getWorkTree(); }