2020-08-31 When were changes from a specific commit merged

A situation that happens to me from time to time is when I have a particular commit in my Git repository (often taken from the output of git blame) and I’d like to know when (and with what pull request) it was merged to the main branch (usually develop).

This is not as easy as it sounds. I could take this commit and find the first merge among its descendants, but this would fail in more complicated scenarios (like develop being merged to the feature branch, which happens pretty often).

It turns out that there is a quite old question about this particular use-case on StackOverflow. There are a few answers to that question, some older, some newer, but the consensus seems to be that what needs to be done is more or less this. First, take the result of

git rev-list some_commit..develop --ancestry-path

This shows all commits that are descendants of some_commit and ancestors of develop – in other words, everything that happened “between” some_commit and develop (where “between” is not in the chronological sense, but in the sense of the position on the history graph). Then, run

git rev-list some_commit..develop --first-parent

This shows all ancestors of develop from the main development line. Now, the last entry present in both lists (git rev-list sorts in reverse chronological order) is the exact place where both sets first crossed, that is, the moment some_commit found its way to develop.

This is pretty much it. The only thing left is to package this knowledge into some nice script, and this is precisely what a few people did (several such scripts are mentioned in the answers to the aforementioned question). I tried the one here (though I only skimmed through the code) and it seems to work very well. (I put the script into ~/bin and called it git-find-merge, so I effectively created for myself a new Git command git find-merge.)

CategoryEnglish, CategoryBlog, CategoryGit

Comments on this page

2020-08-24 Sorting object keys with Lodash

I have been extremely busy recently, so I have only a short tip today. Imagine having a JavaScript object like this:

  a: 1,
  b: 3,
  c: 2,

and needing to have an (almost) identical object, but sorted by increasing values. (Yes, I know that theoretically the order of object properties does not matter and should not be relied upon, but come on.) Here is a very simple way to obtain this (using Lodash): ,

const _ = require('lodash');
const source = {
	a: 1,
	b: 3,
	c: 2,
const target = _(source)

There’s not really a lot more to say about this – we convert the object to an array, sort it, and convert back (well, I used the Lodash chaining, but this is not really necessary).

CategoryEnglish, CategoryBlog, CategoryJavaScript

Comments on this page

2020-08-17 A simple bash one-liner to check a bunch of files for the terminating newline

Some time ago I had a bunch of csv files in a directory, and some of them had terminating newlines. I wanted to check if every one of them was newline-terminated. After a while of thinking, I came up with this Bash one-liner:

for f in *.csv; do tail -c1 $f; done | sort -u | diff -q - <(echo)

Simply enough, we iterate over every file, take its last character (tail puts it on a line on its own), sort these characters and eliminate duplicates. If the result is just a newline, diff​ing with a temporary file containing only a newline will report no differences, otherwise, diff will tell you that “files differ” (which means that at least one of the files did not have a terminating newline). The downside is that it doesn’t tell you which one – fixing that issue is left as an exercise to the reader. ;-)

CategoryEnglish, CategoryBlog

Comments on this page