Skip to content

Commit 8ac6807

Browse files
author
pascalmh
committed
refactor: use PHP git library https://github.com/kbjr/Git.php
1 parent b0c98bf commit 8ac6807

File tree

4 files changed

+68
-24
lines changed

4 files changed

+68
-24
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "Git.php"]
2+
path = Git.php
3+
url = https://github.com/kbjr/Git.php.git

Git.php

Submodule Git.php added at 0d1a996

helpers.php

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,53 @@
11
<?php
2+
3+
require_once('Git.php/Git.php');
4+
25
/**
3-
* Compose Commit message, appends " by Username"
4-
*
5-
* @param string $commitMessage
6-
* @return false
7-
*/
6+
* Compose Commit message, appends " by Username"
7+
*
8+
* @param string $commitMessage
9+
* @return false
10+
*/
811
function gitCommit($commitMessage) {
9-
$branch = c::get('gcapc-branch') || 'master';
10-
$pull = c::get('gcapc-pull') || true;
11-
$push = c::get('gcapc-push') || true;
12-
$commit = c::get('gcapc-commit') || true;
13-
$pushCommand = c::get('gcapc-pushCommand') || 'git push';
14-
15-
exec(
16-
'cd ../content/' .
17-
' && git checkout -b ' . $branch . '' .
18-
$pull ? ' && git pull' : '' .
19-
$commit ? ' && git add -A' : '' .
20-
$commit ? ' && git commit -m "' . $commitMessage . ' by ' . site()->user() . '"' : '' .
21-
$push ? ' && ' . $pushCommand : ''
22-
);
12+
$branch = c::get('gcapc-branch', 'master');
13+
$pull = c::get('gcapc-pull', true);
14+
$push = c::get('gcapc-push', true);
15+
$commit = c::get('gcapc-commit', true);
16+
$gitBin = c::get('gcapc-gitBin', '');
17+
$windowsMode = c::get('gcapc-windowsMode', false);
18+
19+
/*
20+
* Setup git environment
21+
*/
22+
if($windowsMode) {
23+
Git::windows_mode();
24+
}
25+
if ($gitBin) {
26+
Git::set_bin('/usr/local/bin/git');
27+
}
28+
29+
$repo = Git::open('../content');
30+
31+
// We are in the panel-Folder - go into content-Folder
32+
chdir('../content');
33+
34+
/*
35+
* Git Pull, Commit and Push
36+
*/
37+
if ($pull) {
38+
$repo->checkout($branch);
39+
$repo->pull('origin', $branch);
40+
}
41+
if ($commit) {
42+
$repo->add('.');
43+
$repo->commit($commitMessage . ' by ' . site()->user());
44+
}
45+
if ($push) {
46+
$repo->push('origin', $branch);
47+
}
48+
49+
// Go back into panel-Folder
50+
chdir('../panel');
2351

2452
return false;
2553
}

readme.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ git push origin master
3434

3535
### Download and configure the Plugin
3636

37-
Go into your `site/plugins/` folder and `git clone https://github.com/Pascalmh/kirby-git-commit-and-push-content git-commit-and-push-content`.
37+
Go into your `site/plugins/` folder and
38+
```
39+
git submodule add --name git-commit-and-push-content https://github.com/Pascalmh/kirby-git-commit-and-push-content.git site/plugins/git-commit-and-push-content
40+
```
3841

3942
Or: Put all the files into your `site/plugins/git-commit-and-push-content/` folder. If the `git-commit-and-push-content/` plugin folder doesn't exist then create it.
4043

@@ -44,7 +47,7 @@ You can use the following [Options](http://getkirby.com/docs/advanced/options) -
4447

4548
#### gcapc-branch
4649
Type: `String`
47-
Default value: `master`
50+
Default value: `'master'`
4851

4952
branch name to be checked out
5053

@@ -66,11 +69,20 @@ Default value: `true`
6669

6770
Push your changes to remote?
6871

69-
#### gcapc-pushCommand
72+
#### gcapc-gitBin
7073
Type: `String`
71-
Default value: `git push`
74+
Default value: `''`
75+
76+
Sets the location where git can be found
77+
78+
[See Git.php](http://kbjr.github.io/Git.php/) `void Git::set_bin ( string $path )`
79+
80+
81+
#### gcapc-windowsMode
82+
Type: `Boolean`
83+
Default value: `false`
7284

73-
[git push](http://git-scm.com/docs/git-push)
85+
[See Git.php](http://kbjr.github.io/Git.php/) `void Git::windows_mode ( void )`
7486

7587
## Author
7688

0 commit comments

Comments
 (0)