Check if current branch is in newline delimited list
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
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:
I can't figure out how to use tr/sed to only replace '@squashed' if the branch name ends with that string.
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
add a comment |Â
up vote
0
down vote
favorite
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:
I can't figure out how to use tr/sed to only replace '@squashed' if the branch name ends with that string.
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
would it make more sense to assign the output ofgit 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
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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:
I can't figure out how to use tr/sed to only replace '@squashed' if the branch name ends with that string.
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
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:
I can't figure out how to use tr/sed to only replace '@squashed' if the branch name ends with that string.
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
scripting git
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 ofgit 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
add a comment |Â
would it make more sense to assign the output ofgit 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
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
tr
replaces single characters, not strings. As you surmise you can usesed
to delete a string at the end of a line:git branch --merged dev | sed 's/@squashed$//'
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.)
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
tr
replaces single characters, not strings. As you surmise you can usesed
to delete a string at the end of a line:git branch --merged dev | sed 's/@squashed$//'
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.)
add a comment |Â
up vote
1
down vote
tr
replaces single characters, not strings. As you surmise you can usesed
to delete a string at the end of a line:git branch --merged dev | sed 's/@squashed$//'
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.)
add a comment |Â
up vote
1
down vote
up vote
1
down vote
tr
replaces single characters, not strings. As you surmise you can usesed
to delete a string at the end of a line:git branch --merged dev | sed 's/@squashed$//'
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.)
tr
replaces single characters, not strings. As you surmise you can usesed
to delete a string at the end of a line:git branch --merged dev | sed 's/@squashed$//'
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.)
answered Sep 11 at 8:17
Stephen Kitt
147k22323392
147k22323392
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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