JGit - 将特定文件推送到RemoteRepo

时间:2014-12-19 07:52:22

标签: git egit jgit

到目前为止我所做的是 -

  1. 从远程回购中克隆。
  2. 在我的项目中的特定目录中添加了一个文件,并在该文件中进行了一些更改
  3. 提交文件
  4. 尝试将更改推送到远程回购  //它显示消息为空且状态为OK但文件未被推送到远程回购

    另一个问题是,当我尝试打印推送消息时,它无限打印 - 消息 - 状态 - 确定 - 参考/头部/主人


  5. private static final String REMOTE_URL = "http://abc.def.net/git/scm/nt/myprojectname.git"
    
    String srcPath = "D://MyFiles//fol//ErrorFile.txt";
    
    // prepare a new folder for the cloned repository
    File localPath = File.createTempFile("TestGitRepository", "");
    localPath.delete();
    
    // then clone
    System.out.println("Cloning from " + REMOTE_URL + " to " + localPath);
    CredentialsProvider cp = new UsernamePasswordCredentialsProvider("myUserName","mypassword");
    
    Git git = Git.cloneRepository().setURI(REMOTE_URL).setDirectory(localPath).setCredentialsProvider(cp).call();
    
    String head = git.getRepository().getFullBranch();
    if (head.startsWith("refs/heads/")) {
            // Print branch name with "refs/heads/" stripped.
            System.out.println("Current branch is " + git.getRepository().getBranch());
    }
    
    String destPath = localPath+"\\myprojectName\\" ;
    File srcFile = new File(srcPath);
    File destFile = new File(destPath);
    FileUtils.copyFileToDirectory(srcFile, destFile, true) ;
    
    git.checkout(); // MISSED THIS LINE EARLIER
    
    // run the add-call
    git.add().setUpdate(true).addFilepattern(destFile.getAbsolutePath()).call();
    
    RevCommit revCommit = git.commit().setMessage("My First Commit").call();
    System.out.println(revCommit.getShortMessage() );
    Iterable<PushResult> resultIterable = git.push().setCredentialsProvider(cp).call();
    
    while(resultIterable.iterator().hasNext()){               // LEADING TO INFINITE LOOP
    PushResult result = resultIterable.iterator().next();
    Collection<RemoteRefUpdate> rs = result.getRemoteUpdates();
      for(RemoteRefUpdate rf : rs){
        System.out.println("Msg- "+rf.getMessage() + " - Status - "+rf.getStatus() + " - "+rf.getSrcRef());
      }
    }
    
    try {
      System.out.println("Having repository: " + git.getRepository().getDirectory());
    } finally {
      git.getRepository().close();
    }
    

1 个答案:

答案 0 :(得分:0)

您的文件未提交,因为您需要指定要提交的文件。在你的情况下

git.commit().setAll(true) 

应该可以工作,相当于git commit -a

我也有这个工作 -

CommitCommand commit = git.commit();
for (String file : files) {
    commit.setOnly(file);
}
commit.setMessage(message).call()