Java Code Examples for org.eclipse.jgit.util.FileUtils

The following examples show how to use org.eclipse.jgit.util.FileUtils. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: hop   Source File: UIGit.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void add( String filepattern ) {
  try {
    if ( filepattern.endsWith( ".ours" ) || filepattern.endsWith( ".theirs" ) ) {
      FileUtils.rename( new File( directory, filepattern ),
          new File( directory, FilenameUtils.removeExtension( filepattern ) ),
          StandardCopyOption.REPLACE_EXISTING );
      filepattern = FilenameUtils.removeExtension( filepattern );
      org.apache.commons.io.FileUtils.deleteQuietly( new File( directory, filepattern + ".ours" ) );
      org.apache.commons.io.FileUtils.deleteQuietly( new File( directory, filepattern + ".theirs" ) );
    }
    git.add().addFilepattern( filepattern ).call();
  } catch ( Exception e ) {
    showMessageBox( BaseMessages.getString( PKG, "Dialog.Error" ), e.getMessage() );
  }
}
 
Example 2
Source Project: hop   Source File: UIGit.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void revertPath( String path ) {
  try {
    // Delete added files
    Status status = git.status().addPath( path ).call();
    if ( status.getUntracked().size() != 0 || status.getAdded().size() != 0 ) {
      resetPath( path );
      org.apache.commons.io.FileUtils.deleteQuietly( new File( directory, path ) );
    }

    /*
     * This is a work-around to discard changes of conflicting files
     * Git CLI `git checkout -- conflicted.txt` discards the changes, but jgit does not
     */
    git.add().addFilepattern( path ).call();

    git.checkout().setStartPoint( Constants.HEAD ).addPath( path ).call();
    org.apache.commons.io.FileUtils.deleteQuietly( new File( directory, path + ".ours" ) );
    org.apache.commons.io.FileUtils.deleteQuietly( new File( directory, path + ".theirs" ) );
  } catch ( Exception e ) {
    showMessageBox( BaseMessages.getString( PKG, "Dialog.Error" ), e.getMessage() );
  }
}
 
Example 3
public static void removeSubmodule(Repository db, Repository parentRepo, String pathToSubmodule) throws Exception {
	pathToSubmodule = pathToSubmodule.replace("\\", "/");
	StoredConfig gitSubmodulesConfig = getGitSubmodulesConfig(parentRepo);
	gitSubmodulesConfig.unsetSection(CONFIG_SUBMODULE_SECTION, pathToSubmodule);
	gitSubmodulesConfig.save();
	StoredConfig repositoryConfig = parentRepo.getConfig();
	repositoryConfig.unsetSection(CONFIG_SUBMODULE_SECTION, pathToSubmodule);
	repositoryConfig.save();
	Git git = Git.wrap(parentRepo);
	git.add().addFilepattern(DOT_GIT_MODULES).call();
	RmCommand rm = git.rm().addFilepattern(pathToSubmodule);
	if (gitSubmodulesConfig.getSections().size() == 0) {
		rm.addFilepattern(DOT_GIT_MODULES);
	}
	rm.call();
	FileUtils.delete(db.getWorkTree(), FileUtils.RECURSIVE);
	FileUtils.delete(db.getDirectory(), FileUtils.RECURSIVE);
}
 
Example 4
Source Project: orion.server   Source File: GitCloneTest.java    License: Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testCloneNotGitRepository() throws Exception {
	createWorkspace(SimpleMetaStore.DEFAULT_WORKSPACE_NAME);
	String workspaceId = getWorkspaceId(workspaceLocation);
	JSONObject project = createProjectOrLink(workspaceLocation, getMethodName().concat("Project"), null);
	IPath clonePath = new Path("file").append(workspaceId).append(project.getString(ProtocolConstants.KEY_NAME)).makeAbsolute();

	// clone
	File notAGitRepository = createTempDir().toFile();
	assertTrue(notAGitRepository.isDirectory());
	assertTrue(notAGitRepository.list().length == 0);
	WebRequest request = getPostGitCloneRequest(notAGitRepository.toURI().toString(), clonePath);
	WebResponse response = webConversation.getResponse(request);
	ServerStatus status = waitForTask(response);
	assertFalse(status.toString(), status.isOK());

	assertEquals(HttpURLConnection.HTTP_INTERNAL_ERROR, status.getHttpCode());
	assertEquals("Error cloning git repository", status.getMessage());
	assertNotNull(status.getJsonData());
	assertEquals(status.toString(), "Invalid remote: origin", status.getException().getMessage());

	// cleanup the tempDir
	FileUtils.delete(notAGitRepository, FileUtils.RECURSIVE);
}
 
