1

If have several Git repositories containing a file mergedriver.info

This file looks always like this:

<project name>
<repository name>

A script, triggered by a Git merge driver, is evaluating this file:

mergedriverinfo="$(git cat-file -p HEAD:mergedriver.info)"
success=$?
if [[ "$success" == "0" ]]; then
    log "Evaluating mergedriver.info"

    PROJECT_KEY="$(sed -E 's/([^\s]+)\s+([^\s]+)/\1/' <<< $mergedriverinfo)"
    REPO_SLUG="$(sed -E 's/([^\s]+)\s+([^\s]+)/\2/' <<< $mergedriverinfo)"

    log "PROJECT_KEY=$PROJECT_KEY"
    log "REPO_SLUG=$REPO_SLUG"
else
    log "Unable to read mergedriver.info"
    exit 1
fi

I don't understand the behaviour of sed in this case.

For this mergedriver.info:

test
conflict-on-auto-merge

The log output looks like this:

2017-07-20 11:05:51.747 PROJECT_KEY=test
2017-07-20 11:05:51.748 REPO_SLUG=tesconflict-on-auto-merge

At first I tried reading the mergedriver.info with sed -n 1p/2p and head/tail -1, but unfortunately the output of $(git cat-file -p HEAD:mergedriver.info) is different for two different platforms on which this script is running:

Platform 1:

$ od -c <<< $(git cat-file -p HEAD:mergedriver.info)
0000000   t   e   s   t  \n   c   o   n   f   l   i   c   t   -   o   n
0000020   -   a   u   t   o   -   m   e   r   g   e  \n
0000034

Platform 2:

±  od -c <<< $(git cat-file -p HEAD:mergedriver.info)
0000000   t   e   s   t       c   o   n   f   l   i   c   t   -   o   n
0000020   -   a   u   t   o   -   m   e   r   g   e  \n
0000034

How to solve this problem?

BlackEye
  • 775
  • 11
  • 25

1 Answers1

1

Since you are using <<<, then I guess you're using bash, so you can use mapfile to split your variable into an array, one element per line:

$ echo "$mergedriverinfo"
test
conflict-on-auto-merge
$ mapfile -t lines <<<"$mergedriverinfo"
$ echo "${lines[0]}"
test
$ echo "${lines[1]}"
conflict-on-auto-merge

So your project key is element 0 and the repo slug is element 1.

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
  • I tested this soltution and it works fine, but I already solved the problem using another fine solution I got from Rakesh Sharma on unix.stackexchange.com: PROJECT_KEY=$(sed -nEe '$!N;s/(\S+)\s+(\S+)/\1/p' <<<"$mergedriverinfo") REPO_SLUG=$(sed -nEe '$!N;s/(\S+)\s+(\S+)/\2/p' <<<"$mergedriverinfo") – BlackEye Jul 20 '17 at 12:04
  • I'm not sure why you would want to use two calls to `sed` rather than one call to a built-in command. Also note that you shouldn't cross-post the same question on multiple sites. – Tom Fenech Jul 20 '17 at 12:08