Skip to content

Commit

Permalink
options
Browse files Browse the repository at this point in the history
  • Loading branch information
gwen001 committed May 7, 2018
1 parent 1e860b4 commit 32d7a0a
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 44 deletions.
4 changes: 2 additions & 2 deletions Utils.php
Expand Up @@ -32,8 +32,8 @@ class Utils

public static function help( $error='' )
{
if( is_file('README.md') ) {
$help = file_get_contents( 'README.md' )."\n";
if( is_file(__DIR__.'/README.md') ) {
$help = file_get_contents( __DIR__.'/README.md' )."\n";
preg_match_all( '#```(.*)```#s', $help, $matches );
if( count($matches[1]) ) {
echo trim($matches[1][0])."\n\n";
Expand Down
116 changes: 116 additions & 0 deletions github-grabrepo.php
@@ -0,0 +1,116 @@
<?php

function usage( $err=null ) {
echo 'Usage: php '.$_SERVER['argv'][0]." -o/-u <organization/user> [OPTIONS]\n\n";
echo "Options:\n";
echo "\t-d\tset source directory (required)\n";
echo "\t-o\tset organisation (required)\n";
echo "\t-u\tset user (required)\n";
echo "\n";
if( $err ) {
echo 'Error: '.$err."!\n";
}
exit();
}

define( 'PER_PAGE', 100 );

$options = '';
$options .= 'd:'; // source directory
$options .= 'o:'; // organization
$options .= 'u:'; // user
$t_options = getopt( $options );
//var_dump($t_options);
if( !count($t_options) ) {
usage();
}

if( isset($t_options['d']) ) {
$_directory = $t_options['d'];
} else {
$_directory = __DIR__;
}
$_directory = rtrim( $_directory, '/' );

if( isset($t_options['o']) ) {
$_org = $t_options['o'];
} else {
$_org = null;
}

if( isset($t_options['u']) ) {
$_user = $t_options['u'];
} else {
$_user = null;
}

if( is_null($_org) && is_null($_user) ) {
usage( 'Organization/User not found' );
}

if( !is_null($_org) ) {
$url = 'https://api.github.com/orgs/'.$_org.'/repos?per_page='.PER_PAGE.'&page=';
} else {
$url = 'https://api.github.com/users/'.$_user.'/repos?per_page='.PER_PAGE.'&page=';
}

$run = true;
$page = 1;
$n_clone = 0;

do
{
$c = curl_init();
curl_setopt( $c, CURLOPT_URL, $url.$page );
curl_setopt( $c, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux x86_64; rv:56.0) Gecko/20100101 Firefox/56.0' );
//curl_setopt( $c, CURLOPT_SSL_VERIFYPEER, false );
//curl_setopt( $c, CURLOPT_NOBODY, true );
curl_setopt( $c, CURLOPT_CONNECTTIMEOUT, 3 );
curl_setopt( $c, CURLOPT_FOLLOWLOCATION, false );
curl_setopt( $c, CURLOPT_RETURNTRANSFER, true );
$r = curl_exec( $c );
$t_info = curl_getinfo( $c );
//var_dump( $r );
//var_dump( $t_info );

if( $t_info['http_code'] != 200 ) {
usage( 'Cannot grab datas' );
}

if( !is_dir($_directory) ) {
if( !mkdir($_directory,0777,true) ) {
usage( 'Cannot create destination directory' );
}
}

$t_json = json_decode( $r, true );
$cnt = count( $t_json );
//var_dump( $t_json );

if( !$cnt ) {
break;
}

foreach( $t_json as $repo )
{
if( (int)$repo['fork'] == 0 ) {
$n_clone++;
$cmd = 'git clone '.$repo['html_url'].' '.$_directory.'/'.$repo['name'];
echo $cmd."\n";
exec( $cmd );
echo "\n";
}
}

if( $cnt < PER_PAGE ) {
break;
}

$page++;
}
while( $run );

echo $n_clone." repository cloned\n";
exit();

?>
75 changes: 41 additions & 34 deletions github-search.php
Expand Up @@ -17,70 +17,77 @@ function __autoload( $c ) {

// parse command line
{
$gsearch = new GitHubSearch();
$options = '';
$options .= 'c:';
$options .= 'e:';
$options .= 'f:';
$options .= 'h';
$options .= 'l:';
$options .= 'n';
$options .= 'o:';
$options .= 'r:';
$options .= 's:';
$options .= 't:';

$t_options = getopt( $options );
//var_dump( $t_options );

$argc = $_SERVER['argc'] - 1;
$gsearch = new GitHubSearch();

for ($i = 1; $i <= $argc; $i++) {
switch ($_SERVER['argv'][$i]) {
case '-c':
$gsearch->setCookie( $_SERVER['argv'][$i + 1] );
$i++;
foreach( $t_options as $k=>$v )
{
switch( $k )
{
case 'c':
$gsearch->setCookie( $v );
break;

case '-e':
$gsearch->setExtension( $_SERVER['argv'][$i + 1] );
$i++;
case 'e':
$gsearch->setExtension( $v );
break;

case '-f':
$gsearch->setFilename( $_SERVER['argv'][$i + 1] );
$i++;
case 'f':
$gsearch->setFilename( $v );
break;

case '-h':
case 'h':
Utils::help();
break;

case '-l':
$gsearch->setLanguage( $_SERVER['argv'][$i + 1] );
$i++;
case 'l':
$gsearch->setLanguage( $v );
break;

case '-n':
case 'n':
$gsearch->setColorOutput( false );
break;

case '-o':
$gsearch->setOrganization( $_SERVER['argv'][$i + 1] );
$i++;
case 'o':
$gsearch->setOrganization( $v );
break;

case '-r':
$gsearch->setMaxResult( $_SERVER['argv'][$i + 1] );
$i++;
case 'r':
$gsearch->setMaxResult( $v );
break;

case '-s':
$gsearch->setString( $_SERVER['argv'][$i + 1] );
$i++;
case 's':
$gsearch->setString( $v );
break;

case '-t':
$gsearch->setAuthToken( $_SERVER['argv'][$i + 1] );
$i++;
case 't':
$gsearch->setAuthToken( $v );
break;

default:
Utils::help('Unknown option: '.$_SERVER['argv'][$i]);
Utils::help( 'Unknown option: '.$k );
}
}

if( !$gsearch->getString() && !$gsearch->getFilename() ) {
Utils::help('Search param not found, provide at least a filename or a string');
Utils::help( 'Search param not found, provide at least a filename or a string' );
}
if( !$gsearch->getOrganization() && !$gsearch->getCookie() ) {
Utils::help('You must provide cookie session to perform queries without organization name');
if( !$gsearch->getOrganization() && !$gsearch->getCookie() && !$gsearch->getAuthToken() ) {
Utils::help( 'You must provide cookie session to perform queries without organization name' );
}
}
// ---
Expand Down
16 changes: 8 additions & 8 deletions gsearch-reflog.sh
Expand Up @@ -38,28 +38,28 @@ http=${repo:0:4}
repo_name=$(basename $repo)

if [ $http == "http" ] ; then
if [ -d $repo_name ] ; then
cd $repo_name
git pull
cd ..
else
if [ ! -d $repo_name ] ; then
echo "Repository not found, cloning..."
git clone $repo
fi
repo=$repo_name
fi

if [ -d $repo ] ; then
echo "Repository already exists, updating..."
cd $repo
echo $(pwd)
git pull
cd ..
else
echo "Directory not found!"
echo "Something goes wrong!"
exit
fi

cd $repo
echo "Running reflog..."
git log --reflog > reflog.txt
echo

for w in ${t_keywords[@]} ; do
git log --reflog --pretty="format:- (%H) %b" | grep --color "$w"
grep -n --color "$w" reflog.txt -A 5 -B 5
done

0 comments on commit 32d7a0a

Please sign in to comment.