Diff for commit with GitHub API

Here is how to get the diff of a commit using the GitHub API.

For a project I need to programmatically get the diff of a commit of a pull request, and as every good programmer would do, I first checked this topic on Stack Overflow because for sure someone else ran into this problem before and asked about it. That might be the case here, but it must be very well buried somewhere. But I found this post showing how to get that diff using curl instead of GitHubs REST API, which works well on a command line.

The GitHub API is not very clear about it. It just says “You can pass the appropriate media type to fetch diff and patch formats.” – but thats already the right hint we need. Now we can tweak the example we found a little, and here is how:

https://api.github.com/repos/{owner}/{repo-name}/commits/{ref}

The header of this GET request should contain { 'accept' : 'application/vnd.github.v3.diff' }

Now this request will return to us with the real diff file as content.

Work with large numbers of files in a folder

A few shell commands to list, sort or move files in a large folder.

I have a component that writes a log file every time a task is executed, and they all go to the same folder. ls or Midnight Commander take a long time on that folder. I’d like to move the old files out of the way, e.g. to a folder called, well, old. Or I want to delete them. Also, I’d like to list the most recent files, maybe the largest of today, or just the last file that was written. Here a few commands that work on my Ubuntu bash:

find /data/logs/ -maxdepth 1 -mtime +30 -type f -exec mv "{}" old/ \;

Finds the files older than 30 days and moves them to a folder old/

find /data/logs/ -maxdepth 1 -type f -mtime -1

Lists the files no more than 1 day old.

find /data/logs/ -maxdepth 1 -type f -mtime -1 -exec du -ah  "{}" \;  | sort -n -r | head -n 10

Finds the files no older than 1 day, sorts them by size, and shows the largest 10.

out parameter in C++

What is an out-parameter? An out-parameter is a non const reference (or pointer) passed to the function, which will then modify it by setting a value.

In C++, we pass arguments by reference usually to avoid copying the object, but what about the behavior of the function taking these arguments.

Using pass-by-value is clear: the arguments are inputs, whereas pass-by-reference can be inputs, outputs or in-outs. This confuses the reader: one has to take extra steps to find out what it does, these constructs are not self-documenting.

I still often see that methods or functions take one or several references for the purpose to modify it. This is not intuitive, and can lead to unexpected behavior in your Cpp code.

If an object needs to be modified, a method on that object could be used instead. The modified object could also be returned, where it is clear that the return type is an ‘output’.

If you need to return several values, a std::tuple or std::pair can be used.