Check if current branch is in newline delimited list

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite
1












I am trying to create a git pre-commit hook. If the current branch is already merged with the integration branch, I don't want to allow new commits to that branch. I have this:



#!/usr/bin/env bash

git fetch origin dev
current_branch=`git rev-parse --abbrev-ref HEAD`
branches="$(git branch --merged dev | tr '@squashed' '')"


say I have these two branches:



me/feature/x
me/feature/x@squashed


What I want to do is replace '@squashed', so I get this:



me/feature/x
me/feature/x


then if the current branch starts with either of those names, then exit with code 1.



The problem is:



  1. I can't figure out how to use tr/sed to only replace '@squashed' if the branch name ends with that string.


  2. I can't figure out how to loop over the elements and exit with non-zero if the current branch starts with one of the branch names in the list.










share|improve this question























  • would it make more sense to assign the output of git branch to an array than a scalar? Bash also has some amount of variable expansion capabilities, where you can replace text in variables.
    – Jeff Schaller
    Sep 11 at 10:45














up vote
0
down vote

favorite
1












I am trying to create a git pre-commit hook. If the current branch is already merged with the integration branch, I don't want to allow new commits to that branch. I have this:



#!/usr/bin/env bash

git fetch origin dev
current_branch=`git rev-parse --abbrev-ref HEAD`
branches="$(git branch --merged dev | tr '@squashed' '')"


say I have these two branches:



me/feature/x
me/feature/x@squashed


What I want to do is replace '@squashed', so I get this:



me/feature/x
me/feature/x


then if the current branch starts with either of those names, then exit with code 1.



The problem is:



  1. I can't figure out how to use tr/sed to only replace '@squashed' if the branch name ends with that string.


  2. I can't figure out how to loop over the elements and exit with non-zero if the current branch starts with one of the branch names in the list.










share|improve this question























  • would it make more sense to assign the output of git branch to an array than a scalar? Bash also has some amount of variable expansion capabilities, where you can replace text in variables.
    – Jeff Schaller
    Sep 11 at 10:45












up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





I am trying to create a git pre-commit hook. If the current branch is already merged with the integration branch, I don't want to allow new commits to that branch. I have this:



#!/usr/bin/env bash

git fetch origin dev
current_branch=`git rev-parse --abbrev-ref HEAD`
branches="$(git branch --merged dev | tr '@squashed' '')"


say I have these two branches:



me/feature/x
me/feature/x@squashed


What I want to do is replace '@squashed', so I get this:



me/feature/x
me/feature/x


then if the current branch starts with either of those names, then exit with code 1.



The problem is:



  1. I can't figure out how to use tr/sed to only replace '@squashed' if the branch name ends with that string.


  2. I can't figure out how to loop over the elements and exit with non-zero if the current branch starts with one of the branch names in the list.










share|improve this question















I am trying to create a git pre-commit hook. If the current branch is already merged with the integration branch, I don't want to allow new commits to that branch. I have this:



#!/usr/bin/env bash

git fetch origin dev
current_branch=`git rev-parse --abbrev-ref HEAD`
branches="$(git branch --merged dev | tr '@squashed' '')"


say I have these two branches:



me/feature/x
me/feature/x@squashed


What I want to do is replace '@squashed', so I get this:



me/feature/x
me/feature/x


then if the current branch starts with either of those names, then exit with code 1.



The problem is:



  1. I can't figure out how to use tr/sed to only replace '@squashed' if the branch name ends with that string.


  2. I can't figure out how to loop over the elements and exit with non-zero if the current branch starts with one of the branch names in the list.







scripting git






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 11 at 10:36









Jeff Schaller

33.1k849111




33.1k849111










asked Sep 11 at 3:47









Alexander Mills

1,9701031




1,9701031











  • would it make more sense to assign the output of git branch to an array than a scalar? Bash also has some amount of variable expansion capabilities, where you can replace text in variables.
    – Jeff Schaller
    Sep 11 at 10:45
















  • would it make more sense to assign the output of git branch to an array than a scalar? Bash also has some amount of variable expansion capabilities, where you can replace text in variables.
    – Jeff Schaller
    Sep 11 at 10:45















