Commit your uploaded SilverStripe asset files immediately to a VCS repository.
So, your SilverStripe assets
directory has different content in your production server and in your development machine and you want to use a VCS to sync them? Or you just want to backup assets
in a VCS as a crash plan? This module is designed just for that: it automatically commits uploaded/deleted/renamed files in the assets
directory by listening to the File
class's hooks. It can also push to a remote repository. It does not pull/merge from a remote repository, though. That's something that should be done manually.
You can use any version control system you want as long as it's git.
Will it support any other VCS's in the future? No plans, but if it will, it will require other developers' effort (pull requests welcome), as git is the only one I'm currently familiar with. But the module is designed in a way that should make it quite easy to introduce other VC systems too.
Install the module: composer require taitava/silverstripe-assetcommitter
Initialise a git repository in your assets
directory if you haven't done so already: cd assets && git init
(I highly recommend you to use a dedicated repo for your assets - don't mix it in the same repo where your storing your project's source code files). Also run git config user.email "[email protected]" && git config --global user.name "Default User"
. This is to provide a default author name and email for commits where the module fails to determine the author
. The module will override the author
with the logged in user's email and name (by calling git commit
with the --author
parameter), but if no one is logged in, the default author
will be used.
Make sure that your webserver has a permission to write to assets/.git
!
Final fine-tuning via YAML configuration:
GitAssetCommitter:
repository_path: 'assets' # This should point to the 'assets' directory (which should be your repository's root directory.)
push_to_after_committing: false # If you want commit's to be pushed to a remote repository, set this to a string like "origin master" or just "origin", otherwise set this to false.
automatically_define_author: true # If true, the currently logged in CMS user's email and name will be used as an author of commits. If nobody is logged in, the default author of the repository will be used.
supplement_empty_author_email: '[email protected]' # If a logged in user does not have an email address, use this instead. Has no effect if automatically_define_author is false.
supplement_empty_author_name: 'CMS User' # If a logged in user does not have a name, use this instead. Has no effect if automatically_define_author is false.
commit_file_creations: true
commit_file_deletions: true
commit_file_renamings: true # Also affects movings
AssetCommitterFactory:
committer_class: GitAssetCommitter # If you want to write a custom class that should handle committing, define the fully qualified name of the class here. Most of the time you will not want to change this value. Note that you may need to copy the above configuration values and apply them to your new class in your YAML config file!
?flush=all
in your browser to clear cache!There are currently two hooks you can use to customise the behaviour of this module:
updateGitCommitMessage()
updateGitCommitAuthor()
To use those hooks, you need to create two files. Create a new file (or append to if it exists) to mysite/_config/assetcommitter.yml
:
GitAssetCommitter:
extensions:
- MyGitAssetCommitterExtension
And then create a new PHP class to mysite/code/MyGitAssetCommitterExtension.php
:
class MyGitAssetCommitterExtension extends Extension
{
/**
* @param string $action Either 'create', 'replace', 'rename', or 'delete'. Tells what we are doing. Not meant to be modified in the hook method.
* @param string $commit_message The default message generated by `assetcommitter`. Set your new message to this variable.
*/
public function updateGitCommitMessage($action, &$commit_message)
{
$commit_message = 'My superior commit message must be present in every commit! And here is the type of the committed event: '.$action;
}
/**
* @param string $action Either 'create', 'replace', 'rename', or 'delete'. Tells what we are doing. Not meant to be modified in the hook method.
* @param string $author The default author generated by `assetcommitter`. Set your new author name and email to this variable in this format: "Firstname Surname <[email protected]>". If you wish to unset the author completely, set this to null.
*/
public function updateGitCommitAuthor($action, &$author)
{
$author = 'It\'s me! ALWAYS ME! <[email protected]>';
}
}
And finally remember to run ?flush=all
in your browser to clear cache!
This module is very simple and still under construction. Actually: I don't even have a real world project for this where I would use this. At least not at the moment. I made this just out of curiosity :). What it means is that you should be ware that this module is not yet tested in any real website/application and therefore it might contain bugs that have just not been noticed because of lack of usage/testing.
Now that the above thing is covered, let's check what this module does do and does not do:
What it does:
File
class of SilverStripe. It doesn't scan the filesystem for changes. So old, already existing files will not get committed.What it doesn't do:
cd assets && git init
and also configure a default author.Important notes:
assets
folder that is separate from your project's source code. Three reasons: 1) If the module screws up the repository, I don't want to meet angry coders at my front door telling me how many kilometers of beloved code they have lost. 2) Automatic pushing will not work if the remote repository contains commits not present in the local repository (unless you use separate branches). 3) You need to give your webserver user (i.e. www-data
or apache
) write access to the .git
directory in your repository directory.Note that the three dots are just a substitute for a more specific error message that you will get if you run into this problem.
You simply might be missing write permissions to the .git directory of your repository. Make sure that your webserver has permission to write to that directory.
But this is not always the reason for this error message. Unfortunately czproject/git-php does not report original error messages yelled by git (at least that's the case for czproject/git-php version 3.17.1). For further reference, please see czproject/git-php issue #47
I've customised the library (by subclassing GitRepository
class and GitException
exception) to add more information to GitException
. You should be able to see a "Command output:" part in the exception's message
, which contains descriptive error messages from git commands.
I've also made a pull request to bring my improvisations to the original library. If it gets merged, I will remove my customisations from this module.
This section is copied on 2020-01-14 from [czproject/git-php README.md](https://github.com/czproject/git-php/blob/b5e709f0e9c4e4e39b49d4e322f7fa73c1bb21dc/readme.md).
> 1. use SSH instead of HTTPS - https://stackoverflow.com/a/8588786
> 2. store credentials to Git Credential Storage
> http://www.tilcode.com/push-github-without-entering-username-password-windows-git-bash/
> https://help.github.com/articles/caching-your-github-password-in-git/
> https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage
> 3. insert user and password into remote URL - https://stackoverflow.com/a/16381160
> git remote add origin https://user:[email protected]/path/repo.git
> 4. NOT IMPLEMENTED IN THIS MODULE: for push() you can use --repo argument - https://stackoverflow.com/a/12193555
> $git->push(NULL, array('--repo' => 'https://user:[email protected]/path/repo.git'));
assets
as the repository root directory. See issue #4.BuildTask
that could be ran to init a git repository. This would also setup a default author for the repository.BuildTask
that could be ran to check that the git repository can be properly accessed (exists and is writable).Jarkko Linnanvirta
jarkko (at) taitavasti (dot) fi (in English or in Finnish)
Module rating system helping users find modules that are well supported. For more on how the rating system works visit Module standards
Score not correct? Let us know there is a problem