Compare commits

..

11 Commits

5 changed files with 38 additions and 17 deletions

View File

@ -2,9 +2,9 @@
#
# For shell config (e.g., to be sourced into .zshrc) see init.bash
REPOSITORY_BASE=~/src
export REPOSITORY_BASE=~/src
# If a variable is only to be used by a single script, by custom it will be
# prefixed with the name of the script.
REPO_DEFAULT_SEARCH_PREFIX="github.com/buildkite"
export REPO_DEFAULT_SEARCH_PREFIX="github.com/buildkite/"

18
fcl
View File

@ -15,8 +15,11 @@
# [ ] Make a way to turn off the pretty, in case it's getting in the way
#
DEFAULT_ORG = "buildkite"
REPO_BASE = "#{ENV['HOME']}/src"
ConfigPath = File.join(File.expand_path(File.dirname(__FILE__)), 'config.bash')
ConfigValues = `env -i bash --noprofile --norc -c 'source #{ConfigPath}; env'`.lines.map(&:chomp)
ConfigPairs = ConfigValues.map {|v| v.split('=',2) }.to_h
Config = ConfigPairs.reject {|k,v| ['_','SHLVL','PWD'].include?(k) }
def clone(repo_url, destination)
spinner_command = %Q[gum spin --show-output --spinner dot]
@ -39,6 +42,9 @@ def repo_url(repo_str)
# GitHub HTTPS. We like these, use it as-is.
when /^(git|https)\:\/\/github\.com\//
repo_str
# Some other HTTPS. This is probably fine too, as long as it ends with '.git'
when /^(git|https)\:\/\/.+\.git$/
repo_str
# GitHub SSH. Let's convert this to HTTPS.
when /^git@github\.com\:.+\.git$/
repo_str.sub(/^git@github\.com:/, 'https://github.com/')
@ -48,9 +54,9 @@ def repo_url(repo_str)
# Just a user/org name and repo name. Assume it's on GitHub and make an HTTPS URL.
when /^[\w\-_]+\/[\w\-_\.]+$/
"https://github.com/#{repo_str}.git"
# Just a repo name. Assume it's a DEFAULT_ORG repo on GitHub and make an HTTPS URL.
# Just a repo name. Assume it's a FCL_DEFAULT_ORG repo on GitHub and make an HTTPS URL.
when /^[\w\-_\.]+$/
"https://github.com/#{DEFAULT_ORG}/#{repo_str}.git"
"https://github.com/#{Config['FCL_DEFAULT_ORG']}/#{repo_str}.git"
else
raise "I can't make a repo URL out of '#{repo_str}'."
end
@ -60,7 +66,7 @@ def destination_path(repo_str)
repo_elms = repo_str.split('/')
repo = repo_elms.pop.sub(/\.git$/, '')
org = repo_elms.pop || DEFAULT_ORG
org = repo_elms.pop || Config.fetch('FCL_DEFAULT_ORG')
forge = repo_elms.pop || 'github.com'
File.join(forge, org, repo)
@ -69,7 +75,7 @@ end
repo_str = ARGV.empty? ? `gum input --header="Enter a repo name" --placeholder=""`.chomp : ARGV[0]
destination = ARGV[1] || destination_path(repo_str)
full_dest = File.join(REPO_BASE, destination)
full_dest = File.join(Config['REPOSITORY_BASE'], destination)
clone_url = repo_url(repo_str)

16
init-repo-tools Executable file
View File

@ -0,0 +1,16 @@
#!/usr/bin/env bash
#
# Call this your shell config, e.g. ~/.zshrc, and eval the results, e.g.:
#
# eval "$(/path/to/this/repo/init-repo-tools)"
#
# Bash scripts can't change the working directory of their callers. This is
# right and proper, but changing working directory is `repo`'s entire purpose,
# so we need to work around that restriction.
#
cat <<-EOS
repo () {
r=\$($(dirname $(realpath ${BASH_SOURCE[0]}))/repo \$@)
[[ "\$r" != "." ]] && [[ "\$r" != "\$(pwd)/" ]] && cd \$r
}
EOS

View File

@ -1,6 +0,0 @@
# Source this into your shell config, e.g. ~/.zshrc
# Bash scripts can't change the working directory of their callers. This is
# right and proper, but we need to work around it in this case.
#
repo () { cd $($(dirname ${BASH_SOURCE[0]})/repo $@) }

11
repo
View File

@ -22,14 +22,19 @@
source $(dirname ${BASH_SOURCE[0]})/config.bash
repos=$(find "${REPOSITORY_BASE}" -name '.git' | sed "s|${REPO_BASE}/||" | sed 's|.git$||' | sort)
repos=$(find "${REPOSITORY_BASE}" -maxdepth 5 -name '.git' | sed "s|${REPOSITORY_BASE}/||" | sed 's|.git$||' | sort)
# Default to searching within github.com, because that's by far the most common
# place for repos to be.
# selected=$(echo "${repos}" | gum filter --header="Pick a repo" --prompt="Repo: " --height=20 --value "github.com/${1}")
selected="$(echo "${repos}" | fzf -1 -0 -q "github.com/buildkite/${1}" --prompt="Repo: ")"
selected="$(echo "${repos}" | fzf -1 -0 -q "${1:-${REPO_DEFAULT_SEARCH_PREFIX}}" --prompt="Repo: ")"
echo "$REPOSITORY_BASE/$selected"
# Do nothing if no repo was selected
if [ -n "${selected}" ]; then
echo "${REPOSITORY_BASE}/${selected}"
else
echo '.'
fi
# TODO: It could be super nice to bias in favour of more frequently selected options.
# For the last 500 uses (or so):