Skip to content

Instantly share code, notes, and snippets.

@michael-e
Created December 15, 2011 16:44
Show Gist options
  • Save michael-e/1481796 to your computer and use it in GitHub Desktop.
Save michael-e/1481796 to your computer and use it in GitHub Desktop.
Use Symphony and Extensions as subtrees in your own project git repository.

#Symphony project builder These scripts allow you to use Symphony and Symphony extensions as subtrees to your own project.

###What is this:

There are 2 files:

  • SymBuilder.sh
  • AddExtension.sh

Symbuilder creates a new repository (your project) and adds Symphony-cms as a subtree and merges it into a web root (like public_html)

From now on the symphony repository is a real part of your repository. (not a submodule). You can't push back to the original repo but you can pull from it! So when the Symphony repository is updates, you can pull those changes into your own repo.

Symbuilder copies AddExtension.sh into your project folder. This script allows you to add extensions to your project in the same way we just added Symphony.

###How does it work:

#####This is what SymBuilder.sh does:

./SymBuilder.sh [project name] [webroot]

  1. This creates the folder [project name] and runs git init in it. This is your project repository.
  2. It adds some files (.gitignore, .symbuilder, AddExtensions.sh) and commits those.
  3. Then it Adds a remote called symphony-2 from git://github.com/symphonycms/symphony-2.git
  4. Now it merges the symphony-2 / master branch using the "ours" strategy without committing(!)
  5. Now we read the symphony-2 / master into [webroot] and update the files here.
  6. Now we commit this merge to our project repository

The remote name, url and branch are configurable in the script. Default is:

SYM_REMOTE_NAME="symphony-2"
SYM_REMOTE_BRANCH="master"
SYM_GIT_URL="git://github.com/symphonycms/symphony-2.git"

#####This is what AddExtension.sh does:

./AddExtension.sh [extension name] [git url] [branch]

  1. It Adds a remote called [extension name] from [git url]
  2. Now it merges the [extension name]/[branch] using the "ours" strategy without committing(!)
  3. Now we read the [extension name]/[branch] into [webroot]/extensions/[extension name] and update the files here.
  4. Now we commit this merge to our project repository

#####This is what is missing: Submodules. Merging the remote into a path loses the correct submodule tracking… this is annoying, trying to work around this. For the time being copy the .gitmodules to your repo root and edit the paths:

[submodule "[webroot]/extensions/export_ensemble"]
	path = [webroot]/extensions/export_ensemble
	url = git://github.com/symphonycms/export_ensemble.git
[submodule "[webroot]/extensions/markdown"]
	path = [webroot]/extensions/markdown
	url = git://github.com/symphonycms/markdown.git
[submodule "[webroot]/extensions/maintenance_mode"]
	path = [webroot]/extensions/maintenance_mode
	url = git://github.com/symphonycms/maintenance_mode.git
[submodule "[webroot]/extensions/selectbox_link_field"]
	path = [webroot]/extensions/selectbox_link_field
	url = git://github.com/symphonycms/selectbox_link_field.git
[submodule "[webroot]/extensions/jit_image_manipulation"]
	path = [webroot]/extensions/jit_image_manipulation
	url = git://github.com/symphonycms/jit_image_manipulation.git
[submodule "[webroot]/extensions/profiledevkit"]
	path = [webroot]/extensions/profiledevkit
	url = git://github.com/symphonycms/profiledevkit.git
[submodule "[webroot]/extensions/debugdevkit"]
	path = [webroot]/extensions/debugdevkit
	url = git://github.com/symphonycms/debugdevkit.git
[submodule "[webroot]/extensions/xssfilter"]
	path = [webroot]/extensions/xssfilter
	url = git://github.com/symphonycms/xssfilter.git

This script is based on this Github Help article

