Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jGit - how to add all files to staging area

Tags:

I tried in a lot of ways to clone a repo with jGit (it works). Then, I write some archive in the repository, and tried to add all (a git add *, git add -A or something like it).. but it don't work. The files simple are not added to the staging area.

My code is like this:

    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository repository = builder.setGitDir(new File(folder))
            .readEnvironment().findGitDir().setup().build();
    CloneCommand clone = Git.cloneRepository();
    clone.setBare(false).setCloneAllBranches(true);
    clone.setDirectory(f).setURI("[email protected]:test.git");
    try {
        clone.call();
    } catch (GitAPIException e) {
        e.printStackTrace();
    }
    Files.write("testing it...", new File(folder + "/test2.txt"),
            Charsets.UTF_8);
    Git g = new Git(repository);
    g.add().addFilepattern("*").call();

What am I doing wrong? Thanks.


Exception while trying what with addFilePattern("."):

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.getIndexFile(Repository.java:850)
    at org.eclipse.jgit.dircache.DirCache.lock(DirCache.java:264)
    at org.eclipse.jgit.lib.Repository.lockDirCache(Repository.java:906)
    at org.eclipse.jgit.api.AddCommand.call(AddCommand.java:138)
    at net.ciphersec.git.GitTests.main(GitTests.java:110)
like image 953
caarlos0 Avatar asked Oct 04 '12 19:10

caarlos0


People also ask

How do I add files to my staging area?

Create a new file in a root directory or in a subdirectory, or update an existing file. Add files to the staging area by using the "git add" command and passing necessary options. Commit files to the local repository using the "git commit -m <message>" command. Repeat.

How do you add all files git add?

To add and commit files to a Git repositoryEnter git add --all at the command line prompt in your local project directory to add the files or changes to the repository. Enter git status to see the changes to be committed.

Which is the correct git command to add files to the staging area?

git add. The git add command adds a change in the working directory to the staging area. It tells Git that you want to include updates to a particular file in the next commit. However, git add doesn't really affect the repository in any significant way—changes are not actually recorded until you run git commit .

Which of the following is used to add all files and folders in the staging area?

The git add command is used to add file contents to the Index (Staging Area). This command updates the current content of the working tree to the staging area.


1 Answers

One easy way to debug this is to look at the tests of the AddCommand in the JGit repo: AddCommandTest.java

You will see that in order to add all files the pattern "*" is never used, but "." is.
And it is used in the test function named... testAddWholeRepo()(!)

git.add().addFilepattern(".").call();

The Exception:

Exception in thread "main" org.eclipse.jgit.errors.NoWorkTreeException: 
Bare Repository has neither a working tree, nor an index

is quite explicit: you need to add file in a non-bare repo.

See test method testCloneRepository() to compare with your own clone, and see if there is any difference.

like image 65
VonC Avatar answered Sep 19 '22 08:09

VonC