BreakingExpress

Make your personal Git subcommands

Git is fairly well-known for having plenty of subcommands, like clone, init, add, mv, restore, bisect, blame, present, rebase, and plenty of extra. In a earlier article, I wrote concerning the very helpful rev-parse subcommand for Git. Even with all of those subcommands obtainable, customers nonetheless provide you with features to enhance their Git expertise. While you are free to create Git-related instructions and run them as scripts, it is easy to make your personal customized Git subcommands. You may even combine them with Git by rev-parse.

Create a easy Git script

A script that integrates with Git will be as advanced as you want it to be, or it may be quick and simple.

As a easy instance, assume you’ve got created a script that gathers the file names of your newest commit and locations them right into a file known as newest.txt. You use this script after each commit for reporting functions, and you’ve got determined it will be helpful to have the ability to run the script as if it have been a built-in characteristic of Git, as an illustration, with the command git report.

Currently, operating git report renders this error:

$ git report
git: 'report' isn't a git command. See 'git --help'.

The most comparable command is
        bugreport

Here’s the script that generates your report:

#!/bin/sh

TOP=$(git rev-parse --show-toplevel)
HASH=$(git log --pretty=format:'%h' -n 1)

mkdir "${TOP}"/experiences || true

git diff-tree
--no-commit-id --name-only
-r HEAD > "${TOP}"/experiences/$HASH

Save this file as git-report.sh somewhere in your PATH.

You’re not going to run this script instantly, so do embody the .sh extension within the identify. Make the script executable:

$ chmod +x git-report.sh

Create the front-end command

You can pressure Git to run git report by rev-parse and launch your git-report.sh script as an alternative of returning an error. Here’s the script:


Save this file as git-report (don’t use a .sh file extension) someplace in your PATH. Make the script executable:

$ chmod +x git-report

Running your customized Git command

Now try it out. First, create the infrastructure and a few pattern information:

$ mkdir myproject ; cd !$
$ git init
$ echo "foo" > hi there.txt
$ git add hi there.txt
$ git commit -m 'first file'
$ git report

Look contained in the experiences listing to see a file of your newest commit:

$ cat experiences/2e3efd8
hi there.txt

Pass arguments to your script

You can go arguments by rev-parse, too. I assist preserve a Git subcommand known as Git-portal, which helps handle massive multimedia information related to a Git repository.

It’s slightly like Git LFS or Git Annex, besides with out the overhead of versioning the precise media information. (The symlinks to the information are stored underneath model management, however the contents of the information are handled independently, which is beneficial in situations the place the inventive course of is distinct from the event course of.)

To combine Git-portal with Git as a subcommand, I take advantage of a easy front-end shell script, which in flip invokes rev-parse for literal parsing.

The instance script supplied on this article cannot take any parameters, however the precise Git scripts I write virtually at all times do. Here’s how arguments are handed, for instance, to Git-portal:

#!/bin/sh

ARG=$(git rev-parse --sq-quote "$@")
CMD="git-portal.sh $ARG"
eval "$CMD"

The --sq-quote possibility quotes all arguments following git portal and consists of them as new parameters for git-portal.sh, which is the script with all of the helpful features in it. A brand new command is assembled into the CMD variable, after which that variable is evaluated and executed.

Here’s a easy instance you possibly can run. It’s a modified model of a script I take advantage of to resize and apply correct copyright (or copyleft, because the case could also be) EXIF information to photographs for a flat-file CMS I take advantage of Git to handle.

This is a simplified instance, and there is no good motive for this script to be a Git subcommand, nevertheless it’s demonstrative. You can use your creativeness to seek out methods to develop it to make use of Git features.

Call this file git-imager.sh:

#!/bin/sh
## Requires
# GNU Bash
# exiftool (typically packaged as perl-Image-exiftool)
# Image Magick

PIC="${1}"
COPY="-Copyright"
LEFT="${2}"

mogrify -geometry 512^x512 -gravity Center
        -crop 512x512+0+0 "${1}"

exiftool -Copyright="${2}" "${1}"

The front-end script for Git integration passes all parameters (the file identify and the license) by to the git-imager.sh script.

#!/bin/sh

ARG=$(git rev-parse --sq-quote "$@")
CMD="git-imager.sh $ARG"
eval "$CMD"

Try it out:

$ git imager brand.jpg "Tux CC BY-SA"
 1 picture information up to date

Easy Git customization

Creating your personal Git subcommand makes your customized scripts really feel like pure elements of Git. For customers, it makes the brand new subcommands simple to recollect, and it helps your subcommands combine into the remainder of everybody’s Git workflow.

Exit mobile version