-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNovaGitController.php
138 lines (111 loc) · 4.45 KB
/
NovaGitController.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
<?php
namespace Mastani\NovaGitManager\Http\Controllers;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
class NovaGitController extends BaseController
{
private function execGitCommand($command)
{
return shell_exec('cd ' . config('nova-git-manager.git_path', base_path()) . ' && git ' . $command . ' 2>&1');
}
public function branches()
{
$this->execGitCommand('fetch');
$output = $this->execGitCommand('branch -r');
$output = Str::replace(["\n", "\r\n", '*'], '', $output);
$output = Str::replace(' ', ' ', $output);
$output = trim($output);
$branches = explode(' ', $output);
return response()->json([
'branches' => $branches
]);
}
public function log()
{
$output = $this->execGitCommand('log --pretty=format:\'{
@@refs@@: @@%D@@,
@@parents_as_string@@: @@%P@@,
@@id@@: @@%H@@,
@@message@@: @@%s@@,
@@date@@: @@%aI@@,
@@author@@: {
@@name@@: @@%an@@,
@@email@@: @@%ae@@
}
},\' --all');
$output = Str::replace('\\', "\\\\", $output);
$output = Str::replace('"', '\"', $output);
$output = Str::replace('@@', '"', $output);
$output = '[' . rtrim($output, ",") . ']';
$spaces = [];
$lastSpace = 1;
$time = 0;
$data = [];
$data['days'] = [];
$data['commits'] = json_decode($output, true);
$data['commits'] = collect($data['commits'])->map(function ($commit) use (&$time, &$spaces, &$lastSpace, &$data) {
// Format Refs
$formatted_refs = preg_replace("/,|HEAD|->|tag:|origin\//", ' ', $commit['refs']);
$formatted_refs = preg_replace("/\s+/", ' ', $formatted_refs);
$formatted_refs = trim($formatted_refs);
$formatted_refs = explode(' ', $formatted_refs);
$commit['refs'] = end($formatted_refs);
// Format Parents
$lastSpace = in_array($commit['id'], $spaces) ? $spaces[$commit['id']] : 1;
$commit['space'] = $lastSpace;
$commit['parents'] = [];
collect(explode(' ', $commit['parents_as_string']))->each(function ($parent) use (&$commit, &$spaces, &$lastSpace) {
$commit['parents'][] = [$parent, $lastSpace];
if (!in_array($parent, $spaces))
$spaces[$parent] = $lastSpace;
else
$spaces[$parent] = $lastSpace - $commit['space'] + 1;
$lastSpace = $lastSpace + 2;
});
unset($commit['parents_as_string']);
$commit['author']['icon'] = 'https://secure.gravatar.com/avatar/' . md5($commit['author']['email']) . '?s=40&d=wavatar';
$commit['date'] = Str::replace('+', '.000+', $commit['date']);
$commit['time'] = $time;
$time++;
$date = strtotime($commit['date']);
$data['days'][] = [(int)date("d", $date), date("M", $date), (int)date("Y", $date)];
return $commit;
});
Storage::disk('public')->put('nova-git-data.json', json_encode($data));
$url = Storage::disk('public')->url('nova-git-data.json');
return response()->json([
'data_url' => $url,
'commit_url' => config('nova-git-manager.commit_url')
]);
}
public function pull(Request $request)
{
if (!$request->has('branch'))
return response()->json([
'success' => false,
'message' => 'Branch not selected'
]);
$branch = Str::replace('/', ' ', $request->input('branch'));
$output = $this->execGitCommand('pull ' . $branch);
return response()->json([
'success' => !Str::contains($output, 'fatal'),
'message' => $output
]);
}
public function checkout(Request $request)
{
if (!$request->has('branch'))
return response()->json([
'success' => false,
'message' => 'Branch not selected'
]);
$branch = $request->input('branch');
$output = $this->execGitCommand('checkout ' . $branch);
return response()->json([
'success' => !Str::contains($output, 'fatal'),
'message' => $output
]);
}
}