diff mbox series

contrib: Check and if needed set user.name and user.email in gcc-git-customization.sh

Message ID ee261435-f1c6-81e5-2a8f-e87b123f2ce2@arm.com
State New
Headers show
Series contrib: Check and if needed set user.name and user.email in gcc-git-customization.sh | expand

Commit Message

Richard Earnshaw (lists) Jan. 15, 2020, 4:59 p.m. UTC
As discussed on IRC, this adds a couple more checks in the customization 
setup for git.  If the variables user.name and user.email are not set 
anywhere in the git config hierarchy, we set some local values.  We 
always ask about the values we detect and if the user gives an answer 
that is new, we save that in the local config: this gives the 
opportunity to use different values to those configured for the global 
space.

I've also cleaned up a couple of minor niggles, such as using $() rather 
than `` for subshells and some quoting issues when using eval.

	* gcc-git-customization.sh: Use $() instead of ``, and fix variable
	quoting with eval.  Set user.name and user.email if not found in the
	local or config.

Comments

Richard Earnshaw (lists) Jan. 16, 2020, 1:52 p.m. UTC | #1
On 15/01/2020 16:59, Richard Earnshaw (lists) wrote:
> As discussed on IRC, this adds a couple more checks in the customization 

> setup for git.  If the variables user.name and user.email are not set 

> anywhere in the git config hierarchy, we set some local values.  We 

> always ask about the values we detect and if the user gives an answer 

> that is new, we save that in the local config: this gives the 

> opportunity to use different values to those configured for the global 

> space.

> 

> I've also cleaned up a couple of minor niggles, such as using $() rather 

> than `` for subshells and some quoting issues when using eval.

> 

>      * gcc-git-customization.sh: Use $() instead of ``, and fix variable

>      quoting with eval.  Set user.name and user.email if not found in the

>      local or config.


I'm not sure what I did wrong yesterday when I (thought I wrote)

	git config --get "user.name"

and found it not looking across all config scopes, but clearly I did 
something sill as the above does 'just work'.  So this is what I've 
checked in.

R.
diff --git a/contrib/gcc-git-customization.sh b/contrib/gcc-git-customization.sh
index dae2c35bb57..1cde6fd8224 100755
--- a/contrib/gcc-git-customization.sh
+++ b/contrib/gcc-git-customization.sh
@@ -11,9 +11,9 @@ ask () {
     read answer
     if [ "x$answer" = "x" ]
     then
-	eval $var=$default
+	eval $var=\"$default\"
     else
-	eval $var=$answer
+	eval $var=\"$answer\"
     fi
 }
 
@@ -30,7 +30,52 @@ git config alias.gcc-undescr \!"f() { o=\$(git config --get gcc-config.upstream)
 # *.md    diff=md
 git config diff.md.xfuncname '^\(define.*$'
 
-upstream=`git config --get "gcc-config.upstream"`
+set_user=$(git config --get "user.name")
+set_email=$(git config --get "user.email")
+
+if [ "x$set_user" = "x" ]
+then
+    # Try to guess the user's name by looking it up in the password file
+    new_user=$(getent passwd $(whoami) | awk -F: '{ print $5 }')
+    if [ "x$new_user" = "x" ]
+    then
+       new_user="(no default)"
+    fi
+else
+    new_user=$set_user
+fi
+ask "Your name" "${new_user}" new_user
+if [ "x$new_user" = "x(no default)" ]
+then
+    echo "Cannot continue, git needs to record your name against commits"
+    exit 1
+fi
+
+if [ "x$set_email" = "x" ]
+then
+    new_email="(no_default)"
+else
+    new_email=$set_email
+fi
+
+ask "Your email address (for git commits)" "${new_email}" new_email
+if [ "x$new_email" = "x(no default)" ]
+then
+    echo "Cannot continue, git needs to record your email address against commits"
+    exit 1
+fi
+
+if [ "x$set_user" != "x$new_user" ]
+then
+    git config "user.name" "$new_user"
+fi
+
+if [ "x$set_email" != "x$new_email" ]
+then
+    git config "user.email" "$new_email"
+fi
+
+upstream=$(git config --get "gcc-config.upstream")
 if [ "x$upstream" = "x" ]
 then
     upstream="origin"
@@ -38,27 +83,27 @@ fi
 ask "Local name for upstream repository" "origin" upstream
 git config "gcc-config.upstream" "$upstream"
 
-remote_id=`git config --get "gcc-config.user"`
+remote_id=$(git config --get "gcc-config.user")
 if [ "x$remote_id" = "x" ]
 then
     # See if the url specifies the remote user name.
-    url=`git config --get "remote.$upstream.url"`
+    url=$(git config --get "remote.$upstream.url")
     if [ "x$url" = "x" ]
     then
 	# This is a pure guess, but for many people it might be OK.
-	remote_id=`whoami`
+	remote_id=$(whoami)
     else
-	remote_id=`echo $url | sed -r "s|^.*ssh://(.+)@gcc.gnu.org.*$|\1|"`
+	remote_id=$(echo $url | sed -r "s|^.*ssh://(.+)@gcc.gnu.org.*$|\1|")
 	if [ x$remote_id = x$url ]
 	then
