Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions src/Console/Command/GitClone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace Dingo\Api\Console\Command;

use Dingo\Api\Contract\Git\Service;
use Illuminate\Console\Command;

class GitClone extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
public $signature = 'api:git:clone
{repository : The repository URL to clone}
{destination : The destination path}
{--branch= : Branch to clone}
{--depth= : Create a shallow clone with a history truncated to the specified number of commits}
{--single-branch : Clone only one branch}
{--recursive : Clone submodules recursively}';

/**
* The console command description.
*
* @var string
*/
public $description = 'Clone a git repository';

/**
* Git service instance.
*
* @var \Dingo\Api\Contract\Git\Service
*/
protected $git;

/**
* Create a new git clone command instance.
*
* @param \Dingo\Api\Contract\Git\Service $git
*
* @return void
*/
public function __construct(Service $git)
{
$this->git = $git;

parent::__construct();
}

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$repository = $this->argument('repository');
$destination = $this->argument('destination');

$this->info("Cloning repository: {$repository}");
$this->info("Destination: {$destination}");

$options = array_filter([
'branch' => $this->option('branch'),
'depth' => $this->option('depth'),
'single-branch' => $this->option('single-branch'),
'recursive' => $this->option('recursive'),
]);

if (!empty($options)) {
$this->line('Options: ' . json_encode($options));
}

$result = $this->git->clone($repository, $destination, $options);

if ($result['success']) {
$this->info('Repository cloned successfully!');

if (!empty($result['output'])) {
$this->line($result['output']);
}

return 0;
} else {
$this->error('Failed to clone repository.');

if (!empty($result['error'])) {
$this->error($result['error']);
}

if (isset($result['exception'])) {
$this->error($result['exception']);
}

return 1;
}
}
}
105 changes: 105 additions & 0 deletions src/Console/Command/GitFetch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace Dingo\Api\Console\Command;

use Dingo\Api\Contract\Git\Service;
use Illuminate\Console\Command;

class GitFetch extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
public $signature = 'api:git:fetch
{path : The repository path}
{--remote= : Remote to fetch from}
{--branch= : Branch to fetch}
{--prune : Remove remote-tracking references that no longer exist on remote}
{--all : Fetch all remotes}';

/**
* The console command description.
*
* @var string
*/
public $description = 'Fetch from a git repository';

/**
* Git service instance.
*
* @var \Dingo\Api\Contract\Git\Service
*/
protected $git;

/**
* Create a new git fetch command instance.
*
* @param \Dingo\Api\Contract\Git\Service $git
*
* @return void
*/
public function __construct(Service $git)
{
$this->git = $git;

parent::__construct();
}

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$path = $this->argument('path');

if (!$this->git->isRepository($path)) {
$this->error("The path [{$path}] is not a git repository.");
return 1;
}

$this->info("Fetching from repository: {$path}");

$options = array_filter([
'remote' => $this->option('remote'),
'branch' => $this->option('branch'),
'prune' => $this->option('prune'),
'all' => $this->option('all'),
]);

if (!empty($options)) {
$this->line('Options: ' . json_encode($options));
}

$result = $this->git->fetch($path, $options);

if ($result['success']) {
$this->info('Fetch completed successfully!');

if (!empty($result['output'])) {
$this->line($result['output']);
}

if (!empty($result['error'])) {
$this->line($result['error']);
}

return 0;
} else {
$this->error('Failed to fetch from repository.');

if (!empty($result['error'])) {
$this->error($result['error']);
}

if (isset($result['exception'])) {
$this->error($result['exception']);
}

return 1;
}
}
}
45 changes: 45 additions & 0 deletions src/Contract/Git/Service.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Dingo\Api\Contract\Git;

interface Service
{
/**
* Clone a git repository to a specified destination.
*
* @param string $repository
* @param string $destination
* @param array $options
*
* @return array
*/
public function clone($repository, $destination, array $options = []);

/**
* Fetch from a git repository.
*
* @param string $path
* @param array $options
*
* @return array
*/
public function fetch($path, array $options = []);

/**
* Get the status of a git repository.
*
* @param string $path
*
* @return array
*/
public function status($path);

/**
* Check if a directory is a git repository.
*
* @param string $path
*
* @return bool
*/
public function isRepository($path);
}
Loading