在拉取最新更改之前,阻止用户进行提交 [英] Prevent user from doing a commit before pulling latest changes

查看:123
本文介绍了在拉取最新更改之前,阻止用户进行提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,当我从开发中创建一个功能分支 f 时,我拥有以下本地存储库,其中包含这样的提交树:

  master  - > a 
\
\
develop c
\
\
功能f

现在有些时候新特性已经被合并来开发 c ,我的提交树看起来像这样:

  master  - > a 
\
\
开发c - > d - > e
\
\
功能f

现在我希望确保我不会在提交最新的开发变更之前提交。

  master  - > a 
\
\
开发c - > d - > e
\
\
功能f

我试过了,一种方法是将更改推送到 f ,然后尝试合并 f 来开发 e (现在有新的更改)。这样做显然会从git中抛出错误信息,称开发在 f 之前。我非常有把握地认为这可以通过 post-hook code>。但是,我可以用git pre-hook



来做到这一点。如果是这样,怎么做,如果不要请推荐一些其他方式来做到这一点。如果可能的话,请解释一下这些命令在做什么?(我只是一个git初学者)

需要显示一些自定义消息以防用户尝试这样做。

预先致谢!!

更新:
我是工作在一个看起来像这样的解决方案(稍后,这个脚本将在pre-commit钩子中调用)



tomato.sh

 #!/ bin / bash 
WORKING_GIT_DIR = $ 1
cd $ WORKING_GIT_DIR
#切换至BRANCH_TO_MERGE
BRANCH_TO_MERGE = $ 2
git checkout $ BRANCH_TO_MERGE

PARENT_BRANCH = $(git show-branch | grep'*'| grep -v$(git rev-parse --abbrev-ref HEAD)| head -n1 | sed's /.* \ [\(。* \)\]。* / \ 1 /'| sed's / [\ ^〜]。* / /')
echo $ PARENT_BRANCH
#在特性分支和父级的内存合并中寻找可能的冲突
git merge-tree`git merge-base FETCH_HEAD $ PARENT_BRANCH` FETCH_HEAD $ PARENT_BRANCH

如何运行

  sh + x tomato.sh(PATH TO WORKING_GIT_DIR)(BRANCH_TO_MERGE)

脚本如何工作



它试图找到父分支(详情请看[这里] [2])。
然后使用以下三种方式进行合并:

$ g $ merge g tree< base> < BRANCH1> < BRANCH2>

base - >找到当前FETCH_HEAD& amp之间的最佳共同祖先;当前父分支的父母/最近父分支

分支1 - >当前FETCH_HEAD

分支2 - >父母/最近父分支当前父分支


$ b $

这将在远程分支和本地分支内存合并时不实际更改索引。



请评论是否有人改进了此脚本。

预先感谢!

解决方案

git没有像这样的特别支持,这就是为什么像拉请求这样的概念已被添加到git覆盖解决方案,如github和bitbucket,以及诸如 gerrit 促进组织审查,访问等政策。



由于问题标记为github,我假设您正在使用它,然后我会说您应该只需忽略本地功能分支的最新状态,并让开发人员推动他们分支的任何内容并从中创建拉取请求。



如果功能分支落后,那么如果存在冲突 1 ,则拉取请求将显示并且不能合并。然后由功能分支开发人员离开球,并使其负责更新功能分支以便将其合并到开发中。






1 这当然只能通过自动逐行合并分析来确定,这可能不会检测到概念冲突。可以定义额外的要求,我可以假设可以用来拒绝合并任何非最新的拉取请求,如果这是你想要的。
更新:刚刚出现了一个问题:检查PR是否与目标分支保持同步


Lets say I have the following local repository with a commit tree like this when I created a feature branch f from develop:

master --> a
            \
             \
      develop c 
               \
                \
         feature f

Now after sometime new features has been merged to develop c and my commit tree looks like this:

 master --> a
             \
              \
       develop c --> d --> e
                \
                 \
          feature f

Now I want to ensure that I do not commit before pulling the latest changes from develop.

master --> a
            \
             \
      develop c --> d --> e
                           \ 
                            \ 
                     feature f

From what I have tried, one way is to push the changes to f and then try to merge f to develop e (now have new changes). Doing this obviously throw error messages from git saying develop is ahead of f.I am very confident that this can be done with a post-hook. But, can I do it with a git pre-hook ?

If so, how to do it and if not please suggest some other way of doing it. If possible please explain what the commands are doing?( I am just a beginner in git )

The requirement is to display some custom message in case user tries to do this.

Thanks in advance!!

Update: I am working on a solution which looks like this (later this script will be called in pre-commit hook)

tomato.sh

#!/bin/bash
WORKING_GIT_DIR=$1
cd $WORKING_GIT_DIR
# switch to BRANCH_TO_MERGE
BRANCH_TO_MERGE=$2
git checkout $BRANCH_TO_MERGE

PARENT_BRANCH=$(git show-branch | grep '*' | grep -v "$(git rev-parse --abbrev-ref HEAD)" | head -n1 | sed 's/.*\[\(.*\)\].*/\1/' | sed 's/[\^~].*//')
echo $PARENT_BRANCH
# in memory merging of feature branch and parent and look for possible conflicts
git merge-tree `git merge-base FETCH_HEAD $PARENT_BRANCH` FETCH_HEAD $PARENT_BRANCH

How to run

sh +x tomato.sh (PATH TO WORKING_GIT_DIR) (BRANCH_TO_MERGE)

How the script works

It tries to find the parent branch (look [here][2] for detail). Then do a three way merge using :

git merge-tree <base> <branch1> <branch2>

base -> find the best-common-ancestor between current FETCH_HEAD & parent/nearest parent branch of current parent branch
branch1 -> current FETCH_HEAD
branch2 -> parent/nearest parent branch of current parent branch

This will give an in memory merge of the remote and local branch without actually changing the indexes.

Please comment if someone improves this script.

Thanks in advance!

解决方案

As already pointed out, plain git does not have any particular support for things like this, which is why concepts like pull request have been added to git overlay solutions like github and bitbucket, and tools like gerrit facilitate organizational policies for review, access, etc.

Since the question is tagged github, I assume you are using that, and then I would say that you should just ignore the up to date status of local feature branches and let the developers push whatever they branched from and create a pull request from that.

If the feature branch is behind, then the pull request will show and can not be merged if there is a conflict1. Which then leaves the ball by the feature branch developer and makes it his/her responsibility to update the feature branch in order to get it merged into develop.


1 This is of course only determined from automatic line by line merge analysis which might not detect conceptual conflicts. It is possible to define additional requirements which I assume is possible to use to reject merging of any non-up-to-date pull request if that is what you want. Update: There just came a question Check if a PR is up to date with the target branch.

这篇关于在拉取最新更改之前,阻止用户进行提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