summaryrefslogtreecommitdiffstatsabout
diff options
context:
space:
mode:
authorJan Sievers2012-10-09 10:15:23 (EDT)
committer Jan Sievers2012-10-10 17:00:47 (EDT)
commit526be434a79a430efba50d4f54eb91ddaa6cf766 (patch)
tree4c3b90304c10b841d65e6e8988056a4d97a7b670
parent67205a9e65279f43218abd7f8b088d72b05ced47 (diff)
downloadorg.eclipse.tycho.extras-526be434a79a430efba50d4f54eb91ddaa6cf766.zip
org.eclipse.tycho.extras-526be434a79a430efba50d4f54eb91ddaa6cf766.tar.gz
org.eclipse.tycho.extras-526be434a79a430efba50d4f54eb91ddaa6cf766.tar.bz2
380872 sourceref provider for jgitrefs/changes/54/8154/1
Implement SourceReferenceProvider for SCM type "git". Use jgit to find the git repo root and calculate path of project basedir relative to the repo root. Add it as ";path=..." header attribute. Resolve HEAD commit and add it as ";commitId=<SHA1>" attribute If HEAD is also a tag, add it as ";tag=..." attribute. To enable Eclipse-SourceReferences generation for projects using git, you have to: 1. declare ${tycho.scmUrl} in your (parent) pom using maven SCM format [1] 2. Configure tycho-packaging-plugin to generate source reference headers and add a dependency to tycho-sourceref-jgit: <plugin> <groupId>org.eclipse.tycho</groupId> <artifactId>tycho-packaging-plugin</artifactId> <version>${tycho-version}</version> <configuration> <sourceReferences> <generate>true</generate> </sourceReferences> </configuration> <dependencies> <dependency> <groupId>org.eclipse.tycho.extras</groupId> <artifactId>tycho-sourceref-jgit</artifactId> <version>${tycho-extras-version}</version> </dependency> </dependencies> </plugin> [1] http://maven.apache.org/scm/scm-url-format.html Change-Id: I447d7dc244e6d73c797bf43877b13f9e49fd0ded
-rw-r--r--pom.xml13
-rw-r--r--tycho-buildtimestamp-jgit/pom.xml5
-rw-r--r--tycho-sourceref-jgit/pom.xml66
-rw-r--r--tycho-sourceref-jgit/src/main/java/org/eclipse/tycho/extras/sourceref/jgit/JGitSourceReferencesProvider.java146
-rw-r--r--tycho-sourceref-jgit/src/test/java/org/eclipse/tycho/extras/sourceref/jgit/JGitSourceReferencesProviderTest.java77
5 files changed, 302 insertions, 5 deletions
diff --git a/pom.xml b/pom.xml
index 103ab89..fff38d7 100644
--- a/pom.xml
+++ b/pom.xml
@@ -73,6 +73,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven-version>3.0</maven-version>
+ <jgit-version>2.0.0.201206130900-r</jgit-version>
<tycho-version>0.16.0-SNAPSHOT</tycho-version>
</properties>
@@ -84,6 +85,7 @@
<module>tycho-source-feature-plugin</module>
<module>pack200</module>
<module>tycho-buildtimestamp-jgit</module>
+ <module>tycho-sourceref-jgit</module>
<module>target-platform-validation-plugin</module>
</modules>
@@ -140,6 +142,17 @@
<artifactId>tycho-testing-harness</artifactId>
<version>${tycho-version}</version>
</dependency>
+ <dependency>
+ <groupId>org.eclipse.jgit</groupId>
+ <artifactId>org.eclipse.jgit</artifactId>
+ <version>${jgit-version}</version>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.10</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</dependencyManagement>
diff --git a/tycho-buildtimestamp-jgit/pom.xml b/tycho-buildtimestamp-jgit/pom.xml
index 8503ff2..ba079dc 100644
--- a/tycho-buildtimestamp-jgit/pom.xml
+++ b/tycho-buildtimestamp-jgit/pom.xml
@@ -19,10 +19,6 @@
</parent>
<artifactId>tycho-buildtimestamp-jgit</artifactId>
- <properties>
- <jgit-version>2.0.0.201206130900-r</jgit-version>
- </properties>
-
<dependencies>
<dependency>
<groupId>org.eclipse.tycho</groupId>
@@ -32,7 +28,6 @@
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
- <version>${jgit-version}</version>
</dependency>
</dependencies>
diff --git a/tycho-sourceref-jgit/pom.xml b/tycho-sourceref-jgit/pom.xml
new file mode 100644
index 0000000..b42fed9
--- /dev/null
+++ b/tycho-sourceref-jgit/pom.xml
@@ -0,0 +1,66 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+- Copyright (c) 2012 SAP AG and others.
+- All rights reserved. This program and the accompanying materials
+- are made available under the terms of the Eclipse Public License v1.0
+- which accompanies this distribution, and is available at
+- http://www.eclipse.org/legal/epl-v10.html
+-
+- Contributors:
+- SAP AG - initial API and implementation
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.eclipse.tycho.extras</groupId>
+ <artifactId>tycho-extras</artifactId>
+ <version>0.16.0-SNAPSHOT</version>
+ </parent>
+ <artifactId>tycho-sourceref-jgit</artifactId>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.eclipse.tycho</groupId>
+ <artifactId>tycho-packaging-plugin</artifactId>
+ <version>${tycho-version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.eclipse.jgit</groupId>
+ <artifactId>org.eclipse.jgit</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <source>1.5</source>
+ <target>1.5</target>
+ </configuration>
+ </plugin>
+
+ <plugin>
+ <groupId>org.codehaus.plexus</groupId>
+ <artifactId>plexus-component-metadata</artifactId>
+ <version>1.5.5</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>generate-metadata</goal>
+ <goal>generate-test-metadata</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+
+</project> \ No newline at end of file
diff --git a/tycho-sourceref-jgit/src/main/java/org/eclipse/tycho/extras/sourceref/jgit/JGitSourceReferencesProvider.java b/tycho-sourceref-jgit/src/main/java/org/eclipse/tycho/extras/sourceref/jgit/JGitSourceReferencesProvider.java
new file mode 100644
index 0000000..182a729
--- /dev/null
+++ b/tycho-sourceref-jgit/src/main/java/org/eclipse/tycho/extras/sourceref/jgit/JGitSourceReferencesProvider.java
@@ -0,0 +1,146 @@
+/*******************************************************************************
+ * Copyright (c) 2012 SAP AG and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * SAP AG - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.tycho.extras.sourceref.jgit;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URI;
+
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.component.annotations.Component;
+import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.api.errors.GitAPIException;
+import org.eclipse.jgit.errors.AmbiguousObjectException;
+import org.eclipse.jgit.lib.Constants;
+import org.eclipse.jgit.lib.ObjectId;
+import org.eclipse.jgit.lib.Ref;
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.lib.RepositoryBuilder;
+import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
+import org.eclipse.tycho.packaging.sourceref.ScmUrl;
+import org.eclipse.tycho.packaging.sourceref.SourceReferencesProvider;
+
+@Component( role = SourceReferencesProvider.class, hint = "git" )
+public class JGitSourceReferencesProvider
+ implements SourceReferencesProvider
+{
+
+ public String getSourceReferencesHeader( MavenProject project, ScmUrl scmUrl )
+ throws MojoExecutionException
+ {
+ File basedir = project.getBasedir().getAbsoluteFile();
+ FileRepositoryBuilder builder =
+ new FileRepositoryBuilder().readEnvironment().findGitDir( basedir ).setMustExist( true );
+ Repository repo;
+ Git git;
+ try
+ {
+ repo = builder.build();
+ git = Git.wrap( repo );
+ }
+ catch ( IOException e )
+ {
+ throw new MojoExecutionException( "IO exception trying to create git repo ", e );
+ }
+ ObjectId head = resolveHead( repo );
+
+ StringBuilder result = new StringBuilder( scmUrl.getUrl() );
+ result.append( ";path=\"" );
+ result.append( getRelativePath( basedir, repo.getDirectory().getParentFile() ) );
+ result.append( "\"" );
+
+ String tag = findTagForHead( git, head );
+ if ( tag != null )
+ {
+ // may contain e.g. spaces, so we quote it
+ result.append( ";tag=\"" );
+ result.append( tag );
+ result.append( "\"" );
+ }
+ result.append( ";commitId=" );
+ result.append( head.getName() );
+ return result.toString();
+ }
+
+ private ObjectId resolveHead( Repository repo )
+ throws MojoExecutionException
+ {
+ ObjectId head;
+ try
+ {
+ head = repo.resolve( Constants.HEAD );
+ }
+ catch ( AmbiguousObjectException e )
+ {
+ throw new MojoExecutionException( "exception trying resolve HEAD", e );
+ }
+ catch ( IOException e )
+ {
+ throw new MojoExecutionException( "exception trying resolve HEAD", e );
+ }
+ return head;
+ }
+
+ private String findTagForHead( Git git, ObjectId head )
+ throws MojoExecutionException
+ {
+ String tag = null;
+ try
+ {
+ for ( Ref ref : git.tagList().call() )
+ {
+ ObjectId objectId = ref.getPeeledObjectId();
+ if ( objectId == null )
+ {
+ objectId = ref.getObjectId();
+ }
+ if ( head.equals( objectId ) )
+ {
+ tag = ref.getName();
+ if ( tag.startsWith( Constants.R_TAGS ) )
+ {
+ tag = tag.substring( Constants.R_TAGS.length() );
+ }
+ break;
+ }
+ }
+ }
+ catch ( GitAPIException e )
+ {
+ throw new MojoExecutionException( "exception trying to get tag list", e );
+ }
+ return tag;
+ }
+
+ String getRelativePath( File subDir, File parentDir )
+ throws MojoExecutionException
+ {
+ URI subDirUri = subDir.toURI();
+ URI relativeUri = parentDir.toURI().relativize( subDirUri );
+ if ( relativeUri.equals( subDirUri ) )
+ {
+ throw new MojoExecutionException( "Could not find a common basedir of " + subDir + " and " + parentDir );
+ }
+ String relative = relativeUri.getPath();
+ // remove surrounding slashes
+ if ( relative.startsWith( "/" ) )
+ {
+ relative = relative.substring( 1 );
+ }
+ if ( relative.endsWith( "/" ) )
+ {
+ relative = relative.substring( 0, relative.length() - 1 );
+ }
+ return relative;
+ }
+
+}
diff --git a/tycho-sourceref-jgit/src/test/java/org/eclipse/tycho/extras/sourceref/jgit/JGitSourceReferencesProviderTest.java b/tycho-sourceref-jgit/src/test/java/org/eclipse/tycho/extras/sourceref/jgit/JGitSourceReferencesProviderTest.java
new file mode 100644
index 0000000..be05656
--- /dev/null
+++ b/tycho-sourceref-jgit/src/test/java/org/eclipse/tycho/extras/sourceref/jgit/JGitSourceReferencesProviderTest.java
@@ -0,0 +1,77 @@
+/*******************************************************************************
+ * Copyright (c) 2012 SAP AG and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * SAP AG - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.tycho.extras.sourceref.jgit;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+import java.util.Properties;
+
+import org.apache.maven.model.Scm;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.project.MavenProject;
+import org.eclipse.tycho.packaging.sourceref.ScmUrl;
+import org.hamcrest.CoreMatchers;
+import org.junit.Test;
+import org.junit.matchers.JUnitMatchers;
+
+public class JGitSourceReferencesProviderTest
+{
+
+ @Test
+ public void testGetRelativePathWithDots() throws MojoExecutionException
+ {
+ JGitSourceReferencesProvider provider = new JGitSourceReferencesProvider();
+ File projectBasedir = new File("/foo/../test/bar/baz" );
+ File repoRoot = new File("/test/foo/../bar");
+ assertEquals( "baz", provider.getRelativePath( projectBasedir, repoRoot));
+ }
+
+ @Test
+ public void testGetRelativePathWithSpaces() throws MojoExecutionException
+ {
+ JGitSourceReferencesProvider provider = new JGitSourceReferencesProvider();
+ File projectBasedir = new File("/foo/test me/bar/baz" );
+ File repoRoot = new File("/foo/test me/");
+ assertEquals( "bar/baz", provider.getRelativePath( projectBasedir, repoRoot));
+ }
+
+ @Test(expected = MojoExecutionException.class)
+ public void testGetRelativePathNoCommonBasedir() throws MojoExecutionException
+ {
+ JGitSourceReferencesProvider provider = new JGitSourceReferencesProvider();
+ File projectBasedir = new File("/foo/test/bar" );
+ File repoRoot = new File("/baz");
+ provider.getRelativePath( projectBasedir, repoRoot);
+ }
+
+ @Test
+ public void testGetSourceReferencesHeader() throws MojoExecutionException
+ {
+ JGitSourceReferencesProvider provider = new JGitSourceReferencesProvider();
+ MavenProject mockProject = new MavenProject();
+ ScmUrl scmUrl = new ScmUrl( properties("scm:git:foo") );
+ mockProject.setFile( new File("src/test/resources/pom.xml").getAbsoluteFile() );
+ String sourceRef = provider.getSourceReferencesHeader( mockProject , scmUrl);
+ assertTrue( sourceRef.startsWith( "scm:git:foo;path=\"tycho-sourceref-jgit/src/test/resources\""));
+ }
+
+ private Properties properties( String scmUrl )
+ {
+ Properties p = new Properties();
+ p.setProperty( "tycho.scmUrl", scmUrl );
+ return p;
+ }
+
+}