-	    remote_id=`whoami`
+	    remote_id=$(whoami)
 	fi
     fi
 fi
 ask "Account name on gcc.gnu.org (for your personal branches area)" $remote_id remote_id
 git config "gcc-config.user" "$remote_id"
 
-old_pfx=`git config --get "gcc-config.userpfx"`
+old_pfx=$(git config --get "gcc-config.userpfx")
 if [ "x$old_pfx" = "x" ]
 then
     old_pfx="me"
@@ -72,7 +117,7 @@ echo "Setting up tracking for personal namespace $remote_id in remotes/$upstream
 git config --replace-all "remote.${upstream}.fetch" "+refs/users/${remote_id}/heads/*:refs/remotes/${upstream}/${new_pfx}/*" ":refs/remotes/${upstream}/${old_pfx}/"
 git config --replace-all "remote.${upstream}.fetch" "+refs/users/${remote_id}/tags/*:refs/tags/${new_pfx}/*" ":refs/tags/${old_pfx}/"
 
-push_rule=`git config --get "remote.${upstream}.push"`
+push_rule=$(git config --get "remote.${upstream}.push")
 if [ "x$push_rule" != "x" ]
 then
     echo "***********************************************"
Andreas Schwab Jan. 16, 2020, 2:49 p.m. UTC | #2
On Jan 16 2020, Richard Earnshaw (lists) wrote:

> diff --git a/contrib/gcc-git-customization.sh b/contrib/gcc-git-customization.sh

> index dae2c35bb57..1cde6fd8224 100755

> --- a/contrib/gcc-git-customization.sh

> +++ b/contrib/gcc-git-customization.sh

