2012-10-04 12 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 

काफी स्पष्ट है: आप एक गैर नंगे रेपो में फ़ाइल जोड़ने की जरूरत ।

अपने स्वयं के क्लोन से तुलना करने के लिए test method testCloneRepository() देखें, और देखें कि कोई अंतर है या नहीं।

+0

धन्यवाद, लेकिन मुझे अपवाद मिला। मैं इसे – caarlos0

+0

@ caarlos0 उत्तर में संपादित कर दूंगा – VonC

+0

लेकिन मैंने इसे गैर-नंगे भंडार के रूप में क्लोन किया है ... मुझे यह नहीं पता कि मुझे क्या करना है ... – caarlos0

1

यह वाइल्डकार्ड हो सकता है, मैं सिर्फ ऐड आदेश के लिए जावाडोक पढ़ा है, लगता है कि आप क्रम में एक निर्देशिका का नाम भेजने के अपने नहीं सामग्री को वाइल्ड कार्ड जोड़ने के लिए:

addFilepattern 

public AddCommand addFilepattern(String filepattern) 

पैरामीटर: filepattern - से सामग्री जोड़ने के लिए फ़ाइल। इसके अलावा एक अग्रणी निर्देशिका नाम (उदा। dir/file1 और dir/file2 जोड़ने के लिए डीआईआर) निर्देशिका में सभी फ़ाइलों को जोड़ने के लिए दिया जा सकता है। फाइलग्लब्स (उदा। *.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) पर जो उन फ़ाइलों को अपडेट ढूंढता है जिन्हें पहले से ट्रैक किया जा रहा है और नई फाइलें नहीं लेगी। (अधिक जानकारी के लिए जाँच करें जावा-doc)

      तो मैं अपने कोड Git दोनों फ़ाइलों को जोड़ा गया है और संशोधित पहचान सकें, इसके लिए तो जैसे दो लाइनों को बदलने के लिए (जिसे हटाने के भी शामिल है) था:

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

      Git स्थिति अब अपेक्षित परिणाम देता है:

      renamed: f1.hml -> temp/f1.html 
      
    संबंधित मुद्दे