我正在使用 PHP 在 PHP 中运行 Git,exec
以获取有关一组服务器仪表板的一些 Git 项目的一些信息。我遇到了一些奇怪的输出,这让我怀疑我是否误解了“工作树”是什么。
如果我使用此命令,将%s
sprintf 参数替换为 Git 项目的路径:
git --work-tree=%s status
然后我得到这个输出:
On branch server-dashboard
Your branch is ahead of 'origin/server-dashboard' by 1 commit.
(use "git push" to publish your local commits)
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: .gitignore
deleted: README.md
deleted: common.php
deleted: conf/dev/settings.ini
deleted: conf/prod/settings.ini
deleted: conf/settings.ini.example
deleted: lib/HealthSettings.php
deleted: lib/Settings.php
deleted: public/assets/main.css
deleted: public/assets/main.js
deleted: public/assets/refresh.gif
deleted: public/assets/spinner.gif
deleted: public/curl-test.php
deleted: public/dashboard.php
deleted: public/iframes.php
modified: public/index.php
deleted: public/info.php
deleted: public/no-servers.php
deleted: public/sections/apache-mods.php
deleted: public/sections/curl-headers.php
deleted: public/sections/curl-self.php
deleted: public/sections/database.php
deleted: public/sections/env.php
deleted: public/sections/git-table.php
deleted: public/sections/git.php
deleted: public/sections/php-exts.php
deleted: public/sections/php-proxy.php
deleted: public/sections/tableau-proxy-table.php
deleted: public/sections/tableau-proxy.php
deleted: public/sections/user.php
deleted: public/tabs.php
git status
这不是我所期望的,因为如果我在控制台上运行,这不是我得到的。
现在,如果我在 PHP 中运行此命令(再次将字符串参数交换为路径):
cd %s && git status
然后我得到正确的输出:
On branch master
Your branch is ahead of 'origin/master' by 17 commits.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
我猜测,由于这个项目是在 master 后面 17 次提交,因此“错误”输出实际上是对这些提交发生变化的表达。但是,我原以为这两个命令是等效的。我错了吗,如果是这样,我可以无需cd
先以编程方式运行 Git 命令吗?