Today I Learned

This project exists to catalogue the sharing & accumulation of knowledge as it happens day-to-day. Posts have a 200-word limit, and posting is open to any Rocketeer as well as selected friends of JetRockets. We hope you enjoy learning along with us.

4 posts about #devops

Sort lines in the vim editor

Often when writing code, you need to sort the lines in alphabetical order. For example, in javascript, when importing a large number of modules, it is good practice to organize them alphabetically. Users of the vim editor can do this very simply. It is enough to select in the visual mode all the lines that need to be sorted and enter the command :sort. All rows are sorted!

Force Downloading of File instead of Opening in Browser

When you go through the link, some files will be opened in the browser. Such behavior is typical for some content types (e.g., images, pdf, etc.)

However, you can force file downloading instead of opening when an user clicks on the link though.

1st way (frontend):

HTML attribute download allows you to do this.

<a href="/public/report.pdf" download="stat_report">

If the value of the attribute is omitted, the original filename would be used. However, be careful. This attribute isn’t supported in some old browsers without HTML5 support. Renaming does not work if the given file stored on another host.

2nd way (backend):

You can set HTTP header Content-disposition.

for Nginx:

location ~* /public/(.+\.pdf)$ {
    add_header Content-disposition "attachment; filename=$1";
}

for Apache:

<IfModule mod_headers.c>
    <FilesMatch "\.(?i:pdf)$">
        ForceType application/octet-stream
        Header set Content-Disposition "attachment"
    </FilesMatch>
</IfModule>

Keep clean your git repos!

Many developers don’t keep their local repositories clean. There is a way how to automate it though. Let’s look at a few useful commands:

To clean refs to nonexistent branches in the remote:

$ git fetch --prune

--prune before fetching, remove any remote-tracking references that no longer exist on the remote.

To estimate how many branches merged into dev:

$ git branch --merged dev | wc -l

--merged option can filter list to branches that you have merged into the given branch. Squash and rebase merges usually aren’t detected by --merged.

List of branches merged into dev:

$ git branch --merged dev

List of remote branches merged into dev:

$ git branch --merged dev --remote

If you are courageous then:

$ git branch --merged dev | egrep -v "(^\*|master|dev)" | xargs git branch -d

It removes all local branches that merged into dev (except dev and master). This is a potentially damaging operation. It can delete branches actually needed. So if you use the different approach to work with Git, you could remove some of branches manually instead. I hope you do not store all old branches, do you?