2012-10-04 7 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(); 

? 감사합니다. . AddCommandTest.java

다음 JGit repo에서 AddCommand의 시험 보는이를 디버깅하는

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

쉬운 방법 중 하나입니다 : (".") addFilePattern 무엇을하려고


예외하면서 모든 파일을 추가하기 위해 "*"패턴은 사용되지 않지만 "."패턴은 표시됩니다.
그리고 그것이라는 이름의 테스트 기능에 사용됩니다 ... testAddWholeRepo()

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

예외 (!) :

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

는 매우 명시 적이다 :가 아닌 맨손으로 REPO에 파일을 추가 할 필요가 .

test method testCloneRepository()을 (를) 참조하여 자신의 복제본과 비교하여 차이가 있는지 확인하십시오.

addFilepattern 

public AddCommand addFilepattern(String filepattern) 

매개 변수 : 당신이 그 내용하지 와일드 카드를 추가하기 위해 디렉토리의 이름을 보내처럼

+0

감사하지만 예외가 있습니다. – caarlos0

+0

@ caarlos0 답변 편집 됨 – VonC

+0

하지만 비 (非) 베어 리포 지 토리로 복제합니다 ... 나는 그것을하기로되어있는 것을 undestand하지 않습니다 ... – caarlos0

1

그것은, 난 그냥 추가 명령에 대한 javadoc의 읽기, 와일드 카드 수 있습니다 보인다 filepattern - 콘텐츠를 추가 할 파일입니다. 이름 (예 : dir/file1dir/file2을 추가하는 dir)은 디렉토리에있는 모든 파일을 재귀 적으로 추가 할 수 있습니다. 파일 그램 (예 : *.c)은 아직 이 지원되지 않습니다.

3

필자는 파일 f1을 현재 디렉토리에서 'temp'라는 다른 디렉토리로 이동해야하는 상황이있었습니다. .. 자식 상태는 다음과 같은 결과 준 이후() git.add 호출, 파일을 이동 한 후 addFilePattern (".")()를 호출 이상한 방식으로 행동 : 그것은 인식

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)에서 이미 추적중인 파일에 대한 업데이트를 찾고 새 파일을 준비하지 않습니다.

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

      자식 :

      그래서 나는 (삭제 포함) 추가 및 수정 된 파일을 모두 인식하는 자식의 순서과 같이 두 줄에 코드를 변경했다 (자세한 정보를 원하시면 자바 문서를 확인하십시오) 상태는 이제 예상 된 결과를 제공합니다.

      renamed: f1.hml -> temp/f1.html 
      
  • 관련 문제