/[drupal]/contributions/modules/gitbrowser/gb.module
ViewVC logotype

Diff of /contributions/modules/gitbrowser/gb.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.2, Fri May 1 00:00:05 2009 UTC revision 1.3, Fri May 1 00:13:13 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: gb.module,v 1.1 2009/04/30 13:26:16 gordon Exp $  // $Id: gb.module,v 1.2 2009/05/01 00:00:05 gordon Exp $
3  /**  /**
4   * @file   * @file
5   * Provide a git browsing API which allows other modules to add the ability   * Provide a git browsing API which allows other modules to add the ability
# Line 13  function gb_menu() { Line 13  function gb_menu() {
13    $items = array();    $items = array();
14    
15    $items['admin/settings/git'] = array(    $items['admin/settings/git'] = array(
16      'title' => 'Git Browser',      'title' => 'Git browser',
17      'page callback' => 'drupal_get_form',      'page callback' => 'drupal_get_form',
18      'page arguments' => array('gb_settings'),      'page arguments' => array('gb_settings'),
19      'access arguments' => array('administer site configuration'),      'access arguments' => array('administer site configuration'),
20      'description' => 'Configuration settings for Git Brownser',      'description' => 'Configuration settings for Git Brownser',
21        'file' => 'gb.admin.inc',
22    );    );
23    
24    return $items;    return $items;
25  }  }
26    
 function gb_settings() {  
   $form = array();  
   
   $form['git_cmd'] = array(  
     '#type' => 'textfield',  
     '#title' => t('Path to git'),  
     '#default_value' => variable_get('git_cmd', '/usr/bin/git')  
   );  
   
   $version = gb_git(NULL, '--version');  
   $form['git_version'] = array(  
     '#type' => 'item',  
     '#title' => t('Git version'),  
     '#value' => implode("<br />\n", $version),  
   );  
   return system_settings_form($form);  
 }  
   
27  /**  /**
28   * Build menu links   * Build menu links
29   */   */
30  function gb_build_git_menus($params = array()) {  function gb_build_git_menus($params = array()) {
31      module_load_include('inc', 'gb', 'gb');
32    $default = array(    $default = array(
33      'title' => 'Git Repository',      'title' => 'Git Repository',
34      'repos' => './.git',      'repos' => './.git',
# Line 58  function gb_build_git_menus($params = ar Line 42  function gb_build_git_menus($params = ar
42      'page callback' => 'gb_summary',      'page callback' => 'gb_summary',
43      'page arguments' => array($params['repos']),      'page arguments' => array($params['repos']),
44      'access callback' => 1,      'access callback' => 1,
45        'file' => 'gb.inc',
46    );    );
47    $items+= gb_build_git_tabs($params['repos'], $path, 'HEAD');    $items+= gb_build_git_tabs($params['repos'], $path, 'HEAD');
48    
# Line 67  function gb_build_git_menus($params = ar Line 52  function gb_build_git_menus($params = ar
52        'page callback' => 'gb_summary',        'page callback' => 'gb_summary',
53        'page arguments' => array($params['repos'], $head['fullname']),        'page arguments' => array($params['repos'], $head['fullname']),
54        'access callback' => 1,        'access callback' => 1,
55          'file' => 'gb.inc',
56      );      );
57    
58      $items+= gb_build_git_tabs($params['repos'], $path .'/'. $head['name'], $head['fullname']);      $items+= gb_build_git_tabs($params['repos'], $path .'/'. $head['name'], $head['fullname']);
# Line 89  function gb_build_git_tabs($repos, $path Line 75  function gb_build_git_tabs($repos, $path
75      'page arguments' => array($repos, $head),      'page arguments' => array($repos, $head),
76      'access callback' => 1,      'access callback' => 1,
77      'type' => MENU_DEFAULT_LOCAL_TASK,      'type' => MENU_DEFAULT_LOCAL_TASK,
78      'weight' => -1,      'weight' => -1,
79        'file' => 'gb.inc',
80    );    );
81    $items[$path .'/shortlog'] = array(    $items[$path .'/shortlog'] = array(
82      'title' => 'Short log',      'title' => 'Short log',
# Line 97  function gb_build_git_tabs($repos, $path Line 84  function gb_build_git_tabs($repos, $path
84      'page arguments' => array($repos, $head),      'page arguments' => array($repos, $head),
85      'access callback' => 1,      'access callback' => 1,
86      'type' => MENU_LOCAL_TASK,      'type' => MENU_LOCAL_TASK,
87        'file' => 'gb.inc',
88    );    );
89    
90    return $items;    return $items;
91  }  }
92    
93  /**  /**
  * Display Summary Page for the git repository  
  */  
 function gb_summary($repos, $head = 'HEAD') {  
   $repos = realpath($repos);  
   
   $output = '';  
   
   $output.= '<h3>'. t('shortlog') .'</h3>';  
   $output.= gb_get_short_log($repos, $head);  
   
   return $output;  
 }  
   
 function gb_shortlog($repos, $head = 'HEAD') {  
   global $pager_page_array, $pager_total;  
   $page = isset($_GET['page']) ? $_GET['page'] : '';  
   $pager_page_array = explode(',', $page);  
   $element = 0;  
   
   if (empty($pager_page_array[$element])) {  
     $pager_page_array[$element] = 0;  
   }  
   
   $refs = gb_rev_list($repos, NULL, $head);  
   $chunks = array_chunk($refs, 20);  
   
   $pager_total[$element] = count($chunks);  
   
   $start = array_shift($chunks[$pager_page_array[$element]]);  
   
   $output = gb_get_short_log($repos, $start, 20);  
   $output.= theme('pager', array(), 20, $element);  
   
   return $output;  
 }  
   
 /**  
  *  
  */  
 function gb_get_short_log($repos, $start = 'HEAD', $count = 16) {  
   $revs = gb_rev_list($repos, $count, $start);  
   $rows = array();  
   foreach ($revs as $rev) {  
     $info = gb_get_commit_info($repos, $rev);  
   
     $rows[] = array(  
       format_date($info['author'][0]['date'], 'short'),  
       $info['author'][0]['name'],  
       $info['description'][0],  
     );  
   }  
   
   $output.= theme('table', array(), $rows);  
   
   return $output;  
 }  
   
 function gb_get_heads($repos) {  
   $return = gb_git($repos, 'show-ref --heads');  
   
   $heads = array();  
   
   foreach ($return as $line) {  
     if (preg_match('/^([0-9a-f]{40}) (.*)$/i', $line, $match)) {  
       $heads[] = array(  
         'hash' => $match[1],  
         'fullname' => $match[2],  
         'name' => basename($match[2]),  
       );  
     }  
   }  
   
   return $heads;  
 }  
 // ------------------------------------------------------------------------  
   
 /**  
  * Get commit info  
  */  
 function gb_get_commit_info($repos, $hash) {  
   $info = array(  
     'hash' => $hash,  
   );  
   
   $cmd = 'rev-list --header --max-count=1 '. $hash;  
   $output = gb_git($repos, $cmd);  
   array_pop($output);  
   
   while (!empty($output)) {  
     $line = array_shift($output);  
   
     if (empty($line)) {  
       $info['description'] = $output;  
       $output = array();  
       break;  
     }  
     else if (preg_match('/^(\w+)\s(.*)$/i', $line, $match)) {  
       switch ($match[1]) {  
         case 'committer':  
         case 'author':  
           $person = $match[2];  
           if (preg_match('/^(.*) [<](.*)[>] ([0-9]+) (\+[0-9]{4})/i', $person, $details)) {  
             $info[$match[1]][] = array(  
               'name' => $details[1],  
               'mail' => $details[2],  
               'date' => $details[3],  
               'tz' => $details[4],  
             );  
           }  
           break;  
   
         default:  
           $info[$match[1]][] = $match[2];  
       }  
     }  
     else if (preg_match('/^[0-9a-f]{40}$/i', $line)) {  
       $info['hash'] = $line;  
     }  
   }  
   
   return $info;  
 }  
   
 /**  
  * Get rev list  
  */  
 function gb_rev_list($repos, $max_count = NULL, $start = 'HEAD') {  
   $cmd = 'rev-list '. $start;  
   
   if (isset($max_count)) {  
     $cmd = 'rev-list --max-count='. $max_count .' '. $start;  
   }  
   
   return gb_git($repos, $cmd);  
 }  
   
 /**  
94   * Run the Git command   * Run the Git command
95   */   */
96  function gb_git($repos, $command) {  function gb_git($repos, $command) {

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.3

  ViewVC Help
Powered by ViewVC 1.1.2