Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support running in nix develop environments #336

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/result
60 changes: 60 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
description = "scm_breeze shell plugin for git and interactive shell";

inputs = {
flake-utils.url = "github:numtide/flake-utils";
nixpkgs.url = "nixpkgs/nixos-23.11";
};

outputs = { self, flake-utils, nixpkgs, ... }:
flake-utils.lib.eachDefaultSystem (system:
let pkgs = import nixpkgs { inherit system; };
in rec {
formatter = pkgs.nixfmt;
packages = rec {
hello = pkgs.hello;
default = hello;
};
});
}
48 changes: 26 additions & 22 deletions lib/git/aliases.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,48 @@ list_aliases() { alias | grep "$*" --color=never | sed -e 's/alias //' -e "s/=/:
alias git_aliases="list_aliases git"

# Remove any existing git alias or function
unalias git > /dev/null 2>&1
unset -f git > /dev/null 2>&1
unalias git >/dev/null 2>&1
unset -f git >/dev/null 2>&1

# Use the full path to git to avoid infinite loop with git function
export _git_cmd="$(bin_path git)"
# Wrap git with the 'hub' github wrapper, if installed (https://github.com/defunkt/hub)
if type hub > /dev/null 2>&1; then export _git_cmd="hub"; fi
if type hub >/dev/null 2>&1; then export _git_cmd="hub"; fi

# gh is now deprecated, and merged into the `hub` command line tool.
#if type gh > /dev/null 2>&1; then export _git_cmd="gh"; fi

# Create 'git' function that calls hub if defined, and expands all numeric arguments
function git(){
function git() {
# Only expand args for git commands that deal with paths or branches
case $1 in
commit|blame|add|log|rebase|merge|difftool|switch)
exec_scmb_expand_args "$_git_cmd" "$@";;
checkout|diff|rm|reset|restore)
exec_scmb_expand_args --relative "$_git_cmd" "$@";;
branch)
_scmb_git_branch_shortcuts "${@:2}";;
*)
"$_git_cmd" "$@";;
commit | blame | add | log | rebase | merge | difftool | switch)
exec_scmb_expand_args "$_git_cmd" "$@"
;;
checkout | diff | rm | reset | restore)
exec_scmb_expand_args --relative "$_git_cmd" "$@"
;;
branch)
_scmb_git_branch_shortcuts "${@:2}"
;;
*)
"$_git_cmd" "$@"
;;
esac
}

_alias "$git_alias" "git"


