【问题标题】:How to do gitpush using jgit API?如何使用 jgit API 进行 gitpush?
【发布时间】:2018-03-15 06:04:32
【问题描述】:

我使用下面的代码来做推送。

 public static void main(String[] args) throws IOException,GitAPIException
{ 
  Repository localRepo = new 
  FileRepository("C:\\Users\\Joshi\\Desktop\\demo");
  Git git = new Git(localRepo);

// add remote repo:
  RemoteAddCommand remoteAddCommand = git.remoteAdd();
  remoteAddCommand.setName("origin");
  try {
    remoteAddCommand.setUri(new 
    URIish("https://bitbucket.org/nidhi011/bxc"));
    System.out.println("file Added");
    } catch (URISyntaxException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
// you can add more settings here if needed
remoteAddCommand.call();
git.commit().setMessage( "commited" ).call();

// push to remote:
PushCommand pushCommand = git.push();
pushCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider("username", "password"));
// you can add more settings here if needed
pushCommand.call();
}

而我的错误

file Added

Exception in thread "main" 
org.eclipse.jgit.api.errors.WrongRepositoryStateException: Cannot commit on 
a repo with state: BARE
at org.eclipse.jgit.api.CommitCommand.call(CommitCommand.java:171)
at maven_git.push.main(push.java:38)

运行上面的代码后我得到这个异常错误请帮我解决jgit push命令。是的,当我执行此代码时,还有一件事会在我的本地目录“demo”文件夹中生成配置文件,我无法理解。

【问题讨论】:

  • 原因:org.eclipse.jgit.errors.TransportException: 没什么可推送的。 也许你需要一个 git.add 和一个 git.commit 首先?
  • @jonhid 先生是的,我已经完成了提交,然后我还收到“线程“主”org.eclipse.jgit.api.errors.WrongRepositoryStateException 中的异常错误:无法提交状态为:BARE 的仓库在 org.eclipse.jgit.api.CommitCommand.call(CommitCommand.java:171) 在 maven_git.push.main(push.java:38)"
  • 在启动 Java 程序之前,您的目录 demo 是 Git 存储库吗?
  • @MincongHuang 是的,有 .git 文件夹

标签: java git maven api jgit


【解决方案1】:

您的代码中有几行不正确。一起来看看吧。

打开 Git 存储库

使用FileRepository 打开 Git 存储库很棘手。这是一个内部 API,其中给定的字符串是存储库元数据的位置(.git 文件夹)。也就是说,它是用来构建一个 Git 裸仓库的。你可以通过调用Repository#isBare()来证明这一点:

Repository localRepo = new FileRepository("C:\\Users\\Joshi\\Desktop\\demo");
System.out.println(localRepo.isBare()); // true

使用此 API 后,创建的仓库为 BARE 仓库。您不能提交到裸存储库,因为它没有工作区。这就是为什么你有例外说:

org.eclipse.jgit.api.errors.WrongRepositoryStateException:无法提交 一个状态为 BARE 的仓库

更好的方法是使用Git#open()。请注意,您应该在使用 Git 存储库后关闭它。所以我在这里使用 try-with-resources 语句:

try (Git git = Git.open(new File("C:\\Users\\Joshi\\Desktop\\demo"))) {
    // Add your logic here ...
}

将文件添加到 Git

在提交更改之前,您需要将它们添加到索引中,以便为下一次提交准备暂存的内容。例如:

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

请注意,这与RemoteAddCommand 完全不同:我们正在添加文件内容,而RemoteAddCommand 添加了一个新的远程 URL。在本机 Git 命令中,它们分别是:

git add .
git remote add origin https://bitbucket.org/nidhi011/bxc

提交

你对这部分是正确的。

如果本地分支没有从 tracking branch 签出,那么您需要在推送命令中精确地指定远程分支名称。

git.push().setRemote("origin").add("master").call();

如果凭据不正确,用户将无权推送更改。在这种情况下,TransportException 将被抛出。您可以为异常处理添加额外的逻辑:

try {
    git.push().setRemote("origin").add("master").call();
} catch (TransportException e) {
    // Add your own logic here, for example:
    System.out.println("Username or password incorrect.");
}

【讨论】:

  • 在 tempDir 先生中写什么?
  • 哎呀,对不起@NidhiJoshi。我现在更新了我的答案。它是您的存储库的文件路径。
  • 好的,非常感谢我已经完成了我的编码,但我需要做的一件事是添加验证要遵循的步骤
  • 你能准确地说你想验证什么吗,@NikitaJ?
  • 对于 url 它应该是经过身份验证的,否则应该传递错误消息,对于像用户名和密码这样的凭据,如果它不匹配它应该传递像“密码不正确”这样的消息,你能帮我吗使用编码解释
猜你喜欢
  • 2012-11-06
  • 2012-07-13
  • 1970-01-01
  • 2016-12-04
  • 2018-10-30
  • 1970-01-01
  • 2014-11-24
  • 1970-01-01
  • 2012-08-07
相关资源
最近更新 更多