Permalink
Browse files

BZ-1066962: post commit hook support added, but with limitation imposed

by jgit. ie. windows needs cygwin. more info:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=299315
  • Loading branch information...
porcelli authored and manstis committed May 19, 2015
1 parent de1b485 commit e07ced2ae2865c6e628f3a4a5b83aa525350c61d
@@ -68,7 +68,9 @@
import org.eclipse.jgit.transport.resolver.RepositoryResolver;
import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.FileUtils;
import org.eclipse.jgit.util.Hook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.uberfire.commons.async.SimpleAsyncExecutorService;
@@ -181,6 +183,8 @@
private File gitReposParentDir;
private File hookDir;
private int commitLimit;
private boolean daemonEnabled;
private int daemonPort;
@@ -210,12 +214,14 @@
private GitSSHService gitSSHService = null;
private FileSystemAuthenticator authenticator;
private FileSystemAuthorizer fileSystemAuthorizer;
private FS detectedFS = FS.DETECTED;
private void loadConfig( final ConfigProperties config ) {
LOG.debug( "Configuring from properties:" );
final String currentDirectory = System.getProperty( "user.dir" );
final ConfigProperty hookDirProp = config.get( "org.uberfire.nio.git.hooks", null );
final ConfigProperty bareReposDirProp = config.get( "org.uberfire.nio.git.dir", currentDirectory );
final ConfigProperty enabledProp = config.get( "org.uberfire.nio.git.daemon.enabled", DAEMON_DEFAULT_ENABLED );
final ConfigProperty hostProp = config.get( "org.uberfire.nio.git.daemon.host", DEFAULT_HOST_ADDR );
@@ -234,6 +240,13 @@ private void loadConfig( final ConfigProperties config ) {
LOG.debug( config.getConfigurationSummary( "Summary of JGit configuration:" ) );
}
if ( hookDirProp != null && hookDirProp.getValue() != null ) {
hookDir = new File( hookDirProp.getValue() );
if ( !hookDir.exists() ) {
hookDir = null;
}
}
gitReposParentDir = new File( bareReposDirProp.getValue(), REPOSITORIES_CONTAINER_DIR );
commitLimit = commitLimitProp.getIntValue();
@@ -596,7 +609,7 @@ public FileSystem newFileSystem( final URI uri,
git = cloneRepository( repoDest, originURI, bare, credential );
} else {
credential = buildCredential( null );
git = newRepository( repoDest, bare );
git = newRepository( repoDest, bare, hookDir );
}
final JGitFileSystem fs = new JGitFileSystem( this, fullHostNames, git, name, listMode, credential );
@@ -1900,6 +1913,8 @@ private void commit( final JGitPathImpl path,
if ( !batchState ) {
final ObjectId newHead = JGitUtil.getTreeRefObjectId( path.getFileSystem().gitRepo().getRepository(), branchName );
postCommitHook( git.getRepository() );
notifyDiffs( path.getFileSystem(), branchName, commitInfo.getSessionId(), commitInfo.getName(), commitInfo.getMessage(), oldHead, newHead );
} else if ( !oldHeadsOfPendingDiffs.containsKey( path.getFileSystem() ) ||
!oldHeadsOfPendingDiffs.get( path.getFileSystem() ).containsKey( branchName ) ) {
@@ -1922,6 +1937,10 @@ private void commit( final JGitPathImpl path,
fileSystem.unlock();
}
private void postCommitHook( final Repository repository ) {
detectedFS.runIfPresent( repository, Hook.POST_COMMIT, new String[ 0 ] );
}
private void notifyAllDiffs() {
for ( Map.Entry<JGitFileSystem, Map<String, NotificationModel>> jGitFileSystemMapEntry : oldHeadsOfPendingDiffs.entrySet() ) {
for ( Map.Entry<String, NotificationModel> branchNameNotificationModelEntry : jGitFileSystemMapEntry.getValue().entrySet() ) {
@@ -2067,4 +2086,7 @@ public void execute( Runnable command ) {
}
public void setDetectedFS( final FS detectedFS ) {
this.detectedFS = detectedFS;
}
}
@@ -25,7 +25,6 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
@@ -35,6 +34,7 @@
import java.util.Set;
import java.util.TimeZone;
import org.apache.commons.io.FileUtils;
import org.eclipse.jgit.api.CreateBranchCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ListBranchCommand;
@@ -107,6 +107,36 @@ public static Git newRepository( final File repoFolder,
}
}
public static Git newRepository( final File repoFolder,
final boolean bare,
final File hookDir ) throws IOException {
final Git git = newRepository( repoFolder, bare );
if ( hookDir == null ) {
return git;
}
final File repoHookDir;
if ( bare ) {
repoHookDir = new File( repoFolder, "hooks" );
} else {
repoHookDir = new File( repoFolder, ".git/hooks" );
}
try {
FileUtils.copyDirectory( hookDir, repoHookDir );
} catch ( final Exception ex ) {
throw new RuntimeException( ex );
}
for ( final File file : repoHookDir.listFiles() ) {
if ( file != null && file.isFile() ) {
file.setExecutable( true );
}
}
return git;
}
public static List<Ref> branchList( final Git git ) {
checkNotNull( "git", git );
return branchList( git, null );
@@ -0,0 +1,174 @@
/*
* Copyright 2012 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.uberfire.java.nio.fs.jgit;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URI;
import java.util.Map;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicBoolean;
import org.eclipse.jgit.api.errors.JGitInternalException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.util.FS_POSIX_Java6;
import org.eclipse.jgit.util.Hook;
import org.eclipse.jgit.util.ProcessResult;
import org.junit.Test;
import org.uberfire.java.nio.file.FileSystem;
import org.uberfire.java.nio.file.Path;
import static org.fest.assertions.api.Assertions.*;
public class JGitFileSystemProviderHookTest extends AbstractTestInfra {
@Override
public Map<String, String> getGitPreferences() {
Map<String, String> gitPrefs = super.getGitPreferences();
gitPrefs.put( "org.uberfire.nio.git.daemon.enabled", "true" );
int gitDaemonPort = findFreePort();
gitPrefs.put( "org.uberfire.nio.git.daemon.port", String.valueOf( gitDaemonPort ) );
try {
final File myTemp = createTempDirectory();
gitPrefs.put( "org.uberfire.nio.git.hooks", myTemp.getAbsolutePath() );
{
final PrintWriter writer = new PrintWriter( new File( myTemp, "post-commit" ), "UTF-8" );
writer.println( "# something" );
writer.close();
}
{
final PrintWriter writer = new PrintWriter( new File( myTemp, "pre-commit" ), "UTF-8" );
writer.println( "# something" );
writer.close();
}
} catch ( IOException e ) {
e.printStackTrace();
}
return gitPrefs;
}
@Test
public void testInstalledHook() {
final URI newRepo = URI.create( "git://hook-repo-name" );
final FileSystem fs = provider.newFileSystem( newRepo, EMPTY_ENV );
assertThat( fs ).isNotNull();
if ( fs instanceof JGitFileSystem ) {
File[] hooks = new File( ( (JGitFileSystem) fs ).gitRepo().getRepository().getDirectory(), "hooks" ).listFiles();
assertThat( hooks ).isNotEmpty().isNotNull();
assertThat( hooks.length ).isEqualTo( 2 );
boolean foundPreCommitHook = false;
boolean foundPostCommitHook = false;
for ( File hook : hooks ) {
if ( hook.getName().equals( "pre-commit" ) ) {
foundPreCommitHook = hook.canExecute();
} else if ( hook.getName().equals( "post-commit" ) ) {
foundPostCommitHook = hook.canExecute();
}
}
assertThat( foundPreCommitHook ).isTrue();
assertThat( foundPostCommitHook ).isTrue();
}
}
@Test
public void testExecutedPostCommitHook() throws IOException {
final URI newRepo = URI.create( "git://hook-repo-name-executed" );
final AtomicBoolean hookExecuted = new AtomicBoolean( false );
final FileSystem fs = provider.newFileSystem( newRepo, EMPTY_ENV );
provider.setDetectedFS( new FS_POSIX_Java6() {
@Override
public ProcessResult runIfPresent( Repository repox,
Hook hook,
String[] args ) throws JGitInternalException {
if ( hook.equals( Hook.POST_COMMIT ) ) {
hookExecuted.set( true );
}
return null;
}
} );
assertThat( fs ).isNotNull();
final Path path = provider.getPath( URI.create( "git://user_branch@hook-repo-name-executed/some/path/myfile.txt" ) );
final OutputStream outStream = provider.newOutputStream( path );
assertThat( outStream ).isNotNull();
outStream.write( "my cool content".getBytes() );
outStream.close();
final InputStream inStream = provider.newInputStream( path );
final String content = new Scanner( inStream ).useDelimiter( "\\A" ).next();
inStream.close();
assertThat( content ).isNotNull().isEqualTo( "my cool content" );
assertThat( hookExecuted.get() ).isTrue();
}
@Test
public void preCommitHookNotSupported() throws IOException {
final URI newRepo = URI.create( "git://hook-repo-name-executed-pre-commit" );
final AtomicBoolean hookExecuted = new AtomicBoolean( false );
final FileSystem fs = provider.newFileSystem( newRepo, EMPTY_ENV );
provider.setDetectedFS( new FS_POSIX_Java6() {
@Override
public ProcessResult runIfPresent( Repository repox,
Hook hook,
String[] args ) throws JGitInternalException {
if ( hook.equals( Hook.PRE_COMMIT ) ) {
hookExecuted.set( true );
}
return null;
}
} );
assertThat( fs ).isNotNull();
final Path path = provider.getPath( URI.create( "git://user_branch@hook-repo-name-executed-pre-commit/some/path/myfile.txt" ) );
final OutputStream outStream = provider.newOutputStream( path );
assertThat( outStream ).isNotNull();
outStream.write( "my cool content".getBytes() );
outStream.close();
final InputStream inStream = provider.newInputStream( path );
final String content = new Scanner( inStream ).useDelimiter( "\\A" ).next();
inStream.close();
assertThat( content ).isNotNull().isEqualTo( "my cool content" );
assertThat( hookExecuted.get() ).isFalse();
}
}
@@ -85,7 +85,7 @@
<version.org.apache.lucene>4.0.0</version.org.apache.lucene>
<version.org.apache.helix>0.6.2-incubating</version.org.apache.helix>
<version.org.eclipse.jgit>3.3.2.201404171909-r</version.org.eclipse.jgit>
<version.org.eclipse.jgit>3.7.1.201504261725-r</version.org.eclipse.jgit>
<version.com.jcraft>0.1.50</version.com.jcraft>
<version.org.apache.sshd>0.12.0</version.org.apache.sshd>

0 comments on commit e07ced2

Please sign in to comment.