~tijlvdb/coral

3bb6acc9d907e300833a295d0cccf6d7de4c1cfc — Tijl Van den Brugghen 8 months ago 28680d6
Simplify and optimize getting file information from Git
1 files changed, 9 insertions(+), 21 deletions(-)

M app/Helpers/Git.php
M app/Helpers/Git.php => app/Helpers/Git.php +9 -21
@@ 17,32 17,20 @@ class Git
	}
	
	/**
	 * 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)
	 * Returns info about the file's creation and last update
	 */
	public function fileInformation($path)
	{
		$props = [
			'updated_at' => '%at',
			'updated_by' => '%an',
			'last_commit_message' => '%s'
		];
		$res = [];
		$cmd = 'log --pretty=format:"%at%n%an%n%s" -n 1 -- ' . $path;
		$this->run($cmd, $out, $status);
		
		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);
		}
		if ($status !== 0) $out = ['', ''];
		
		return $res;
		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)