一点背景
我正在开发一个流程,它将允许人们访问一个共同的环境。这样做的目的是建立一个只应该被推入的存储库,而不是一个简单的存储库,因为我希望人们能够阅读它。让我们称它为公共
这是因为我不希望人们必须克隆存储库才能访问数据。
为此,我有以下结构:
成问题
公共存储库不应该有任何直接修改。但是,由于多个用户都是sudoers,我不能100%肯定有人不会直接修改它。
或者,对于Murphy,我百分之百地确信有一天会有人在公共存储库中对某些地方进行修改……
为了确保当某个人正确地插入远程目录(因此在公共存储库中)时,没有冲突,我考虑了公共目录的以下钩子:
#!/bin/bash
#Stash local modifications if there are ones to avoid conflicts
echo "## Local modifications on the Product-Line are stashed with the tag: 'local modifications @ $(date)' "
git stash push -m "local modifications @ $(date)"
exit 0#!/bin/bash
#Checks out the the branch that contains the last commit
git checkout $( git log --branches -1 --pretty=format:'%D' | sed -e 's/.*-> //g' -e 's/.*, //g' )
#Forces synchronization with the last commit
git stash
git stash drop stash@{0}
exit 0但是,隐藏命令似乎不像预期的那样工作。在本地存储库中执行推送时返回的日志如下:
$> echo toto >> stash_test ; git commit -am "test" ; git push
[tbranch f6e8fbb] test
1 file changed, 1 insertion(+)
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 48 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 351 bytes | 351.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: remote: ## Local modifications on the Product-Line are stashed with the tag: 'local modifications @ Mon Dec 23 17:44:59 CET 2019'
remote: remote: No local changes to save
remote: remote: Already on 'tbranch'
remote: remote: M stash_test
remote: remote: Saved working directory and index state WIP on tbranch: f6e8fbb test
remote: remote: stash@{0}: WIP on tbranch: f6e8fbb test
remote: remote: Dropped stash@{0} (b29b76ceb2b2dfd2fcf6d0577c7200e517641328)
remote: To ../pl
remote: f75b5d1..f6e8fbb tbranch -> tbranch
To /home/usr/TEMP/remote
f75b5d1..f6e8fbb tbranch -> tbranch这里有两个问题:
git stash push -m "msg"时,它返回No local changes to save。git stash + git stash drop stash@{0}应该模拟对当前分支的最后提交的更新。但是,在日志中,如果获得公共存储库中的状态,则该操作似乎有效:On branch tbranch
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: stash_test
modified: teststash_test是上一次提交修改的文件。test是本地修改以测试所有过程的文件。双龙效应: 1. git stash list为空,其中应包含test的修改。但是,日志与此相一致,因为它没有将test存储在预接收钩子中。
stash_test没有用最后一次提交更新.这里很奇怪,因为日志显示一切都很好.我迷路的地方是我试着手动执行这些步骤.如我所料。
不过,我在git管理方面还是很新的,所以我可能错过了一些东西.
问题
git stash命令在钩子中有一种不允许我做我想做的事情的花哨行为吗?编辑:找出根本原因
执行钩子时,它位于我的公共dir的.git目录中。因此,git stash不检测修改。
如果在预接收钩子中执行git status --porcelain,就会得到:
?? HEAD
?? ORIG_HEAD
?? config
?? description
?? gitk.cache
?? hooks/applypatch-msg.sample
?? hooks/commit-msg.sample
?? hooks/fsmonitor-watchman.sample
?? hooks/post-receive
?? hooks/post-update.sample
?? hooks/pre-applypatch.sample
?? hooks/pre-commit
?? hooks/pre-commit.sample
?? hooks/pre-push.sample
?? hooks/pre-rebase.sample
?? hooks/pre-receive
?? hooks/pre-receive.sample
...因为.git dir的内容不在版本控制之下。
事实上,即使使用@knittl建议使用git reset --hard也不起作用,因为它不是在公共的“work”目录中完成的。
我想过的最简单的解决方法是将操作封装在钩子中:
pwd # returns COMMON/.git
pushd .. > /dev/null
pwd # returns COMMON
#hook operations
popd > /dev/null在我最终位于正确目录中的钩子中检查pwd命令,以执行我的存储/重置。
但是,在执行此操作时,远程消息将返回我:
remote: remote: fatal: not a git repository: '.' 所以现在我有点困惑,因为我实际上是我的共同的根源,所以它应该被认为是一个git存储库。
即使像@knittl和@torek所建议的那样,这不是部署某些东西的黄金方式,我还是想了解为什么会有这些限制--,如果有,可以重写它们--。
EDIT2:藏在预提交中
在预提交钩子中,我执行一个存储以保存本地修改并中止提交,这样‘鲁莽的sudoer’不能提交它的更改,但是可以稍后选择他的修改。在这个钩子中,git stash按预期执行。
我猜想这个问题与以下事实有关:预/后接收钩子是服务器端的钩子,通常应该只用于裸存储库。
发布于 2019-12-24 16:05:49
我终于找到了一个让我心烦的一份文件:
在Git调用钩子之前,它将其工作目录更改为裸存储库中的$GIT_DIR或非裸存储库中工作树的根。异常是在推送(预接收、更新、后接收、后更新、推送到签出)期间触发的钩子,这些钩子总是在$GIT_DIR中执行。
因此,GIT_DIR环境变量使我的钩子在公共/..git中运行。为此,我最终将当前工作目录更改为公共目录并设置为GIT_DIR=$(pwd -P)/.git。
如果GIT_DIR不是这样设置的,那么git就不认为通用于git目录(因为默认情况下,GIT_DIR='.'和git在上层寻找查找.git目录)。
作为参考,我的邮件接收钩子最终看起来像:
#!/bin/bash
# Make sure that we are on the branch containing the last commit
git checkout -f $( git log --branches -1 --pretty=format:'%D' | sed -e 's/.*-> //g' -e 's/.*, //g' )
# Forces synchronization with the last commit by discarding the local modifications. A bit trickerish but it works...
cd ..
export GIT_DIR=$(\pwd -P)/.git
git reset --hard
exit 0https://stackoverflow.com/questions/59459181
复制相似问题