~tijlvdb/coral

ref: 28680d6a5b95d2b307523f2887768c7777fdbdff coral/app/Helpers/Git.php -rw-r--r-- 1.2 KiB View raw
28680d6a — Tijl Van den Brugghen Add full support for caching 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
51
52
53
54
55
56
57
58
59
60
61
62
<?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. We run the Git command once for each piece of information
	 * we need to get, as I couldn't find a straightforward way of ensuring correct delimitation of the content (mostly
	 * that any chosen delimiter could be used in the commit message)
	 */
	public function fileInformation($path)
	{
		$props = [
			'updated_at' => '%at',
			'updated_by' => '%an',
			'last_commit_message' => '%s'
		];
		$res = [];
		
		foreach ($props as $prop => $val) {
			$cmd = 'log --pretty=format:"' . $val . '" -n 1';
			$cmd .= ' -- ' . $path;
			
			// Make sure we do not re-use variables from old runs
			unset($out);
			unset($status);
			
			$this->run($cmd, $out, $status);
			$res[$prop] = implode('\n', $out);
		}
		
		return $res;
	}
	
	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);
	}
}