List of usage examples for org.eclipse.jgit.api Git getRepository
public Repository getRepository()
From source file:org.kie.commons.java.nio.fs.jgit.util.JGitUtil.java
License:Apache License
public static Git cloneRepository(final File repoFolder, final String fromURI, final boolean bare, final CredentialsProvider credentialsProvider) { if (!repoFolder.getName().endsWith(DOT_GIT_EXT)) { throw new RuntimeException("Invalid name"); }/*from ww w. ja v a2s . co m*/ try { final File gitDir = RepositoryCache.FileKey.resolve(repoFolder, DETECTED); final Repository repository; final Git git; if (gitDir != null && gitDir.exists()) { repository = new FileRepository(gitDir); git = new Git(repository); } else { git = Git.cloneRepository().setBare(bare).setCloneAllBranches(true).setURI(fromURI) .setDirectory(repoFolder).setCredentialsProvider(credentialsProvider).call(); repository = git.getRepository(); } fetchRepository(git, credentialsProvider); repository.close(); return git; } catch (final Exception ex) { throw new RuntimeException(ex); } }
From source file:org.kie.commons.java.nio.fs.jgit.util.JGitUtil.java
License:Apache License
public static void syncRepository(final Git git, final CredentialsProvider credentialsProvider, final String origin, boolean force) throws InvalidRemoteException { if (origin == null || origin.isEmpty()) { fetchRepository(git, credentialsProvider); } else {//w ww.j a v a 2 s. c om try { final StoredConfig config = git.getRepository().getConfig(); config.setString("remote", "upstream", "url", origin); config.save(); } catch (final Exception ex) { throw new RuntimeException(ex); } final List<RefSpec> specs = new ArrayList<RefSpec>(); specs.add(new RefSpec("+refs/heads/*:refs/remotes/upstream/*")); specs.add(new RefSpec("+refs/tags/*:refs/tags/*")); specs.add(new RefSpec("+refs/notes/*:refs/notes/*")); try { git.fetch().setCredentialsProvider(credentialsProvider).setRefSpecs(specs).setRemote(origin).call(); git.branchCreate().setName("master") .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.SET_UPSTREAM) .setStartPoint("upstream/master").setForce(true).call(); } catch (final InvalidRemoteException e) { throw e; } catch (final Exception ex) { throw new RuntimeException(ex); } } }
From source file:org.kie.commons.java.nio.fs.jgit.util.JGitUtil.java
License:Apache License
public static void commit(final Git git, final String branchName, final String name, final String email, final String message, final TimeZone timeZone, final Date when, final Map<String, File> content) { final PersonIdent author = buildPersonIdent(git, name, email, timeZone, when); try {/* ww w . j a v a2 s. c o m*/ final ObjectInserter odi = git.getRepository().newObjectInserter(); try { // Create the in-memory index of the new/updated issue. final ObjectId headId = git.getRepository().resolve(branchName + "^{commit}"); final DirCache index = createTemporaryIndex(git, headId, content); final ObjectId indexTreeId = index.writeTree(odi); // Create a commit object final CommitBuilder commit = new CommitBuilder(); commit.setAuthor(author); commit.setCommitter(author); commit.setEncoding(Constants.CHARACTER_ENCODING); commit.setMessage(message); //headId can be null if the repository has no commit yet if (headId != null) { commit.setParentId(headId); } commit.setTreeId(indexTreeId); // Insert the commit into the repository final ObjectId commitId = odi.insert(commit); odi.flush(); final RevWalk revWalk = new RevWalk(git.getRepository()); try { final RevCommit revCommit = revWalk.parseCommit(commitId); final RefUpdate ru = git.getRepository().updateRef("refs/heads/" + branchName); if (headId == null) { ru.setExpectedOldObjectId(ObjectId.zeroId()); } else { ru.setExpectedOldObjectId(headId); } ru.setNewObjectId(commitId); ru.setRefLogMessage("commit: " + revCommit.getShortMessage(), false); final RefUpdate.Result rc = ru.forceUpdate(); switch (rc) { case NEW: case FORCED: case FAST_FORWARD: break; case REJECTED: case LOCK_FAILURE: throw new ConcurrentRefUpdateException(JGitText.get().couldNotLockHEAD, ru.getRef(), rc); default: throw new JGitInternalException(MessageFormat.format(JGitText.get().updatingRefFailed, Constants.HEAD, commitId.toString(), rc)); } } finally { revWalk.release(); } } finally { odi.release(); } } catch (final Throwable t) { throw new RuntimeException(t); } }
From source file:org.kie.commons.java.nio.fs.jgit.util.JGitUtil.java
License:Apache License
private static PersonIdent buildPersonIdent(final Git git, final String name, final String email, final TimeZone timeZone, final Date when) { final TimeZone tz = timeZone == null ? TimeZone.getDefault() : timeZone; if (name != null) { if (when != null) { return new PersonIdent(name, email, when, tz); } else {//from ww w . j av a 2 s . com return new PersonIdent(name, email); } } return new PersonIdent(git.getRepository()); }
From source file:org.kie.commons.java.nio.fs.jgit.util.JGitUtil.java
License:Apache License
/** * Creates an in-memory index of the issue change. *//*from w w w . j av a2s . co m*/ private static DirCache createTemporaryIndex(final Git git, final ObjectId headId, final Map<String, File> content) { final DirCache inCoreIndex = DirCache.newInCore(); final DirCacheBuilder dcBuilder = inCoreIndex.builder(); final ObjectInserter inserter = git.getRepository().newObjectInserter(); boolean hadFile = false; final Set<String> paths = new HashSet<String>(content.size()); try { for (final Map.Entry<String, File> pathAndContent : content.entrySet()) { final String gPath = fixPath(pathAndContent.getKey()); paths.add(gPath); if (pathAndContent.getValue() != null) { hadFile = true; final DirCacheEntry dcEntry = new DirCacheEntry(gPath); dcEntry.setLength(pathAndContent.getValue().length()); dcEntry.setLastModified(pathAndContent.getValue().lastModified()); dcEntry.setFileMode(REGULAR_FILE); final InputStream inputStream = new FileInputStream(pathAndContent.getValue()); try { final ObjectId objectId = inserter.insert(Constants.OBJ_BLOB, pathAndContent.getValue().length(), inputStream); dcEntry.setObjectId(objectId); } finally { inputStream.close(); } dcBuilder.add(dcEntry); } if (!hadFile) { final DirCacheEditor editor = inCoreIndex.editor(); editor.add(new DirCacheEditor.DeleteTree(gPath)); editor.finish(); } } if (headId != null) { final TreeWalk treeWalk = new TreeWalk(git.getRepository()); final int hIdx = treeWalk.addTree(new RevWalk(git.getRepository()).parseTree(headId)); treeWalk.setRecursive(true); while (treeWalk.next()) { final String walkPath = treeWalk.getPathString(); final CanonicalTreeParser hTree = treeWalk.getTree(hIdx, CanonicalTreeParser.class); if (!paths.contains(walkPath)) { // add entries from HEAD for all other paths // create a new DirCacheEntry with data retrieved from HEAD final DirCacheEntry dcEntry = new DirCacheEntry(walkPath); dcEntry.setObjectId(hTree.getEntryObjectId()); dcEntry.setFileMode(hTree.getEntryFileMode()); // add to temporary in-core index dcBuilder.add(dcEntry); } } treeWalk.release(); } dcBuilder.finish(); } catch (Exception e) { throw new RuntimeException(e); } finally { inserter.release(); } return inCoreIndex; }
From source file:org.kie.commons.java.nio.fs.jgit.util.JGitUtil.java
License:Apache License
public static ObjectId resolveObjectId(final Git git, final String name) { try {/*from www .j ava 2s . c o m*/ final Ref refName = getBranch(git, name); if (refName != null) { return refName.getObjectId(); } try { final ObjectId id = ObjectId.fromString(name); if (git.getRepository().getObjectDatabase().has(id)) { return id; } } catch (final IllegalArgumentException ex) { } return null; } catch (java.io.IOException e) { } return null; }
From source file:org.kie.commons.java.nio.fs.jgit.util.JGitUtil.java
License:Apache License
public static Ref getBranch(final Git git, final String name) { try {/*from ww w . j a v a 2 s.c o m*/ return git.getRepository().getRefDatabase().getRef(name); } catch (java.io.IOException e) { } return null; }
From source file:org.kie.commons.java.nio.fs.jgit.util.JGitUtil.java
License:Apache License
public static Pair<PathType, ObjectId> checkPath(final Git git, final String branchName, final String path) { checkNotNull("git", git); checkNotNull("path", path); checkNotEmpty("branchName", branchName); final String gitPath = fixPath(path); if (gitPath.isEmpty()) { return newPair(PathType.DIRECTORY, null); }/*w w w.j av a2s . com*/ TreeWalk tw = null; try { final ObjectId tree = git.getRepository().resolve(branchName + "^{tree}"); tw = new TreeWalk(git.getRepository()); tw.setFilter(PathFilter.create(gitPath)); tw.reset(tree); while (tw.next()) { if (tw.getPathString().equals(gitPath)) { if (tw.getFileMode(0).equals(FileMode.TYPE_TREE)) { return newPair(PathType.DIRECTORY, tw.getObjectId(0)); } else if (tw.getFileMode(0).equals(FileMode.TYPE_FILE)) { return newPair(PathType.FILE, tw.getObjectId(0)); } } if (tw.isSubtree()) { tw.enterSubtree(); continue; } } } catch (final Throwable t) { } finally { if (tw != null) { tw.release(); } } return newPair(PathType.NOT_FOUND, null); }
From source file:org.kie.commons.java.nio.fs.jgit.util.JGitUtil.java
License:Apache License
public static JGitPathInfo resolvePath(final Git git, final String branchName, final String path) { checkNotNull("git", git); checkNotNull("path", path); checkNotEmpty("branchName", branchName); final String gitPath = fixPath(path); if (gitPath.isEmpty()) { return new JGitPathInfo(null, "/", TREE); }/*from www . ja v a2 s. c o m*/ TreeWalk tw = null; try { final ObjectId tree = git.getRepository().resolve(branchName + "^{tree}"); tw = new TreeWalk(git.getRepository()); tw.setFilter(PathFilter.create(gitPath)); tw.reset(tree); while (tw.next()) { if (tw.getPathString().equals(gitPath)) { if (tw.getFileMode(0).equals(TREE)) { return new JGitPathInfo(tw.getObjectId(0), tw.getPathString(), TREE); } else if (tw.getFileMode(0).equals(REGULAR_FILE) || tw.getFileMode(0).equals(EXECUTABLE_FILE)) { final long size = tw.getObjectReader().getObjectSize(tw.getObjectId(0), OBJ_BLOB); return new JGitPathInfo(tw.getObjectId(0), tw.getPathString(), REGULAR_FILE, size); } } if (tw.isSubtree()) { tw.enterSubtree(); continue; } } } catch (final Throwable t) { } finally { if (tw != null) { tw.release(); } } return null; }
From source file:org.kie.commons.java.nio.fs.jgit.util.JGitUtil.java
License:Apache License
public static List<JGitPathInfo> listPathContent(final Git git, final String branchName, final String path) { checkNotNull("git", git); checkNotNull("path", path); checkNotEmpty("branchName", branchName); final String gitPath = fixPath(path); TreeWalk tw = null;// w w w. ja v a2 s.c o m final List<JGitPathInfo> result = new ArrayList<JGitPathInfo>(); try { final ObjectId tree = git.getRepository().resolve(branchName + "^{tree}"); tw = new TreeWalk(git.getRepository()); boolean found = false; if (gitPath.isEmpty()) { found = true; } else { tw.setFilter(PathFilter.create(gitPath)); } tw.reset(tree); while (tw.next()) { if (!found && tw.isSubtree()) { tw.enterSubtree(); } if (tw.getPathString().equals(gitPath)) { found = true; continue; } if (found) { result.add(new JGitPathInfo(tw.getObjectId(0), tw.getPathString(), tw.getFileMode(0))); } } } catch (final Throwable t) { } finally { if (tw != null) { tw.release(); } } return result; }