~tijlvdb/coral

ref: 3bb6acc9d907e300833a295d0cccf6d7de4c1cfc coral/app/Helpers/Git.php -rw-r--r-- 881 bytes View raw
3bb6acc9 — Tijl Van den Brugghen Simplify and optimize getting file information from Git 6 months ago
                                                                                
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
<?php
// SPDX-License-Identifier: AGPL-3.0-or-later

namespace Helpers;

class Git
{
	public $dir;
	
	public function __construct(string $dir)
	{
		$this->dir = $dir;
	}
	
	public function fileHistory($path)
	{
	}
	
	/**
	 * Returns info about the file's creation and last update
	 */
	public function fileInformation($path)
	{
		$cmd = 'log --pretty=format:"%at%n%an%n%s" -n 1 -- ' . $path;
		$this->run($cmd, $out, $status);
		
		if ($status !== 0) $out = ['', ''];
		
		return [
			'updated_at' => $out[0],
			'updated_by' => $out[1],
			'last_commit_message' => implode('\n', array_slice($out, 2))
		];
	}
	
	public function run(string $command, &$out, &$status)
	{
		$f3 = \Base::instance();
		
		$command = "cd '"
			. $f3->get('config.git_dir')
			. "' 2>&1; "
			. $f3->get('config.git.binary')
			. ' '
			. $command
			. ' 2>&1;';
		
		exec($command, $out, $status);
	}
}