2012-10-04 30 views
12

我嘗試了很多方法來使用jGit克隆回購(它有效)。 然後,我在存儲庫中寫入一些存檔,並嘗試添加所有(git add *git add -A或類似的東西)..但它不起作用。簡單的文件不會被添加到暫存區域。jGit - 如何將所有文件添加到分段區域

我的代碼是這樣的:

FileRepositoryBuilder builder = new FileRepositoryBuilder(); 
    Repository repository = builder.setGitDir(new File(folder)) 
      .readEnvironment().findGitDir().setup().build(); 
    CloneCommand clone = Git.cloneRepository(); 
    clone.setBare(false).setCloneAllBranches(true); 
    clone.setDirectory(f).setURI("[email protected]:test.git"); 
    try { 
     clone.call(); 
    } catch (GitAPIException e) { 
     e.printStackTrace(); 
    } 
    Files.write("testing it...", new File(folder + "/test2.txt"), 
      Charsets.UTF_8); 
    Git g = new Git(repository); 
    g.add().addFilepattern("*").call(); 

我在做什麼錯? 謝謝。


異常而與addFilePattern想什麼( 「」):

Exception in thread "main" org.eclipse.jgit.errors.NoWorkTreeException: Bare Repository has neither a working tree, nor an index 
    at org.eclipse.jgit.lib.Repository.getIndexFile(Repository.java:850) 
    at org.eclipse.jgit.dircache.DirCache.lock(DirCache.java:264) 
    at org.eclipse.jgit.lib.Repository.lockDirCache(Repository.java:906) 
    at org.eclipse.jgit.api.AddCommand.call(AddCommand.java:138) 
    at net.ciphersec.git.GitTests.main(GitTests.java:110) 

回答

17

一個簡單的方法來調試,這是看AddCommandJGit repo測試:AddCommandTest.java

你會看到爲了添加所有文件,模式「*」從不使用,但「.」是。
它是在一個名爲test函數中使用... testAddWholeRepo()

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

除外(!):

Exception in thread "main" org.eclipse.jgit.errors.NoWorkTreeException: 
Bare Repository has neither a working tree, nor an index 

相當明確的:你需要添加文件在非裸露的回購協議。

請參閱test method testCloneRepository()與您自己的克隆進行比較,看看是否有任何區別。

+0

謝謝,但我得到了一個異常。我會把它放在這個問題 – caarlos0

+0

@ caarlos0回答編輯 – VonC

+0

但我克隆它作爲一個非裸存儲庫...我不明白我應該這樣做... – caarlos0

1

這可能是通配符,我剛讀了add命令的Javadoc,看起來就像你爲了發送目錄的名稱添加它的內容不是通配符:

addFilepattern 

public AddCommand addFilepattern(String filepattern) 

參數:filepattern - 從中​​添加內容的文件。 還可以給出名稱(例如,要添加dir/file1dir/file2的目錄)的引導目錄 以遞歸方式將所有 文件添加到目錄中。 Fileglobs(例如*.c)尚未支持 。

3

我有一種情況,我必須將文件f1從當前目錄移動到另一個名爲'temp'的目錄。 。移動文件,調用git.add()調用後addFilePattern(「」)()以一種不可思議的方式行事,因爲git的狀態提供了以下結果:

Changes to be committed: 
    (use "git reset HEAD <file>..." to unstage) 

    new file: temp/f1.html 

Changes not staged for commit: 
    (use "git add/rm <file>..." to update what will be committed) 
    (use "git checkout -- <file>..." to discard changes in working directory) 

    deleted: f1.html 

它認識到一個新文件的臨時/ f1已創建,但未檢測到該文件先被刪除。這或許是因爲移動的文件可以看到如下

  • 刪除/切割文件F1
  • 創建一個所謂的氣溫
  • 創建/粘貼文件F1

然後我就夾跨越setUpdate(true),查找已被跟蹤的文件的更新並且不會暫存新文件。 (檢查java-doc的更多信息)

所以我必須對我的代碼更改爲兩行,像這樣爲了混帳識別添加和修改兩個文件(其中包括刪除):

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

混帳狀態現在給出了預期的結果:

renamed: f1.hml -> temp/f1.html 
相關問題