I know much about SVN and accessing repositories from Java code using the SVNKit APIs. I am able to do these operations on SVN:

  • Open a repository
  • User's authentication check before accessing a repository
  • Validating an SVN URL
  • Get the project list under a given URL
  • Export a project/file by a given revision number
  • Get the latest revision of repository/project/file
  • Fetch updates

Now I need to provide the support for Git as well with the same functionality. I have installed Git/EGit/JGit, and I've looked at some samples as well with the JGit APIs but I'm not able to do most of the above tasks using Java code. It seems too complex for me, being an SVN user. Any guidance/help on this?

有帮助吗?

解决方案 2

As commented by Sameer, you will have to get familiar with how Git works, see the Pro Git book.

To your list of questions, there are not always direct equivalents because the concepts are different. Hints to some of the questions:

Open a repository

File workTree = new File("/path/to/git-repository");
Repository repository = new FileRepositoryBuilder().setWorkTree(workTree).build();

Export a project/file by a given revision number

See How to “cat” a file in JGit? .

Get the latest revision of repository/project/file

Git git = new Git(repository);
ObjectId head = repository.resolve(Constants.HEAD);
// All commits, use setMaxCount(1) for newest, use addPath for filtering to path
Iterable<RevCommit> commits = git.log().add(head).call();

Fetch updates

Use the FetchCommand (using git.fetch()) or PullCommand, depending on what you want, see API documentation of Git and navigate to the commands there.

其他提示

My project jgit-cookbook provides samples for many common tasks, maybe that gets you started. Also the jgit userguide explains some of basic concepts and provides samples as well. Third i would look at the javadoc of classes which are in a fairly good state, even if the API is a bit confusion sometimes.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top