chriseldredge / git-slack-hook

Git receive hook to notify Slack channels
291 stars 51 forks source link

Commit messages garbled in notifications; sed-related #20

Closed targi closed 8 years ago

targi commented 9 years ago

I've recently set up git-slack-hook on a machine running Debian squeeze. My config looks like this:

[hooks "slack"]
        webhook-url = https://hooks.slack.com/services/...
        channel = "#notify"
        username = Git
        icon-emoji = :twisted_rightwards_arrows:
        repo-nice-name = testrepo
        show-only-last-commit = false

Messages from the hook appear garbled, with all commit messages but the last one smashed together:

GitBOT [12:35]
3 new commits ​*pushed*​ to ​*master*​ in testrepo

fog;;;;;test: Fix typo in README fog;;;;;test: Modify README fog
----------------
Add script in Python

On my Mac (running OS X 10.10.3), the same configuration works correctly:

GitBOT [12:35]
3 new commits ​*pushed*​ to ​*master*​ in testrepo

fog
----------------
test: Fix typo in README

fog
----------------
test: Modify README

fog
----------------
Add script in Python

Closed investigation reveals that a certain call to sed works differently on my Debian machine and on my Mac.

On the Mac (as intended):

mac:testrepo targi$ git log --pretty=format:"%cN;;;;;%s" 27fac..73e2 | sed ':a;N;$!ba;s/\n/\\n/g'
fog;;;;;test: Fix typo in README
fog;;;;;test: Modify README
fog;;;;;Add script in Python

On the Debian machine (problem):

targi@debian:~/tmp/testrepo.git$ git log --pretty=format:"%cN;;;;;%s" 27fac..73e2 | sed ':a;N;$!ba;s/\n/\\n/g'
fog;;;;;test: Fix typo in README\nfog;;;;;test: Modify README\nfog;;;;;Add script in Python

On the Debian machine, I have GNU sed version 4.2.1.

As I don't need full commit messages in the notification, I replaced the call to sed with a no-op:

-sed ':a;N;$!ba;s/\n/\\n/g'
+sed ':a;N;$!ba;s/\n/\n/g'

This is obviously a work-around (breaks full commit messages). For reference, it's available at targi/git-slack-hook@2148553cb0c1761948426c49fa42355c76ee4f71.

grappler commented 9 years ago

I had the same problem. I solved it by changing the format to match more how the GitHub integration looks like.

    # Process the log and escape double quotes; assuming for now that committer names don't have five semicolons in them
    log_out=$( git log --pretty=format:"${formattedurl}${commitformat} - %cN" $countarg ${start}..${end} \
             | sed ':a;N;$!ba;s/\n/\n/g' \
             | sed -e 's/\\/\\\\/g' \
             | sed -e 's/"/\\"/g' \
             | sed -e 's/\(.*\)\(.*\)/{"title":"","value":"\1","short":false},/' )

    fields=${log_out%?}