Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ghthor committed Feb 27, 2024
1 parent 6d936bb commit 7e1705e
Show file tree
Hide file tree
Showing 12 changed files with 266 additions and 101 deletions.
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;
};
});
}
2 changes: 1 addition & 1 deletion git.scmbrc.example
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,6 @@ git_add_and_amend_commit_keys="\C-xz" # CTRL+x, z
# Expand numbered args for common shell commands
shell_command_wrapping_enabled="true"
# Here you can tweak the list of wrapped commands.
scmb_wrapped_shell_commands=(vim emacs gedit cat rm cp mv ln cd ls less subl code)
scmb_wrapped_shell_commands=("vim" "emacs" "gedit" "cat" "rm" "cp" "mv" "ln" "cd" "ls" "less" "subl" "code")
# Add numbered shortcuts to output of ls -l, just like 'git status'
shell_ls_aliases_enabled="true"
41 changes: 23 additions & 18 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,11 +67,13 @@ __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=("$@")
Expand Down
8 changes: 5 additions & 3 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 Down
132 changes: 103 additions & 29 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,9 +51,13 @@ 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"

Expand All @@ -66,34 +75,98 @@ git_status_shortcuts() {
# Index modification states
msg=""
case "$x$y" in
"DD") msg=" both deleted"; col="$c_del"; grp="2";;
"AU") msg=" added by us"; col="$c_new"; grp="2";;
"UD") msg="deleted by them"; col="$c_del"; grp="2";;
"UA") msg=" added by them"; col="$c_new"; grp="2";;
"DU") msg=" deleted by us"; col="$c_del"; grp="2";;
"AA") msg=" both added"; col="$c_new"; grp="2";;
"UU") msg=" both modified"; col="$c_mod"; grp="2";;
"M"?) msg=" modified"; col="$c_mod"; grp="1";;
"A"?) msg=" new file"; col="$c_new"; grp="1";;
"D"?) msg=" deleted"; col="$c_del"; grp="1";;
"R"?) msg=" renamed"; col="$c_ren"; grp="1";;
"C"?) msg=" copied"; col="$c_cpy"; grp="1";;
"??") msg="untracked"; col="$c_ign"; grp="4";;
"DD")
msg=" both deleted"
col="$c_del"
grp="2"
;;
"AU")
msg=" added by us"
col="$c_new"
grp="2"
;;
"UD")
msg="deleted by them"
col="$c_del"
grp="2"
;;
"UA")
msg=" added by them"
col="$c_new"
grp="2"
;;
"DU")
msg=" deleted by us"
col="$c_del"
grp="2"
;;
"AA")
msg=" both added"
col="$c_new"
grp="2"
;;
"UU")
msg=" both modified"
col="$c_mod"
grp="2"
;;
"M"?)
msg=" modified"
col="$c_mod"
grp="1"
;;
"A"?)
msg=" new file"
col="$c_new"
grp="1"
;;
"D"?)
msg=" deleted"
col="$c_del"
grp="1"
;;
"R"?)
msg=" renamed"
col="$c_ren"
grp="1"
;;
"C"?)
msg=" copied"
col="$c_cpy"
grp="1"
;;
"??")
msg="untracked"
col="$c_ign"
grp="4"
;;
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 @@ -135,10 +208,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 @@ -152,14 +225,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/}"
}

Loading

0 comments on commit 7e1705e

Please sign in to comment.