Rando scripts

The following code snippets are for general reuse.

Some more scripts can be found here too: cockroachdb/scripts

  • Configure git to fetch PRs (particularly nice with git log --decorate [try git config log.decorate auto]). Add this magic line under the existing fetch line for your GitHub remote in $repo/.git/config:
 fetch = +refs/pull/*/head:refs/pull/origin/*
  • Get a PR locally, or create a PR from the current branch. Courtesy of @tamird. TODO: refuse to create a PR if the current branch diverged from the tracker.
#!/usr/bin/env sh

set -eu

error_message() {
  ret=$?
  if [ $ret -ne 0 ]; then
    echo "Something went wrong. Did you push your branch? Did you set a remote tracking branch?"
  fi
  exit $ret
}

trap error_message EXIT

if [ $# == 0 ]; then
  branch_name=$(git rev-parse --abbrev-ref HEAD)
  remote_name=$(git config branch."$branch_name".remote)

  github_forked_org_name=$(git config remote."$remote_name".url | sed -E -e 's,^(https://|git@)github.com[:/]([^\./]+)/[^\./]+(\.git)?,\2,')
  github_origin_proj_url=$(git config remote.origin.url         | sed -E -e 's,^(https://|git@)github.com[:/]([^\./]+/[^\./]+)(\.git)?,\2,')

  url="https://github.com/$github_origin_proj_url/compare/master...$github_forked_org_name:$branch_name?expand="

  open "$url"
else
  hub fetch origin refs/pull/"$1"/head:pr/"$1" && hub checkout pr/"$1"
fi
  • Determine the variables which are escaping to the heap for a given file. Courtesy of @nvanbenschoten.
#!/usr/bin/env bash

set -eu

if [ $# -eq 0 ]
then
        echo "usage: goescape <filename>"
        exit 1
fi

FILEPATH="$1"
DIR=$(dirname "$FILEPATH")
FILE=$(basename "$FILEPATH")

pushd "$DIR" > /dev/null
touch "$FILE"

go build -gcflags='-m' 2>&1 | \
        grep 'to heap' | \
        grep "./$FILE"

popd > /dev/null

Taking this one step further, we can annotate a source file with information on heap allocations:

WHAT=./storage/engine/mvcc.go
goescape "${WHAT}" |
  awk -F: '{ print "-e " $2 "," $2 "\"s~$~ /* heap:" $4 " */~\"" }' |
  cat - <(echo "${WHAT}") | xargs sed -i

Which, in this example, would create diffs which look like this:

@@ -1283,7 +1283,7 @@ func MVCCMerge(
        key roachpb.Key,
        timestamp hlc.Timestamp,
        value roachpb.Value,
-) error {
+) error { /* heap: timestamp */
        if len(key) == 0 {
                return emptyKeyError()
        }
@@ -1324,8 +1324,8 @@ func MVCCDeleteRange(
        txn *roachpb.Transaction,
        returnKeys bool,
 ) ([]roachpb.Key, error) {
-       var keys []roachpb.Key
-       num := int64(0)
+       var keys []roachpb.Key /* heap: keys */
+       num := int64(0) /* heap: num */
        buf := newPutBuffer()
        iter := engine.NewIterator(true)
        f := func(kv roachpb.KeyValue) (bool, error) {
@@ -2119,7 +2119,7 @@ func MVCCFindSplitKey(
        logf("searching split key for %d [%s, %s)", rangeID, key, endKey)

        // Get range size from stats.
-       var ms enginepb.MVCCStats
+       var ms enginepb.MVCCStats /* heap: ms */
        if err := MVCCGetRangeStats(ctx, engine, rangeID, &ms); err != nil {
  • Hate how Github collapses all comments that are not on the most recent commit? Use this javascript code to quickly open all the old comments. You can add this to a bookmark on Chrome, just set the URL to the code below.
javascript: (function() {var list = document.getElementsByClassName("outdated-diff-comment-container"); for (var i = 0; i < list.length; i++) {list[i].classList.add("open");} ;}());`