would it make more sense to assign the output of git branch to an array than a scalar? Bash also has some amount of variable expansion capabilities, where you can replace text in variables.
– Jeff Schaller
Sep 11 at 10:45




would it make more sense to assign the output of git branch to an array than a scalar? Bash also has some amount of variable expansion capabilities, where you can replace text in variables.
– Jeff Schaller
Sep 11 at 10:45










1 Answer
1






active

oldest

votes

















up vote
1
down vote














  1. tr replaces single characters, not strings. As you surmise you can use sed to delete a string at the end of a line:



    git branch --merged dev | sed 's/@squashed$//'



  2. Instead of looping, use grep to determine whether a list of strings, one per line, contains a line starting with a reference string:



    git branch --merged dev | sed 's/@squashed$//' | grep -q "^..$current_branch"


Better yet, since you’re using git, ask it to filter for you:



git branch --list "$current_branch*" --merged dev


will list any branch merged with dev whose name starts with the current branch. (Since you’re filtering by matching the start of branch names, you don’t need to drop the “@squashed” part, unless I’m missing something.)






share|improve this answer




















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    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',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f468159%2fcheck-if-current-branch-is-in-newline-delimited-list%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote














    1. tr replaces single characters, not strings. As you surmise you can use sed to delete a string at the end of a line:



      git branch --merged dev | sed 's/@squashed$//'



    2. Instead of looping, use grep to determine whether a list of strings, one per line, contains a line starting with a reference string:



      git branch --merged dev | sed 's/@squashed$//' | grep -q "^..$current_branch"


    Better yet, since you’re using git, ask it to filter for you:



    git branch --list "$current_branch*" --merged dev


    will list any branch merged with dev whose name starts with the current branch. (Since you’re filtering by matching the start of branch names, you don’t need to drop the “@squashed” part, unless I’m missing something.)






    share|improve this answer
























      up vote
      1
      down vote














      1. tr replaces single characters, not strings. As you surmise you can use sed to delete a string at the end of a line:



        git branch --merged dev | sed 's/@squashed$//'



      2. Instead of looping, use grep to determine whether a list of strings, one per line, contains a line starting with a reference string:



        git branch --merged dev | sed 's/@squashed$//' | grep -q "^..$current_branch"


      Better yet, since you’re using git, ask it to filter for you:



      git branch --list "$current_branch*" --merged dev


      will list any branch merged with dev whose name starts with the current branch. (Since you’re filtering by matching the start of branch names, you don’t need to drop the “@squashed” part, unless I’m missing something.)






      share|improve this answer






















        up vote
        1
        down vote










        up vote
        1
        down vote










        1. tr replaces single characters, not strings. As you surmise you can use sed to delete a string at the end of a line:



          git branch --merged dev | sed 's/@squashed$//'



        2. Instead of looping, use grep to determine whether a list of strings, one per line, contains a line starting with a reference string:



          git branch --merged dev | sed 's/@squashed$//' | grep -q "^..$current_branch"


        Better yet, since you’re using git, ask it to filter for you:



        git branch --list "$current_branch*" --merged dev


        will list any branch merged with dev whose name starts with the current branch. (Since you’re filtering by matching the start of branch names, you don’t need to drop the “@squashed” part, unless I’m missing something.)






        share|improve this answer













        1. tr replaces single characters, not strings. As you surmise you can use sed to delete a string at the end of a line:



          git branch --merged dev | sed 's/@squashed$//'



        2. Instead of looping, use grep to determine whether a list of strings, one per line, contains a line starting with a reference string:



          git branch --merged dev | sed 's/@squashed$//' | grep -q "^..$current_branch"


        Better yet, since you’re using git, ask it to filter for you:



        git branch --list "$current_branch*" --merged dev


        will list any branch merged with dev whose name starts with the current branch. (Since you’re filtering by matching the start of branch names, you don’t need to drop the “@squashed” part, unless I’m missing something.)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Sep 11 at 8:17









        Stephen Kitt

        147k22323392




        147k22323392



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f468159%2fcheck-if-current-branch-is-in-newline-delimited-list%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay