0

I can find commits whose message is equal to $1 if I'm sure $1 has no special characters with git log --grep "^${1}$"

I can ignore special characters and search for strings containing a pattern with git log --fixed-strings --grep "$1"

I would like to have a bash function which returns all commits which have exactly the commit message $1

get_commits_by_message() {
  echo git log --format=%H ... $1;
}

How can I get the hashes of commits which have messages equal to $1, if $1 contains special characters?

Marvin Irwin
  • 938
  • 7
  • 17
  • 1
    Does this answer your question? [Is it possible to escape regex metacharacters reliably with sed](https://stackoverflow.com/questions/29613304/is-it-possible-to-escape-regex-metacharacters-reliably-with-sed) – LeGEC Sep 19 '20 at 06:48
  • Would you by any chance try to spot commits that were cherry-picked or rebased from one branch to another ? – LeGEC Sep 19 '20 at 19:19
  • @LeGEC Yes, I'm trying to make sure that I have cherry picked all commits from branch A to branch B. – Marvin Irwin Sep 19 '20 at 23:15

1 Answers1

1

[edit] If you want to spot what commits have been cherry-picked from A to B, try :

git log --graph --oneline --boundaries --cherry-mark A...B  # 3 dots

--cherry-mark will flag commits that bring the same changes on both sides with an =.

If you replace --cherry-mark with --cherry-pick, the matching commits will be completely hidden, and you can compare the messages of the remaining commits to see if they were replayed, but modified (to fix a conflict for example).


[original answer]

a. write a function (or a script) which turns an input string, with special characters, into its escaped form

e.g : turns \ . [ ] * ^ $ into \\ \. \[ \] \* \^ \$ (I may be forgetting some other special chars)

and use it in your git log command :

# example for single line patterns :
rescape () { echo "$1" | sed -e 's/\([\^\[$.*]\)/\\\1/g' -e 's/]/\\]/g'; }

git log --grep "^$(rescape 'I can grep * and . and [ and ] !')$"

b. (technically not exact, but should work in most cases) if you want a quick way to manually type the message, replace any special char with . :

# instead of grepping for :
^[1234] expand . to *$

# grep for :
^.1234. expand . to .$
LeGEC
  • 46,477
  • 5
  • 57
  • 104