List of usage examples for org.eclipse.jgit.lib Constants HEAD
String HEAD
To view the source code for org.eclipse.jgit.lib Constants HEAD.
Click Source Link
From source file:org.commonjava.gitwrap.BareGitRepository.java
License:Open Source License
protected void postClone(final String remoteUrl, final String branchRef) throws GitWrapException { try {//w w w. ja v a 2s . c o m final FetchResult fetchResult = getLatestFetchResult(); final Ref remoteHead = fetchResult.getAdvertisedRef(branchRef); if (remoteHead != null && remoteHead.getObjectId() != null) { final RevWalk walk = new RevWalk(repository); final RevCommit commit = walk.parseCommit(remoteHead.getObjectId()); final RefUpdate u; u = repository.updateRef(Constants.HEAD); u.setNewObjectId(commit); u.forceUpdate(); } } catch (final IOException e) { throw new GitWrapException("Failed to clone from: %s. Reason: %s", e, remoteUrl, e.getMessage()); } }
From source file:org.commonjava.gitwrap.BareGitRepository.java
License:Open Source License
public BareGitRepository createTagFromHead(final String tagName, final String message) throws GitWrapException { return createTag(Constants.HEAD, tagName, message); }
From source file:org.commonjava.gitwrap.BareGitRepository.java
License:Open Source License
public String getHeadRevision() throws GitWrapException { try {/*from w ww . j a va2s .c o m*/ return repository.resolve(Constants.HEAD).getName(); } catch (final IOException e) { throw new GitWrapException("Failed to retrieve HEAD revision. Reason: %s", e, e.getMessage()); } }
From source file:org.commonjava.gitwrap.BareGitRepository.java
License:Open Source License
public BareGitRepository createBranchFromHead(final String name) throws GitWrapException { return createBranch(Constants.HEAD, name); }
From source file:org.commonjava.gitwrap.GitRepository.java
License:Open Source License
public GitRepository checkoutBranch(final String name) throws GitWrapException { final String refName; if (name == null) { refName = Constants.HEAD; } else if (name.startsWith(Constants.R_HEADS) || name.startsWith(Constants.R_TAGS)) { refName = name;//w w w . j a v a 2 s .co m } else { refName = Constants.R_HEADS + name; } if (hasBranch(refName)) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Checking out: " + refName); } final FileRepository repository = getRepository(); final boolean detach = !refName.startsWith(Constants.R_HEADS); try { final RevWalk walk = new RevWalk(repository); final RevCommit newCommit = walk.parseCommit(repository.resolve(refName)); final RevCommit oldCommit = walk.parseCommit(repository.resolve(Constants.HEAD)); final GitIndex index = repository.getIndex(); final RevTree newTree = newCommit.getTree(); final RevTree oldTree = oldCommit.getTree(); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Checking out: " + newCommit + " (resolved from: " + refName + ")"); } final WorkDirCheckout checkout = new WorkDirCheckout(repository, repository.getWorkTree(), repository.mapTree(oldTree), index, repository.mapTree(newTree)); checkout.checkout(); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Writing index..."); } index.write(); final RefUpdate u = repository.updateRef(Constants.HEAD, detach); Result result; if (detach) { u.setNewObjectId(newCommit.getId()); u.setRefLogMessage("Switching to detached branch: " + refName, false); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Updating head ref to point to: " + newCommit.getId()); } result = u.forceUpdate(); } else { u.setRefLogMessage("Switching to linked branch: " + refName, false); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Linking head ref to: " + refName); } result = u.link(refName); } switch (result) { case NEW: case FORCED: case NO_CHANGE: case FAST_FORWARD: break; default: throw new GitWrapException("Error checking out branch: %s. Result: %s", refName, u.getResult().name()); } } catch (final IOException e) { throw new GitWrapException("Failed to checkout branch: %s.\nReason: %s", e, refName, e.getMessage()); } } else { throw new GitWrapException( "Cannot checkout non-existent branch: %s. Perhaps you meant to call createBranch(..)?", refName); } return this; }
From source file:org.commonjava.gitwrap.GitRepositoryTest.java
License:Open Source License
@Test public void cloneNonBare_BranchMasterToNakedRefname() throws IOException, GitWrapException, URISyntaxException { final File gitDir = new File(".git"); if (gitDir.exists()) { final FileRepositoryBuilder builder = new FileRepositoryBuilder(); builder.setGitDir(gitDir);/* ww w . java 2 s . c o m*/ builder.setup(); final FileRepository repository = new FileRepository(builder); final RemoteConfig config = new RemoteConfig(repository.getConfig(), "origin"); workDir = File.createTempFile("git-clone", ""); workDir.delete(); final File newGitDir = new File(workDir, ".git"); GitRepository.setProgressMonitor(new TextProgressMonitor()); final GitRepository clone = GitRepository.cloneWithWorkdir(config.getURIs().get(0).toString(), "origin", newGitDir, false); clone.createBranch(Constants.HEAD, "test-branch"); System.out.println(clone.getGitDir()); System.out.println(); final FetchResult fetchResult = clone.getLatestFetchResult(); for (final Ref ref : fetchResult.getAdvertisedRefs()) { System.out.println(ref.getName()); } System.out.println(); for (final TrackingRefUpdate update : fetchResult.getTrackingRefUpdates()) { System.out.println(update.getLocalName() + " -> " + update.getRemoteName()); } } }
From source file:org.commonjava.indy.subsys.git.GitManager.java
License:Apache License
public List<ChangeSummary> getChangelog(final File f, final int start, final int length) throws GitSubsystemException { return lockAnd(me -> { if (length == 0) { return Collections.emptyList(); }/*from w w w .j a va 2s . c o m*/ try { final ObjectId oid = repo.resolve(Constants.HEAD); final PlotWalk pw = new PlotWalk(repo); final RevCommit rc = pw.parseCommit(oid); toChangeSummary(rc); pw.markStart(rc); final String filepath = relativize(f); logger.info("Getting changelog for: {} (start: {}, length: {})", filepath, start, length); if (!isEmpty(filepath) && !filepath.equals("/")) { pw.setTreeFilter(AndTreeFilter.create(PathFilter.create(filepath), TreeFilter.ANY_DIFF)); } else { pw.setTreeFilter(TreeFilter.ANY_DIFF); } final List<ChangeSummary> changelogs = new ArrayList<ChangeSummary>(); int count = 0; final int stop = length > 0 ? length + 1 : 0; RevCommit commit = null; while ((commit = pw.next()) != null && (stop < 1 || count < stop)) { if (count < start) { count++; continue; } // printFiles( commit ); changelogs.add(toChangeSummary(commit)); count++; } if (length < -1) { final int remove = (-1 * length) - 1; for (int i = 0; i < remove; i++) { changelogs.remove(changelogs.size() - 1); } } return changelogs; } catch (RevisionSyntaxException | IOException e) { throw new GitSubsystemException("Failed to resolve HEAD commit for: %s. Reason: %s", e, f, e.getMessage()); } }); }
From source file:org.craftercms.deployer.impl.processors.GitDiffProcessor.java
License:Open Source License
protected ObjectId getLatestCommitId(Git git) throws DeployerException { try {/* w w w . jav a 2s .c o m*/ return git.getRepository().resolve(Constants.HEAD); } catch (IOException e) { throw new DeployerException("Unable to retrieve HEAD commit ID", e); } }
From source file:org.craftercms.deployer.impl.processors.GitPullProcessor.java
License:Open Source License
protected ChangeSet pullChanges(Git git) { try {// w w w. j a va 2 s . com logger.info("Executing git pull for repository {}...", localRepositoryFolder); ObjectId head = git.getRepository().resolve(Constants.HEAD); PullResult pullResult = git.pull().call(); if (pullResult.isSuccessful()) { MergeResult mergeResult = pullResult.getMergeResult(); switch (mergeResult.getMergeStatus()) { case FAST_FORWARD: logger.info("Changes successfully pulled from remote {} for repository {}. Processing them...", remoteRepositoryUrl, localRepositoryFolder); return resolveChangeSetFromPull(git, head, mergeResult.getNewHead()); case ALREADY_UP_TO_DATE: logger.info("Git repository {} up to date (no changes pulled from remote {})", localRepositoryFolder, remoteRepositoryUrl); return new ChangeSetImpl(); default: // Not supported merge results throw new DeploymentException("Received unsupported merge result after executing pull command: " + mergeResult.getMergeStatus()); } } } catch (Exception e) { throw new DeploymentException("Git pull for repository " + localRepositoryFolder + " failed", e); } return new ChangeSetImpl(); }
From source file:org.craftercms.studio.impl.v1.repository.git.GitContentRepository.java
License:Open Source License
@Override public VersionTO[] getContentVersionHistory(String site, String path) { List<VersionTO> versionHistory = new ArrayList<VersionTO>(); synchronized (helper.getRepository(site, StringUtils.isEmpty(site) ? GitRepositories.GLOBAL : SANDBOX)) { Repository repo = helper.getRepository(site, StringUtils.isEmpty(site) ? GitRepositories.GLOBAL : GitRepositories.SANDBOX); try {/* w w w. j a v a2 s . c om*/ ObjectId head = repo.resolve(Constants.HEAD); String gitPath = helper.getGitPath(path); try (Git git = new Git(repo)) { Iterable<RevCommit> commits = git.log().add(head).addPath(gitPath).call(); Iterator<RevCommit> iterator = commits.iterator(); while (iterator.hasNext()) { RevCommit revCommit = iterator.next(); VersionTO versionTO = new VersionTO(); versionTO.setVersionNumber(revCommit.getName()); versionTO.setLastModifier(revCommit.getAuthorIdent().getName()); versionTO.setLastModifiedDate(new Date(revCommit.getCommitTime() * 1000)); versionTO.setComment(revCommit.getFullMessage()); versionHistory.add(versionTO); } git.close(); } catch (IOException e) { logger.error("error while getting history for content item " + path); } } catch (IOException | GitAPIException e) { logger.error("Failed to create Git repo for site: " + site + " path: " + path, e); } } VersionTO[] toRet = new VersionTO[versionHistory.size()]; return versionHistory.toArray(toRet); }