3

At day job there are some automated commits made to our git repos with a specific subject: "Job: sync <unique-id> <timestamp>"

A script exists that finds the latest commit of this type and extracts the <unique-id>, using something like: git log --pretty=format:"%s" --grep='Job: sync' -n 1 | awk '{print $3}'

A problem arose when someone created a commit that contains the text "Job: sync" somewhere in the commit message body (not in the first line of the message).

It turns out git log --grep= searches the whole message and not just the subject.

Problem has been partially mitigated by changing --grep='Job: sync' to --grep='^Job: sync' (so it only matches at the beginning of a line). But in theory someone can create a commit message that will also match the new grep pattern.

My question: How do I best search for all commit messages in a git repo that contains a specific text in its subject (first line of the commit message)?

2
  • Someone can also manually create a commit message that exactly matches the format of those automated commits, so this can never be bulletproof. Maybe also filter on author or committer? That would probably rule out all accidents (but of course not prevent malice). Commented Nov 14, 2024 at 12:44
  • @thomas I do not think someone would create a commit with the same text in the subject. But it is still something good to keep in mind. Thank you. Commented Nov 14, 2024 at 14:23

1 Answer 1

2
git log --oneline | grep " Job: sync"

A space before the pattern to match the pattern after the commit IDs. Try git log --oneline to see its output.

grep "Job: sync" (without the leading space) to search the pattern everywhere in the subject line.

Sign up to request clarification or add additional context in comments.

1 Comment

I was hoping for something built into git. But maybe piping to grep is the best available solution. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.