Skip to content

Commit e55bca2

Browse files
authored
Merge pull request #12 from hlipnick/git-branch-display
Add git branch name to badge display
2 parents 928cceb + 2d3f346 commit e55bca2

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

readme.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,20 @@ $panel->plugins([
9191
]);
9292
```
9393

94+
### Git Branch
95+
96+
You can enable the display of the current git branch in the badge via `->showGitBranch()`. This requires the `exec()` function to be enabled in your PHP configuration.
97+
98+
```php
99+
use pxlrbt\FilamentEnvironmentIndicator\EnvironmentIndicatorPlugin;
100+
use Filament\Support\Colors\Color;
101+
102+
$panel->plugins([
103+
EnvironmentIndicatorPlugin::make()
104+
->showGitBranch()
105+
]);
106+
```
107+
94108
## Contributing
95109

96110
If you want to contribute to this packages, you may want to test it in a real Filament project:

resources/views/badge.blade.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@ class="
1313
"
1414
>
1515
{{ $environment }}
16+
17+
@isset($branch)
18+
<code class="text-xs ml-1" style="margin-top: 1px">({{ $branch }})</code>
19+
@endisset
1620
</div>

src/EnvironmentIndicatorPlugin.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Filament\Support\Concerns\EvaluatesClosures;
1010
use Illuminate\Support\Facades\View;
1111
use Illuminate\Support\HtmlString;
12+
use Throwable;
1213

1314
class EnvironmentIndicatorPlugin implements Plugin
1415
{
@@ -22,6 +23,8 @@ class EnvironmentIndicatorPlugin implements Plugin
2223

2324
public array|Closure|null $color = null;
2425

26+
public bool|Closure|null $showGitBranch = null;
27+
2528
public static function make(): static
2629
{
2730
$plugin = app(static::class);
@@ -83,6 +86,7 @@ public function register(Panel $panel): void
8386
return View::make('filament-environment-indicator::badge', [
8487
'color' => $this->getColor(),
8588
'environment' => ucfirst(app()->environment()),
89+
'branch' => $this->getGitBranch()
8690
]);
8791
});
8892

@@ -131,6 +135,13 @@ public function showBorder(bool|Closure $showBorder = true): static
131135
return $this;
132136
}
133137

138+
public function showGitBranch(bool|Closure $showGitBranch = true): static
139+
{
140+
$this->showGitBranch = $showGitBranch;
141+
142+
return $this;
143+
}
144+
134145
public function color(array|Closure $color = Color::Pink): static
135146
{
136147
$this->color = $color;
@@ -142,4 +153,17 @@ protected function getColor(): array
142153
{
143154
return $this->evaluate($this->color);
144155
}
156+
157+
protected function getGitBranch(): ?string
158+
{
159+
if (! $this->evaluate($this->showGitBranch)) {
160+
return null;
161+
}
162+
163+
try {
164+
return trim(exec('git branch --show-current'));
165+
} catch (Throwable $th) {
166+
return null;
167+
}
168+
}
145169
}

0 commit comments

Comments
 (0)