📜 ⬆️ ⬇️

Turning Midnight Commander into a console browser for GIT

Instead of the preface ...


I have long wanted to see the code about how it was done in meld, but only in the console, and I also wanted it all to be linked to the GIT file version control system. In general, everything is done with 1 button without leaving mc.

Ydiff

Once, on our channel (or it was on the truck ...), someone named Daniel Borca made a little more noise and ran away, apparently didn’t like our bureaucracy, but he left behind him a few raw patches for the upcoming release 4.7.0-pre1 midnight commander. One of them contained ydiff. [Ch] and zdiff. [Ch] ... As it turned out, what I wanted to get was already implemented in the ancient fork mc-mp and in the latest version of ru-fork . After a 2-day "sawing", I connected the file comparison function to the latest version of mc ... There was one done. :)

View GIT repository

Gitfs panel plugin

The second problem was that I would like to view patches from the GIT repository directly to the midnight commander, directly from the repository directory. And I thought about writing a VFS for midnight commander with the help of which it would be possible to navigate the repository in the right panel. A few minutes later, “picking” was born the first raw version of gitfs
The current fully working but not final version is presented below:
#! / bin / sh

LANG = C
umask 077
')
gitfs_list ( )
{
DATE = ` date + "% b% d% H:% M " `
GIT_DIR = "$ 2 / .git"
user = ` whoami`
git ls-files | while read fname
do
echo "-r - r - r-- 1 $ user 0 0 $ DATE` dirname $ fname` / (git) `basename $ fname` "
done
}

gitfs_copyout ( )
{

echo -e "$ 2" > "$ 4"
echo "$ 3" | sed -e s / \ ( git \ ) \ // >> "$ 4"
}

case "$ 1" in
list ) gitfs_list "$ @" ;;
copyout ) gitfs_copyout "$ @" ;;
* ) exit 1 ;;
esac
exit 0



Gitlogfs panel plugin

I wanted to not only navigate the repository, but also be able to receive files of any revision. For this plugin gitlogfs was written
#! / bin / sh

LANG = C

changesetfs_list ( )
{
DATE = ` date + "% b% d% H:% M " `
GIT_DIR = ` head -n1 $ 2` ".git"
fname = ` tail -n1 $ 2`
USER = ` whoami`
git log --abbrev = 7 --pretty = "format:% at% h% an" " $ fname " | while read chset
do
TIMESTAMP = ` echo $ chset | cut -f1 -d "" `
DATE = ` date -d @ " $ TIMESTAMP " + "% b% d% H:% M " `
NAME = ` echo $ chset | cut -f2 -d "" ` " " ` basename $ fname `
echo "-rw-rw-rw- 1 $ USER 0 0 $ DATE $ NAME "
done

}

changesetfs_copyout ( )
{
GIT_DIR = ` head -n1 $ 2` ".git"
fname = ` tail -n1 $ 2`
chset = ` echo " $ 3 " | cut -f 1 -d "" `
filecommit = ` git show --raw --pretty = tformat: % h " $ chset " " $ fname " | grep ^: | grep ... | cut -f3 -d "" | cut -f1 -d "." `
git show " $ filecommit " > "$ 4"
}

case "$ 1" in
list ) changesetfs_list "$ @" ; exit 0 ;;
copyout ) changesetfs_copyout "$ @" ; exit 0 ;;
esac
exit 1


Connecting gitfs and gitlogfs plugins to a midnight commander

In order for any panel plugins to connect to mc, it’s enough to put them in the home directory of the midnight commander ~ / .mc / extfs.d . In order for the gitlogfs plugin to be called when you click on any file starting with the prefix (git), you need to add a description of this file type in the file ~ / .mc / bindings , like this:
regex / ^ \ ( git \ )
Open = % cd % p #gitlogfs


Illustrations

The gitfs call is made by the cd #gitfs command in the GIT repository directory.
Free Image Hosting at FunkyIMG.com

Navigation through the repository is the same as in any file system.
Free Image Hosting at FunkyIMG.com

Log in to gitlogfs by clicking on any project file with a prefix (git)
Free Image Hosting at FunkyIMG.com

Comparison of 2 arbitrary file revisions is performed using the hot key Ctrl-x, Ctrl-y
Free Image Hosting at FunkyIMG.com

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


All Articles