List of usage examples for org.eclipse.jgit.lib Repository getDirectory
public File getDirectory()
From source file:com.gitblit.utils.JGitUtils.java
License:Apache License
/** * Returns the date and author of the most recent commit on a branch. If the * repository does not exist Date(0) is returned. If it does exist but is * empty, the last modified date of the repository folder is returned. * * @param repository//from ww w.j a va 2 s.c o m * @return a LastChange object */ public static LastChange getLastChange(Repository repository) { if (!hasCommits(repository)) { // null repository if (repository == null) { return new LastChange(); } // fresh repository return new LastChange(repository.getDirectory().lastModified()); } List<RefModel> branchModels = getLocalBranches(repository, true, -1); if (branchModels.size() > 0) { // find most recent branch update LastChange lastChange = new LastChange(); for (RefModel branchModel : branchModels) { if (branchModel.getDate().after(lastChange.when)) { lastChange.when = branchModel.getDate(); lastChange.who = branchModel.getAuthorIdent().getName(); } } return lastChange; } // default to the repository folder modification date return new LastChange(repository.getDirectory().lastModified()); }
From source file:com.gitblit.utils.JGitUtils.java
License:Apache License
/** * Sets the symbolic ref HEAD to the specified target ref. The * HEAD will be detached if the target ref is not a branch. * * @param repository/*from w ww .j av a 2 s . com*/ * @param targetRef * @return true if successful */ public static boolean setHEADtoRef(Repository repository, String targetRef) { try { // detach HEAD if target ref is not a branch boolean detach = !targetRef.startsWith(Constants.R_HEADS); RefUpdate.Result result; RefUpdate head = repository.updateRef(Constants.HEAD, detach); if (detach) { // Tag RevCommit commit = getCommit(repository, targetRef); head.setNewObjectId(commit.getId()); result = head.forceUpdate(); } else { result = head.link(targetRef); } switch (result) { case NEW: case FORCED: case NO_CHANGE: case FAST_FORWARD: return true; default: LOGGER.error(MessageFormat.format("{0} HEAD update to {1} returned result {2}", repository.getDirectory().getAbsolutePath(), targetRef, result)); } } catch (Throwable t) { error(t, repository, "{0} failed to set HEAD to {1}", targetRef); } return false; }
From source file:com.gitblit.utils.JGitUtils.java
License:Apache License
/** * Sets the local branch ref to point to the specified commit id. * * @param repository/*from ww w . j ava2 s . c o m*/ * @param branch * @param commitId * @return true if successful */ public static boolean setBranchRef(Repository repository, String branch, String commitId) { String branchName = branch; if (!branchName.startsWith(Constants.R_REFS)) { branchName = Constants.R_HEADS + branch; } try { RefUpdate refUpdate = repository.updateRef(branchName, false); refUpdate.setNewObjectId(ObjectId.fromString(commitId)); RefUpdate.Result result = refUpdate.forceUpdate(); switch (result) { case NEW: case FORCED: case NO_CHANGE: case FAST_FORWARD: return true; default: LOGGER.error(MessageFormat.format("{0} {1} update to {2} returned result {3}", repository.getDirectory().getAbsolutePath(), branchName, commitId, result)); } } catch (Throwable t) { error(t, repository, "{0} failed to set {1} to {2}", branchName, commitId); } return false; }
From source file:com.gitblit.utils.JGitUtils.java
License:Apache License
/** * Deletes the specified branch ref.//from w w w .ja v a2 s . com * * @param repository * @param branch * @return true if successful */ public static boolean deleteBranchRef(Repository repository, String branch) { try { RefUpdate refUpdate = repository.updateRef(branch, false); refUpdate.setForceUpdate(true); RefUpdate.Result result = refUpdate.delete(); switch (result) { case NEW: case FORCED: case NO_CHANGE: case FAST_FORWARD: return true; default: LOGGER.error(MessageFormat.format("{0} failed to delete to {1} returned result {2}", repository.getDirectory().getAbsolutePath(), branch, result)); } } catch (Throwable t) { error(t, repository, "{0} failed to delete {1}", branch); } return false; }
From source file:com.gitblit.utils.JGitUtils.java
License:Apache License
/** * Returns the list of submodules for this repository. * * @param repository/*from w w w . java 2 s. c o m*/ * @param commit * @return list of submodules */ public static List<SubmoduleModel> getSubmodules(Repository repository, RevTree tree) { List<SubmoduleModel> list = new ArrayList<SubmoduleModel>(); byte[] blob = getByteContent(repository, tree, ".gitmodules", false); if (blob == null) { return list; } try { BlobBasedConfig config = new BlobBasedConfig(repository.getConfig(), blob); for (String module : config.getSubsections("submodule")) { String path = config.getString("submodule", module, "path"); String url = config.getString("submodule", module, "url"); list.add(new SubmoduleModel(module, path, url)); } } catch (ConfigInvalidException e) { LOGGER.error("Failed to load .gitmodules file for " + repository.getDirectory(), e); } return list; }
From source file:com.gitblit.utils.JGitUtils.java
License:Apache License
/** * creates a tag in a repository/*from ww w.java 2 s .co m*/ * * @param repository * @param objectId, the ref the tag points towards * @param tagger, the person tagging the object * @param tag, the string label * @param message, the string message * @return boolean, true if operation was successful, otherwise false */ public static boolean createTag(Repository repository, String objectId, PersonIdent tagger, String tag, String message) { try { Git gitClient = Git.open(repository.getDirectory()); TagCommand tagCommand = gitClient.tag(); tagCommand.setTagger(tagger); tagCommand.setMessage(message); if (objectId != null) { RevObject revObj = getCommit(repository, objectId); tagCommand.setObjectId(revObj); } tagCommand.setName(tag); Ref call = tagCommand.call(); return call != null ? true : false; } catch (Exception e) { error(e, repository, "Failed to create tag {1} in repository {0}", objectId, tag); } return false; }
From source file:com.gitblit.utils.JGitUtils.java
License:Apache License
/** * Automatic repair of (some) invalid refspecs. These are the result of a * bug in JGit cloning where a double forward-slash was injected. :( * * @param repository//from www. ja v a2 s .c o m * @return true, if the refspecs were repaired */ public static boolean repairFetchSpecs(Repository repository) { StoredConfig rc = repository.getConfig(); // auto-repair broken fetch ref specs for (String name : rc.getSubsections("remote")) { int invalidSpecs = 0; int repairedSpecs = 0; List<String> specs = new ArrayList<String>(); for (String spec : rc.getStringList("remote", name, "fetch")) { try { RefSpec rs = new RefSpec(spec); // valid spec specs.add(spec); } catch (IllegalArgumentException e) { // invalid spec invalidSpecs++; if (spec.contains("//")) { // auto-repair this known spec bug spec = spec.replace("//", "/"); specs.add(spec); repairedSpecs++; } } } if (invalidSpecs == repairedSpecs && repairedSpecs > 0) { // the fetch specs were automatically repaired rc.setStringList("remote", name, "fetch", specs); try { rc.save(); rc.load(); LOGGER.debug("repaired {} invalid fetch refspecs for {}", repairedSpecs, repository.getDirectory()); return true; } catch (Exception e) { LOGGER.error(null, e); } } else if (invalidSpecs > 0) { LOGGER.error("mirror executor found {} invalid fetch refspecs for {}", invalidSpecs, repository.getDirectory()); } } return false; }
From source file:com.gitblit.utils.JGitUtils.java
License:Apache License
/** * Tries to merge a commit into a branch. If there are conflicts, the merge * will fail./*from w w w.j ava 2s . c om*/ * * @param repository * @param src * @param toBranch * @param mergeType * Defines the integration strategy to use for merging. * @param committer * @param message * @return the merge result */ public static MergeResult merge(Repository repository, String src, String toBranch, MergeType mergeType, PersonIdent committer, String message) { if (!toBranch.startsWith(Constants.R_REFS)) { // branch ref doesn't start with ref, assume this is a branch head toBranch = Constants.R_HEADS + toBranch; } IntegrationStrategy strategy = IntegrationStrategyFactory.create(mergeType, repository, src, toBranch); MergeResult mergeResult = strategy.merge(committer, message); if (mergeResult.status != MergeStatus.MERGED) { return mergeResult; } try { // Update the integration branch ref RefUpdate mergeRefUpdate = repository.updateRef(toBranch); mergeRefUpdate.setNewObjectId(strategy.getMergeCommit()); mergeRefUpdate.setRefLogMessage(strategy.getRefLogMessage(), false); mergeRefUpdate.setExpectedOldObjectId(strategy.branchTip); RefUpdate.Result rc = mergeRefUpdate.update(); switch (rc) { case FAST_FORWARD: // successful, clean merge break; default: mergeResult = new MergeResult(MergeStatus.FAILED, null); throw new GitBlitException(MessageFormat.format("Unexpected result \"{0}\" when {1} in {2}", rc.name(), strategy.getOperationMessage(), repository.getDirectory())); } } catch (IOException e) { LOGGER.error("Failed to merge", e); } return mergeResult; }
From source file:com.gitblit.utils.RefLogUtils.java
License:Apache License
/** * Returns a RefModel for the reflog branch in the repository. If the * branch can not be found, null is returned. * * @param repository//from w ww. j a v a 2 s.co m * @return a refmodel for the reflog branch or null */ public static RefModel getRefLogBranch(Repository repository) { List<RefModel> refs = JGitUtils.getRefs(repository, "refs/"); Ref oldRef = null; for (RefModel ref : refs) { if (ref.reference.getName().equals(GB_REFLOG)) { return ref; } else if (ref.reference.getName().equals("refs/gitblit/reflog")) { oldRef = ref.reference; } else if (ref.reference.getName().equals("refs/gitblit/pushes")) { oldRef = ref.reference; } } if (oldRef != null) { // rename old ref to refs/meta/gitblit/reflog RefRename cmd; try { cmd = repository.renameRef(oldRef.getName(), GB_REFLOG); cmd.setRefLogIdent(new PersonIdent("Gitblit", "gitblit@localhost")); cmd.setRefLogMessage("renamed " + oldRef.getName() + " => " + GB_REFLOG); Result res = cmd.rename(); switch (res) { case RENAMED: LOGGER.info(repository.getDirectory() + " " + cmd.getRefLogMessage()); return getRefLogBranch(repository); default: LOGGER.error( "failed to rename " + oldRef.getName() + " => " + GB_REFLOG + " (" + res.name() + ")"); } } catch (IOException e) { LOGGER.error("failed to rename reflog", e); } } return null; }
From source file:com.github.checkstyle.regression.internal.GitUtils.java
License:Open Source License
public static File addAnEmptyFileAndCommit(Repository repository, String fileName) throws IOException, GitAPIException { try (Git git = new Git(repository)) { final File file = new File(repository.getDirectory().getParent(), fileName); if (!file.getParentFile().exists() && !file.getParentFile().mkdirs()) { throw new IOException("Could not create directory " + file.getParentFile()); }/*from w w w .j a va2s. c om*/ if (!file.createNewFile()) { throw new IOException("Could not create file " + file); } git.add().addFilepattern(fileName).call(); git.commit().setMessage("add " + fileName).call(); return file; } }