21

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("git@192.168.2.43: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)
caarlos0
  • 20,020
  • 27
  • 85
  • 160

4 Answers4

25

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.

metasim
  • 4,793
  • 3
  • 46
  • 70
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • But I clone it as a non-bare repository... I don't undestand what I'm supposed to do that... – caarlos0 Oct 04 '12 at 20:49
  • Lol, it works. I just do `Git git = cloneCmd.call()`, and use this instance of git to manage the things... thanks @vonc – caarlos0 Oct 04 '12 at 20:52
  • git.add().addFilepattern(".").call(); is not staging the deleted file – swaheed May 30 '18 at 08:29
  • 1
    @swaheed I see, indeed: https://github.com/eclipse/jgit/blob/2ab42b74d9803d8239ff4f9eda1cf153666a4079/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java#L676-L679 – VonC May 30 '18 at 08:31
  • 1
    @swaheed Would `git.add().addFilepattern("sub").setUpdate(true).call();` work better? (https://github.com/eclipse/jgit/blob/2ab42b74d9803d8239ff4f9eda1cf153666a4079/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java#L676-L679) – VonC May 30 '18 at 08:32
12

I had a situation where I had to move a file f1 from the current directory to another directory called 'temp'. After moving the file, calling git.add().addFilePattern(".").call() acted in a weird way since git status gave the following result:

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   temp/f1.html

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    deleted:    f1.html

It recognized that a new file temp/f1 was created but didn't detect that the file was deleted first. This was perhaps because moving the file can be seen as follows

  • Deleting/Cutting the file f1
  • Creating a folder called temp
  • Creating/Pasting the file f1

Then I came across the setUpdate(true) that looks for updates to files that are already being tracked and will not stage new files. (Check java-doc for more info)

So I had to change my code to two lines like so in order for git to recognize both files added and modified (which includes deletion):

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

git status now gives the expected result:

renamed:    f1.hml -> temp/f1.html
hipsandy
  • 982
  • 8
  • 7
1

It might be the wildcard, I just read the javadoc for the add command, looks like you send the name of a directory in order to add its contents not a wild card:

addFilepattern

public AddCommand addFilepattern(String filepattern)

Parameters: filepattern - File to add content from. Also a leading directory name (e.g. dir to add dir/file1 and dir/file2) can be given to add all files in the directory, recursively. Fileglobs (e.g. *.c) are not yet supported.

ishmaelMakitla
  • 3,784
  • 3
  • 26
  • 32
jbusick
  • 11
  • 1
0

Just to make a note about a problem i got where i was using File.separatorChar (Wich will give you either "/" or "\" depending on your OS) to change directory but actually jgit use only "/" and will do the job himself if you use separatorChar it will not work on windows.

LeDevin
  • 1
  • 1