threads / patch / 23061

patchRe: [PATCH] pull: replace unnecessary sed invocation

Subject: Re: [PATCH] pull: replace unnecessary sed invocation

## tl;dr

4 messages between Mar 18, 2010 and Mar 18, 2010. Diffs are folded; open one to read it.

replies: 3people: 4as markdown or json

Stephen Boyd· Mar 18, 2010, 05:10 UTC · lore

[PATCH] pull: replace unnecessary sed invocation

Getting the shortened branch name is as easy as using the shell's parameter expansion.

Signed-off-by: Stephen Boyd <bebarino@gmail.com>
---
Saw this while reading the code.
 git-pull.sh |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
Show changes to git-pull.sh +1 −1
diff --git a/git-pull.sh b/git-pull.sh
index 38331a8..246a3a4 100755
--- a/git-pull.sh
+++ b/git-pull.sh
@@ -41,7 +41,7 @@ strategy_args= diffstat= no_commit= squash= no_ff= ff_only=
 log_arg= verbosity=
 merge_args=
 curr_branch=$(git symbolic-ref -q HEAD)
-curr_branch_short=$(echo "$curr_branch" | sed "s|refs/heads/||")
+curr_branch_short="${curr_branch#refs/heads/}"
 rebase=$(git config --bool branch.$curr_branch_short.rebase)
 while :
 do
-- 
1.7.0.2.276.g60daf
Gabriel Filion· Mar 18, 2010, 13:30 UTC · re: Stephen Boyd · lore
On 2010-03-18 01:10, Stephen Boyd wrote:
Show 6 quoted lines
> Getting the shortened branch name is as easy as using the shell's
> parameter expansion.
> 
>  curr_branch=$(git symbolic-ref -q HEAD)
> -curr_branch_short=$(echo "$curr_branch" | sed "s|refs/heads/||")
> +curr_branch_short="${curr_branch#refs/heads/}"

This modification is a bashism. This syntax is not compatible with other shells.

-- 
Gabriel Filion
Brandon Casey· Mar 18, 2010, 14:46 UTC · re: Gabriel Filion · lore
On 03/18/2010 08:30 AM, Gabriel Filion wrote:
Show 10 quoted lines
> On 2010-03-18 01:10, Stephen Boyd wrote:
>> Getting the shortened branch name is as easy as using the shell's
>> parameter expansion.
>>
>>  curr_branch=$(git symbolic-ref -q HEAD)
>> -curr_branch_short=$(echo "$curr_branch" | sed "s|refs/heads/||")
>> +curr_branch_short="${curr_branch#refs/heads/}"
> 
> This modification is a bashism. This syntax is not compatible with other
> shells.

But, there is already precedence for using syntax in scripts, so it is probably ok.

$ egrep -- '\$\{[^}]+#[^}]+\}' *.sh git-bisect.sh: start_head="${head#refs/heads/}" git-filter-branch.sh: ref="${ref#refs/tags/}" git-parse-remote.sh: heads/*) remote=${remote#heads/} ;; git-parse-remote.sh: refs/heads/*) remote=${remote#refs/heads/} ;; git-pull.sh: curr_branch=${curr_branch#refs/heads/} git-pull.sh: echo "Your configuration specifies to $op_type $op_prep the ref '${upstream#refs/heads/}'" git-rebase--interactive.sh: pend="${pend# $p}" git-rebase--interactive.sh: new_parents=${new_parents# $first_parent} git-rebase--interactive.sh: if left=${1%...*} right=${1#*...} && git-rebase.sh: eval GITHEAD_$cmt='"${cmt_name##refs/heads/}~$(($end - $msgnum))"' git-rebase.sh: if left=${onto_name%...*} right=${onto_name#*...} && git-stash.sh: branch=${branch#refs/heads/} git-submodule.sh: url="${url#../}" git-submodule.sh: url="${url#./}"

Junio C Hamano· Mar 18, 2010, 14:48 UTC · re: Gabriel Filion · lore
Gabriel Filion <lelutin@gmail.com> writes:
>> +curr_branch_short="${curr_branch#refs/heads/}"
>
> This modification is a bashism. This syntax is not compatible with other
> shells.

To me, the above looks like "${parameter#word}", which is a plain vanilla POSIX shell construct that we adopted to use in our scripts.

There are bash-only constructs we decided never to use in our scripts; among them are "${parameter/pattern/string}" and "${parameter:offset}", both are not in POSIX.

← back to recent threads