Git use of --work-tree and --git-dir on commands - php

So, I have a php script that integrates with BitBucket's API and make changes to some local/shared repositories.
To do that, I use --git-dir and --work-tree [when applicable] together with every command, as in:
/usr/bin/git --git-dir=/var/www/staging.repo/.git --work-tree=/var/www/staging.repo merge "origin/master" 2>&1; echo $?
I use git version 1.7.0.4 on my development environment, and git version 1.5.6.5 on the live server. The reason for it is that there's no newer version of git for Debian Lenny (5), so I'm stuck with the older one.
The problem is, some of the commands that correctly accept --work-tree and --git-dir on git v1.7 doesn't seem to care much about it on the older version. One example is:
/usr/bin/git --git-dir=/var/www/staging.repo/.git --work-tree=/var/www/staging.repo merge "origin/master" 2>&1; echo $?
That outputs:
fatal: /usr/bin/git-merge cannot be used without a working tree.
So, how can I make broad use of those parameters using git 1.5.6.5?
Solution:
Downloaded git source:
myself:/some/folder$ curl -O http://git-core.googlecode.com/files/git-1.7.7.tar.gz
Installed:
myself:/some/folder/git-1.7.7.tar.gz$ ./configure
myself:/some/folder/git-1.7.7.tar.gz$ make
myself:/some/folder/git-1.7.7.tar.gz$ sudo make install
Done! :)
Ref: http://www.mikepilat.com/blog/2011/06/how-to-build-git-from-source-on-ubuntu/

Please compile the latest git.
Yes, some commands don't observe those parameters. You can try and set their equivalent environment variables instead.

Related

Is there a way to hide "funding" messages when running composer commands?

