Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Git repositories for contrib modules: Difference between revisions

From MoodleDocs
(Created page with "{{draft}} The instructions here assume that you have a working knowledge of git. Managing contrib plugin with git makes it easy to make local modifications and pull in updates....")
 
 
(5 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{draft}}
The instructions here assume that you have a working knowledge of git.
The instructions here assume that you have a working knowledge of git.


Managing contrib plugin with git makes it easy to make local modifications and pull in updates.
Managing contrib plugin with git makes it easy to make local modifications and pull in updates.
There are several options for managing a contrib plugin with git:
 
# Add the module to your main moodle git repository
The best option for managing a contrib plugin with git is to create a self-contained repository for the plugin in a subdirectory of Moodle installation.
# Create a self-contained repository for the plugin in a subdirectory
# Create a git submodule, which creates a repository for the plugin but tracks updates in the main repository too. It also allows you to clone the plugins with the main repository
Having used all of the above, the method I'd recommend is number 2, as it's most convenient. The main disadvantage of 1 is that you can't easily pull in updates, and the main disadvantage of 3 is that you have to make each commit to the module twice (once in the submodule, and once in the main repo).


== Creating a new plugin ==
== Creating a new plugin ==
If you intend to publish the plugin, I highly recommend creating a [http://github.org github] repository from the start. This will allow you to publish with a single command when you're ready.  Just go to the site, sign up for an account, create a new repository. The naming convention for Moodle plugin repositories is <tt>moodle-plugintype_pluginname</tt>.
If you intend to publish the plugin, it is highly recommended to create a [http://github.org github] repository from the start. This will allow you to publish with a single command when you're ready.  Just go to the site, sign up for an account, create a new repository. The naming convention for Moodle plugin repositories is <tt>moodle-plugintype_pluginname</tt>.
 
Github will give you instructions for cloning your (blank) repository. After that in your local moodle installation navigate to the directory for the plugin type you're developing (e.g. /block, /mod, /local, admin/tool) and run the <tt>git clone</tt> command there.
 
('''Note''': in order to run <tt>git clone</tt> from the command line, you'll need to configure an SSH key and install it in your github account. See https://help.github.com/en/articles/checking-for-existing-ssh-keys for details.)
 
For example for the block plugin called "mysuperblock":
 
  cd blocks/
  git clone git@github.com:yourname/moodle-block_mysuperblock.git mysuperblock


Github will give you instructions for cloning your (blank) repository. If you're using option 2 above, navigate to the directory for the plugin type you're developing (e.g. /block, /mod, /local) and run the <tt>git clone</tt> command there. This will create a subdirectory called <tt>moodle-plugintype_pluginname</tt>, rename this to <tt>pluginname</tt> and you're good to go. <tt>cd</tt> into this directory to perform git commands on this plugin's repository - outside of this directory will perform them on the main Moodle repository.
This will create a subdirectory called <tt>mysuperblock</tt>. <tt>cd</tt> into this directory to perform git commands on this plugin's repository - outside of this directory will perform them on the main Moodle repository. To ignore this folder from Moodle git repository you can add the path to <tt>.git/info/exclude</tt>. Do not add paths to <tt>.gitignore</tt> file because it is also part of moodle repository checkout.


You can then develop and test your changes locally, and when you're ready to publish, run
Inside the plugin directory you can then develop and test your changes locally, and when you're ready to publish, run
  git push origin
  git push origin


Line 37: Line 41:
When installing updates, you'll need to copy the new files in place of the old one, and re run the <tt>git add</tt> and <tt>git commit</tt> commands.
When installing updates, you'll need to copy the new files in place of the old one, and re run the <tt>git add</tt> and <tt>git commit</tt> commands.


== Moving a plugin to it's own repository ==
== Moving a plugin to its own repository ==
You may have developed a plugin within your main Moodle repository, but want to move it to a separate one to make it easier to publish.  Using <tt>git filter-branch</tt>, we can achieve this and maintain all existing history for the plugin's files.
You may have developed a plugin within your main Moodle repository, but want to move it to a separate one to make it easier to publish.  Using <tt>git filter-branch</tt>, we can achieve this and maintain all existing history for the plugin's files.


Line 59: Line 63:
This will clone the block's files, with the full history, into their own repository in the myblock subdirectory.
This will clone the block's files, with the full history, into their own repository in the myblock subdirectory.
You can now delete ~/moodleclone/moodle, and repeat with any other plugins you've developed.
You can now delete ~/moodleclone/moodle, and repeat with any other plugins you've developed.
== See also ==
* [[:en:Git for Administrators|Git for administrators]]
* [[Git for developers]]
; Moodle forum discussions
* [http://moodle.org/mod/forum/discuss.php?d=168094 GIT help needed]
* [http://moodle.org/mod/forum/discuss.php?d=165236 Best way to manage CONTRIB code with GIT]
* [http://moodle.org/mod/forum/discuss.php?d=167063 Handy Git tip for tracking 3rd-party modules and plugins]
* [http://moodle.org/mod/forum/discuss.php?d=167730 Moodle Git repositories]
; External resources
* [http://www.kernel.org/pub/software/scm/git/docs/everyday.html Everyday GIT With 20 Commands Or So]
* [http://gitref.org/ Git Reference]
* [http://progit.org/book/ Pro Git book]


[[Category:Git]]
[[Category:Git]]

Latest revision as of 23:44, 5 May 2019

The instructions here assume that you have a working knowledge of git.

Managing contrib plugin with git makes it easy to make local modifications and pull in updates.

The best option for managing a contrib plugin with git is to create a self-contained repository for the plugin in a subdirectory of Moodle installation.

Creating a new plugin

If you intend to publish the plugin, it is highly recommended to create a github repository from the start. This will allow you to publish with a single command when you're ready. Just go to the site, sign up for an account, create a new repository. The naming convention for Moodle plugin repositories is moodle-plugintype_pluginname.

Github will give you instructions for cloning your (blank) repository. After that in your local moodle installation navigate to the directory for the plugin type you're developing (e.g. /block, /mod, /local, admin/tool) and run the git clone command there.

(Note: in order to run git clone from the command line, you'll need to configure an SSH key and install it in your github account. See https://help.github.com/en/articles/checking-for-existing-ssh-keys for details.)

For example for the block plugin called "mysuperblock":

 cd blocks/
 git clone git@github.com:yourname/moodle-block_mysuperblock.git mysuperblock

This will create a subdirectory called mysuperblock. cd into this directory to perform git commands on this plugin's repository - outside of this directory will perform them on the main Moodle repository. To ignore this folder from Moodle git repository you can add the path to .git/info/exclude. Do not add paths to .gitignore file because it is also part of moodle repository checkout.

Inside the plugin directory you can then develop and test your changes locally, and when you're ready to publish, run

git push origin

Installing a third-party plugin

Installing a plugin developed by a third party differs depending on whether they have used git or not. If they have used git, you can follow the instructions above, but skip out the part where you create a new github repository and clone their own github (or other public repository) instead.

If they haven't used git, you can still use git (and even github, if you like) to track local modifications to the plugin. Simply create a directory for the plugin

mkdir ~/moodle/blocks/newblock

Initialise a new git repository there

cd ~/moodle/blocks/newblock
git init

unzip or copy the files to the repository

cd ~
tar -xf newblock.tar.gz
cp newblock/* ~/moodle/blocks/newblock

add and commit the files

cd ~/moodle/blocks/newblock
git add .
git commit . -m "Added files for newblock"

When installing updates, you'll need to copy the new files in place of the old one, and re run the git add and git commit commands.

Moving a plugin to its own repository

You may have developed a plugin within your main Moodle repository, but want to move it to a separate one to make it easier to publish. Using git filter-branch, we can achieve this and maintain all existing history for the plugin's files.

First, you'll need a fresh copy of moodle (this will become your new moodle repository)

mkdir ~/newmoodle
cd ~/newmoodle
git clone git://git.moodle.org/moodle.git

This will create a clone of Moodle without your plugins in ~/newmoodle/moodle Next, you need to clone your local Moodle repository (why will become clear).

mkdir ~/moodleclone
cd ~/moodleclone
git clone ~/moodle

This will create a clone of your current moodle development repository, with your plugins, in ~/moodleclone/moodle Now, we'll use the git filter-branch command to reduce this clone to just the plugin.

cd ~/moodleclone/moodle
git filter-branch --subdirectory-filter blocks/myblock/

The files from blocks/myblock/ will now be at the root of the repository. All other files will have been removed (this is why we're using a clone). You can now track the plugin in it's own repository by cloning this reduced repository to a subdirectory of the new moodle clone.

cd ~/newmoodle/blocks
git clone ~/moodleclone/moodle myblock

This will clone the block's files, with the full history, into their own repository in the myblock subdirectory. You can now delete ~/moodleclone/moodle, and repeat with any other plugins you've developed.

See also

Moodle forum discussions
External resources