2013-10-19 10 views
7

hat numaralarını almak nasıl:JGIT kullanarak, bir Git depo kararlıdır aşağıdaki kod parçasını varsayarsak eklendi/silinmiş hatlar

int test(){ 
    int a = 3; 
    int b = 4; 
    int c = a + b; 
    return c; 
} 

ve üstü

int test(){ 
    return 7; 
} 
şekilde güncellenir

Şu anda, yukarıdaki işlemin yapıldığı Git deposuna erişmek için JGit API'sini kullanan bir yönteme sahibim ve aşağıdakine benzer bir dize çıktılar:

int test(){ 
-int a = 3; 
-int b = 4; 
-int c = a + b; 
-return c; 
+return 7; 
} 

Şimdi, gereksinimlerim değişti ve yalnızca değişen satırların satır numaralarını bilmek istiyorum. Bir güncelleme yapıldığında GitHub uygulama verdiğini

2 -int a = 3; 
3 -int b = 4; 
4 -int c = a + b; 
5 -return c; 
2 +return 7; 

Temelde, aynı bilgileri: Yani aşağıdaki gibi bir şey istemez.

Herhangi bir yardım büyük takdir :) nasıl

pasajı -/+ hatları hesaplanır: Bu sorun olabilir herkes için

  String oldHash = "ee3e216ab5047748a22e9ec5ad3e92834704f0cc"; 
     Git git = null; 
     try { 
      //the path where the repo is. 
      git = Git.open(new File("C:\\Users\\Administrator\\Documents\\GitHub\\Trial")); 
     } catch (IOException e1) { 
      e1.printStackTrace(); 
     } 
     Repository repository = git.getRepository(); 
     ObjectId old = null; 
     ObjectId head = null; 
     //a new reader to read objects from getObjectDatabase() 
     ObjectReader reader = repository.newObjectReader(); 
     //Create a new parser. 
     CanonicalTreeParser oldTreeIter = new CanonicalTreeParser(); 
     CanonicalTreeParser newTreeIter = new CanonicalTreeParser(); 
     List<DiffEntry> diffs = null; 

     try { 
      //parse a git repository string and return an ObjectId 
      old = repository.resolve(oldHash + "^{tree}"); 
      head = repository.resolve("HEAD^{tree}"); 
      //Reset this parser to walk through the given tree 
      oldTreeIter.reset(reader, old); 
      newTreeIter.reset(reader, head); 
      diffs = git.diff()//Returns a command object to execute a diff command 
        .setNewTree(newTreeIter) 
        .setOldTree(oldTreeIter) 
        .call();//returns a DiffEntry for each path which is different 

     } catch (RevisionSyntaxException | IOException | GitAPIException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     //DiffLineCountFilter d = new DiffLineCountFilter(); 
     //out is the stream the formatter will write to 
     ByteArrayOutputStream out = new ByteArrayOutputStream(); 
     //Create a new formatter with a default level of context. 
     DiffFormatter df = new DiffFormatter(out); 
     //Set the repository the formatter can load object contents from. 
     df.setRepository(git.getRepository()); 
     ArrayList<String> diffText = new ArrayList<String>(); 
     //A DiffEntry is 'A value class representing a change to a file' therefore for each file you have a diff entry 
     for(DiffEntry diff : diffs) 
     { 
      try { 
       //Format a patch script for one file entry. 
       df.format(diff); 
       RawText r = new RawText(out.toByteArray()); 
       r.getLineDelimiter(); 


       diffText.add(out.toString()); 
       out.reset(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

     } 
+0

İstediğiniz çıktının ne olduğunu anlamıyorum, neden + dönüşü 7; ve 5 numaralı hat? – robinst

+0

Üzgünüz, bu bir hatadır. Olması gereken: 2 -int a = 3; 3 -int b = 4; 4 -int c = a + b; 5 -return c; 2 + dönüş 7; – gracey

+0

sadece düzenledi :) – gracey

cevap

3

Sadece bir ipucu. Eklenen ve silinen satırların satır numaralarını almayı başaramadım ancak değiştirilmeyen diğer satırlar olmadan yalnızca eklenen ve silinen satırları içeren bir dize almayı başardım. Ben

df.format(diff); 
+0

Koddan bahsetmiştim ve hat çıktılarını diff çıkışından hesaplayabileceğinizi düşünüyorum. @@@4,4 +4,4 @@ ', @@ - , + , ' anlamına gelmelidir. – surenyonjan

1

Satır endeksler ve B A arasındaki farkı yapmanız gereken satırdan önce sağ yukarıda sağlanan snippet'te

df.setContext(0); 

:

Bu

basitçe satır ekleyerek yapıldı Dizin sonucundan satır indeksleri:

int linesAdded = 0; 
int linesDeleted = 0; 
int filesChanged = 0; 
try { 
    repo = new FileRepository(new File("repo/.git")); 
    RevWalk rw = new RevWalk(repo); 
    RevCommit commit = rw.parseCommit(repo.resolve("486817d67b")); // Any ref will work here (HEAD, a sha1, tag, branch) 
    RevCommit parent = rw.parseCommit(commit.getParent(0).getId()); 
    DiffFormatter df = new DiffFormatter(DisabledOutputStream.INSTANCE); 
    df.setRepository(repo); 
    df.setDiffComparator(RawTextComparator.DEFAULT); 
    df.setDetectRenames(true); 
    List<DiffEntry> diffs; 
    diffs = df.scan(parent.getTree(), commit.getTree()); 
    filesChanged = diffs.size(); 
    for (DiffEntry diff : diffs) { 
     for (Edit edit : df.toFileHeader(diff).toEditList()) { 
      linesDeleted += edit.getEndA() - edit.getBeginA(); 
      linesAdded += edit.getEndB() - edit.getBeginB(); 
     } 
    } 
} catch (IOException e1) { 
    throw new RuntimeException(e1); 
}