A collection of useful commands for dealing with various systems.

### git

List all (local) branches and report date and sort in descending order:

```
git for-each-ref --sort=-committerdate refs/heads/ --format='%(committerdate:short) %(refname:short)'
```

Remove all untracked files:

```
git status --porcelain=2 | grep -E "^\?{1}" | cut -c3- | xargs rm -rf
```

Checking that you haven't "soiled" a local branch with a bad rebase (replace `master` with the base branch as required):

```
git diff --name-only $(git merge-base --fork-point master) | xargs git diff origin/$(git status -b | head -n1 | cut -f3 -d' ')
```

Getting a list of commits (in a relatively useful short form) since a branch diverged from `master`:

```
git log --format="%h %an: %s" $(git merge-base release-name master)..
```

All the modified files on the current branch since forking from master.  To see the content of the changes, remove the `--name-only` flag.

```
git diff --name-only $(git merge-base --fork-point master)
```

Reset a feature branch to the latest commit on master (after a `git rebase master`):

```
git reset $(git log master --format=%H -n 1)
```

List all the remote branches for a particular prefix:

```
git branch -a --list *damon-* --color=never | cut -c3- | grep -E '^remotes\/.*\/damon-' | cut -f3 -d'/'
```

List all local branches that aren't tracked with a remote branch:

```
git branch -vv | cut -c 3- | awk '$3 !~/\[/ { print $1 }'
```

Create tags for a list of `release-` branches on remote (note the `-n-2` for how many "active" branches you want to leave):

```
git branch -r | grep release- | head -n-2 | cut -f2 -d'/' | xargs -I '{}' git tag {} origin/{}
```

Move forward in a git history:

```
git checkout $(git log master --format="%h" | grep -B 1 $(git log -n1 --format="%h") | head -n 1)
```

Find all the files that are in the current branch that also exist in an another branch.  This is especially useful when you have broken out another branch from an existing branch and are trying to make a series of useful pull requests:

```
comm -23 <(git diff --name-only master..) <(git diff --name-only damon-user-reactivation-facebook..)
```

Find the commits I have made on the current branch:

```
git log --author='Damon Oehlman' --format='%H %s'
```

### shell

Find CRLF files in a directory.

```
grep -IUlr $'\r' --exclude-dir=node_modules --exclude-dir=.git
```

Find all files matching a particular pattern and exec a command on each of them.  In the example below we are looking for all `.ts` and `.tsx` files and running [prettier](https://github.com/prettier/prettier) over them in place.

```
find <path> -regextype posix-extended  -regex ".*\.tsx?$" -exec $(yarn bin)/prettier --write {} \;
```

Using the command above, you may find that some `.d.ts` file have slipped in (no way around this AFAIK due to not being able to negative look behind in the regex), so you will want to unstage those:

```
git diff --name-only | grep -E "\.d.ts$" | xargs git checkout --
```

### jq

Getting all the resolved paths from an `npm-shrinkwrap.json` file:

```
jq ".. | objects | select(.resolved) | { resolved: .resolved }" < npm-shrinkwrap.json
```

General black magic for finding the most recent versions of a packages when something like `yarn outdated` won't work (can't generate lock file for instance):

```
jq ".dependencies | keys | @tsv" -r < package.json | xargs -I "{}" -d '\t' yarn info --silent --json {} | jq "[.data.name, .data.version]"
```

Downloading all the files referenced in the npm-shrinkwrap file:

```
jq -r ".. | objects | select(.resolved) | .resolved" < /path/to/npm-shrinkwrap.json | grep -e "^http" | xargs -L1 -P50 wget -nc --no-verbose
```

Apparently achievable with the following also:

```
grep resolved < /path/to/npm-shrinkwrap.json | grep -E '\"https?' | cut -d\" -f4 | xargs -L1 -P50 wget -nc --no-verbose
```

Get some useful info out of the github pull requests API:

```
curl -s -X GET \
  -H "Accept: application/vnd.github.cerberus-preview" \
  -H "Authorization: token f333fdf73f1316116981f5b2394505aabdff3ca5"  \
  "https://api.github.com/repos/canva/canva/issues?labels=bugathon&state=all&sort=updated&direction=desc&base=master&per_page=250" \
    | jq '.[] | { title: .title, user: .user.login, state: .state, assignees: [ .assignees[] | .login ], closed_at: .closed_at }'
```

### docker

Run the interactive command prompt on a particular container:

```
docker exec -it $(docker ps --filter "name=mysql*" --format "{{.ID}}") /bin/bash
```

Remove ALL registered containers:

```
docker container ls -a --format="{{.ID}}" | xargs docker container rm
```

Remove ALL images:

```
docker image ls -a --format="{{.ID}}" | xargs docker image rm
```

(you'll need to remove containers that might be attached to the images first)

### yarn

Compiling the yarn workspaces that have changed since master:

```
git diff --name-only master | sed -E 's:packages/([A-Za-z0-9_\-]+)\/.*:\1:' | uniq | xargs -I {} yarn workspace {} compile
```

### imagemagick

Convert a series of images into an MPEG file:

```
MAGICK_THREAD_LIMIT=2 MAGICK_MEMORY_LIMIT=2G convert -delay 5 -quality 100 *.JPG out.mpeg
```