Subject: bug#28056: closed (Re: bug#28056: Substitution doing global when it should not.)

From: [email-masked] (GNU bug Tracking System)
Date: Fri, 11 Aug 2017 22:40:03 +0000

Your bug report

#28056: Substitution doing global when it should not.

which was filed against the sed package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
28056: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=28056
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: bug#28056: Substitution doing global when it should not.
From: Eric Blake <[email-masked]>
To: Techwolf Lupindo <[email-masked]>, [email-masked]
Date: Fri, 11 Aug 2017 17:39:27 -0500
List-Id: <debbugs-submit.debbugs.gnu.org>

tag 28056 notabug
thanks

On 08/11/2017 03:04 PM, Techwolf Lupindo wrote:

> techwolf@laptop ~/test $ sed -e 's:COMMAND ${MERCURIAL}:COMMAND
> ${MERCURIAL} --cwd ${CMAKE_SOURCE_DIR}:' BuildVersion.cmake >
> BuildVersion1.cmake
> techwolf@laptop ~/test $ sed -e 's:COMMAND ${MERCURIAL}:COMMAND
> ${MERCURIAL} --cwd ${CMAKE_SOURCE_DIR}:g' BuildVersion.cmake >
> BuildVersion2.cmake

> 
> The only difference in the sed commands was s/// and s///g.

Which tells sed whether to make one substitution on every affected line,
or as many substitutions as possible on every affected line.  But the
number of affected lines remains unchanged for the two variants.

> The diff
> between BuildVersion1.cmake and BuildVersion2.cmake should had one change.

Rather, one change per affected line.

> But sed is matching two lines without the s///g. All docs I have read says
> that without 'g', only the first match is changed.

Only the first match per line - but when both lines affected only had
one match per line, there's no difference in using the 'g' flag.

If you want sed to stop processing as soon as it has changed one line,
rather than going on to look for additional affected lines, you can do so:

sed -e '/COMMAND ${MERCURIAL}/ { s::COMMAND ${MERCURIAL} -- cwd
${CMAKE_SOURCE_DIR}: ; q }'

which says find the first line that matches the pattern, and on that
line make the substitution and immediately quit.

Since sed is behaving as documented, I'm closing this as not a bug;
however, feel free to add further comments to this thread if you need
followup clarification.

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org

Subject: Substitution doing global when it should not.
From: Techwolf Lupindo <[email-masked]>
To: [email-masked]
Date: Fri, 11 Aug 2017 16:04:03 -0400
List-Id: <debbugs-submit.debbugs.gnu.org>

techwolf@laptop ~/test $ sed --version
sed (GNU sed) 4.4
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html
>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Jay Fenlason, Tom Lord, Ken Pizzini,
and Paolo Bonzini.
GNU sed home page: <http://www.gnu.org/software/sed/>.
General help using GNU software: <http://www.gnu.org/gethelp/>.
E-mail bug reports to: <[email-masked]>.

techwolf@laptop ~/test $ wget "
https://hg.phoenixviewer.com/phoenix-firestorm-lgpl/raw-file/9d58c58cca90/indra/cmake/BuildVersion.cmake
"

<snipped>

2017-08-11 15:39:45 (38.2 MB/s) - ‘BuildVersion.cmake’ saved [3605/3605]

techwolf@laptop ~/test $ sed -e 's:COMMAND ${MERCURIAL}:COMMAND
${MERCURIAL} --cwd ${CMAKE_SOURCE_DIR}:' BuildVersion.cmake >
BuildVersion1.cmake
techwolf@laptop ~/test $ sed -e 's:COMMAND ${MERCURIAL}:COMMAND
${MERCURIAL} --cwd ${CMAKE_SOURCE_DIR}:g' BuildVersion.cmake >
BuildVersion2.cmake
techwolf@laptop ~/test $ diff -u BuildVersion1.cmake BuildVersion2.cmake
techwolf@laptop ~/test $ diff -u BuildVersion.cmake BuildVersion1.cmake
--- BuildVersion.cmake  2017-08-11 15:39:45.639970357 -0400
+++ BuildVersion1.cmake 2017-08-11 15:40:47.646763237 -0400
@@ -25,10 +25,10 @@
                  #         building an earlier revision. Instead, we use
                  #         "hg identify -n" to get the local revision
number
                  #         of the actual state of the repository.
-                 #COMMAND ${MERCURIAL} log -r tip:0 --template '\\n'
+                 #COMMAND ${MERCURIAL} --cwd ${CMAKE_SOURCE_DIR} log -r
tip:0 --template '\\n'
                  #COMMAND ${WORDCOUNT} -l
                  #COMMAND ${SED} "s/ //g"
-                 COMMAND ${MERCURIAL} identify -n
+                 COMMAND ${MERCURIAL} --cwd ${CMAKE_SOURCE_DIR} identify -n
                  COMMAND ${SED} "s/+//"        # [CR] Strip off any + from
the revision number
                  OUTPUT_VARIABLE VIEWER_VERSION_REVISION
                  OUTPUT_STRIP_TRAILING_WHITESPACE

The only difference in the sed commands was s/// and s///g. The diff
between BuildVersion1.cmake and BuildVersion2.cmake should had one change.
But sed is matching two lines without the s///g. All docs I have read says
that without 'g', only the first match is changed. However, this is not in
my case and was not notice for over two years due to my build script not
breaking. I only notice this bug when I was doing some code cleanup and
scratching my head as to why my build script was not breaking when in
theory, it should had an error printed when hg could not see the repo.