Reputation: 1345
Today I signed up for github, and converted an existing filesystem into a git repo using the technique described here:
Most importantly (I think) it involved this line:
git --bare init
I then followed the rest of github.com's setup tutorials (this was part of that) and was done. The existing filesystem was within Dropbox, so I performed the same setup on the two other machines that use the filesystem (now a git repo).
Tonight I tried to get JGit to add a file, commit it and then push it. Here's the gist of the code up until the point it breaks:
FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repository = builder.setGitDir(new File("path/to/my/repo"))
.readEnvironment() // scan environment GIT_* variables
.findGitDir() // scan up the file system tree
.build();
Git git = new Git(repository);
AddCommand add = git.add();
try{
add.addFilepattern("PlayState.as").call();`
This is basically taken verbatim from a JGit tutorial, incidentally. It throws an exception at that last quoted line and states:
org.eclipse.jgit.errors.NoWorkTreeException: Bare Repository has neither a working tree, nor an index
at org.eclipse.jgit.lib.Repository.getIndexFile(Repository.java:838)
at org.eclipse.jgit.lib.Repository.lockDirCache(Repository.java:886)
at org.eclipse.jgit.api.AddCommand.call(AddCommand.java:136)
at flipa.FLIPAGame.writeToFlixel(FLIPAGame.java:77)
at flipa.FLIPAGame.main(FLIPAGame.java:58)
Now, I'm not saying it's unreasonable to claim that, because truth be told I am not the best friend of version control. I get that a bare repo is one with just git in and no other files, but it seems to me that now it has files in it. I've already manually added, committed and pushed to github using git from Terminal. So I can't immediately see why it won't even recognise the repo.
Any takers?
EDIT - For clarification, killing off this repo is no big deal if someone can propose another solution. I want a git repo to use the filesystem in my dropbox, and be able to commit to github via Java.
Upvotes: 2
Views: 8535
Reputation: 1452
In your case since you are working in an existing directory, you may find more convenient using Git.open().
Git git = Git.open(new File(".git"));
System.out.println("Repository: " + git.getRepository().toString());
//Do some git action for instance:
RevCommit rev = git.commit().setAmend(true)
.setAuthor("me", "[email protected]")
.setMessage("Testing commit from jGit").call();
git.close();
Source: Article written by Rüdiger Herrmann available in Code Affine.
Upvotes: 0
Reputation: 361
Your third line of code, right after .build(); should be:
repository.create();
This is the equivalent of the "git init" command.
Upvotes: 0
Reputation: 164
For your code, I would say the options setGitdir() and findGitDir() are not supposed to be used at the same time. To retrieve an existing repository, I use findGitDir(new File("path/to/my/repo") and it is enough.
Upvotes: 8
Reputation: 61715
This sounds like you've added the files to a bare repository. A bare repository should not be touched, except through git push and pull commands (or git commands in general). As a guide, I don't ever look in my bare repositories.
It should be used as a central location. Once you've created the git bare repo, you should clone it and then work on it from the clone, pushing and pulling from the clone.
$ cd /dropbox/repo
$ git init --bare
$ cd /workdir
$ git clone file:///dropbox/repo
$ add files in here
$ git add .
$ git commit -m "initial version"
$ git push origin master
$ more changes here
$ git add etc.
The difference between this and github is the git clone, which then comes from a different place. To be quite honest, unless you've got a really good reason to have a local copy, I'd just forget about the dropbox repo and just use github.
Upvotes: 3