Example 5
Source Project: ParallelGit   Source File: FilesWriteTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void writeLargeFile_shouldWork() throws IOException {
  repoDir = FileUtils.createTempDir(getClass().getSimpleName(), null, null);
  repo = RepositoryUtils.createRepository(repoDir, true);
  DirCacheBuilder builder = CacheUtils.keepEverything(cache);
  byte[] largeData = new byte[50*1024*1024+1];
  Random random = new Random();
  random.nextBytes(largeData);
  AnyObjectId blobId = BlobUtils.insertBlob(largeData, repo);
  addFile("large.txt", REGULAR_FILE, blobId, builder);
  builder.finish();
  commitToMaster();
  initGitFileSystemForBranch(MASTER);
  byte[] data = someBytes();
  Path file = gfs.getPath("/large.txt");
  Files.write(file, data, APPEND);
}
 
Example 6
Source Project: ParallelGit   Source File: FilesWriteTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void writeLargeFile_shouldWork() throws IOException {
  repoDir = FileUtils.createTempDir(getClass().getSimpleName(), null, null);
  repo = RepositoryUtils.createRepository(repoDir, true);
  DirCacheBuilder builder = CacheUtils.keepEverything(cache);
  byte[] largeData = new byte[50*1024*1024+1];
  Random random = new Random();
  random.nextBytes(largeData);
  AnyObjectId blobId = BlobUtils.insertBlob(largeData, repo);
  addFile("large.txt", REGULAR_FILE, blobId, builder);
  builder.finish();
  commitToMaster();
  initGitFileSystemForBranch(MASTER);
  byte[] data = someBytes();
  Path file = gfs.getPath("/large.txt");
  Files.write(file, data, APPEND);
}
 
Example 7
Source Project: hop   Source File: UIGit.java    License: Apache License 2.0 5 votes vote down vote up
private void checkout( String path, String commitId, String postfix ) {
  InputStream stream = open( path, commitId );
  File file = new File( directory + Const.FILE_SEPARATOR + path + postfix );
  try {
    org.apache.commons.io.FileUtils.copyInputStreamToFile( stream, file );
    stream.close();
  } catch ( IOException e ) {
    e.printStackTrace();
  }
}
 
Example 8
Source Project: spring-cloud-release-tools   Source File: TestUtils.java    License: Apache License 2.0 5 votes vote down vote up
private static void prepareLocalRepo(String buildDir, String repoPath)
		throws IOException {
	File dotGit = new File(buildDir + repoPath + "/.git");
	File git = new File(buildDir + repoPath + "/git");
	if (git.exists()) {
		if (dotGit.exists()) {
			FileUtils.delete(dotGit, FileUtils.RECURSIVE);
		}
	}
	git.renameTo(dotGit);
}
 
Example 9
Source Project: spring-cloud-release-tools   Source File: TestUtils.java    License: Apache License 2.0 5 votes vote down vote up
private static void prepareLocalRepo(String buildDir, String repoPath)
		throws IOException {
	File dotGit = new File(buildDir + repoPath + "/.git");
	File git = new File(buildDir + repoPath + "/git");
	if (git.exists()) {
		if (dotGit.exists()) {
			FileUtils.delete(dotGit, FileUtils.RECURSIVE);
		}
	}
	git.renameTo(dotGit);
}
 
Example 10
Source Project: spring-cloud-release-tools   Source File: TestUtils.java    License: Apache License 2.0 5 votes vote down vote up
private static void prepareLocalRepo(String buildDir, String repoPath)
		throws IOException {
	File dotGit = new File(buildDir + repoPath + "/.git");
	File git = new File(buildDir + repoPath + "/git");
	if (git.exists()) {
		if (dotGit.exists()) {
			FileUtils.delete(dotGit, FileUtils.RECURSIVE);
		}
	}
	git.renameTo(dotGit);
}
 
Example 11
Source Project: spring-cloud-release-tools   Source File: GitRepo.java    License: Apache License 2.0 5 votes vote down vote up
private void deleteBaseDirIfExists() {
	if (this.basedir.exists()) {
		try {
			FileUtils.delete(this.basedir, FileUtils.RECURSIVE);
		}
		catch (IOException e) {
			throw new IllegalStateException("Failed to initialize base directory", e);
		}
	}
}
 
Example 12
Source Project: spring-cloud-release-tools   Source File: TestUtils.java    License: Apache License 2.0 5 votes vote down vote up
private static void prepareLocalRepo(String buildDir, String repoPath)
		throws IOException {
	File dotGit = new File(buildDir + repoPath + "/.git");
	File git = new File(buildDir + repoPath + "/git");
	if (git.exists()) {
		if (dotGit.exists()) {
			FileUtils.delete(dotGit, FileUtils.RECURSIVE);
		}
	}
	git.renameTo(dotGit);
}
 
