CodeMonkey CodeMonkey - 7 days ago 5
Bash Question

how to pre-fix a piece of text in github "git log" using shell-script

I need to make a github commit (the text), from the git command

git log
into a link in an email. So the recipient can click on the link and go directly to the change.

I receive a long list containing lines with the text:

commit some_long_string_of_hexadecimals


and I need to transform this into:

commit https://github.com/account/repo/commit/some_long_string_of_hexadecimals


The log I am receiving contain n-amount of these logs, so I need the script to do this for all instances of this (some_long_string_of_hexadecimals).

Here are a few example log statements:

commit a98a897a67896a987698a769786a987a6987697a6
Author: Some Person <some@email.com>
Date: Thu Sep 29 09:48:52 2016 +0200

long message describing change.

commit a98a897a67896a987698a769786a987a6987697a6
Author: Some Person <some@email.com>
Date: Thu Sep 29 09:48:52 2016 +0200

more description


I'd like it to look like this:

commit https://github.com/account/repo/commit/a98a897a67896a987698a769786a987a6987697a6
Author: Some Person <some@email.com>
Date: Thu Sep 29 09:48:52 2016 +0200

added handling of running tests from within a docker container


How do I achieve this using a shell command ?

Thanks in advance.

Answer
awk '$1 == "commit" {$2 = "https://github.com/account/repo/commit/" $2} 1'
  1. check if field 1 equals "commit"

  2. if so, prepend to field 2

  3. if line matched, print modified line, else print line as is

Comments