-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathRepository.php
652 lines (557 loc) · 18.2 KB
/
Repository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
<?php
/**
* This file is part of Gitonomy.
*
* (c) Alexandre Salomé <[email protected]>
* (c) Julien DIDIER <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Gitonomy\Git;
use Gitonomy\Git\Diff\Diff;
use Gitonomy\Git\Exception\InvalidArgumentException;
use Gitonomy\Git\Exception\ProcessException;
use Gitonomy\Git\Exception\RuntimeException;
use Psr\Log\LoggerInterface;
use Symfony\Component\Process\Process;
/**
* Git repository object.
*
* Main entry point for browsing a Git repository.
*
* @author Alexandre Salomé <[email protected]>
*/
class Repository
{
const DEFAULT_DESCRIPTION = "Unnamed repository; edit this file 'description' to name the repository.\n";
/**
* Directory containing git files.
*
* @var string
*/
protected $gitDir;
/**
* Working directory.
*
* @var string
*/
protected $workingDir;
/**
* Cache containing all objects of the repository.
*
* Associative array, indexed by object hash
*
* @var array
*/
protected $objects;
/**
* Reference bag associated to this repository.
*
* @var ReferenceBag
*/
protected $referenceBag;
/**
* Logger (can be null).
*
* @var LoggerInterface
*/
protected $logger;
/**
* Path to git command.
*/
protected $command;
/**
* Debug flag, indicating if errors should be thrown.
*
* @var bool
*/
protected $debug;
/**
* Environment variables that should be set for every running process.
*
* @var array
*/
protected $environmentVariables;
/**
* @var bool
*/
protected $inheritEnvironmentVariables;
/**
* Timeout that should be set for every running process.
*
* @var int
*/
protected $processTimeout;
/**
* Constructs a new repository.
*
* Available options are:
*
* * working_dir : specify where working copy is located (option --work-tree)
*
* * debug : (default: true) enable/disable minimize errors and reduce log level
*
* * logger : a logger to use for logging actions (Psr\Log\LoggerInterface)
*
* * environment_variables : define environment variables for every ran process
*
* @param string $dir path to git repository
* @param array $options array of options values
*
* @throws InvalidArgumentException The folder does not exists
*/
public function __construct($dir, $options = [])
{
$options = array_merge([
'working_dir' => null,
'debug' => true,
'logger' => null,
'command' => 'git',
'environment_variables' => [],
'inherit_environment_variables' => false,
'process_timeout' => 3600,
], $options);
if (null !== $options['logger'] && !$options['logger'] instanceof LoggerInterface) {
throw new InvalidArgumentException(sprintf('Argument "logger" passed to Repository should be a Psr\Log\LoggerInterface. A %s was provided', is_object($options['logger']) ? get_class($options['logger']) : gettype($options['logger'])));
}
$this->logger = $options['logger'];
$this->initDir($dir, $options['working_dir']);
$this->objects = [];
$this->command = $options['command'];
$this->debug = (bool) $options['debug'];
$this->processTimeout = $options['process_timeout'];
if (defined('PHP_WINDOWS_VERSION_BUILD') && isset($_SERVER['PATH']) && !isset($options['environment_variables']['PATH'])) {
$options['environment_variables']['PATH'] = $_SERVER['PATH'];
}
$this->environmentVariables = $options['environment_variables'];
$this->inheritEnvironmentVariables = $options['inherit_environment_variables'];
if (true === $this->debug && null !== $this->logger) {
$this->logger->debug(sprintf('Repository created (git dir: "%s", working dir: "%s")', $this->gitDir, $this->workingDir ?: 'none'));
}
}
/**
* Initializes directory attributes on repository:.
*
* @param string $gitDir directory of a working copy with files checked out
* @param string $workingDir directory containing git files (objects, config...)
*/
private function initDir($gitDir, $workingDir = null)
{
$realGitDir = realpath($gitDir);
if (false === $realGitDir) {
throw new InvalidArgumentException(sprintf('Directory "%s" does not exist or is not a directory', $gitDir));
} elseif (!is_dir($realGitDir)) {
throw new InvalidArgumentException(sprintf('Directory "%s" does not exist or is not a directory', $realGitDir));
} elseif (null === $workingDir && is_file($realGitDir . '/.git')) {
if (!preg_match('/^gitdir: ?(.+)$/', file_get_contents($realGitDir . '/.git'), $matches)) {
throw new InvalidArgumentException(sprintf('Directory "%s" contains a .git file, but it is not in the expected format', $realGitDir));
}
$foundGitPath = realpath($realGitDir . DIRECTORY_SEPARATOR . $matches[1]);
if (!is_dir($foundGitPath)) {
throw new InvalidArgumentException(sprintf('Directory "%s" contains a .git file, but the directory it points to cannot be found', $realGitDir));
}
$workingDir = $realGitDir;
$realGitDir = $foundGitPath;
} elseif (null === $workingDir && is_dir($realGitDir . '/.git')) {
$workingDir = $realGitDir;
$realGitDir = $realGitDir . '/.git';
}
$this->gitDir = $realGitDir;
$this->workingDir = $workingDir;
}
/**
* Tests if repository is a bare repository.
*
* @return bool
*/
public function isBare()
{
return null === $this->workingDir;
}
/**
* Returns the HEAD resolved as a commit.
*
* @return Commit|null returns a Commit or ``null`` if repository is empty
*/
public function getHeadCommit()
{
$head = $this->getHead();
if ($head instanceof Reference) {
return $head->getCommit();
}
return $head;
}
/**
* @throws RuntimeException Unable to find file HEAD (debug-mode only)
*
* @return Reference|Commit|null current HEAD object or null if error occurs
*/
public function getHead()
{
$file = $this->gitDir.'/HEAD';
if (!file_exists($file)) {
$message = sprintf('Unable to find HEAD file ("%s")', $file);
if (null !== $this->logger) {
$this->logger->error($message);
}
if (true === $this->debug) {
throw new RuntimeException($message);
}
}
$content = trim(file_get_contents($file));
if (null !== $this->logger) {
$this->logger->debug('HEAD file read: '.$content);
}
if (preg_match('/^ref: (.+)$/', $content, $vars)) {
return $this->getReferences()->get($vars[1]);
} elseif (preg_match('/^[0-9a-f]{40}$/', $content)) {
return $this->getCommit($content);
}
$message = sprintf('Unexpected HEAD file content (file: %s). Content of file: %s', $file, $content);
if (null !== $this->logger) {
$this->logger->error($message);
}
if (true === $this->debug) {
throw new RuntimeException($message);
}
}
/**
* @return bool
*/
public function isHeadDetached()
{
return !$this->isHeadAttached();
}
/**
* @return bool
*/
public function isHeadAttached()
{
return $this->getHead() instanceof Reference;
}
/**
* Returns the path to the git repository.
*
* @return string A directory path
*/
public function getPath()
{
return $this->workingDir === null ? $this->gitDir : $this->workingDir;
}
/**
* Returns the directory containing git files (git-dir).
*
* @return string
*/
public function getGitDir()
{
return $this->gitDir;
}
/**
* Returns the work-tree directory. This may be null if repository is
* bare.
*
* @return string path to repository or null if repository is bare
*/
public function getWorkingDir()
{
return $this->workingDir;
}
/**
* Instanciates a revision.
*
* @param string $name Name of the revision
*
* @return Revision
*/
public function getRevision($name)
{
return new Revision($this, $name);
}
/**
* @return WorkingCopy
*/
public function getWorkingCopy()
{
return new WorkingCopy($this);
}
/**
* Returns the reference list associated to the repository.
*
* @param bool $reload Reload references from the filesystem
*
* @return ReferenceBag
*/
public function getReferences($reload = false)
{
if (null === $this->referenceBag || $reload) {
$this->referenceBag = new ReferenceBag($this);
}
return $this->referenceBag;
}
/**
* Instanciates a commit object or fetches one from the cache.
*
* @param string $hash A commit hash, with a length of 40
*
* @return Commit
*/
public function getCommit($hash)
{
if (!isset($this->objects[$hash])) {
$this->objects[$hash] = new Commit($this, $hash);
}
return $this->objects[$hash];
}
/**
* Instanciates a tree object or fetches one from the cache.
*
* @param string $hash A tree hash, with a length of 40
*
* @return Tree
*/
public function getTree($hash)
{
if (!isset($this->objects[$hash])) {
$this->objects[$hash] = new Tree($this, $hash);
}
return $this->objects[$hash];
}
/**
* Instanciates a blob object or fetches one from the cache.
*
* @param string $hash A blob hash, with a length of 40
*
* @return Blob
*/
public function getBlob($hash)
{
if (!isset($this->objects[$hash])) {
$this->objects[$hash] = new Blob($this, $hash);
}
return $this->objects[$hash];
}
/**
* @return Blame
*/
public function getBlame($revision, $file, $lineRange = null)
{
if (is_string($revision)) {
$revision = $this->getRevision($revision);
}
return new Blame($this, $revision, $file, $lineRange);
}
/**
* Returns log for a given set of revisions and paths.
*
* All those values can be null, meaning everything.
*
* @param array $revisions An array of revisions to show logs from. Can be
* any text value type
* @param array $paths Restrict log to modifications occurring on given
* paths.
* @param int $offset Start from a given offset in results.
* @param int $limit Limit number of total results.
*
* @return Log
*/
public function getLog($revisions = null, $paths = null, $offset = null, $limit = null)
{
return new Log($this, $revisions, $paths, $offset, $limit);
}
/**
* @return Diff
*/
public function getDiff($revisions)
{
if (null !== $revisions && !$revisions instanceof RevisionList) {
$revisions = new RevisionList($this, $revisions);
}
$args = array_merge(['-r', '-p', '-m', '-M', '--no-commit-id', '--full-index'], $revisions->getAsTextArray());
$diff = Diff::parse($this->run('diff', $args));
$diff->setRepository($this);
return $diff;
}
/**
* Returns the size of repository, in kilobytes.
*
* @return int A sum, in kilobytes
*/
public function getSize()
{
$totalBytes = 0;
$path = realpath($this->gitDir);
if ($path && file_exists($path)) {
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS)) as $object) {
$totalBytes += $object->getSize();
}
}
return (int) ($totalBytes / 1000 + 0.5);
}
/**
* Executes a shell command on the repository, using PHP pipes.
*
* @param string $command The command to execute
*/
public function shell($command, array $env = [])
{
$argument = sprintf('%s \'%s\'', $command, $this->gitDir);
$prefix = '';
foreach ($env as $name => $value) {
$prefix .= sprintf('export %s=%s;', escapeshellarg($name), escapeshellarg($value));
}
proc_open($prefix.'git shell -c '.escapeshellarg($argument), [STDIN, STDOUT, STDERR], $pipes);
}
/**
* Returns the hooks object.
*
* @return Hooks
*/
public function getHooks()
{
return new Hooks($this);
}
/**
* Returns description of repository from description file in git directory.
*
* @return string The description
*/
public function getDescription()
{
$file = $this->gitDir.'/description';
$exists = is_file($file);
if (null !== $this->logger && true === $this->debug) {
if (false === $exists) {
$this->logger->debug(sprintf('no description file in repository ("%s")', $file));
} else {
$this->logger->debug(sprintf('reading description file in repository ("%s")', $file));
}
}
if (false === $exists) {
return static::DEFAULT_DESCRIPTION;
}
return file_get_contents($this->gitDir.'/description');
}
/**
* Tests if repository has a custom set description.
*
* @return bool
*/
public function hasDescription()
{
return static::DEFAULT_DESCRIPTION !== $this->getDescription();
}
/**
* Changes the repository description (file description in git-directory).
*
* @return Repository the current repository
*/
public function setDescription($description)
{
$file = $this->gitDir.'/description';
if (null !== $this->logger && true === $this->debug) {
$this->logger->debug(sprintf('change description file content to "%s" (file: %s)', $description, $file));
}
file_put_contents($file, $description);
return $this;
}
/**
* This command is a facility command. You can run any command
* directly on git repository.
*
* @param string $command Git command to run (checkout, branch, tag)
* @param array $args Arguments of git command
*
* @throws RuntimeException Error while executing git command (debug-mode only)
*
* @return string Output of a successful process or null if execution failed and debug-mode is disabled.
*/
public function run($command, $args = [])
{
$process = $this->getProcess($command, $args);
if ($this->logger) {
$this->logger->info(sprintf('run command: %s "%s" ', $command, implode(' ', $args)));
$before = microtime(true);
}
$process->run();
$output = $process->getOutput();
if ($this->logger && $this->debug) {
$duration = microtime(true) - $before;
$this->logger->debug(sprintf('last command (%s) duration: %sms', $command, sprintf('%.2f', $duration * 1000)));
$this->logger->debug(sprintf('last command (%s) return code: %s', $command, $process->getExitCode()));
$this->logger->debug(sprintf('last command (%s) output: %s', $command, $output));
}
if (!$process->isSuccessful()) {
$error = sprintf("error while running %s\n output: \"%s\"", $command, $process->getErrorOutput());
if ($this->logger) {
$this->logger->error($error);
}
if ($this->debug) {
throw new ProcessException($process);
}
return;
}
return $output;
}
/**
* Set repository logger.
*
* @param LoggerInterface $logger A logger
*
* @return Repository The current repository
*/
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
return $this;
}
/**
* Returns repository logger.
*
* @return LoggerInterface the logger or null
*/
public function getLogger()
{
return $this->logger;
}
/**
* Clones the current repository to a new directory and return instance of new repository.
*
* @param string $path path to the new repository in which current repository will be cloned
* @param bool $bare flag indicating if repository is bare or has a working-copy
* @param array $options options for Repository creation
* @param array $args arguments to be added to the command-line
*
* @return Repository the newly created repository
*/
public function cloneTo($path, $bare = true, array $options = [], array $args = [])
{
return Admin::cloneTo($path, $this->gitDir, $bare, $options, $args);
}
/**
* This internal method is used to create a process object.
*
* Made private to be sure that process creation is handled through the run method.
* run method ensures logging and debug.
*
* @see self::run
*/
private function getProcess($command, $args = [])
{
$base = [$this->command, '--git-dir', $this->gitDir];
if ($this->workingDir) {
$base = array_merge($base, ['--work-tree', $this->workingDir]);
}
$base[] = $command;
$process = new Process(array_merge($base, $args));
if ($this->inheritEnvironmentVariables) {
$process->setEnv(array_replace($_SERVER, $this->environmentVariables));
} else {
$process->setEnv($this->environmentVariables);
}
$process->setTimeout($this->processTimeout);
$process->setIdleTimeout($this->processTimeout);
return $process;
}
}