How to restrict merging specific branch into other branches in Gitlab?












6















Is there a way to restrict merging from a specific branch into other branches? Allow me to explain:



I have a 'testing' branch and a 'master' branch in Gitlab. The team creates feature branches, merges them into 'testing' for approval and then merge the feature branch into 'master' once approved.



Sometimes, it can take months to get approval of some features, and therefore code is sat in the 'testing' branch for a while. Meanwhile, another feature branch may try to merge into 'testing' and conflicts will arise. This is expected, however, we are only human, and occasionally someone may accidentally merge 'testing' into their feature branch when handling the conflict, which is obviously wrong. Instead, we should switch to 'testing' and merge our feature branch into 'testing' thus managing the conflict within the testing branch.



Any advise is appreciated.










share|improve this question



























    6















    Is there a way to restrict merging from a specific branch into other branches? Allow me to explain:



    I have a 'testing' branch and a 'master' branch in Gitlab. The team creates feature branches, merges them into 'testing' for approval and then merge the feature branch into 'master' once approved.



    Sometimes, it can take months to get approval of some features, and therefore code is sat in the 'testing' branch for a while. Meanwhile, another feature branch may try to merge into 'testing' and conflicts will arise. This is expected, however, we are only human, and occasionally someone may accidentally merge 'testing' into their feature branch when handling the conflict, which is obviously wrong. Instead, we should switch to 'testing' and merge our feature branch into 'testing' thus managing the conflict within the testing branch.



    Any advise is appreciated.










    share|improve this question

























      6












      6








      6


      3






      Is there a way to restrict merging from a specific branch into other branches? Allow me to explain:



      I have a 'testing' branch and a 'master' branch in Gitlab. The team creates feature branches, merges them into 'testing' for approval and then merge the feature branch into 'master' once approved.



      Sometimes, it can take months to get approval of some features, and therefore code is sat in the 'testing' branch for a while. Meanwhile, another feature branch may try to merge into 'testing' and conflicts will arise. This is expected, however, we are only human, and occasionally someone may accidentally merge 'testing' into their feature branch when handling the conflict, which is obviously wrong. Instead, we should switch to 'testing' and merge our feature branch into 'testing' thus managing the conflict within the testing branch.



      Any advise is appreciated.










      share|improve this question














      Is there a way to restrict merging from a specific branch into other branches? Allow me to explain:



      I have a 'testing' branch and a 'master' branch in Gitlab. The team creates feature branches, merges them into 'testing' for approval and then merge the feature branch into 'master' once approved.



      Sometimes, it can take months to get approval of some features, and therefore code is sat in the 'testing' branch for a while. Meanwhile, another feature branch may try to merge into 'testing' and conflicts will arise. This is expected, however, we are only human, and occasionally someone may accidentally merge 'testing' into their feature branch when handling the conflict, which is obviously wrong. Instead, we should switch to 'testing' and merge our feature branch into 'testing' thus managing the conflict within the testing branch.



      Any advise is appreciated.







      git merge gitlab






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 2 '18 at 8:28









      DavidDavid

      5,0652984137




      5,0652984137
























          2 Answers
          2






          active

          oldest

          votes


















          4





          +500









          To begin, be sure your needs is very normal and traditional.
          The answer is ... Yes.



          How to prevent merging from a branch to another, setting up a server Git Hook



          These are some useful links:





          • Git Hook explanations in Official Git Book

          • GitLab server-side Hook explanations


          • An example with a Git Hook written in Ruby to prevent merging 'staging' branch to 'master' one


          To help you (and for fun ^^), I wrote a dedicated hook in Python to reach your specific needs (you just need to adapt FORBIDDEN_SOURCE_BRANCH and FORBIDDEN_IF_NOT_DEST_BRANCH if you want to work with some other branches).



          #!/bin/python
          ##
          ## Author: Bertrand Benoit <mailto:contact@bertrand-benoit.net>
          ## Description: Git Hook (server-side) allowing to prevent merge from some branches to anothers
          ## Version: 0.9

          import sys, subprocess, re

          FORBIDDEN_SOURCE_BRANCH='testing'
          FORBIDDEN_IF_NOT_DEST_BRANCH='master'

          # Considers only merge commit.
          if not (len(sys.argv) >=2 and sys.argv[2] == 'merge'):
          sys.exit(0)

          # Defines which is the source branch.
          with open(sys.argv[1], 'r') as f:
          mergeMessage=f.readline()
          mergeBranchExtract=re.compile("Merge branch '([^']*)'.*$").search(mergeMessage)
          if not mergeBranchExtract:
          print('Unable to extract branch to merge from message: ', mergeMessage)
          sys.exit(0) # Ensures normal merge as failback

          # Checks if the merge (source) branch is one of those to check.
          mergeBranch=mergeBranchExtract.group(1)
          if mergeBranch != FORBIDDEN_SOURCE_BRANCH:
          sys.exit(0) # It is NOT the forbidden source branch, so keeps on normal merge

          # Defines which is the current branch.
          currentBranchFullName=subprocess.check_output(['git', 'symbolic-ref', 'HEAD'])
          currentBranchExtract=re.compile("^.*/([^/]*)n$").search(currentBranchFullName)
          if not currentBranchExtract:
          print('Unable to extract current branch from: ', currentBranchFullName)
          sys.exit(1) # Ensures normal merge as failback

          # Checks if the current (destination) branch is one of those to check.
          currentBranch=currentBranchExtract.group(1)
          if currentBranch != FORBIDDEN_IF_NOT_DEST_BRANCH:
          print("FORBIDDEN: Merging from '" + mergeBranch + "' to '" + currentBranch + "' is NOT allowed. Contact your administrator. Now, you should use git merge --abort and keep on your work.")
          sys.exit(1) # This is exactly the situation which is forbidden

          # All is OK, so keeps on normal merge
          sys.exit(0)


          To share all this work, I created a new Github repository, in which I'll add further hooks when needed :)



          For information, you can also setup protected branches to keep them safe from some users



          This is the complete documentation about that.



          Let me know if you need further help.






          share|improve this answer


























          • But I've had the impression that OP wanted a way to prevent merging from a specific branch rather than to a specific branch...

            – RomainValeri
            Nov 13 '18 at 16:04













          • @RomainValeri That's correct, I want to prevent merging FROM a specific branch.

            – David
            Nov 14 '18 at 8:11











          • I updated my answer consequently to reach your needs.

            – Bsquare
            Nov 14 '18 at 9:54











          • To help you (and for fun ^^), I wrote a dedicated hook in Python to reach your specific needs. I updated my answer consequently.

            – Bsquare
            Nov 14 '18 at 11:48



















          2














          So, what you want your pre-receive hook to reject is any branch push that incorporates the current testing tip if there's something there, unless the push is to testing itself, or to master. It's almost as easy to do that as to say it:



          testtip=`git rev-parse testing`
          [[ `git merge-base testing master` = $testtip ]] && exit 0 # okay if testing's merged

          while read old new ref; do
          [[ $ref = refs/heads/* ]] || continue # only care about branches
          [[ $new = *[^0]* ]] || continue # not checking branch deletions
          [[ $ref = */master ]] && continue # not checking pushes to master
          [[ $ref = */testing ]] && continue # nor testing


          range=$new; [[ $old = *[^0]* ]] && range=$old..$new

          [[ `git rev-list --first-parent $range` != *$testtip* ]]
          && [[ `git rev-list $range` = *$testtip* ]]
          && {
          echo push to $ref merges from unmerged testing commits, rejecting push
          exit 1
          }
          done


          edit: whoops, it was rejecting anything based on testing, not just anything that merged it. fixed.






          share|improve this answer


























          • Could you explain where your code is suppose to be run please? I do not recognize the language nor the usage of it sorry. Looks like bash?

            – Julien Rousé
            Nov 14 '18 at 15:41











          • @JulienRousé That's a pre-receive hook, yes, it's bash. say git help hooks.

            – jthill
            Nov 14 '18 at 15:45











          • Thaks I'll look it up

            – Julien Rousé
            Nov 14 '18 at 15:49











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53115040%2fhow-to-restrict-merging-specific-branch-into-other-branches-in-gitlab%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4





          +500









          To begin, be sure your needs is very normal and traditional.
          The answer is ... Yes.



          How to prevent merging from a branch to another, setting up a server Git Hook



          These are some useful links:





          • Git Hook explanations in Official Git Book

          • GitLab server-side Hook explanations


          • An example with a Git Hook written in Ruby to prevent merging 'staging' branch to 'master' one


          To help you (and for fun ^^), I wrote a dedicated hook in Python to reach your specific needs (you just need to adapt FORBIDDEN_SOURCE_BRANCH and FORBIDDEN_IF_NOT_DEST_BRANCH if you want to work with some other branches).



          #!/bin/python
          ##
          ## Author: Bertrand Benoit <mailto:contact@bertrand-benoit.net>
          ## Description: Git Hook (server-side) allowing to prevent merge from some branches to anothers
          ## Version: 0.9

          import sys, subprocess, re

          FORBIDDEN_SOURCE_BRANCH='testing'
          FORBIDDEN_IF_NOT_DEST_BRANCH='master'

          # Considers only merge commit.
          if not (len(sys.argv) >=2 and sys.argv[2] == 'merge'):
          sys.exit(0)

          # Defines which is the source branch.
          with open(sys.argv[1], 'r') as f:
          mergeMessage=f.readline()
          mergeBranchExtract=re.compile("Merge branch '([^']*)'.*$").search(mergeMessage)
          if not mergeBranchExtract:
          print('Unable to extract branch to merge from message: ', mergeMessage)
          sys.exit(0) # Ensures normal merge as failback

          # Checks if the merge (source) branch is one of those to check.
          mergeBranch=mergeBranchExtract.group(1)
          if mergeBranch != FORBIDDEN_SOURCE_BRANCH:
          sys.exit(0) # It is NOT the forbidden source branch, so keeps on normal merge

          # Defines which is the current branch.
          currentBranchFullName=subprocess.check_output(['git', 'symbolic-ref', 'HEAD'])
          currentBranchExtract=re.compile("^.*/([^/]*)n$").search(currentBranchFullName)
          if not currentBranchExtract:
          print('Unable to extract current branch from: ', currentBranchFullName)
          sys.exit(1) # Ensures normal merge as failback

          # Checks if the current (destination) branch is one of those to check.
          currentBranch=currentBranchExtract.group(1)
          if currentBranch != FORBIDDEN_IF_NOT_DEST_BRANCH:
          print("FORBIDDEN: Merging from '" + mergeBranch + "' to '" + currentBranch + "' is NOT allowed. Contact your administrator. Now, you should use git merge --abort and keep on your work.")
          sys.exit(1) # This is exactly the situation which is forbidden

          # All is OK, so keeps on normal merge
          sys.exit(0)


          To share all this work, I created a new Github repository, in which I'll add further hooks when needed :)



          For information, you can also setup protected branches to keep them safe from some users



          This is the complete documentation about that.



          Let me know if you need further help.






          share|improve this answer


























          • But I've had the impression that OP wanted a way to prevent merging from a specific branch rather than to a specific branch...

            – RomainValeri
            Nov 13 '18 at 16:04













          • @RomainValeri That's correct, I want to prevent merging FROM a specific branch.

            – David
            Nov 14 '18 at 8:11











          • I updated my answer consequently to reach your needs.

            – Bsquare
            Nov 14 '18 at 9:54











          • To help you (and for fun ^^), I wrote a dedicated hook in Python to reach your specific needs. I updated my answer consequently.

            – Bsquare
            Nov 14 '18 at 11:48
















          4





          +500









          To begin, be sure your needs is very normal and traditional.
          The answer is ... Yes.



          How to prevent merging from a branch to another, setting up a server Git Hook



          These are some useful links:





          • Git Hook explanations in Official Git Book

          • GitLab server-side Hook explanations


          • An example with a Git Hook written in Ruby to prevent merging 'staging' branch to 'master' one


          To help you (and for fun ^^), I wrote a dedicated hook in Python to reach your specific needs (you just need to adapt FORBIDDEN_SOURCE_BRANCH and FORBIDDEN_IF_NOT_DEST_BRANCH if you want to work with some other branches).



          #!/bin/python
          ##
          ## Author: Bertrand Benoit <mailto:contact@bertrand-benoit.net>
          ## Description: Git Hook (server-side) allowing to prevent merge from some branches to anothers
          ## Version: 0.9

          import sys, subprocess, re

          FORBIDDEN_SOURCE_BRANCH='testing'
          FORBIDDEN_IF_NOT_DEST_BRANCH='master'

          # Considers only merge commit.
          if not (len(sys.argv) >=2 and sys.argv[2] == 'merge'):
          sys.exit(0)

          # Defines which is the source branch.
          with open(sys.argv[1], 'r') as f:
          mergeMessage=f.readline()
          mergeBranchExtract=re.compile("Merge branch '([^']*)'.*$").search(mergeMessage)
          if not mergeBranchExtract:
          print('Unable to extract branch to merge from message: ', mergeMessage)
          sys.exit(0) # Ensures normal merge as failback

          # Checks if the merge (source) branch is one of those to check.
          mergeBranch=mergeBranchExtract.group(1)
          if mergeBranch != FORBIDDEN_SOURCE_BRANCH:
          sys.exit(0) # It is NOT the forbidden source branch, so keeps on normal merge

          # Defines which is the current branch.
          currentBranchFullName=subprocess.check_output(['git', 'symbolic-ref', 'HEAD'])
          currentBranchExtract=re.compile("^.*/([^/]*)n$").search(currentBranchFullName)
          if not currentBranchExtract:
          print('Unable to extract current branch from: ', currentBranchFullName)
          sys.exit(1) # Ensures normal merge as failback

          # Checks if the current (destination) branch is one of those to check.
          currentBranch=currentBranchExtract.group(1)
          if currentBranch != FORBIDDEN_IF_NOT_DEST_BRANCH:
          print("FORBIDDEN: Merging from '" + mergeBranch + "' to '" + currentBranch + "' is NOT allowed. Contact your administrator. Now, you should use git merge --abort and keep on your work.")
          sys.exit(1) # This is exactly the situation which is forbidden

          # All is OK, so keeps on normal merge
          sys.exit(0)


          To share all this work, I created a new Github repository, in which I'll add further hooks when needed :)



          For information, you can also setup protected branches to keep them safe from some users



          This is the complete documentation about that.



          Let me know if you need further help.






          share|improve this answer


























          • But I've had the impression that OP wanted a way to prevent merging from a specific branch rather than to a specific branch...

            – RomainValeri
            Nov 13 '18 at 16:04













          • @RomainValeri That's correct, I want to prevent merging FROM a specific branch.

            – David
            Nov 14 '18 at 8:11











          • I updated my answer consequently to reach your needs.

            – Bsquare
            Nov 14 '18 at 9:54











          • To help you (and for fun ^^), I wrote a dedicated hook in Python to reach your specific needs. I updated my answer consequently.

            – Bsquare
            Nov 14 '18 at 11:48














          4





          +500







          4





          +500



          4




          +500





          To begin, be sure your needs is very normal and traditional.
          The answer is ... Yes.



          How to prevent merging from a branch to another, setting up a server Git Hook



          These are some useful links:





          • Git Hook explanations in Official Git Book

          • GitLab server-side Hook explanations


          • An example with a Git Hook written in Ruby to prevent merging 'staging' branch to 'master' one


          To help you (and for fun ^^), I wrote a dedicated hook in Python to reach your specific needs (you just need to adapt FORBIDDEN_SOURCE_BRANCH and FORBIDDEN_IF_NOT_DEST_BRANCH if you want to work with some other branches).



          #!/bin/python
          ##
          ## Author: Bertrand Benoit <mailto:contact@bertrand-benoit.net>
          ## Description: Git Hook (server-side) allowing to prevent merge from some branches to anothers
          ## Version: 0.9

          import sys, subprocess, re

          FORBIDDEN_SOURCE_BRANCH='testing'
          FORBIDDEN_IF_NOT_DEST_BRANCH='master'

          # Considers only merge commit.
          if not (len(sys.argv) >=2 and sys.argv[2] == 'merge'):
          sys.exit(0)

          # Defines which is the source branch.
          with open(sys.argv[1], 'r') as f:
          mergeMessage=f.readline()
          mergeBranchExtract=re.compile("Merge branch '([^']*)'.*$").search(mergeMessage)
          if not mergeBranchExtract:
          print('Unable to extract branch to merge from message: ', mergeMessage)
          sys.exit(0) # Ensures normal merge as failback

          # Checks if the merge (source) branch is one of those to check.
          mergeBranch=mergeBranchExtract.group(1)
          if mergeBranch != FORBIDDEN_SOURCE_BRANCH:
          sys.exit(0) # It is NOT the forbidden source branch, so keeps on normal merge

          # Defines which is the current branch.
          currentBranchFullName=subprocess.check_output(['git', 'symbolic-ref', 'HEAD'])
          currentBranchExtract=re.compile("^.*/([^/]*)n$").search(currentBranchFullName)
          if not currentBranchExtract:
          print('Unable to extract current branch from: ', currentBranchFullName)
          sys.exit(1) # Ensures normal merge as failback

          # Checks if the current (destination) branch is one of those to check.
          currentBranch=currentBranchExtract.group(1)
          if currentBranch != FORBIDDEN_IF_NOT_DEST_BRANCH:
          print("FORBIDDEN: Merging from '" + mergeBranch + "' to '" + currentBranch + "' is NOT allowed. Contact your administrator. Now, you should use git merge --abort and keep on your work.")
          sys.exit(1) # This is exactly the situation which is forbidden

          # All is OK, so keeps on normal merge
          sys.exit(0)


          To share all this work, I created a new Github repository, in which I'll add further hooks when needed :)



          For information, you can also setup protected branches to keep them safe from some users



          This is the complete documentation about that.



          Let me know if you need further help.






          share|improve this answer















          To begin, be sure your needs is very normal and traditional.
          The answer is ... Yes.



          How to prevent merging from a branch to another, setting up a server Git Hook



          These are some useful links:





          • Git Hook explanations in Official Git Book

          • GitLab server-side Hook explanations


          • An example with a Git Hook written in Ruby to prevent merging 'staging' branch to 'master' one


          To help you (and for fun ^^), I wrote a dedicated hook in Python to reach your specific needs (you just need to adapt FORBIDDEN_SOURCE_BRANCH and FORBIDDEN_IF_NOT_DEST_BRANCH if you want to work with some other branches).



          #!/bin/python
          ##
          ## Author: Bertrand Benoit <mailto:contact@bertrand-benoit.net>
          ## Description: Git Hook (server-side) allowing to prevent merge from some branches to anothers
          ## Version: 0.9

          import sys, subprocess, re

          FORBIDDEN_SOURCE_BRANCH='testing'
          FORBIDDEN_IF_NOT_DEST_BRANCH='master'

          # Considers only merge commit.
          if not (len(sys.argv) >=2 and sys.argv[2] == 'merge'):
          sys.exit(0)

          # Defines which is the source branch.
          with open(sys.argv[1], 'r') as f:
          mergeMessage=f.readline()
          mergeBranchExtract=re.compile("Merge branch '([^']*)'.*$").search(mergeMessage)
          if not mergeBranchExtract:
          print('Unable to extract branch to merge from message: ', mergeMessage)
          sys.exit(0) # Ensures normal merge as failback

          # Checks if the merge (source) branch is one of those to check.
          mergeBranch=mergeBranchExtract.group(1)
          if mergeBranch != FORBIDDEN_SOURCE_BRANCH:
          sys.exit(0) # It is NOT the forbidden source branch, so keeps on normal merge

          # Defines which is the current branch.
          currentBranchFullName=subprocess.check_output(['git', 'symbolic-ref', 'HEAD'])
          currentBranchExtract=re.compile("^.*/([^/]*)n$").search(currentBranchFullName)
          if not currentBranchExtract:
          print('Unable to extract current branch from: ', currentBranchFullName)
          sys.exit(1) # Ensures normal merge as failback

          # Checks if the current (destination) branch is one of those to check.
          currentBranch=currentBranchExtract.group(1)
          if currentBranch != FORBIDDEN_IF_NOT_DEST_BRANCH:
          print("FORBIDDEN: Merging from '" + mergeBranch + "' to '" + currentBranch + "' is NOT allowed. Contact your administrator. Now, you should use git merge --abort and keep on your work.")
          sys.exit(1) # This is exactly the situation which is forbidden

          # All is OK, so keeps on normal merge
          sys.exit(0)


          To share all this work, I created a new Github repository, in which I'll add further hooks when needed :)



          For information, you can also setup protected branches to keep them safe from some users



          This is the complete documentation about that.



          Let me know if you need further help.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 16:09

























          answered Nov 13 '18 at 16:03









          BsquareBsquare

          3,24231033




          3,24231033













          • But I've had the impression that OP wanted a way to prevent merging from a specific branch rather than to a specific branch...

            – RomainValeri
            Nov 13 '18 at 16:04













          • @RomainValeri That's correct, I want to prevent merging FROM a specific branch.

            – David
            Nov 14 '18 at 8:11











          • I updated my answer consequently to reach your needs.

            – Bsquare
            Nov 14 '18 at 9:54











          • To help you (and for fun ^^), I wrote a dedicated hook in Python to reach your specific needs. I updated my answer consequently.

            – Bsquare
            Nov 14 '18 at 11:48



















          • But I've had the impression that OP wanted a way to prevent merging from a specific branch rather than to a specific branch...

            – RomainValeri
            Nov 13 '18 at 16:04













          • @RomainValeri That's correct, I want to prevent merging FROM a specific branch.

            – David
            Nov 14 '18 at 8:11











          • I updated my answer consequently to reach your needs.

            – Bsquare
            Nov 14 '18 at 9:54











          • To help you (and for fun ^^), I wrote a dedicated hook in Python to reach your specific needs. I updated my answer consequently.

            – Bsquare
            Nov 14 '18 at 11:48

















          But I've had the impression that OP wanted a way to prevent merging from a specific branch rather than to a specific branch...

          – RomainValeri
          Nov 13 '18 at 16:04







          But I've had the impression that OP wanted a way to prevent merging from a specific branch rather than to a specific branch...

          – RomainValeri
          Nov 13 '18 at 16:04















          @RomainValeri That's correct, I want to prevent merging FROM a specific branch.

          – David
          Nov 14 '18 at 8:11





          @RomainValeri That's correct, I want to prevent merging FROM a specific branch.

          – David
          Nov 14 '18 at 8:11













          I updated my answer consequently to reach your needs.

          – Bsquare
          Nov 14 '18 at 9:54





          I updated my answer consequently to reach your needs.

          – Bsquare
          Nov 14 '18 at 9:54













          To help you (and for fun ^^), I wrote a dedicated hook in Python to reach your specific needs. I updated my answer consequently.

          – Bsquare
          Nov 14 '18 at 11:48





          To help you (and for fun ^^), I wrote a dedicated hook in Python to reach your specific needs. I updated my answer consequently.

          – Bsquare
          Nov 14 '18 at 11:48













          2














          So, what you want your pre-receive hook to reject is any branch push that incorporates the current testing tip if there's something there, unless the push is to testing itself, or to master. It's almost as easy to do that as to say it:



          testtip=`git rev-parse testing`
          [[ `git merge-base testing master` = $testtip ]] && exit 0 # okay if testing's merged

          while read old new ref; do
          [[ $ref = refs/heads/* ]] || continue # only care about branches
          [[ $new = *[^0]* ]] || continue # not checking branch deletions
          [[ $ref = */master ]] && continue # not checking pushes to master
          [[ $ref = */testing ]] && continue # nor testing


          range=$new; [[ $old = *[^0]* ]] && range=$old..$new

          [[ `git rev-list --first-parent $range` != *$testtip* ]]
          && [[ `git rev-list $range` = *$testtip* ]]
          && {
          echo push to $ref merges from unmerged testing commits, rejecting push
          exit 1
          }
          done


          edit: whoops, it was rejecting anything based on testing, not just anything that merged it. fixed.






          share|improve this answer


























          • Could you explain where your code is suppose to be run please? I do not recognize the language nor the usage of it sorry. Looks like bash?

            – Julien Rousé
            Nov 14 '18 at 15:41











          • @JulienRousé That's a pre-receive hook, yes, it's bash. say git help hooks.

            – jthill
            Nov 14 '18 at 15:45











          • Thaks I'll look it up

            – Julien Rousé
            Nov 14 '18 at 15:49
















          2














          So, what you want your pre-receive hook to reject is any branch push that incorporates the current testing tip if there's something there, unless the push is to testing itself, or to master. It's almost as easy to do that as to say it:



          testtip=`git rev-parse testing`
          [[ `git merge-base testing master` = $testtip ]] && exit 0 # okay if testing's merged

          while read old new ref; do
          [[ $ref = refs/heads/* ]] || continue # only care about branches
          [[ $new = *[^0]* ]] || continue # not checking branch deletions
          [[ $ref = */master ]] && continue # not checking pushes to master
          [[ $ref = */testing ]] && continue # nor testing


          range=$new; [[ $old = *[^0]* ]] && range=$old..$new

          [[ `git rev-list --first-parent $range` != *$testtip* ]]
          && [[ `git rev-list $range` = *$testtip* ]]
          && {
          echo push to $ref merges from unmerged testing commits, rejecting push
          exit 1
          }
          done


          edit: whoops, it was rejecting anything based on testing, not just anything that merged it. fixed.






          share|improve this answer


























          • Could you explain where your code is suppose to be run please? I do not recognize the language nor the usage of it sorry. Looks like bash?

            – Julien Rousé
            Nov 14 '18 at 15:41











          • @JulienRousé That's a pre-receive hook, yes, it's bash. say git help hooks.

            – jthill
            Nov 14 '18 at 15:45











          • Thaks I'll look it up

            – Julien Rousé
            Nov 14 '18 at 15:49














          2












          2








          2







          So, what you want your pre-receive hook to reject is any branch push that incorporates the current testing tip if there's something there, unless the push is to testing itself, or to master. It's almost as easy to do that as to say it:



          testtip=`git rev-parse testing`
          [[ `git merge-base testing master` = $testtip ]] && exit 0 # okay if testing's merged

          while read old new ref; do
          [[ $ref = refs/heads/* ]] || continue # only care about branches
          [[ $new = *[^0]* ]] || continue # not checking branch deletions
          [[ $ref = */master ]] && continue # not checking pushes to master
          [[ $ref = */testing ]] && continue # nor testing


          range=$new; [[ $old = *[^0]* ]] && range=$old..$new

          [[ `git rev-list --first-parent $range` != *$testtip* ]]
          && [[ `git rev-list $range` = *$testtip* ]]
          && {
          echo push to $ref merges from unmerged testing commits, rejecting push
          exit 1
          }
          done


          edit: whoops, it was rejecting anything based on testing, not just anything that merged it. fixed.






          share|improve this answer















          So, what you want your pre-receive hook to reject is any branch push that incorporates the current testing tip if there's something there, unless the push is to testing itself, or to master. It's almost as easy to do that as to say it:



          testtip=`git rev-parse testing`
          [[ `git merge-base testing master` = $testtip ]] && exit 0 # okay if testing's merged

          while read old new ref; do
          [[ $ref = refs/heads/* ]] || continue # only care about branches
          [[ $new = *[^0]* ]] || continue # not checking branch deletions
          [[ $ref = */master ]] && continue # not checking pushes to master
          [[ $ref = */testing ]] && continue # nor testing


          range=$new; [[ $old = *[^0]* ]] && range=$old..$new

          [[ `git rev-list --first-parent $range` != *$testtip* ]]
          && [[ `git rev-list $range` = *$testtip* ]]
          && {
          echo push to $ref merges from unmerged testing commits, rejecting push
          exit 1
          }
          done


          edit: whoops, it was rejecting anything based on testing, not just anything that merged it. fixed.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 15:37

























          answered Nov 14 '18 at 14:34









          jthilljthill

          27.9k34477




          27.9k34477













          • Could you explain where your code is suppose to be run please? I do not recognize the language nor the usage of it sorry. Looks like bash?

            – Julien Rousé
            Nov 14 '18 at 15:41











          • @JulienRousé That's a pre-receive hook, yes, it's bash. say git help hooks.

            – jthill
            Nov 14 '18 at 15:45











          • Thaks I'll look it up

            – Julien Rousé
            Nov 14 '18 at 15:49



















          • Could you explain where your code is suppose to be run please? I do not recognize the language nor the usage of it sorry. Looks like bash?

            – Julien Rousé
            Nov 14 '18 at 15:41











          • @JulienRousé That's a pre-receive hook, yes, it's bash. say git help hooks.

            – jthill
            Nov 14 '18 at 15:45











          • Thaks I'll look it up

            – Julien Rousé
            Nov 14 '18 at 15:49

















          Could you explain where your code is suppose to be run please? I do not recognize the language nor the usage of it sorry. Looks like bash?

          – Julien Rousé
          Nov 14 '18 at 15:41





          Could you explain where your code is suppose to be run please? I do not recognize the language nor the usage of it sorry. Looks like bash?

          – Julien Rousé
          Nov 14 '18 at 15:41













          @JulienRousé That's a pre-receive hook, yes, it's bash. say git help hooks.

          – jthill
          Nov 14 '18 at 15:45





          @JulienRousé That's a pre-receive hook, yes, it's bash. say git help hooks.

          – jthill
          Nov 14 '18 at 15:45













          Thaks I'll look it up

          – Julien Rousé
          Nov 14 '18 at 15:49





          Thaks I'll look it up

          – Julien Rousé
          Nov 14 '18 at 15:49


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53115040%2fhow-to-restrict-merging-specific-branch-into-other-branches-in-gitlab%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()