Viewed   56 times

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

 Answers

1

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.

(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)

Saturday, November 5, 2022
1

You could create the patch using git diff and then apply it using the patch utility, which allows you to specify the file you want to apply the diff to.

For example:

cd first-repo
git diff HEAD^ -- hello.test > ~/patch_file

cd ../second-repo
patch -p1 blue/red/hi.test ~/patch_file
Tuesday, August 2, 2022
 
the_dog
 
5

You can add a post-receive hook to the ~/bar.com.git repo for this. To do this add to the ~/bar.com.git/hooks/ directory an executable file post-receive with the content:

#!/bin/sh

unset $(git rev-parse --local-env-vars)
cd ~/bar.com
git pull

Make sure the post-receive file has the executable bits(e.g. 755).

Now whenever something is pushed to the ~/bar.com.git repo, the ~/bar.com repo is updated automatically.

See also

  • getting "fatal: not a git repository: '.'" when using post-update hook to execute 'git pull' on another repo
  • Git - post-receive hook with git pull “Failed to find a valid git directory”

to understand why unsetting some environment variables is necessary.

Tuesday, December 27, 2022
 
levix
 
3

It is possible to define your own hooks in a git template, but even there, those hooks would be non-executable ones.

I.e. the user would still have to activate them (rename or activate the executable bit) once the repo is cloned.
That way, said user won't have any unwanted script executed without his/her full knowledge and explicit approval.
Doing otherwise would be too much of a security risk for anyone "blindly" cloning a repo.

Saturday, October 1, 2022
 
badfun
 
1

use tokens=1-4,*

* means "the fifth token is the rest of the line"

Complete code:

@echo off
setlocal enabledelayedexpansion
set inputCSV=%1
set outputCSV=%2

(for /f "tokens=1-4,* delims=," %%a IN (%inputCSV%) DO (
    if "%%d"=="""" (set "value="000"") else (set "value=%%d")
    echo %%a,%%b,%%c,!value!,%%e
))>%output.csv

EDIT for the additional info in the comment

@echo off
setlocal enabledelayedexpansion
set inputCSV=%1
set outputCSV=%2

(for /f "tokens=*" %%a IN (%inputCSV%) DO (
  set column=0
  set "line="
  for %%i in ( %%a ) do ( 
    set /a column+=1
    set value=%%~i
    if !column!==4 (   
      if "!value!"=="" set "value=0"
    ) 
    set "line=!line!,"!value!"" 
  )
  echo !line:~1!
))>%outputCSV%

change the 4 to the correct column number.

Attention: there is a limit for the number of characters per line (don't remember how much, could affect %%a)

Also some special characters will make trouble.

Wednesday, August 24, 2022
 
lukkea
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :