shell functions

For rebase

Often I find the need to squash commits, especially those that don’t need to stand-alone. And so little a function, which is more an alias with arguments, is used for the job, so I don’t have to think. The git command I use for this in, say, master branch, looks like this:

git rebase -i origin/master~4 master

The -i switch makes it interactive, and in the (below) list it produces, I can mark lines selectively from pick to squash for items to be squashed, followed by :wq.1 If there are multiples marked up in the list, then git produces parent commits with an option to edit commit message(s) should you want to, and you can follow it up with :wq. Here’s an example of the list, in which, one can markup the first column as required.

pick fa77b22 # m:homepage updates
pick 04863ce # link titles update
squash c8fb9a8 # m:edit

# Rebase 69a0567..c8fb9a8 onto 69a0567 (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
...

I follow it up with a forced push like so — for sending it upstream. It’s only if the remote already has commits you’re trying to squash.

git push -u origin +master

fish shell

The rebase command feels a little tedious to type out at the prompt, and so this function (placed in ~/.config/fish/config.fish) helps me do the job quickly by running the command gr 4 master, which translates to the first command above:

# fish function to rebase, e.g., gr 4 master
function gr
  git rebase -i origin/$argv[2]~$argv[1] $argv[2]
end

Using two arguments for the freedom to define the number of commits, and the other being able to choose the branch.

For the push, I could write a function to run gpp master like so:

# fish function for push following rebase, e.g., gpp master
function gpp
  git push -u origin +$argv[1]
end

bash shell

Of course, I have machines where bash is still the default. For those, the function (placed in, e.g., ~/.bashrc) becomes:

# bash function for rebase, e.g., gr 4 master
gr() {
  git rebase -i origin/"${2}~${1}" "$2"
}

And for the push in bash, the function would be:

# bash function for the push post rebase, e.g., gpp master
gpp() {
  git push -u origin +"$1"
}

Pulling from a force-pushed remote repository, say, origin/master needs some care. The right way is as follows:

git fetch origin
git reset --hard origin/master

Fish functions

This is a placeholder for my commonly used fish shell functions — saved as files under ~/.config/fish/functions/.

  1. For system information: sbcinf.fish

    # sbcinf.fish
    function sbcinf
      set -l mem (free -h | awk '/Mem:/ {print $3 "/" $2}')
      set -l dsk (df -h | awk '/\/$/ {print $3 "/" $2}')
      set -l tem (vcgencmd measure_temp | grep -o -E '[[:digit:]].*')
      set -l pow (vcgencmd measure_volts | sed 's/volt=//g')
      set -l ipa (hostname -I | awk '{print $1}')
      set -l upt (uptime -p | sed 's/^up //')
    
      echo "Board temp: $tem"
      echo "Disk usage: $dsk"
      echo "IP address: $ipa"
      echo "Memory use: $mem"
      echo "Power cons: $pow"
      echo "Sys uptime: $upt"
    end
    
  2. For downloading a file from GitHub repo. with ghdl user/repo/file.ext. If specific about a branch, then this works too: ghdl user/repo@master/file.ext

    # ghdl.fish
    function ghdl
      if test (count $argv) -ne 1
        echo "Usage: ghdl user/repo[@branch]/path/to/file"
        return 1
      end
    
      set input $argv[1]
    
      # Split the input into repo and path
      set repo_part (echo $input | cut -d'/' -f1-2)
      set path_part (echo $input | cut -d'/' -f3-)
    
      # Extract user and repo (and optional branch)
      set user (echo $repo_part | cut -d'/' -f1)
      set repo_branch (echo $repo_part | cut -d'/' -f2)
    
      if string match -q '*@*' $repo_branch
        set repo (echo $repo_branch | cut -d'@' -f1)
        set branch (echo $repo_branch | cut -d'@' -f2)
      else
        set repo $repo_branch
        set branch "master"  # default fallback branch
      end
    
      if test -z "$user" -o -z "$repo" -o -z "$path_part"
        echo "Error: Must be in the form user/repo[@branch]/path/to/file"
        return 1
      end
    
      # Construct modern raw.githubusercontent.com URL
      set url "https://raw.githubusercontent.com/$user/$repo/refs/heads/$branch/$path_part"
    
      echo "Downloading from: $url"
      curl -LO $url
    end
    
  3. Push functions

    # push function
    #   gp master // normal push to master repo
    #   gpp bb    // rebased push to bb repo
    #
    function gp
      git push -u origin $argv[1]
    end
    function gpp
      git push -u origin +$argv[1]
    end
    
  4. Rebase function

    # gr.fish
    # examples:
    #   gr 4 master
    #   gr 3 bb
    #
    function gr
      git rebase -i origin/$argv[2]~$argv[1] $argv[2]
    end
    
  5. Get the latest release version of a GitHub repo.

    # glr.fish
    # e.g. glr "ckunte/tce"
    function glr \
      --argument-names user_repo
      curl \
          --silent \
          "https://api.github.com/repos/$user_repo/releases/latest" \
      | string match --regex '"tag_name": "\K.*?(?=")'
    end
    
  6. Check file hash

    # check_hashes.fish
    function check_hashes
      # Ensure the user provided a file argument
      if test (count $argv) -ne 1
        echo "Usage: check_hashes <file>"
        return 1
      end
      # Store the file path
      set file $argv[1]
      # Check if the file exists
      if not test -e $file
        echo "Error: File '$file' does not exist."
        return 1
      end
      # Calculate and display the RMD160 hash
      echo "RMD160 hash:"
      printf "%s" "$file" | openssl dgst -rmd160
      # Calculate and display the SHA256 hash
      echo "SHA256 hash:"
      printf "%s" "$file" | openssl dgst -sha256
    end
    
  7. Open folder from within WSL

    # open.fish
    function open
      if test (count $argv) -eq 0
        # No arguments: open current directory
        explorer.exe .
      else
        for arg in $argv
          # If it's a URL
          if string match -qr '^https?://|^mailto:' $arg
            explorer.exe $arg
          # If it's a file or directory that exists
          else if test -e $arg
            explorer.exe (wslpath -w $arg)
          # If it's a Windows-style path (C:\Users\...), try passing directly
          else if string match -qr '^[A-Za-z]:\\' $arg
            explorer.exe $arg
          else
            echo "open: '$arg' not found or invalid" >&2
          end
        end
      end
    end
    
  8. Rename photos

    # renfoto.fish: rename photos e.g. renfoto ST-159
    function renfoto
      set prefix $argv[1]
      rename -N ...01 -X -e '$_ = $prefix . "-photo-$N"' *.png
    end
    

  1. Write and quit in Vim, which is my default git commit editor.