Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use git smudge/clean to replace file contents

Tags:

git

php

githooks

I am attempting to use git to manage deployment to my live website. The problem that I'm having is that I have a couple of settings files that I don't want to be updated when I push to production

what I'm looking at doing is either using a hook or smudge/clean to change the file contents for example from

<?php
define('DB_NAME', 'live');
define('DB_HOST', '127.0.0.1');
define('DB_USER', 'live_user');
define('DB_PASS', 'livePass');

to

<?php
define('DB_NAME', 'local');
define('DB_HOST', '127.0.0.1');
define('DB_USER', 'local_user');
define('DB_PASS', 'localPass');

Is there anyone who could talk me through the process please

I did wonder about using post-receive hook and a shell script to replace the contents, but ideally I want the contents in the repo to be changed before I run git checkout -f not changed in the live copy after

like image 929
Matt Indeedhat Holmes Avatar asked Jan 11 '13 16:01

Matt Indeedhat Holmes


People also ask

What is smudge in git?

The Git smudge filter is what converts the LFS pointer stored in Git with the actual large file from the LFS server. If your local repository does not have the LFS object, the smudge filter will have to download it. This means that network issues could affect the smudge filter.

What is a .gitattributes file?

DESCRIPTION. A gitattributes file is a simple text file that gives attributes to pathnames. Each line in gitattributes file is of form: pattern attr1 attr2 ... That is, a pattern followed by an attributes list, separated by whitespaces.


1 Answers

ideally i want the contents in the repo to be changed before i run git checkout -f not changed in the live copy after

The closest is a filter content driver which will replace the value at the git checkout.

smudge

(from Scott Schacon's Pro Git book page on Git Attributes: section "Keyword Expansion")

So in your case: a smudge filter, declared in a .gitattributes file.

See "Can git automatically switch between spaces and tabs?", except you would use a sed to replace local to live (as in this example)

like image 71
VonC Avatar answered Sep 25 '22 13:09

VonC