diff --git a/bin/gbf b/bin/gbf index 4a3303d..259ac69 100755 --- a/bin/gbf +++ b/bin/gbf @@ -3,4 +3,6 @@ # `gbf ` git branch file filename=$1; -git_log_oneline_new --follow --patch -- "$filename" +range=${2:-git_current_branch_range} +shift 2 +git_log_oneline --follow --patch "$range" "$@" -- "$filename" diff --git a/bin/gbl b/bin/gbl index ceea216..fa794f1 100755 --- a/bin/gbl +++ b/bin/gbl @@ -3,5 +3,6 @@ # `gbl []` git branch log # list commits added to this branch since forked from or main branch. -base_branch=${1:-"$(git_main_base_branch)"}; -git_log_oneline "$base_branch" | more -eRSF +base_branch=${1:-"$(git_current_branch_range)"}; +shift +git_log_oneline "$base_branch" "$@" diff --git a/bin/git_commit_during_rebase b/bin/git_commit_during_rebase index 737811b..03041b1 100755 --- a/bin/git_commit_during_rebase +++ b/bin/git_commit_during_rebase @@ -1,4 +1,4 @@ #!/usr/bin/env bash echo git_commit_during_rebase; -git_add_p && ( git_status_clean || git commit --no-edit --no-verify 2> /dev/null || ( git commit --amend --no-edit --no-verify ) ) && ( git_status_clean || git_stash ) +git_add_p && ( git_status_clean || git commit --no-edit --no-verify 2> /dev/null || ( ! git merge-base --is-ancestor HEAD "$(git_main_base_branch)" && git commit --amend --no-edit --no-verify || git commit -m "Auto lint" --no-verify ) ) && ( git_status_clean || git_stash ) diff --git a/bin/git_current_branch_range b/bin/git_current_branch_range new file mode 100755 index 0000000..7a7a768 --- /dev/null +++ b/bin/git_current_branch_range @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +if [[ "$(git_branch_name)" == "$(git_main_branch)" ]]; then + echo HEAD +else + echo "$(git_main_base_branch)"..HEAD +fi diff --git a/bin/git_find_sha b/bin/git_find_sha index d9b1a2e..b5a65cc 100755 --- a/bin/git_find_sha +++ b/bin/git_find_sha @@ -12,7 +12,7 @@ commits=(); while IFS= read -r line; do commits+=("$line"); - done < <(git_log_oneline "$root" 2>/dev/null | grep -E -e '^([^\sm]+m)?'"$val" -e '\s.*'"$val"); + done < <(git_log_oneline "$root"..HEAD 2>/dev/null | grep -E -e '^([^\sm]+m)?'"$val" -e '\s.*'"$val"); if (( ${#commits[@]} > 1 )); then echoerr "Multiple possible commits found:"; for commit in "${commits[@]}"; diff --git a/bin/git_log_oneline b/bin/git_log_oneline index 0a420c3..1945eef 100755 --- a/bin/git_log_oneline +++ b/bin/git_log_oneline @@ -1,33 +1,8 @@ #!/usr/bin/env bash - - - - -echo ${COLOR_GREY}git log --oneline $(git_log_range "$1")$COLOR_RESET 1>&2; -commits_in_origin=$(echo $(git log --format="%h" $(git merge-base --fork-point $(git_main_base_branch)) 2>/dev/null)); -# local commits_in_origin=$(echo -e $(git log --format="%h" $(git_log_range "$1" HEAD) 2>/dev/null)) -commit_in_origin_condition='index("'$commits_in_origin'", $2) > 0'; -git log --reverse --format="%b%n§%h§%s§%cr" | awk -F'§' '{ - if ($0 ~ "^$") { - # do nothing - } else if ($1 != "" ) { - body = body " " $1 - } else { - if('"$commit_in_origin_condition"') { - printf "%s", "'$COLOR_AQUA'" - } else { - printf "%s", "'$COLOR_GREEN'" - } - printf "%s%s%s%s%s", $2, " '$COLOR_RESET'", $3, " '$COLOR_LOWLIGHT", $4 - - if (body != "") { - gsub("\r", "", body) - printf "%s%s", "'$COLOR_GREY''$COLOR_LOWLIGHT'", body - } - - body="" - - print "'$COLOR_RESET'" - } -}' +git --no-pager \ +log \ + --color \ + --reverse \ + --format='%h%Creset%Cred%Cgreen%Cblue%Creset%(decorate) %s %C(dim)%cr - %cn%Creset' \ + "$@" | git_log_oneline_process diff --git a/bin/git_log_oneline_new b/bin/git_log_oneline_new deleted file mode 100755 index fd5421d..0000000 --- a/bin/git_log_oneline_new +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env bash -# git_main_remote_branch >/dev/null 2>&1 # ensure we have origin/HEAD and upstream/HEAD if we should -git --no-pager log --color --reverse --format='%h%Creset%(decorate) %s %C(dim)%cr - %cn%Creset' "$@" diff --git a/bin/git_log_oneline_new_2 b/bin/git_log_oneline_new_2 deleted file mode 100755 index 20d602b..0000000 --- a/bin/git_log_oneline_new_2 +++ /dev/null @@ -1,2 +0,0 @@ -#!/usr/bin/env bash - diff --git a/bin/git_log_oneline_process b/bin/git_log_oneline_process index 962d152..6cee94f 100755 --- a/bin/git_log_oneline_process +++ b/bin/git_log_oneline_process @@ -1,11 +1,8 @@ #!/usr/bin/env ruby --disable-all -if ARGV.delete('--off') - while gets - puts $_ - end - exit 0 -end +# frozen_string_literal: true + +# rubocop:disable Style/SpecialGlobalVars def branch_exist?(branch) ( @@ -27,38 +24,52 @@ def current_branch_main? main_branch == current_branch end -$forks = { - magenta: 'upstream/HEAD', - blue: (branch_exist?('origin/HEAD') ? 'origin/HEAD' : main_branch), - cyan: ('@{u}' unless current_branch_main?) -} +def forks + @forks ||= { + magenta: 'upstream/HEAD', + blue: (branch_exist?('origin/HEAD') ? 'origin/HEAD' : main_branch), + cyan: ('@{u}' unless current_branch_main?), + green: 'HEAD' + }.select { |_, v| branch_exist?(v) } +end -$forks.select! { |k, v| branch_exist?(v) } +# this is split on the magic sigil in git_log_oneline +def next_line_split(line) + line.split("\e[m\e[31m\e[32m\e[34m\e[m", 2) +end gets -exit 0 unless $_ -sha, rest = $_.split("\e", 2) +line = $_ +exit 0 unless line +sha, rest = next_line_split(line) first = `git rev-parse --short #{sha}^ 2>/dev/null`.chomp first = first.empty? ? '' : "#{first}.." -$forks.transform_values! do |v| +forks.transform_values! do |v| `git log --format=%h #{first}#{v}`.chomp.split("\n").to_h { |k| [k, nil] } end -def render(sha, rest) - color = if $forks[:magenta]&.key?(sha) + +def render(sha, rest) # rubocop:disable Metrics + color = if forks[:magenta]&.key?(sha) "\e[35m" - elsif $forks[:blue]&.key?(sha) + elsif forks[:blue]&.key?(sha) "\e[34m" - elsif $forks[:cyan]&.key?(sha) + elsif forks[:cyan]&.key?(sha) "\e[36m" - else + elsif forks[:green]&.key?(sha) "\e[32m" end - "#{color}#{sha}\e#{rest.chomp}\e[0m" + "#{color}#{sha}\e[m#{rest.chomp}\e[0m" end -print "\x1b[?7l" -print render(sha, rest) + +print "\e[?7l" # disable line wrap +print render(sha.chomp, rest) + while gets puts - print render(*$_.split("\e", 2)) + sha, rest = next_line_split($_) + print render(sha.chomp, rest.to_s) end -puts "\x1b[?7h" + +puts "\e[?7h" # reenable line wrap + +# rubocop:enable Style/SpecialGlobalVars diff --git a/bin/git_rebase_onto b/bin/git_rebase_onto index c0eebbf..ce38b62 100755 --- a/bin/git_rebase_onto +++ b/bin/git_rebase_onto @@ -1,3 +1,3 @@ #!/usr/bin/env bash - cat $(git rev-parse --git-path rebase-merge/onto) $(git rev-parse --git-path rebase-apply/onto) 2> /dev/null +cat $(git rev-parse --git-path rebase-merge/onto) $(git rev-parse --git-path rebase-apply/onto) 2> /dev/null diff --git a/bin/rtr b/bin/rtr index fd99676..ef011b0 100755 --- a/bin/rtr +++ b/bin/rtr @@ -1,4 +1,4 @@ #!/usr/bin/env bash - rt --failure-exit-code 2 "$@"; - [[ "$?" != "1" ]] && rtr "$@" +rt --failure-exit-code 2 "$@"; +[[ "$?" != "1" ]] && rtr "$@" diff --git a/bin/strip_color b/bin/strip_color index d2853e7..b505ae5 100755 --- a/bin/strip_color +++ b/bin/strip_color @@ -1,7 +1,9 @@ #!/usr/bin/env bash +# also strip the disabling of line wrap +# that gets added by git_log_oneline_process if (( $# == 0 )); then - sed -E "/^[[:cntrl:]]\\[1;90m.*[[:cntrl:]]\\[0m$/d;s/([[:cntrl:]]\\[[0-9]{1,3}(;[0-9]{1,3})*m)//g" + sed -E 's/(\x1b\[[0-9;]*m|\x1b\[\?7[lh])//g' else echo -e "$@" | "$BASH_SOURCE" fi diff --git a/spec/bash_support_spec.rb b/spec/bash_support_spec.rb index 132ca84..342f9be 100644 --- a/spec/bash_support_spec.rb +++ b/spec/bash_support_spec.rb @@ -1,15 +1,5 @@ # frozen_string_literal: true -COLOR_RED = ENV.fetch('COLOR_RED') -COLOR_GREEN = ENV.fetch('COLOR_GREEN') -COLOR_YELLOW = ENV.fetch('COLOR_YELLOW') -COLOR_BLUE = ENV.fetch('COLOR_BLUE') -COLOR_AQUA = ENV.fetch('COLOR_AQUA') -COLOR_GREY = ENV.fetch('COLOR_GREY') -COLOR_PINK = ENV.fetch('COLOR_PINK') -COLOR_RESET = ENV.fetch('COLOR_RESET') -COLOR_LIGHT_PINK = ENV.fetch('COLOR_LIGHT_PINK') - RSpec.describe 'bash_support' do it 'returns current ruby version' do copy_file '.ruby-version' @@ -20,8 +10,8 @@ describe 'echoerr' do it 'returns red text for echoerr' do - expect(run('echoerr No', expect_exit: 1)).to have_output( - stderr: "#{COLOR_RED}error: No#{COLOR_RESET}\n", + expect(run('echoerr No', exit_with: 1)).to have_output( + stderr: "\e[31merror: No\e[0m\n", stdout: nil ) end @@ -30,140 +20,140 @@ describe 'echodo' do it 'returns grey text for echodo to stderr and does the thing to stdout' do expect(run('echodo echo 1')).to have_output( - stderr: "#{COLOR_GREY}echo 1#{COLOR_RESET}\n", + stderr: "\e[0;2mecho 1\e[0m\n", stdout: "1\n" ) end it 'removes unnecessary single quotes' do expect(run("echodo echo '1'")).to have_output( - stderr: "#{COLOR_GREY}echo 1#{COLOR_RESET}\n", + stderr: "\e[0;2mecho 1\e[0m\n", stdout: "1\n" ) end it 'removes unnecessary double quotes' do expect(run('echodo echo "1"')).to have_output( - stderr: "#{COLOR_GREY}echo 1#{COLOR_RESET}\n", + stderr: "\e[0;2mecho 1\e[0m\n", stdout: "1\n" ) end it 'retains quoted empty double quoted strings' do expect(run('echodo echo ""')).to have_output( - stderr: "#{COLOR_GREY}echo ''#{COLOR_RESET}\n", + stderr: "\e[0;2mecho ''\e[0m\n", stdout: "\n" ) end it 'retains quoted empty single quoted strings' do expect(run("echodo echo ''")).to have_output( - stderr: "#{COLOR_GREY}echo ''#{COLOR_RESET}\n", + stderr: "\e[0;2mecho ''\e[0m\n", stdout: "\n" ) end it 'uses single quotes to escape spaces when given a single quoted string' do expect(run("echodo echo '1 and 2'")).to have_output( - stderr: "#{COLOR_GREY}echo '1 and 2'#{COLOR_RESET}\n", + stderr: "\e[0;2mecho '1 and 2'\e[0m\n", stdout: "1 and 2\n" ) end it 'uses single quotes to escape spaces when given a double quoted string' do expect(run('echodo echo "1 and 2"')).to have_output( - stderr: "#{COLOR_GREY}echo '1 and 2'#{COLOR_RESET}\n", + stderr: "\e[0;2mecho '1 and 2'\e[0m\n", stdout: "1 and 2\n" ) end it 'uses single quotes to escape spaces when given a backslash escaped string' do expect(run('echodo echo 1\\ and\\ 2')).to have_output( - stderr: "#{COLOR_GREY}echo '1 and 2'#{COLOR_RESET}\n", + stderr: "\e[0;2mecho '1 and 2'\e[0m\n", stdout: "1 and 2\n" ) end it 'uses single quotes to escape \\(whatever\\)' do expect(run('echodo echo \\(whatever\\)')).to have_output( - stderr: "#{COLOR_GREY}echo '(whatever)'#{COLOR_RESET}\n", + stderr: "\e[0;2mecho '(whatever)'\e[0m\n", stdout: "(whatever)\n" ) end it "uses single quotes to escape '(whatever)'" do expect(run("echodo echo '(whatever)'")).to have_output( - stderr: "#{COLOR_GREY}echo '(whatever)'#{COLOR_RESET}\n", + stderr: "\e[0;2mecho '(whatever)'\e[0m\n", stdout: "(whatever)\n" ) end it "uses single quotes to escape '[whatever]" do expect(run("echodo echo '[whatever]'")).to have_output( - stderr: "#{COLOR_GREY}echo '[whatever]'#{COLOR_RESET}\n", + stderr: "\e[0;2mecho '[whatever]'\e[0m\n", stdout: "[whatever]\n" ) end it "uses single quotes to escape 'what)ever]'" do expect(run("echodo echo 'what)ever]'")).to have_output( - stderr: "#{COLOR_GREY}echo 'what)ever]'#{COLOR_RESET}\n", + stderr: "\e[0;2mecho 'what)ever]'\e[0m\n", stdout: "what)ever]\n" ) end it "uses single quotes to escape 'what) ever]'" do expect(run("echodo echo 'what) ever]'")).to have_output( - stderr: "#{COLOR_GREY}echo 'what) ever]'#{COLOR_RESET}\n", + stderr: "\e[0;2mecho 'what) ever]'\e[0m\n", stdout: "what) ever]\n" ) end it "uses single quotes to escape 'what)ever'" do expect(run("echodo echo 'what)ever'")).to have_output( - stderr: "#{COLOR_GREY}echo 'what)ever'#{COLOR_RESET}\n", + stderr: "\e[0;2mecho 'what)ever'\e[0m\n", stdout: "what)ever\n" ) end it "uses single quotes to escape 'what>ever'" do expect(run("echodo echo 'what>ever'")).to have_output( - stderr: "#{COLOR_GREY}echo 'what>ever'#{COLOR_RESET}\n", + stderr: "\e[0;2mecho 'what>ever'\e[0m\n", stdout: "what>ever\n" ) end it "uses single quotes to escape 'what