Example 13
Source Project: orion.server   Source File: GitUriTest.java    License: Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testGitUrisForFile() throws Exception {
	createWorkspace(SimpleMetaStore.DEFAULT_WORKSPACE_NAME);

	File dir = createTempDir().toFile();
	dir.mkdir();
	File file = new File(dir, "test.txt");
	file.createNewFile();

	ServletTestingSupport.allowedPrefixes = dir.toString();

	String projectName = getMethodName().concat("Project");
	JSONObject project = createProjectOrLink(workspaceLocation, projectName, dir.toString());
	String location = project.getString(ProtocolConstants.KEY_CONTENT_LOCATION);

	WebRequest request = getGetRequest(location);
	WebResponse response = webConversation.getResponse(request);
	assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
	JSONObject files = new JSONObject(response.getText());

	assertNull(files.optString(GitConstants.KEY_STATUS, null));
	assertNull(files.optString(GitConstants.KEY_DIFF, null));
	assertNull(files.optString(GitConstants.KEY_DIFF, null));
	assertNull(files.optString(GitConstants.KEY_COMMIT, null));
	assertNull(files.optString(GitConstants.KEY_REMOTE, null));
	assertNull(files.optString(GitConstants.KEY_TAG, null));
	assertNull(files.optString(GitConstants.KEY_CLONE, null));

	FileUtils.delete(dir, FileUtils.RECURSIVE);
}
 
Example 14
Source Project: orion.server   Source File: GitTest.java    License: Eclipse Public License 1.0 5 votes vote down vote up
@After
public void tearDown() throws Exception {
	//close all repositories
	for (Repository repo : toClose) {
		repo.close();
	}
	toClose.clear();
	FileUtils.delete(gitDir, FileUtils.RECURSIVE);
}
 
Example 15
Source Project: spring-cloud-contract   Source File: GitRepo.java    License: Apache License 2.0 5 votes vote down vote up
private void deleteBaseDirIfExists() {
	if (this.basedir.exists()) {
		try {
			FileUtils.delete(this.basedir, FileUtils.RECURSIVE);
		}
		catch (IOException e) {
			throw new IllegalStateException("Failed to initialize base directory", e);
		}
	}
}
 
Example 16
Source Project: spring-cloud-contract   Source File: TestUtils.java    License: Apache License 2.0 5 votes vote down vote up
private static void prepareLocalRepo(String buildDir, String repoPath)
		throws IOException {
	File dotGit = new File(buildDir + repoPath + "/.git");
	File git = new File(buildDir + repoPath + "/git");
	if (git.exists()) {
		if (dotGit.exists()) {
			FileUtils.delete(dotGit, FileUtils.RECURSIVE);
		}
	}
	git.renameTo(dotGit);
}
 
Example 17
Source Project: ParallelGit   Source File: AbstractParallelGitTest.java    License: Apache License 2.0 5 votes vote down vote up
@After
public void closeRepository() throws IOException {
  if(repo != null)
    repo.close();
  if(repoDir != null && repoDir.exists())
    FileUtils.delete(repoDir, FileUtils.RECURSIVE);
}
 
Example 18
Source Project: ParallelGit   Source File: AbstractParallelGitTest.java    License: Apache License 2.0 5 votes vote down vote up
@After
public void closeRepository() throws IOException {
  if(repo != null)
    repo.close();
  if(repoDir != null && repoDir.exists())
    FileUtils.delete(repoDir, FileUtils.RECURSIVE);
}
 
Example 19
protected void closeRepository() throws Exception{
    if (repository != null) {
        repository.close();
    }
    if (Files.exists(getDotGitDir())) {
        FileUtils.delete(getDotGitDir().toFile(), FileUtils.RECURSIVE | FileUtils.RETRY);
    }
    if(Files.exists(getDotGitIgnore())) {
        FileUtils.delete(getDotGitIgnore().toFile(), FileUtils.RECURSIVE | FileUtils.RETRY);
    }
}
 
Example 20
@After
public void deleteDirectoriesAndFiles() throws Exception {
    if (repository != null) {
        repository.close();
    }
    FileUtils.delete(root.toFile(), FileUtils.RECURSIVE | FileUtils.RETRY);
}
 
Example 21
private void deleteBaseDirIfExists() {
	if (getBasedir().exists()) {
		for (File file : getBasedir().listFiles()) {
			try {
				FileUtils.delete(file, FileUtils.RECURSIVE);
			}
			catch (IOException e) {
				throw new IllegalStateException("Failed to initialize base directory",
						e);
			}
		}
	}
}
 
