Bashrc for non confusing git and mercury

Scenario: I prefer Mercurial, but I often have to deal with git repositories, so I end up using both of them on a daily basis. As such, I often print:

hg commit -m "message"

... where instead I want:

git commit -am "message"

... and vice versa.

I would like my bashrc file to be smart enough so that it can detect this confusion by first checking to see if the directory I am in has the ".git" or ".hg" and "fix" directories, the syntax is consistent.

Basically, I think what I'm looking for is a way to override both commands using the bash function (I know how to do this) and somehow check if the first part of the command starts with "hg ci -m" or " git ci -am "(I don’t know this).

I'm not sure if bashrc's aliases / functions can be expanded to this level of expressiveness, but I'm sure I'm not the only one who was bitten by this "confusion", so I wonder if someone has already come up with a solution.

+4
source share
2 answers

I don’t know enough about hgto suggest rewriting commands, but here are some convenient features for you:

is_git_repo() { command git rev-parse --git-dir &> /dev/null; }
is_hg_repo() { command hg root &> /dev/null; }

Usage example:

git() { 
  if is_hg_repo
  then
    echo "This is is a hg repo, you silly goose!"
    return 1
  else
    command git "$@"
  fi
}

hg() { 
  if is_git_repo
  then
    echo "Surely you're joking... this is git repo!"
    return 1
  else
    command hg "$@"
  fi
}
+3
source

How to make a script to check for the presence of directories .gitor .hg, and then in yours .bashrcjust overlay the general command to run the script?

As a matter of choosing one or the other, perhaps something like strings

#!/bin/bash

if [ -d ./.git ]
  then
    cmd='git commit -am '
  else
    cmd='hg commit -m '
fi

cmd=$cmd+$1
echo $cmd
$cmd

~/.scripts/commit.sh bashrc

alias commit='~/.scripts/commit.sh'

, , commit 'awesome bugfixes', .

+1

Source: https://habr.com/ru/post/1536693/


All Articles