It has become necessary to set a hard path when performing actions by a script, namely:

There is a PHP file that updates the Web directory from a remote git repository. The problem is that the path it substitutes from the place where it was launched from.

Now in more detail. There are folders:

  • Remote origin:

    • /home/sdc/projects/one.git
    • /home/sdc/projects/two.git
  • Web directories:

    • /home/sdc/webpath/one
    • /home/sdc/webpath/two
    • /home/sdc/webpath/git.update/index.php (the page that makes exec() for the update.sh script. update.sh must be in the one and two folders)

Text update.sh :

 git pull origin master 

So here. When exec executed, I get an error that says that I am not in the git repository directory. That's right: since the path I see is the following: /home/sdc/webpath/git.update/ .

Question: How to substitute the program launch path from the directory where update.sh is located? ( /home/sdc/webpath/one or /home/sdc/webpath/two ).

    1 answer 1

    Did not quite understand the question.

    If the problem is to change the directory, then:

     cd /home/sdc/webpath/one # что-то делаем /home/sdc/webpath/git.update/update.sh # если нужно вернуться в старую директорию cd - 

    If it is to work with git from another directory, then:

     # вызываем скрипт с соответствующим environment GIT_DIR=/home/sdc/webpath/one update.sh 

    If the problem is that commands from another / cloned repository do not work from under git hook, then:

     # сначала очищаем текущую переменную GIT_DIR # поскольку она указывает на bare репозиторий unset GIT_DIR cd /home/sdc/webpath/one # ... и т.д. 

    If the problem is to run a script from under php with the corresponding PWD, then:

     exec("cd /home/sdc/webpath/one && /home/sdc/webpath/git.update/update.sh"); 

    Or through proc_open :

     $process = proc_open('/home/sdc/webpath/git.update/update.sh', $descriptorspec, $pipes, '/home/sdc/webpath/one'); 
    • Yes, thanks :) Straight to update.sh wrote cd ../../../ and everything went))) Sorry, hell blunt :) - Stanislav Komar