Example 22
@Before
public void init() throws Exception {
	if (this.basedir.exists()) {
		FileUtils.delete(this.basedir, FileUtils.RECURSIVE);
	}
	ConfigServerTestUtils.deleteLocalRepo("config-copy");
}
 
Example 23
@Before
public void init() throws Exception {
	if (this.basedir.exists()) {
		FileUtils.delete(this.basedir, FileUtils.RECURSIVE);
	}
	ConfigServerTestUtils.deleteLocalRepo("config-copy");
}
 
Example 24
@Before
public void init() throws Exception {
	String uri = ConfigServerTestUtils.prepareLocalSvnRepo(
			"src/test/resources/" + REPOSITORY_NAME,
			"target/repos/" + REPOSITORY_NAME);
	this.repository.setUri(uri);
	if (this.basedir.exists()) {
		FileUtils.delete(this.basedir, FileUtils.RECURSIVE | FileUtils.RETRY);
	}
}
 
Example 25
public static void main(String[] args) throws Exception {
	File basedir = new File("target/config");
	String uri = ConfigServerTestUtils.prepareLocalSvnRepo(
			"src/test/resources/" + REPOSITORY_NAME,
			"target/repos/" + REPOSITORY_NAME);
	if (basedir.exists()) {
		FileUtils.delete(basedir, FileUtils.RECURSIVE | FileUtils.RETRY);
	}
	new SpringApplicationBuilder(TestApplication.class).profiles("subversion")
			.properties("server.port=8888",
					"spring.cloud.config.server.svn.uri:" + uri)
			.run(args);
}
 
Example 26
@Before
public void init() throws Exception {
	if (this.basedir.exists()) {
		FileUtils.delete(this.basedir, FileUtils.RECURSIVE);
	}
	ConfigServerTestUtils.deleteLocalRepo("");
}
 
Example 27
@Before
public void init() throws Exception {
	String uri = ConfigServerTestUtils.prepareLocalRepo();
	this.repository = new JGitEnvironmentRepository(this.environment,
			new JGitEnvironmentProperties());
	this.repository.setUri(uri);
	if (this.basedir.exists()) {
		FileUtils.delete(this.basedir, FileUtils.RECURSIVE | FileUtils.RETRY);
	}
}
 
Example 28
Source Project: spring-cloud-config   Source File: ConfigServerTestUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static String prepareLocalRepo(String baseDir, String buildDir,
		String repoPath, String checkoutDir) throws IOException {
	buildDir = baseDir + buildDir;
	new File(buildDir).mkdirs();
	if (!repoPath.startsWith("/")) {
		repoPath = "/" + repoPath;
	}
	if (!repoPath.endsWith("/")) {
		repoPath = repoPath + "/";
	}
	File source = new File(baseDir + "src/test/resources" + repoPath);
	File dest = new File(buildDir + repoPath);
	if (dest.exists()) {
		FileUtils.delete(dest, FileUtils.RECURSIVE | FileUtils.RETRY);
	}
	FileSystemUtils.copyRecursively(source, dest);
	File dotGit = new File(buildDir + repoPath + ".git");
	File git = new File(buildDir + repoPath + "git");
	if (git.exists()) {
		if (dotGit.exists()) {
			FileUtils.delete(dotGit, FileUtils.RECURSIVE);
		}
	}
	git.renameTo(dotGit);
	File local = new File(checkoutDir);
	if (local.exists()) {
		FileUtils.delete(local, FileUtils.RECURSIVE);
	}
	if (!buildDir.startsWith("/")) {
		buildDir = "./" + buildDir;
	}
	return "file:" + buildDir + repoPath;
}
 
Example 29
Source Project: spring-cloud-config   Source File: ConfigServerTestUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static String prepareLocalSvnRepo(String sourceDir, String checkoutDir)
		throws Exception {
	File sourceDirFile = new File(sourceDir);
	sourceDirFile.mkdirs();
	File local = new File(checkoutDir);
	if (local.exists()) {
		FileUtils.delete(local, FileUtils.RECURSIVE);
	}
	local.mkdirs();
	FileSystemUtils.copyRecursively(sourceDirFile, local);
	return StringUtils.cleanPath("file:///" + local.getAbsolutePath());
}
 
Example 30
Source Project: ParallelGit   Source File: AbstractParallelGitTest.java    License: Apache License 2.0 4 votes vote down vote up
protected void initRepositoryDir() throws IOException {
  if(repoDir == null)
    repoDir = FileUtils.createTempDir(getClass().getSimpleName(), null, null);
}