# --------------------------------------------------------------------
# Thanks to Scott Bronson for coming up the following git tab completion workaround,
# which I've altered slightly to be more flexible.
# https://github.com/bronson/dotfiles/blob/731bfd951be68f395247982ba1fb745fbed2455c/.bashrc#L81
# (only works for bash)
__define_git_completion () {
eval "
__define_git_completion() {
eval "
_git_$1_shortcut () {
COMP_LINE=\"git $2 \${COMP_LINE/$1 }\"
let COMP_POINT+=$((4+${#2}-${#1}))
let COMP_POINT+=$((4 + ${#2} - ${#1}))
COMP_WORDS=(git $2 \"\${COMP_WORDS[@]:1}\")
let COMP_CWORD+=1

Expand All @@ -64,18 +67,20 @@ __git_wrap__git_main

# Define git alias with tab completion
# Usage: __git_alias <alias> <command_prefix> <command>
__git_alias () {
__git_alias() {
if [ -n "$1" ]; then
local alias_str cmd_prefix cmd cmd_args

alias_str="$1"; cmd_prefix="$2"; cmd="$3";
alias_str="$1"
cmd_prefix="$2"
cmd="$3"
if [ $# -gt 2 ]; then
shift 3 2>/dev/null
cmd_args=("$@")
fi

alias $alias_str="$cmd_prefix $cmd${cmd_args:+ }${cmd_args[*]}"
if [ "$shell" = "bash" ]; then
if breeze_shell_is "bash"; then
__define_git_completion "$alias_str" "$cmd"
complete -o default -o nospace -F _git_"$alias_str"_shortcut "$alias_str"
fi
Expand Down Expand Up @@ -177,10 +182,9 @@ if [ "$git_setup_aliases" = "yes" ]; then
_alias "$git_pull_request_alias" 'gh pr'
fi



# TODO(ghthor): apply these same fixes for NixOS
# Tab completion
if [ $shell = "bash" ]; then
if breeze_shell_is "bash"; then
# Fix to preload Arch bash completion for git
[[ -s "/usr/share/git/completion/git-completion.bash" ]] && source "/usr/share/git/completion/git-completion.bash"
# new path in Ubuntu 13.04
Expand Down
10 changes: 6 additions & 4 deletions lib/git/branch_shortcuts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

# Function wrapper around 'll'
# Adds numbered shortcuts to output of ls -l, just like 'git status'
unalias $git_branch_alias > /dev/null 2>&1; unset -f $git_branch_alias > /dev/null 2>&1
unalias $git_branch_alias >/dev/null 2>&1
unset -f $git_branch_alias >/dev/null 2>&1
function _scmb_git_branch_shortcuts {
fail_if_not_git_repo || return 1

Expand All @@ -21,15 +22,16 @@ function _scmb_git_branch_shortcuts {
fi

# Use ruby to inject numbers into git branch output
ruby -e "$( cat <<EOF
ruby -e "$(
cat <<EOF
output = %x($_git_cmd branch --color=always $(token_quote "$@"))
line_count = output.lines.to_a.size
output.lines.each_with_index do |line, i|
spaces = (line_count > 9 && i < 9 ? " " : " ")
puts line.sub(/^([ *]{2})/, "\\\1\033[2;37m[\033[0m#{i+1}\033[2;37m]\033[0m" << spaces)
end
EOF
)"
)"

# Set numbered file shortcut in variable
local e=1 IFS=$'\n'
Expand All @@ -47,7 +49,7 @@ __git_alias "$git_branch_delete_alias" "_scmb_git_branch_shortcuts" "-d"
__git_alias "$git_branch_delete_force_alias" "_scmb_git_branch_shortcuts" "-D"

# Define completions for git branch shortcuts
if [ "$shell" = "bash" ]; then
if breeze_shell_is "bash"; then
for alias_str in $git_branch_alias $git_branch_all_alias $git_branch_move_alias $git_branch_delete_alias; do
__define_git_completion $alias_str branch
complete -o default -o nospace -F _git_"$alias_str"_shortcut $alias_str
Expand Down
64 changes: 45 additions & 19 deletions lib/git/fallback/status_shortcuts_shell.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,26 @@
git_status_shortcuts() {
zsh_compat # Ensure shwordsplit is on for zsh
local IFS=$'\n'
local git_status="$(git status --porcelain 2> /dev/null)"
local git_status="$(git status --porcelain 2>/dev/null)"
local i

if [ -n "$git_status" ] && [[ $(echo "$git_status" | wc -l) -le $gs_max_changes ]]; then
unset stat_file; unset stat_col; unset stat_msg; unset stat_grp; unset stat_x; unset stat_y
unset stat_file
unset stat_col
unset stat_msg
unset stat_grp
unset stat_x
unset stat_y
# Clear numbered env variables.
for (( i=1; i<=$gs_max_changes; i++ )); do unset $git_env_char$i; done
for ((i = 1; i <= $gs_max_changes; i++)); do unset $git_env_char$i; done

# Get branch
local branch=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
local branch=$(git branch 2>/dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
# Get project root
if [ -d .git ]; then
local project_root="$PWD"
else
local project_root=$(git rev-parse --git-dir 2> /dev/null | sed "s%/\.git$%%g")
local project_root=$(git rev-parse --git-dir 2>/dev/null | sed "s%/\.git$%%g")
fi

# Colors
Expand All @@ -46,17 +51,25 @@ git_status_shortcuts() {
local c_cpy="\033[0;33m"
local c_ign="\033[0;36m"
# Following colors must be prepended with modifiers e.g. '\033[1;', '\033[0;'
local c_grp_1="33m"; local c_grp_2="31m"; local c_grp_3="32m"; local c_grp_4="36m"
local c_grp_1="33m"
local c_grp_2="31m"
local c_grp_3="32m"
local c_grp_4="36m"

local f=1; local e=1 # Counters for number of files, and ENV variables
local f=1
local e=1 # Counters for number of files, and ENV variables

echo -e "$c_dark#$c_rst On branch: $c_branch$branch$c_rst $c_dark| [$c_rst*$c_dark]$c_rst => \$$git_env_char*\n$c_dark#$c_rst"

for line in $git_status; do
if [[ $shell == *bash ]]; then
x=${line:0:1}; y=${line:1:1}; file=${line:3}
if breeze_shell_is "bash"; then
x=${line:0:1}
y=${line:1:1}
file=${line:3}
else
x=$line[1]; y=$line[2]; file=$line[4,-1]
x=$line[1]
y=$line[2]
file=$line[4,-1]
fi

# Index modification states
Expand All @@ -78,18 +91,30 @@ git_status_shortcuts() {
esac
if [ -n "$msg" ]; then
# Store data at array index and add to group
stat_file[$f]=$file; stat_msg[$f]=$msg; stat_col[$f]=$col
stat_file[$f]=$file
stat_msg[$f]=$msg
stat_col[$f]=$col
stat_grp[$grp]="${stat_grp[$grp]} $f"
let f++
fi

# Work tree modification states
msg=""
if [[ "$y" == "M" ]]; then msg=" modified"; col="$c_mod"; grp="3"; fi
if [[ "$y" == "M" ]]; then
msg=" modified"
col="$c_mod"
grp="3"
fi
# Don't show {Y} as deleted during a merge conflict.
if [[ "$y" == "D" && "$x" != "D" && "$x" != "U" ]]; then msg=" deleted"; col="$c_del"; grp="3"; fi
if [[ "$y" == "D" && "$x" != "D" && "$x" != "U" ]]; then
msg=" deleted"
col="$c_del"
grp="3"
fi
if [ -n "$msg" ]; then
stat_file[$f]=$file; stat_msg[$f]=$msg; stat_col[$f]=$col
stat_file[$f]=$file
stat_msg[$f]=$msg
stat_col[$f]=$col
stat_grp[$grp]="${stat_grp[$grp]} $f"
let f++
fi
Expand Down Expand Up @@ -131,10 +156,10 @@ _gs_output_file_group() {
local absolute="$project_root/${stat_file[$i]}"
local dest=$(readlink -f "$absolute")
local pwd=$(readlink -f "$PWD")
relative="$(_gs_relative_path "$pwd" "${dest:-$absolute}" )"
relative="$(_gs_relative_path "$pwd" "${dest:-$absolute}")"
fi

if [[ $f -gt 10 && $e -lt 10 ]]; then local pad=" "; else local pad=""; fi # (padding)
if [[ $f -gt 10 && $e -lt 10 ]]; then local pad=" "; else local pad=""; fi # (padding)
echo -e "$c_hash#$c_rst ${stat_col[$i]}${stat_msg[$i]}:\
$pad$c_dark [$c_rst$e$c_dark] $c_group$relative$c_rst"
# Export numbered variables in the order they are displayed.
Expand All @@ -148,14 +173,15 @@ $pad$c_dark [$c_rst$e$c_dark] $c_group$relative$c_rst"
}

# Show relative path if current directory is not project root
_gs_relative_path(){
_gs_relative_path() {
# Credit to 'pini' for the following script.
# (http://stackoverflow.com/questions/2564634/bash-convert-absolute-path-into-relative-path-given-a-current-directory)
target=$2; common_part=$1; back=""
target=$2
common_part=$1
back=""
while [[ -n "${common_part}" && "${target#$common_part}" == "${target}" ]]; do
common_part="${common_part%/*}"
back="../${back}"
done
echo "${back}${target#$common_part/}"
}

23 changes: 12 additions & 11 deletions lib/git/keybindings.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
# See [here](http://qntm.org/bash#sec1) for info about why I wanted a prompt.

# Cross-shell key bindings
_bind(){
_bind() {
if [ -n "$1" ]; then
if [[ $shell == "zsh" ]]; then
if breeze_shell_is "zsh"; then
bindkey -s "$1" "$2"
else # bash
bind "\"$1\": $2"
Expand All @@ -24,11 +24,11 @@ _bind(){
if [[ "$git_keyboard_shortcuts_enabled" = "true" ]]; then
case "$-" in
*i*)
if [ -n "$ZSH_VERSION" ]; then
RETURN_CHAR="^M"
else
RETURN_CHAR="\n"
fi
if [ -n "$ZSH_VERSION" ]; then
RETURN_CHAR="^M"
else
RETURN_CHAR="\n"
fi

# Uses emacs style keybindings, so vi mode is not supported for now
if ! set -o | grep -q '^vi .*on$'; then
Expand All @@ -45,9 +45,10 @@ if [[ "$git_keyboard_shortcuts_enabled" = "true" ]]; then
fi
fi

# Commands are prepended with a space so that they won't be added to history.
# Make sure this is turned on with:
# zsh: setopt histignorespace histignoredups
# bash: HISTCONTROL=ignorespace:ignoredups
# Commands are prepended with a space so that they won't be added to history.
# Make sure this is turned on with:
# zsh: setopt histignorespace histignoredups
# bash: HISTCONTROL=ignorespace:ignoredups
;;
esac
fi
Loading