#!/usr/bin/env bash
DEFAULT_BRANCH="master"
. .symbuilder
extensions=$webroot"/extensions/"
if [ $# -eq 1 ]
then
# get a name for new project from command line arguments
extension_name="$1"
fi
if [ $# -eq 2 ]
then
# get a webroot from command line arguments
extension_name="$1"
git_url="$2"
fi
if [ $# -eq 3 ]
then
# get a webroot from command line arguments
extension_name="$1"
git_url="$2"
git_branch="$3"
fi
while [[ -z $extension_name ]]
do
echo "Enter the extension name (ie pagesfield):"
read extension_name || exit
done
while [[ -z $git_url ]]
do
echo "Enter the github url (ie git://github.com/symphonists/pagesfield.git):"
read git_url || exit
done
while [[ -z $git_branch ]]
do
# ask branch
read -p "Enter the branch [$DEFAULT_BRANCH]: " input
git_branch="${input:-$DEFAULT_BRANCH}"
done
echo "Adding extension $extension_name from url $git_url"
git remote add -f $extension_name $git_url
git merge -s ours --no-commit $extension_name/$git_branch
git read-tree --prefix=$extensions$extension_name/ -u $extension_name/$git_branch
git commit -m "$extension_name merged in $extensions"
#success message
echo ""
echo ""
echo "******************************************"
echo "Extension $extension_name merged into $extensions"
echo ""
echo "To update $extension_name run: git pull -s subtree $extension_name $git_branch"
echo "******************************************"
echo "done."
#!/usr/bin/env bash
DEFAULT_WEBROOT="public_html"
SYM_REMOTE_NAME="symphony-2"
SYM_REMOTE_BRANCH="master"
SYM_GIT_URL="git://github.com/symphonycms/symphony-2.git"
# This lists the unmerged submodules
module_list()
{
git ls-files --error-unmatch --stage -- "$@" |
perl -e '
my %unmerged = ();
my ($null_sha1) = ("0" x 40);
while (<STDIN>) {
chomp;
my ($mode, $sha1, $stage, $path) =
/^([0-7]+) ([0-9a-f]{40}) ([0-3])\t(.*)$/;
next unless $mode eq "160000";
if ($stage ne "0") {
if (!$unmerged{$path}++) {
print "$mode $null_sha1 U\t$path\n";
}
next;
}
print "$_\n";
}
'
}
# This gets the submodule's name from the .gitignore file in your webroot
module_name()
{
# Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
name=$( git config -f $2/.gitmodules --get-regexp '^submodule\..*\.path$' |
sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
test -z "$name" &&
die "$(eval_gettext "No submodule mapping found in .gitmodules for path '\$path'")"
echo "$name"
}
# Re-Adds the extension as a subtree
add_extension()
{
full_path=$1
fake_path=$2
stupid_name=$3
git_url=$4
new_remote=$5
rm -rf $path
echo "### Adding $new_remote"
git remote add -f $new_remote $git_url
git merge -s ours --no-commit $new_remote/master
git read-tree --prefix=$full_path/ -u $new_remote/master
git commit -m "$new_remote merged in $full_path"
echo "### ADDED $new_remote to $full_path (from: $git_url)"
}
if [ $# -eq 1 ]
then
# get a name for new project from command line arguments
name="$1"
fi
if [ $# -eq 2 ]
then
# get a webroot from command line arguments
name="$1"
webroot="$2"
fi
while [[ -z $name ]]
do
# ask for project name
echo "Enter the website name:"
read name || exit
done
while [[ -z $webroot ]]
do
# ask for webroot
read -p "Enter the webroot name [$DEFAULT_WEBROOT]: " input
webroot="${input:-$DEFAULT_WEBROOT}"
done
if [[ -d $name ]]
then
echo "$name exists, quitting"
else
#Create project
mkdir -p -- "$name" || exit 1
echo ""
echo "### Created Directory: $name"
echo ""
cp AddExtension.sh $name
cd -- "$name"
touch .symbuilder
echo "name=\"$name\"" >> .symbuilder
echo "webroot=\"$webroot\"" >> .symbuilder
git init
touch .gitignore
git add .gitignore
git add .symbuilder
git add AddExtension.sh
git commit -m "initial commit"
git remote add -f $SYM_REMOTE_NAME $SYM_GIT_URL
git merge -s ours --no-commit $SYM_REMOTE_NAME/$SYM_REMOTE_BRANCH
git read-tree --prefix=$webroot/ -u $SYM_REMOTE_NAME/$SYM_REMOTE_BRANCH
git commit -m "Symphony-CMS merged in $webroot"
echo "### Merging symphony submodules as subtrees (Fingers crossed!)"
module_list |
while read mode sha1 stage path
do
full_path=$path
fake_path=${path[*]/$webroot\//}
stupid_name=$(module_name "$fake_path" "$webroot")
git_url=$(git config -f $webroot/.gitmodules submodule."$stupid_name".url)
new_remote=$(echo $git_url | cut -d. -f2 | cut -d/ -f2- | cut -d/ -f2)
add_extension $full_path $fake_path $stupid_name $git_url $new_remote
done
#success message
echo ""
echo ""
echo "******************************************"
echo "Created Project: $name"
echo "Symphony is merged into $name/$webroot"
echo ""
echo "To add extensions (for example, pagesfield), cd in $name and run:"
echo ""
echo "git remote add -f pagesfield git://github.com/symphonists/pagesfield.git"
echo "git merge -s ours --no-commit pagesfield/master"
echo "git read-tree --prefix=$webroot/extensions/pagesfield/ -u pagesfield/master"
echo "git commit -m \"Pagesfield merged in extensions\""
echo ""
echo "Or use the AddExtension.sh script that is copied into $name:"
echo ""
echo "./AddExtension.sh pagesfield git://github.com/symphonists/pagesfield.git master"
echo ""
echo "To update Symphony run: git pull -s subtree $SYM_REMOTE_NAME $SYM_REMOTE_BRANCH"
echo "To update extensions run: git pull -s subtree [extension] [branch]"
echo "******************************************"
echo "done."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment