import java.util.Map.Entry;\r
import java.util.regex.Pattern;\r
\r
+import org.apache.commons.io.filefilter.TrueFileFilter;\r
import org.eclipse.jgit.api.CloneCommand;\r
import org.eclipse.jgit.api.FetchCommand;\r
import org.eclipse.jgit.api.Git;\r
* @return Repository\r
*/\r
public static Repository createRepository(File repositoriesFolder, String name) {\r
- try {\r
- Git git = Git.init().setDirectory(new File(repositoriesFolder, name)).setBare(true).call();\r
- return git.getRepository();\r
- } catch (GitAPIException e) {\r
- throw new RuntimeException(e);\r
- }\r
+ return createRepository(repositoriesFolder, name, "FALSE");\r
}\r
\r
/**\r
*/\r
public static Repository createRepository(File repositoriesFolder, String name, String shared) {\r
try {\r
- Repository repo = createRepository(repositoriesFolder, name);\r
+ Repository repo = null;\r
+ try {\r
+ Git git = Git.init().setDirectory(new File(repositoriesFolder, name)).setBare(true).call();\r
+ repo = git.getRepository();\r
+ } catch (GitAPIException e) {\r
+ throw new RuntimeException(e);\r
+ }\r
\r
GitConfigSharedRepository sharedRepository = new GitConfigSharedRepository(shared);\r
if (sharedRepository.isShared()) {\r
config.save();\r
\r
if (! JnaUtils.isWindows()) {\r
-\r
- //libc.chmod("/path/to/file", 0755);\r
+ Iterator<File> iter = org.apache.commons.io.FileUtils.iterateFilesAndDirs(repo.getDirectory(),\r
+ TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);\r
+ // Adjust permissions on file/directory\r
+ while (iter.hasNext()) {\r
+ adjustSharedPerm(iter.next(), sharedRepository);\r
+ }\r
}\r
}\r
\r
throw new RuntimeException(e);\r
}\r
}\r
- private enum GitConfigSharedRepositoryValue {\r
+\r
+ private enum GitConfigSharedRepositoryValue\r
+ {\r
UMASK("0", 0), FALSE("0", 0), OFF("0", 0), NO("0", 0),\r
GROUP("1", 0660), TRUE("1", 0660), ON("1", 0660), YES("1", 0660),\r
ALL("2", 0664), WORLD("2", 0664), EVERYBODY("2", 0664),\r
public int getPerm() { return permValue; };\r
\r
}\r
+\r
private static class GitConfigSharedRepository\r
{\r
private int intValue;\r
- GitConfigSharedRepositoryValue enumValue;\r
+ private GitConfigSharedRepositoryValue enumValue;\r
\r
- GitConfigSharedRepository(String s)\r
- {\r
+ GitConfigSharedRepository(String s) {\r
if ( s == null || s.trim().isEmpty() ) {\r
enumValue = GitConfigSharedRepositoryValue.GROUP;\r
}\r
}\r
}\r
\r
- String getValue()\r
- {\r
+ String getValue() {\r
if ( enumValue == GitConfigSharedRepositoryValue.Oxxx ) return Integer.toOctalString(intValue);\r
return enumValue.getConfigValue();\r
}\r
\r
- int getPerm()\r
- {\r
+ int getPerm() {\r
if ( enumValue == GitConfigSharedRepositoryValue.Oxxx ) return intValue;\r
return enumValue.getPerm();\r
}\r
\r
- boolean isShared()\r
- {\r
+ boolean isCustom() {\r
+ return enumValue == GitConfigSharedRepositoryValue.Oxxx;\r
+ }\r
+\r
+ boolean isShared() {\r
return (enumValue.getPerm() > 0) || enumValue == GitConfigSharedRepositoryValue.Oxxx;\r
}\r
}\r
\r
\r
+ public static int adjustSharedPerm(File path, String configShared) {\r
+ return adjustSharedPerm(path, new GitConfigSharedRepository(configShared));\r
+ }\r
+\r
+\r
+ public static int adjustSharedPerm(File path, GitConfigSharedRepository configShared) {\r
+ if (! configShared.isShared()) return 0;\r
+\r
+ int perm = configShared.getPerm();\r
+ int mode = JnaUtils.getFilemode(path);\r
+ if (mode < 0) return -1;\r
+\r
+ // If the owner has no write access, delete it from group and other, too.\r
+ if ((mode & JnaUtils.S_IWUSR) == 0) perm &= ~0222;\r
+ // If the owner has execute access, set it for all blocks that have read access.\r
+ if ((mode & JnaUtils.S_IXUSR) == JnaUtils.S_IXUSR) perm |= (perm & 0444) >> 2;\r
+\r
+ if (configShared.isCustom()) {\r
+ // Use the custom value for access permissions.\r
+ mode |= (mode & ~0777) | perm;\r
+ }\r
+ else {\r
+ // Just add necessary bits to existing permissions.\r
+ mode |= perm;\r
+ }\r
+\r
+ if (path.isDirectory()) {\r
+ mode |= (mode & 0444) >> 2;\r
+ mode |= JnaUtils.S_ISGID;\r
+ }\r
+\r
+ return JnaUtils.setFilemode(path, mode);\r
+ }\r
+\r
+\r
/**\r
* Returns a list of repository names in the specified folder.\r
* \r
import com.gitblit.models.RefModel;\r
import com.gitblit.utils.CompressionUtils;\r
import com.gitblit.utils.JGitUtils;\r
+import com.gitblit.utils.JnaUtils;\r
import com.gitblit.utils.StringUtils;\r
\r
public class JGitUtilsTest {\r
\r
@Test\r
public void testCreateRepositoryShared() throws Exception {\r
- String[] repositories = { "NewTestRepository.git", "NewTestRepository" };\r
+ String[] repositories = { "NewSharedTestRepository.git" };\r
for (String repositoryName : repositories) {\r
Repository repository = JGitUtils.createRepository(GitBlitSuite.REPOSITORIES,\r
repositoryName, "group");\r
assertNotNull(repository);\r
assertFalse(JGitUtils.hasCommits(repository));\r
assertNull(JGitUtils.getFirstCommit(repository, null));\r
- assertEquals(folder.lastModified(), JGitUtils.getFirstChange(repository, null)\r
- .getTime());\r
- assertEquals(folder.lastModified(), JGitUtils.getLastChange(repository).when.getTime());\r
- assertNull(JGitUtils.getCommit(repository, null));\r
+\r
+ assertTrue(folder.exists());\r
+ int mode = JnaUtils.getFilemode(folder);\r
+ assertEquals(JnaUtils.S_ISGID, mode & JnaUtils.S_ISGID);\r
+ assertEquals(JnaUtils.S_IRWXG, mode & JnaUtils.S_IRWXG);\r
+\r
+ mode = JnaUtils.getFilemode(folder.getAbsolutePath() + "/HEAD");\r
+ assertEquals(JnaUtils.S_IRGRP | JnaUtils.S_IWGRP, mode & JnaUtils.S_IRWXG);\r
+\r
+ mode = JnaUtils.getFilemode(folder.getAbsolutePath() + "/config");\r
+ assertEquals(JnaUtils.S_IRGRP | JnaUtils.S_IWGRP, mode & JnaUtils.S_IRWXG);\r
+\r
repository.close();\r
RepositoryCache.close(repository);\r
-// FileUtils.delete(repository.getDirectory(), FileUtils.RECURSIVE);\r
+ FileUtils.delete(repository.getDirectory(), FileUtils.RECURSIVE);\r
}\r
}\r
\r