Skip to content

Instantly share code, notes, and snippets.

@sgen
Created February 27, 2022 17:11
Show Gist options
  • Save sgen/4f8fea0dfd66f610b8ad19867a9369a4 to your computer and use it in GitHub Desktop.
Save sgen/4f8fea0dfd66f610b8ad19867a9369a4 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# generate-github-references
#
[[ ! -d '.git' ]] && { echo 'not in a git directory' >&2; exit 1; }
term="$term"
name="$name"
username="$username"
organization="$organization"
[[ -z "$name" ]] && name="$(git config --local repository.name)"
[[ -z "$name" ]] && name="$(basename "$PWD")"
[[ -z "$username" ]] && username="$(git config github.username)"
[[ -z "$organization" ]] && organization="$(git config github.organziation)"
[[ -z "$term" ]] && { echo 'no $term provided' >&2; exit 1; }
[[ -z "$username" ]] && { echo 'no github $username provided' >&2; exit 1; }
commit="$(git rev-parse 'HEAD')"
urlbase="https://github.com/$username/$name/blob/$commit"
[[ ! -z "$organization" ]] && urlbase="/https://github.com/$organization/$name/blob/$commit"
grep -n "$term" ** | while read -r line
do
filename="${line%%:*}"
extname="${filename##*.}"
[[ "$extname" = "$filename" ]] && extname=''
line="${line#$filename}"
line="${line#:*}"
linenumber="${line%%:*}"
line="${line#$linenumber}"
line="${line#:*}"
text="$line"
url="$urlbase/$filename#L$linenumber"
[[ "$extname" == 'md' ]] && url="$urlbase/$filename?plain=1#L$linenumber"
reftext="[$filename:$linenumber]($url)"
echo "$reftext"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment