2012-11-19 24 views
14

Tôi đang cố gắng xây dựng một ứng dụng Java cho phép người dùng sử dụng kho lưu trữ dựa trên Git. Tôi đã có thể làm điều này từ dòng lệnh, sử dụng lệnh sau:Làm cách nào để thực hiện việc đẩy git với JGit?

git init 
<create some files> 
git add . 
git commit 
git remote add <remote repository name> <remote repository URI> 
git push -u <remote repository name> master 

Điều này cho phép tôi để tạo ra, thêm và cam kết nội dung vào kho lưu trữ địa phương của tôi và đẩy nội dung vào kho từ xa. Tôi hiện đang cố gắng làm điều tương tự trong mã Java của mình, bằng cách sử dụng JGit. Tôi đã có thể dễ dàng làm git init, thêm và cam kết sử dụng JGit API.

Repository localRepo = new FileRepository(localPath); 
this.git = new Git(localRepo);   
localRepo.create(); 
git.add().addFilePattern(".").call(); 
git.commit().setMessage("test message").call(); 

Một lần nữa, tất cả đều hoạt động tốt. Tôi không thể tìm thấy bất kỳ ví dụ hoặc mã tương đương nào cho git remote addgit push. Tôi đã xem cái này SO question.

testPush() không thành công với thông báo lỗi TransportException: origin not found. Trong các ví dụ khác tôi đã thấy https://gist.github.com/2487157 do git clonetrướcgit push và tôi không hiểu tại sao lại cần thiết.

Bất kỳ con trỏ nào về cách tôi có thể thực hiện việc này sẽ được đánh giá cao.

Trả lời

13

Bạn sẽ tìm thấy trong org.eclipse.jgit.test tất cả các ví dụ bạn cần:

  • RemoteconfigTest.java sử dụng Config:

    config.setString("remote", "origin", "pushurl", "short:project.git"); 
    config.setString("url", "https://server/repos/", "name", "short:"); 
    RemoteConfig rc = new RemoteConfig(config, "origin"); 
    assertFalse(rc.getPushURIs().isEmpty()); 
    assertEquals("short:project.git", rc.getPushURIs().get(0).toASCIIString()); 
    
  • PushCommandTest.java minh họa kịch bản đẩy khác nhau, using RemoteConfig.
    Xem testTrackingUpdate() để có ví dụ hoàn chỉnh, hãy đẩy một theo dõi nhánh từ xa.
    chiết xuất:

    String trackingBranch = "refs/remotes/" + remote + "/master"; 
    RefUpdate trackingBranchRefUpdate = db.updateRef(trackingBranch); 
    trackingBranchRefUpdate.setNewObjectId(commit1.getId()); 
    trackingBranchRefUpdate.update(); 
    
    URIish uri = new URIish(db2.getDirectory().toURI().toURL()); 
    remoteConfig.addURI(uri); 
    remoteConfig.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/" 
        + remote + "/*")); 
    remoteConfig.update(config); 
    config.save(); 
    
    
    RevCommit commit2 = git.commit().setMessage("Commit to push").call(); 
    
    RefSpec spec = new RefSpec(branch + ":" + branch); 
    Iterable<PushResult> resultIterable = git.push().setRemote(remote) 
        .setRefSpecs(spec).call(); 
    
+0

Cảm ơn vì những đóng góp của bạn. Tôi đã xem PushCommandTest.java nhưng không hiểu đủ để sử dụng nó. Tôi sẽ dùng thử và cập nhật. Cảm ơn một lần nữa. –

+0

Đã thử ngay bây giờ và nó hoạt động rất tốt! Cảm ơn sự giúp đỡ của bạn! –

4

Cách đơn giản nhất là sử dụng các API JGit Porcelain:

Repository localRepo = new FileRepository(localPath); 
    Git git = new Git(localRepo); 

    // add remote repo: 
    RemoteAddCommand remoteAddCommand = git.remoteAdd(); 
    remoteAddCommand.setName("origin"); 
    remoteAddCommand.setUri(new URIish(httpUrl)); 
    // you can add more settings here if needed 
    remoteAddCommand.call(); 

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

Thay thế tốt cho câu trả lời (5 tuổi) của tôi. +1 – VonC

Các vấn đề liên quan