When using Composer, sometimes messages are displayed after installing or updating:
X packages you are using are looking for funding.
Use the `composer fund` command to find out more!
I want to know if there's a solution similar to this answer for npm, but for Composer.
Is there a way to hide the messages about projects needing funding? I checked the output of composer --help and didn't see any obvious flags.
There is no specific flag to target those two lines.
You can always use --quiet to get rid of all output, and have a completely silent run.
If for some reason you are particularly bothered by those two lines, but do not want to lose the rest of the output, you could always pipe stderr through grep and exclude those lines:
composer update 2> >(grep -v "composer fund" | grep -v "looking for funding")
Which results in:
Notice in the screenshot above the conspicuous lack of any reference to funding.
If all this is worth doing or not, I'll leave up to you.
Spam! In your terminal! Worse when "the good guys" do it!
But this is open source, so let's fix it.
You'll need to already have Composer installed for this (you need Composer to compile Composer like this).
You'll also need jq.
All together:
sudo apt install jq
cd "$(mktemp -d)"
ver=$(curl -s 'https://getcomposer.org/versions' | jq -r '.stable[0].version')
git clone https://github.com/composer/composer.git .
git checkout ${ver}
unset ver
sed -Ei 's/^(\s+if\s?\()\$fundingCount(\) \{)$/\1FALSE\2/g' ./src/Composer/Installer.php
composer install
composer compile
composer_location=$(which composer)
if [[ -f "${composer_location}" ]]; then
\cp -f composer.phar "${composer_location}"
chmod u+x "${composer_location}"
fi
unset composer_location
Separately:
Install jq:
sudo apt install jq
Make a temporary folder and change directory to it:
cd "$(mktemp -d)"
Get the version number of the latest stable Composer and store it in the ver variable:
ver=$(curl -s 'https://getcomposer.org/versions' | jq -r '.stable[0].version')
Clone the Composer git repository to this temporary directory and check out the code at the latest stable version of Composer:
git clone https://github.com/composer/composer.git .
git checkout ${ver}
Clean up after ourselves, unsetting the ver variable which we don't plan to use again.
unset ver
Replace if ($fundingCount) { with if (FALSE) { in src/Composer/Installer.php:
sed -Ei 's/^(\s+if\s?\()\$fundingCount(\) \{)$/\1FALSE\2/g' ./src/Composer/Installer.php
Obtain the dependencies for compiling Composer, but using Composer (which is why you need Composer installed first). I mean, you can do this manually, but heck, why.
composer install
Compose a new composer.phar with this current, altered code base:
composer compile
Store the current location of teh Composer binary in a variable.
composer_location=$(which composer)
Just in case you aliased the composer command, in which case that wouldn't have saves a file name's location, we check if it is a file and then proceed to replace it with our new one and make our new one executable by you, the user.
if [[ -f "${composer_location}" ]]; then
\cp -f composer.phar "${composer_location}"
chmod u+x "${composer_location}"
fi
That backslash before the cp is also an alias buster. Often people alias cp to cp -i and we just want this to work right now.
Finally just unset the composer_location variable to be neat.
If you follow the regex in that sed line, great, if not, it is best to skip that line and manually apply the change so that you know what is happening on your own device, vim src/Composer/Installer.php then replace if ($fundingCount) { with if (FALSE) {.
Off course this means you are running an unsigned copy of composer (with the alteration being your own). But since they breached your trust already who cares about thát "trust" chain.
Also, if you run composer self-update it will replace your Composer with an unpatched one again and you will have to follow these steps again. Since they breached your trust (yes again) best to update manually like this anyway (just follow these steps again and you will update too), I just put it in Ansible for all our company's developers' desktops.

How to run bundle from PHP script

I'm writing a webhook to automatically publish a site when I push to GitHub. Part of the process requires that I build the site with
bundle exec middleman build --clean
I'm trying to invoke that with a PHP script, the script called by the GitHub webhook, so the user is www-data. No matter what I try, however, I'm getting an error that bundle cannot be found.
How can I run a bundle command from a PHP script?
I was able to figure this out. First, I installed rvm as a multi-user installation to ensure the www-data account can access it.
$ curl -sSL https://get.rvm.io | sudo bash -s stable
Install the desired ruby version, in my case 2.3.1, then set rvm to use it:
$ rvm install 2.3.1
$ rvm use 2.3.1
Run gem to install any gems that are needed. Because rvm is a multi-user installation, these gems are stored to the system and not your specific user.
$ gem install packagename
I don't know if this is necessary, but I would close the SSH session and reopen it. rvm messes with environment variables, so better safe than sorry.
Run env to print all environment variables. printenv also works if env doesn't for some reason. You'll get a big list of everything set, you only need the ruby-related ones. Do not copy/paste these values, they are examples I pulled from my system. Yours will be different!
PATH=/usr/local/rvm/gems/ruby-2.3.1/bin:/usr/local/rvm/gems/ruby-2.3.1#global/bin:/usr/local/rvm/rubies/ruby-2.3.1/bin:/usr/local/rvm/bin:/home/steven/bin:/home/steven/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
rvm_bin_path=/usr/local/rvm/bin
GEM_HOME=/usr/local/rvm/gems/ruby-2.3.1
IRBRC=/usr/local/rvm/rubies/ruby-2.3.1/.irbrc
MY_RUBY_HOME=/usr/local/rvm/rubies/ruby-2.3.1
rvm_path=/usr/local/rvm
rvm_prefix=/usr/local
rvm_ruby_string=ruby-2.3.1
GEM_PATH=/usr/local/rvm/gems/ruby-2.3.1:/usr/local/rvm/gems/ruby-2.3.1#global
RUBY_VERSION=ruby-2.3.1
Now we need PHP to recognize these variables. You'll need to find the right file on your system, which can be tricky. I don't have a way of knowing which one is correct, I used trial and error.
The file on my system is /etc/php/5.6/fpm/pool.d/www.conf. Add all of the environment variables you previously grabbed into this file with the below format. Note that you DO need PATH in here as well!
env[rvm_path] = /usr/local/rvm
env[rvm_prefix] = /usr/local
Now restart php-fpm. Your service name may be different from mine; I'm using the 5.6 build from ondrej/php.
Ubuntu 15.04 and newer (systemd):
$ sudo systemctl restart php5.6-fpm
Ubuntu 14.10 and newer:
$ sudo service php5.6-fpm restart
Finally, in the script itself you'll need to cd to the directory you're running the bundle command from. My short script is this:
cd /opt/slate
/usr/bin/git reset --hard
/usr/bin/git pull
bundle exec middleman build --clean
cp -R /opt/slate/build/* /var/www/docs
Works for me!

How can I composer update on OpenShift?

I am trying to use Slim on OpenShift with a free node. I can run composer update from the SSH sessions without any problem.
The only problem is every time I want to commit files through git I have to go to the console and run composer install again. My question is there is any easy way to workaround this? I tried a BASH script in /project/.openshift/action_hooks/post_deploy but the server is not creating the vendor folder under runtime/repo
I always do it via action hooks:
Inside my project directory I have a script called by /project/.openshift/action_hooks/post_deploy where post_deploy is a bash script.
Here goes what I have been using:
#!/bin/bash
export MY_PHPCOMPOSER=$OPENSHIFT_DATA_DIR/composer.phar
# if composer not exists, download
if [ ! -f $MY_PHPCOMPOSER ]; then
cd $OPENSHIFT_DATA_DIR
echo "Downloading composer..."
php -r "readfile('https://getcomposer.org/installer');" | php
fi
$MY_PHPCOMPOSER -n -q self-update
cd $OPENSHIFT_REPO_DIR
# install
php -dmemory_limit=1G $MY_PHPCOMPOSER install
So post_deploy script will perform every time which you push your repo to openshit. It work like a charm!
Side note
Since not always the OpenShift composer's version is updated it's safe
to download a new composer copy and use it.
Also, don't forget adjusting permissions settings.
Helpful links
Openshift builds
Openshift Default Build Lifecycle
I know that my answer is late but according to the Openshift documentation you can enable composer install after each build by just creating a marker file:
touch .openshift/markers/use_composer

How to change directory and run git commands using PHPseclib?

I'm trying to use PHPseclib to SSH and run commands on the remote server. I want to change directory and commands like git pull or clone. Is there a way to do this? I know that "cd" doesn't work well with exec. So any alternatives to this?
Thanks
You don't need to change folder, only to specify it to your git command.
git --git-dir=/path/to/repo/.git --work-tree=/path/to/repo remote add xxx
git --git-dir=/path/to/repo/.git --work-tree=/path/to/repo pull
Since git 1;8.5 (if your server hasd a recent enough git version installed), you even can use the short version (detailed here)
git -C /path/to/repo remote add xxx
git -C /path/to/repo pull

How do I install/use the phpize command?

I am intending to use SQLite 3 with PHP 5. I found this: http://packages.debian.org/etch/web/php5-sqlite3 but I am having problems with installation:
unzip & untar the package
run "phpize"
run "./configure --with-sqlite3=/path/to/your/sqlite3/install
make && make install
(optionally) copy DB/sqlite3.php to /path/to/php/lib/php/DB/sqlite3.php
I understand that phpize is a command used in cmd. But in which folder should I run this command? It seems that I don't have this command installed yet. How do I install this? I'm on Windows, and is using Wampserver for PHP.
Thanks!
The instructions you've got don't typically make sense for Windows, unless you're running GNU Make.
You ought to have a look at this:
http://us2.php.net/manual/en/sqlite3.installation.php

Categories