List of usage examples for org.eclipse.jgit.lib ObjectId getName
public final String getName()
From source file:org.modeshape.connector.git.GitBinaryValue.java
License:Apache License
public GitBinaryValue(ObjectId id, ObjectLoader loader, String sourceName, String nameHint, MimeTypeDetector mimeTypeDetector) { super(new BinaryKey(id.getName()), sourceName, id.getName(), loader.getSize(), nameHint, mimeTypeDetector); this.loader = loader; }
From source file:org.modeshape.connector.git.GitCommitDetails.java
License:Apache License
protected static Object referenceToCommit(ObjectId id, Values values) { return values.referenceTo(ID + DELIMITER + id.getName()); }
From source file:org.modeshape.connector.git.GitHistory.java
License:Apache License
protected static Object referenceToHistory(ObjectId id, String branchOrTagName, Values values) { return values.referenceTo(ID + DELIMITER + branchOrTagName + DELIMITER + id.getName()); }
From source file:org.modeshape.connector.git.GitTree.java
License:Apache License
protected void addInformationForPath(Repository repository, Git git, DocumentWriter writer, RevCommit commit, String path, CallSpecification spec, Values values) throws GitAPIException, IOException { // Make sure the path is in the canonical form we need ... if (path.startsWith("/")) { if (path.length() == 1) path = ""; else/*from www . ja va2s. c o m*/ path = path.substring(1); } // Now see if we're actually referring to the "jcr:content" node ... boolean isContentNode = false; if (path.endsWith(JCR_CONTENT_SUFFIX)) { isContentNode = true; path = path.substring(0, path.length() - JCR_CONTENT_SUFFIX.length()); } // Create the TreeWalk that we'll use to navigate the files/directories ... final TreeWalk tw = new TreeWalk(repository); tw.addTree(commit.getTree()); if ("".equals(path)) { // This is the top-level directory, so we don't need to pre-walk to find anything ... tw.setRecursive(false); while (tw.next()) { String childName = tw.getNameString(); String childId = spec.childId(childName); writer.addChild(childId, childName); } } else { // We need to first find our path *before* we can walk the children ... PathFilter filter = PathFilter.create(path); tw.setFilter(filter); while (tw.next()) { if (filter.isDone(tw)) { break; } else if (tw.isSubtree()) { tw.enterSubtree(); } } // Now that the TreeWalk is the in right location given by the 'path', we can get the if (tw.isSubtree()) { // The object at the 'path' is a directory, so go into it ... tw.enterSubtree(); // Find the commit in which this folder was last modified ... // This may not be terribly efficient, but it seems to work faster on subsequent runs ... RevCommit folderCommit = git.log().addPath(path).call().iterator().next(); writer.setPrimaryType(GitLexicon.FOLDER); // could happen if not enough permissions, for example if (folderCommit != null) { // Add folder-related properties ... String committer = commiterName(folderCommit); String author = authorName(folderCommit); DateTime committed = values.dateFrom(folderCommit.getCommitTime()); writer.addProperty(JcrLexicon.CREATED, committed); writer.addProperty(JcrLexicon.CREATED_BY, committer); writer.addProperty(GitLexicon.OBJECT_ID, folderCommit.getId().name()); writer.addProperty(GitLexicon.AUTHOR, author); writer.addProperty(GitLexicon.COMMITTER, committer); writer.addProperty(GitLexicon.COMMITTED, committed); writer.addProperty(GitLexicon.TITLE, folderCommit.getShortMessage()); } else { connector.getLogger().warn(GitI18n.cannotReadCommit, path); } // And now walk the contents of the directory ... while (tw.next()) { String childName = tw.getNameString(); String childId = spec.childId(childName); writer.addChild(childId, childName); } } else { // The path specifies a file (or a content node) ... // Find the commit in which this folder was last modified ... // This may not be terribly efficient, but it seems to work faster on subsequent runs ... RevCommit fileCommit = git.log().addPath(path).call().iterator().next(); if (isContentNode) { writer.setPrimaryType(GitLexicon.RESOURCE); if (fileCommit == null) { // could happen if not enough permissions, for example connector.getLogger().warn(GitI18n.cannotReadCommit, path); return; } // Add file-related properties ... String committer = commiterName(fileCommit); String author = authorName(fileCommit); DateTime committed = values.dateFrom(fileCommit.getCommitTime()); writer.addProperty(JcrLexicon.LAST_MODIFIED, committed); writer.addProperty(JcrLexicon.LAST_MODIFIED_BY, committer); writer.addProperty(GitLexicon.OBJECT_ID, fileCommit.getId().name()); writer.addProperty(GitLexicon.AUTHOR, author); writer.addProperty(GitLexicon.COMMITTER, committer); writer.addProperty(GitLexicon.COMMITTED, committed); writer.addProperty(GitLexicon.TITLE, fileCommit.getShortMessage()); // Create the BinaryValue ... ObjectId fileObjectId = tw.getObjectId(0); ObjectLoader fileLoader = repository.open(fileObjectId); BinaryKey key = new BinaryKey(fileObjectId.getName()); BinaryValue value = values.binaryFor(key, fileLoader.getSize()); if (value == null) { // It wasn't found in the binary store ... if (fileLoader.isLarge()) { // Too large to hold in memory, so use the binary store (which reads the file immediately) ... value = values.binaryFrom(fileLoader.openStream()); } else { // This is small enough to fit into a byte[], but it still may be pretty big ... value = new GitBinaryValue(fileObjectId, fileLoader, connector.getSourceName(), name, connector.getMimeTypeDetector()); } } writer.addProperty(JcrLexicon.DATA, value); if (connector.includeMimeType()) { try { String filename = spec.parameter(spec.parameterCount() - 1); // the last is 'jcr:content' String mimeType = value.getMimeType(filename); if (mimeType != null) writer.addProperty(JcrLexicon.MIMETYPE, mimeType); } catch (RepositoryException e) { // do nothing } catch (IOException e) { // do nothing } } } else { writer.setPrimaryType(GitLexicon.FILE); if (fileCommit == null) { // could happen if not enough permissions, for example connector.getLogger().warn(GitI18n.cannotReadCommit, path); return; } // Add file-related properties ... String committer = commiterName(fileCommit); String author = authorName(fileCommit); DateTime committed = values.dateFrom(fileCommit.getCommitTime()); writer.addProperty(JcrLexicon.CREATED, committed); writer.addProperty(JcrLexicon.CREATED_BY, committer); writer.addProperty(GitLexicon.OBJECT_ID, fileCommit.getId().name()); writer.addProperty(GitLexicon.AUTHOR, author); writer.addProperty(GitLexicon.COMMITTER, committer); writer.addProperty(GitLexicon.COMMITTED, committed); writer.addProperty(GitLexicon.TITLE, fileCommit.getShortMessage()); // Add the "jcr:content" child node ... String childId = spec.childId(JCR_CONTENT); writer.addChild(childId, JCR_CONTENT); } } } }
From source file:org.moxie.utils.JGitUtils.java
License:Apache License
public static String getCommitId(File folder) { // try specified folder or subfolder File gitDir = FileKey.resolve(folder, FS.DETECTED); if (gitDir == null || !gitDir.exists()) { // try parent folder gitDir = FileKey.resolve(folder.getParentFile(), FS.DETECTED); }/*w w w . jav a2s. co m*/ if (gitDir == null || !gitDir.exists()) { throw new MoxieException("Can not find .git folder for " + folder); } String hashid = ""; try { Repository repository = new FileRepository(gitDir); ObjectId objectId = repository.resolve(org.eclipse.jgit.lib.Constants.HEAD); hashid = objectId.getName().toString(); repository.close(); } catch (IOException io) { io.printStackTrace(); throw new MoxieException("IOException accessing " + gitDir.getAbsolutePath(), io); } return hashid; }
From source file:org.springframework.cloud.config.server.environment.JGitEnvironmentRepositoryTests.java
License:Apache License
@Test public void testFetchException() throws Exception { Git git = mock(Git.class); CloneCommand cloneCommand = mock(CloneCommand.class); MockGitFactory factory = new MockGitFactory(git, cloneCommand); JGitEnvironmentRepository repo = new JGitEnvironmentRepository(this.environment); this.repository.setGitFactory(factory); //refresh()->shouldPull StatusCommand statusCommand = mock(StatusCommand.class); Status status = mock(Status.class); when(git.status()).thenReturn(statusCommand); Repository repository = mock(Repository.class); when(git.getRepository()).thenReturn(repository); StoredConfig storedConfig = mock(StoredConfig.class); when(repository.getConfig()).thenReturn(storedConfig); when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git"); when(statusCommand.call()).thenReturn(status); when(status.isClean()).thenReturn(true); //refresh()->fetch FetchCommand fetchCommand = mock(FetchCommand.class); when(git.fetch()).thenReturn(fetchCommand); when(fetchCommand.setRemote(anyString())).thenReturn(fetchCommand); when(fetchCommand.call()).thenThrow(new InvalidRemoteException("invalid mock remote")); //here is our exception we are testing //refresh()->checkout CheckoutCommand checkoutCommand = mock(CheckoutCommand.class); //refresh()->checkout->containsBranch ListBranchCommand listBranchCommand = mock(ListBranchCommand.class); when(git.checkout()).thenReturn(checkoutCommand); when(git.branchList()).thenReturn(listBranchCommand); List<Ref> refs = new ArrayList<>(); Ref ref = mock(Ref.class); refs.add(ref);/* w w w . j av a2 s. c om*/ when(ref.getName()).thenReturn("/master"); when(listBranchCommand.call()).thenReturn(refs); //refresh()->merge MergeCommand mergeCommand = mock(MergeCommand.class); when(git.merge()).thenReturn(mergeCommand); when(mergeCommand.call()).thenThrow(new NotMergedException()); //here is our exception we are testing //refresh()->return git.getRepository().getRef("HEAD").getObjectId().getName(); Ref headRef = mock(Ref.class); when(repository.getRef(anyString())).thenReturn(headRef); ObjectId newObjectId = ObjectId.fromRaw(new int[] { 1, 2, 3, 4, 5 }); when(headRef.getObjectId()).thenReturn(newObjectId); SearchPathLocator.Locations locations = this.repository.getLocations("bar", "staging", null); assertEquals(locations.getVersion(), newObjectId.getName()); }
From source file:org.springframework.cloud.config.server.environment.JGitEnvironmentRepositoryTests.java
License:Apache License
@Test public void testMergeException() throws Exception { Git git = mock(Git.class); CloneCommand cloneCommand = mock(CloneCommand.class); MockGitFactory factory = new MockGitFactory(git, cloneCommand); JGitEnvironmentRepository repo = new JGitEnvironmentRepository(this.environment); this.repository.setGitFactory(factory); //refresh()->shouldPull StatusCommand statusCommand = mock(StatusCommand.class); Status status = mock(Status.class); when(git.status()).thenReturn(statusCommand); Repository repository = mock(Repository.class); when(git.getRepository()).thenReturn(repository); StoredConfig storedConfig = mock(StoredConfig.class); when(repository.getConfig()).thenReturn(storedConfig); when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git"); when(statusCommand.call()).thenReturn(status); when(status.isClean()).thenReturn(true); //refresh()->fetch FetchCommand fetchCommand = mock(FetchCommand.class); FetchResult fetchResult = mock(FetchResult.class); when(git.fetch()).thenReturn(fetchCommand); when(fetchCommand.setRemote(anyString())).thenReturn(fetchCommand); when(fetchCommand.call()).thenReturn(fetchResult); when(fetchResult.getTrackingRefUpdates()).thenReturn(Collections.EMPTY_LIST); //refresh()->checkout CheckoutCommand checkoutCommand = mock(CheckoutCommand.class); //refresh()->checkout->containsBranch ListBranchCommand listBranchCommand = mock(ListBranchCommand.class); when(git.checkout()).thenReturn(checkoutCommand); when(git.branchList()).thenReturn(listBranchCommand); List<Ref> refs = new ArrayList<>(); Ref ref = mock(Ref.class); refs.add(ref);/* w ww . ja va 2 s . com*/ when(ref.getName()).thenReturn("/master"); when(listBranchCommand.call()).thenReturn(refs); //refresh()->merge MergeCommand mergeCommand = mock(MergeCommand.class); when(git.merge()).thenReturn(mergeCommand); when(mergeCommand.call()).thenThrow(new NotMergedException()); //here is our exception we are testing //refresh()->return git.getRepository().getRef("HEAD").getObjectId().getName(); Ref headRef = mock(Ref.class); when(repository.getRef(anyString())).thenReturn(headRef); ObjectId newObjectId = ObjectId.fromRaw(new int[] { 1, 2, 3, 4, 5 }); when(headRef.getObjectId()).thenReturn(newObjectId); SearchPathLocator.Locations locations = this.repository.getLocations("bar", "staging", "master"); assertEquals(locations.getVersion(), newObjectId.getName()); }
From source file:org.springframework.cloud.config.server.environment.JGitEnvironmentRepositoryTests.java
License:Apache License
@Test public void testResetHardException() throws Exception { Git git = mock(Git.class); CloneCommand cloneCommand = mock(CloneCommand.class); MockGitFactory factory = new MockGitFactory(git, cloneCommand); JGitEnvironmentRepository repo = new JGitEnvironmentRepository(this.environment); this.repository.setGitFactory(factory); //refresh()->shouldPull StatusCommand statusCommand = mock(StatusCommand.class); Status status = mock(Status.class); when(git.status()).thenReturn(statusCommand); Repository repository = mock(Repository.class); when(git.getRepository()).thenReturn(repository); StoredConfig storedConfig = mock(StoredConfig.class); when(repository.getConfig()).thenReturn(storedConfig); when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git"); when(statusCommand.call()).thenReturn(status); when(status.isClean()).thenReturn(true).thenReturn(false); //refresh()->fetch FetchCommand fetchCommand = mock(FetchCommand.class); FetchResult fetchResult = mock(FetchResult.class); when(git.fetch()).thenReturn(fetchCommand); when(fetchCommand.setRemote(anyString())).thenReturn(fetchCommand); when(fetchCommand.call()).thenReturn(fetchResult); when(fetchResult.getTrackingRefUpdates()).thenReturn(Collections.EMPTY_LIST); //refresh()->checkout CheckoutCommand checkoutCommand = mock(CheckoutCommand.class); //refresh()->checkout->containsBranch ListBranchCommand listBranchCommand = mock(ListBranchCommand.class); when(git.checkout()).thenReturn(checkoutCommand); when(git.branchList()).thenReturn(listBranchCommand); List<Ref> refs = new ArrayList<>(); Ref ref = mock(Ref.class); refs.add(ref);//from ww w . java2s . c om when(ref.getName()).thenReturn("/master"); when(listBranchCommand.call()).thenReturn(refs); //refresh()->merge MergeCommand mergeCommand = mock(MergeCommand.class); when(git.merge()).thenReturn(mergeCommand); when(mergeCommand.call()).thenThrow(new NotMergedException()); //here is our exception we are testing //refresh()->hardReset ResetCommand resetCommand = mock(ResetCommand.class); when(git.reset()).thenReturn(resetCommand); when(resetCommand.call()).thenReturn(ref); //refresh()->return git.getRepository().getRef("HEAD").getObjectId().getName(); Ref headRef = mock(Ref.class); when(repository.getRef(anyString())).thenReturn(headRef); ObjectId newObjectId = ObjectId.fromRaw(new int[] { 1, 2, 3, 4, 5 }); when(headRef.getObjectId()).thenReturn(newObjectId); SearchPathLocator.Locations locations = this.repository.getLocations("bar", "staging", "master"); assertEquals(locations.getVersion(), newObjectId.getName()); }
From source file:org.webcat.core.git.GitTreeEntry.java
License:Open Source License
public static GitTreeEntry fromTreeWalk(TreeWalk walk, GitRepository repository, String pathPrefix) { ObjectId oid = walk.getObjectId(0); boolean isTree = (walk.getFileMode(0) == FileMode.TREE); String name = walk.getNameString(); String path = (pathPrefix == null) ? walk.getPathString() : pathPrefix + "/" + walk.getPathString(); long size = 0; if (!isTree) { try {/*from w w w.j a v a2s. com*/ size = walk.getObjectReader().getObjectSize(oid, ObjectReader.OBJ_ANY); } catch (IOException e) { log.warn("There was an error getting the size of the object " + oid.getName(), e); } } return new GitTreeEntry(repository, oid, isTree, name, path, size); }
From source file:pl.project13.jgit.DescribeCommandIntegrationTest.java
License:Open Source License
@Test public void shouldReturnJustTheNearestTagWhenAbbrevIsZero() throws Exception { // given//from w w w. j a v a 2s.c om int zeroAbbrev = 0; mavenSandbox.withParentProject(PROJECT_NAME, "jar").withNoChildProject() .withGitRepoInParent(AvailableGitTestRepo.WITH_LIGHTWEIGHT_TAG_BEFORE_ANNOTATED_TAG) .create(FileSystemMavenSandbox.CleanUp.CLEANUP_FIRST); Repository repo = git().getRepository(); Git.wrap(repo).reset().setMode(ResetCommand.ResetType.HARD).call(); // when DescribeResult res = DescribeCommand.on(repo).abbrev(zeroAbbrev).setVerbose(true).call(); // then assertThat(res.toString()).isEqualTo("annotated-tag"); ObjectId objectId = res.commitObjectId(); assert objectId != null; assertThat(objectId.getName()).isNotEmpty(); }