Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EGit » Getting status of repository(Simple example)
Getting status of repository [message #1313288] Thu, 24 April 2014 20:22 Go to next message
Dol Bik is currently offline Dol BikFriend
Messages: 9
Registered: April 2014
Junior Member
Hello.

I'm desperately trying to find a simple working example of JGit.
I found an example in the "jgit-cookbook" and changed it a little:

import java.io.File;
import java.io.IOException;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.Status;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;

public class Main {

	public static void main(String[] args) throws IOException, GitAPIException {
		FileRepositoryBuilder builder = new FileRepositoryBuilder();
		Repository repository = builder
				.setGitDir(
						new File(
								"....git")) // I'm not allowed to post links here
				.readEnvironment() // scan environment GIT_* variables
				.findGitDir() // scan up the file system tree
				.build();

		Status status = new Git(repository).status().call();
		System.out.println("Added: " + status.getAdded());
		System.out.println("Changed: " + status.getChanged());
		System.out.println("Conflicting: " + status.getConflicting());
		System.out.println("ConflictingStageState: "
				+ status.getConflictingStageState());
		System.out.println("IgnoredNotInIndex: "
				+ status.getIgnoredNotInIndex());
		System.out.println("Missing: " + status.getMissing());
		System.out.println("Modified: " + status.getModified());
		System.out.println("Removed: " + status.getRemoved());
		System.out.println("Untracked: " + status.getUntracked());
		System.out.println("UntrackedFolders: " + status.getUntrackedFolders());

		repository.close();
	}

}


But unfortunately I get the following error:
Exception in thread "main" org.eclipse.jgit.errors.NoWorkTreeException: Bare Repository has neither a working tree, nor an index
	at org.eclipse.jgit.lib.Repository.getWorkTree(Repository.java:1245)
	at org.eclipse.jgit.treewalk.FileTreeIterator.<init>(FileTreeIterator.java:88)
	at org.eclipse.jgit.api.StatusCommand.call(StatusCommand.java:126)
	at Main.main(Main.java:23)


I just want to get some simple information like name, date, size, etc. of all files. So I think, I need an iterator.
Re: Getting status of repository [message #1314521 is a reply to message #1313288] Fri, 25 April 2014 12:22 Go to previous messageGo to next message
R Shapiro is currently offline R ShapiroFriend
Messages: 386
Registered: June 2011
Senior Member
I'm no JGIt expert but the error message seems pretty clear: the file path you're providing is pointing at a bare repository, where the calls you're doing don't make any sense.
A bare repo can never have added, changed, untracked or removed files. It does't contain working all files at all. All it has are commits. You would get the same sort of error with standard Git if you ran 'git status' on a bare repository.

Try pointing at a clone instead.
Re: Getting status of repository [message #1315068 is a reply to message #1314521] Fri, 25 April 2014 19:37 Go to previous messageGo to next message
Dol Bik is currently offline Dol BikFriend
Messages: 9
Registered: April 2014
Junior Member
Thank you!

I've edited my code, but still get the same error:

import java.io.File;
import java.io.IOException;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.Status;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;

public class Main {

	private static final String REMOTE_URL = "....git"; // a URL

	public static void main(String[] args) throws IOException, GitAPIException {

		File localPath = File.createTempFile("TestGitRepository", "");
		localPath.delete();

		System.out.println("Cloning from " + REMOTE_URL + " to " + localPath);
		Git.cloneRepository().setURI(REMOTE_URL).setDirectory(localPath).call();

		FileRepositoryBuilder builder = new FileRepositoryBuilder();
		Repository repository = builder.setGitDir(localPath).readEnvironment()
				.findGitDir().build();

		Status status = new Git(repository).status().call();
		System.out.println("Added: " + status.getAdded());
		System.out.println("Changed: " + status.getChanged());
		System.out.println("Conflicting: " + status.getConflicting());
		System.out.println("ConflictingStageState: "
				+ status.getConflictingStageState());
		System.out.println("IgnoredNotInIndex: "
				+ status.getIgnoredNotInIndex());
		System.out.println("Missing: " + status.getMissing());
		System.out.println("Modified: " + status.getModified());
		System.out.println("Removed: " + status.getRemoved());
		System.out.println("Untracked: " + status.getUntracked());
		System.out.println("UntrackedFolders: " + status.getUntrackedFolders());

		repository.close();
	}

}
Re: Getting status of repository [message #1315461 is a reply to message #1315068] Sat, 26 April 2014 00:50 Go to previous message
Matthias Sohn is currently online Matthias SohnFriend
Messages: 612
Registered: July 2009
Senior Member
See StatusCommandTest for examples how to use the StatusCommand
Previous Topic:resetting a path or paths
Next Topic:.git/description use as git repo name
Goto Forum:
  


Current Time: Mon Dec 22 09:15:51 GMT 2014

Powered by FUDForum. Page generated in 0.01826 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software