2012-12-09 54 views
1

我花了兩天時間研究一個庫或某種PHP函數來解析git補丁GitHub的API返回,我找不到任何可以使用的東西。Git補丁到HTML解析器

我能夠從Phabricator中取出相當數量的代碼,但它只是有太多的依賴關係。

有誰知道PHP代碼,將解析類似如下:

 commit d87635a1001a23d3a20422fcc8f2484b6bf3ab1b 
     Author: Stav <[email protected]> 
     Date: Thu Dec 6 23:59:16 2012 +0200 

      #666390 test chargify create and cancel subscription. 

     diff --git a/.gitignore b/.gitignore 
     index 796276b..b7fc8a1 100644 
     --- a/.gitignore 
     +++ b/.gitignore 
     @@ -1,2 +1,3 @@ 
     application/config/config.php 
     -application/config/database.php 
     \ No newline at end of file 
     +application/config/database.php 
     +/local_file.php 
     diff --git a/testing/Unit/libs/ChargifyTest.php b/testing/Unit/libs/ChargifyTest.php 
     new file mode 100644 
     index 0000000..f6ea674 
     --- /dev/null 
     +++ b/testing/Unit/libs/ChargifyTest.php 
     @@ -0,0 +1,38 @@ 
     +<?php 
     + 
     +require_once dirname(__FILE__).'/../../../local_file.php'; 
     + 

成HTML表是這樣的:

phabricator parse tool

預先感謝您:)

+1

爲什麼不自己寫一個?解析統一差異並不困難:「 - 」線在左側是紅色的,「+」在右側是綠色,沒有任何一個的線是正常的,「---」/「+++」/「 @@「行是信息。 –

+0

我真的結束了自己做:)沒有完善它尚未發佈在這裏 – agrublev

+0

這很酷:)請發佈後,一旦你完成,我感興趣;) –

回答

4

這是我想出的。

 <? foreach($commit['files'] as $file) { ?> 
     <div><h3><?=$file['filename']?></h3> 
      <? 
      $firstLine = true; 
      $patch = explode("@@",$file['patch']); 
      $lines_info = explode(",",$patch[1]); 
      $st_ln_num = substr($lines_info[0], 2); 
      $start_line['original'] = $st_ln_num; 
      $start_line['left'] = $st_ln_num; 
      $start_line['right'] = $st_ln_num; 
      $lines = explode("\n",$file['patch']); 
      ?> 
      <div style="overflow: auto"> 
      <table class="parseDiff" cellpadding="0" cellspacing="0"> 
       <? foreach ($lines as $line) { 
        if (! $firstLine) { 
          $line_left = ""; 
          $line_right = ""; 
          $char = strlen($line) ? $line[0] : '~'; 
          $type = "neutral"; 
          switch ($char) { 
           case '-': 
            $line_left = $start_line['left']++; 
            $type = "removed"; 
            $line = $line; 
            break; 
           case '+': 
            $line_right = $start_line['right']++; 
            $type = "added"; 
            $line = $line; 
            break; 
           default: 
            $line_left = $start_line['left']++; 
            $line_right = $start_line['right']++; 
            $type = "neutral"; 
            break; 
          } 
        ?> 
        <tr class="line-type-<?=$type?>"> 
         <td class="line-number line-number-left"><?=$line_left?></td> 
         <td class="line-number line-number-right"><?=$line_right?></td> 
         <td class="line-code"><pre><?=htmlspecialchars($line)?></pre></td> 
        </tr> 
       <? 
        } else { 
       ?> 
        <tr class="line-type-first"> 
         <td class="line-number line-number-left">&middot;&middot;&middot;</td> 
         <td class="line-number line-number-right">&middot;&middot;&middot;</td> 
         <td class="line-code"><pre><?=htmlspecialchars($line)?></pre></td> 
        </tr> 
       <? 
         $firstLine = false; 
        } // end if firstLine 
       } // end foreach 
       ?> 
      </table> 
      </div> 
     </div> 
    <? } ?>