> @@ -11,9 +11,9 @@ ask () {

>      read answer

>      if [ "x$answer" = "x" ]

>      then

> -	eval $var=$default

> +	eval $var=\"$default\"

>      else

> -	eval $var=$answer

> +	eval $var=\"$answer\"


This still isn't the safe way to do indirect assignment.  The expansion
of the rhs needs to be delayed, so that the result isn't subject to
further expansions.

Andreas.

Subject: [PATCH] gcc-git-customization.sh: avoid double expansion

---
 contrib/gcc-git-customization.sh | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/contrib/gcc-git-customization.sh b/contrib/gcc-git-customization.sh
index b7e4ce308062..26f4389bcc8a 100755
--- a/contrib/gcc-git-customization.sh
+++ b/contrib/gcc-git-customization.sh
@@ -11,9 +11,9 @@ ask () {
     read answer
     if [ "x$answer" = "x" ]
     then
-	eval $var=\"$default\"
+	eval $var=\$default
     else
-	eval $var=\"$answer\"
+	eval $var=\$answer
     fi
 }
 
-- 
2.25.0


-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."
Richard Earnshaw (lists) Jan. 17, 2020, 11:24 a.m. UTC | #3
On 16/01/2020 14:49, Andreas Schwab wrote:
> On Jan 16 2020, Richard Earnshaw (lists) wrote:

> 

>> diff --git a/contrib/gcc-git-customization.sh b/contrib/gcc-git-customization.sh

>> index dae2c35bb57..1cde6fd8224 100755

>> --- a/contrib/gcc-git-customization.sh

>> +++ b/contrib/gcc-git-customization.sh

>> @@ -11,9 +11,9 @@ ask () {

>>       read answer

>>       if [ "x$answer" = "x" ]

>>       then

>> -	eval $var=$default

>> +	eval $var=\"$default\"

>>       else

>> -	eval $var=$answer

>> +	eval $var=\"$answer\"

> 

> This still isn't the safe way to do indirect assignment.  The expansion

> of the rhs needs to be delayed, so that the result isn't subject to

> further expansions.

> 

> Andreas.

> 

> Subject: [PATCH] gcc-git-customization.sh: avoid double expansion

> 

> ---

>   contrib/gcc-git-customization.sh | 4 ++--

>   1 file changed, 2 insertions(+), 2 deletions(-)

> 

> diff --git a/contrib/gcc-git-customization.sh b/contrib/gcc-git-customization.sh

> index b7e4ce308062..26f4389bcc8a 100755

> --- a/contrib/gcc-git-customization.sh

> +++ b/contrib/gcc-git-customization.sh

> @@ -11,9 +11,9 @@ ask () {

>       read answer

>       if [ "x$answer" = "x" ]

>       then

> -	eval $var=\"$default\"

> +	eval $var=\$default

>       else

> -	eval $var=\"$answer\"

> +	eval $var=\$answer

>       fi

>   }

>   

> 


OK.
diff mbox series

Patch

diff --git a/contrib/gcc-git-customization.sh b/contrib/gcc-git-customization.sh
index dae2c35bb57..60694fd8cc0 100755
--- a/contrib/gcc-git-customization.sh
+++ b/contrib/gcc-git-customization.sh
@@ -11,12 +11,49 @@  ask () {
     read answer
     if [ "x$answer" = "x" ]
     then
-	eval $var=$default
+	eval $var=\"$default\"
     else
-	eval $var=$answer
+	eval $var=\"$answer\"
     fi
 }
 
+# Search the git config in multiple scopes upto the level specified.
+# Start in --local, if not set there then try global, then system.
+get_conf_upto () {
+    if [ $# -ne 3 ]
+    then
+	echo "get_conf_upto --local|--global|--system <confvar> <setvar>"
+	exit 1
+    fi
+    opt=$1
+    conf=$2
+    result=$3
+    case $opt in
+	--local|--global|--system) ;;
+	*) echo "get_conf_upto --local|--global|--system <confvar> <setvar>"
+	   exit 1
+	   ;;
+    esac
+    v=$(git config --get --local $conf)
+    if [ -n "$v" -o "$opt" = "--local" ]
+    then
+	eval $result=\"$v\"
+	return
+    fi
+
+    v=$(git config --get --global $conf)
+    if [ -n "$v" -o "$opt" = "--global" ]
+    then
+	eval $result=\"$v\"
+	return
+    fi
+
+    v=$(git config --get --system $conf)
+    eval $result=\"$v\"
+    return
+}
+
+
 # Add a git command to find the git commit equivalent to legacy SVN revision NNN
 git config alias.svn-rev '!f() { rev=$1; shift; git log --all --grep="From-SVN: r\\?$rev\\b" "${@}"; } ; f'
 
@@ -30,7 +67,52 @@  git config alias.gcc-undescr \!"f() { o=\$(git config --get gcc-config.upstream)
 # *.md    diff=md
 git config diff.md.xfuncname '^\(define.*$'
 
-upstream=`git config --get "gcc-config.upstream"`
+get_conf_upto --global "user.name" set_user
+get_conf_upto --global "user.email" set_email
+
+if [ "x$set_user" = "x" ]
+then
+    # Try to guess the user's name by looking it up in the password file
+    new_user=$(getent passwd $(whoami) | awk -F: '{ print $5 }')
+    if [ "x$new_user" = "x" ]
+    then
+       new_user="(no default)"
+    fi
+else
+    new_user=$set_user
+fi
+ask "Your name" "${new_user}" new_user
+if [ "x$new_user" = "x(no default)" ]
+then
+    echo "Cannot continue, git needs to record your name against commits"
+    exit 1
+fi
+
+if [ "x$set_email" = "x" ]
+then
+    new_email="(no_default)"
+else
+    new_email=$set_email
+fi
+
+ask "Your email address (for git commits)" "${new_email}" new_email
+if [ "x$new_email" = "x(no default)" ]
+then
+    echo "Cannot continue, git needs to record your email address against commits"
+    exit 1
+fi
+
+if [ "x$set_user" != "x$new_user" ]
+then
+    git config "user.name" "$new_user"
+fi
+
+if [ "x$set_email" != "x$new_email" ]
+then
+    git config "user.email" "$new_email"
+fi
+
+upstream=$(git config --get "gcc-config.upstream")
 if [ "x$upstream" = "x" ]
 then
     upstream="origin"
@@ -38,27 +120,27 @@  fi
 ask "Local name for upstream repository" "origin" upstream
 git config "gcc-config.upstream" "$upstream"
 
-remote_id=`git config --get "gcc-config.user"`
+remote_id=$(git config --get "gcc-config.user")
 if [ "x$remote_id" = "x" ]
 then
     # See if the url specifies the remote user name.
-    url=`git config --get "remote.$upstream.url"`
+    url=$(git config --get "remote.$upstream.url")
     if [ "x$url" = "x" ]
     then
 	# This is a pure guess, but for many people it might be OK.
-	remote_id=`whoami`
+	remote_id=$(whoami)
     else
-	remote_id=`echo $url | sed -r "s|^.*ssh://(.+)@gcc.gnu.org.*$|\1|"`
+	remote_id=$(echo $url | sed -r "s|^.*ssh://(.+)@gcc.gnu.org.*$|\1|")
 	if [ x$remote_id = x$url ]
 	then
-	    remote_id=`whoami`
+	    remote_id=$(whoami)
 	fi
     fi
 fi
 ask "Account name on gcc.gnu.org (for your personal branches area)" $remote_id remote_id
 git config "gcc-config.user" "$remote_id"
 
-old_pfx=`git config --get "gcc-config.userpfx"`
+old_pfx=$(git config --get "gcc-config.userpfx")
 if [ "x$old_pfx" = "x" ]
 then
     old_pfx="me"
@@ -72,7 +154,7 @@  echo "Setting up tracking for personal namespace $remote_id in remotes/$upstream
 git config --replace-all "remote.${upstream}.fetch" "+refs/users/${remote_id}/heads/*:refs/remotes/${upstream}/${new_pfx}/*" ":refs/remotes/${upstream}/${old_pfx}/"
 git config --replace-all "remote.${upstream}.fetch" "+refs/users/${remote_id}/tags/*:refs/tags/${new_pfx}/*" ":refs/tags/${old_pfx}/"
 
-push_rule=`git config --get "remote.${upstream}.push"`
+push_rule=$(git config --get "remote.${upstream}.push")
 if [ "x$push_rule" != "x" ]
 then
     echo "***********************************************"