Skip to content
Freed-WuPublic

About

Use luagit2 to realize a :Git in neovim

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Repository files navigation

git2.nvim

pre-commit.ci status github/workflow

github/downloads github/downloads/latest github/issues github/issues-closed github/issues-pr github/issues-pr-closed github/discussions github/milestones github/forks github/stars github/watchers github/contributors github/commit-activity github/last-commit github/release-date

github/license github/languages github/languages/top github/directory-file-count github/code-size github/repo-size github/v

luarocks

Use luagit2 to realize a :Git in neovim.

Related Projects

CLI

  • vim-fugitive: call git process synchronously. written in vim script.
  • vim-gina: call git process asynchronously. written in vim script. stop maintenance.
  • vim-gin: call git process asynchronously. written in denojs. same author as vim-gina.
  • git.nvim: call git process asynchronously. written in lua.

UI

  • vim-flog: call git process synchronously. written in vim script.
  • committia.vim: UI for CLI's git commit. call git process synchronously. written in vim script.
  • nvim-tinygit: call git process asynchronously. written in lua.
  • neogit: call git process asynchronously. written in lua.
  • blame.nvim: UI for git blame. call git process asynchronously. written in lua. inspired fugit2.nvim's Fugit2Blame
  • fugit2.nvim: call libgit2 by luajit FFI. written in lua.

Plugin

Similar Projects

  • magit: git UI for emacs

Motivation

  • Many vim users are experienced CLI users, a vim command :Git like CLI's git should be helpful to save their learning time. A good CLI even is a more significant matter than UI. In this aspect, vim-fugitive, vim-gina do well.
  • call git process synchronously is slow. many vim plugins select use vim script's jobstart() or neovim's vim.uv.timer to call git process asynchronously. fugit2.nvim is 1st plugin to call libgit2 and bring novelty to vim community. Don't call git process can avoid some bug of incorrect shell settings. On Oct 2024, official lua binding of libgit2 lua-git2 is released firstly. I believe it can bring more convenience to vim plugin developing and start the experiment.

Dependence

# Ubuntu
sudo apt-get -y install libgit2-dev libgit2
sudo apt-mark auto libgit2-dev
# ArchLinux
sudo pacman -S libgit2
# Android Termux
apt-get -y install libgit2
# Nix
# use nix-shell to create a virtual environment then build
# homebrew
brew install libgit2 pkg-config
# Windows msys2
pacboy -S --noconfirm pkg-config libgit2 gcc

Install

rocks.nvim

Command style

:Rocks install git2.nvim

Declare style

~/.config/nvim/rocks.toml:

[plugins]
"git2.nvim" = "scm"

Then

:Rocks sync

or:

$ luarocks --lua-version 5.1 --local --tree ~/.local/share/nvim/rocks install git2.nvim
# ~/.local/share/nvim/rocks is the default rocks tree path
# you can change it according to your vim.g.rocks_nvim.rocks_path

lazy.nvim

require("lazy").setup {
  spec = {
    { "Freed-Wu/git2.nvim", lazy = false },
  },
}

Usage

Git

:edit subdir/test.txt
:Git init
:cd subdir
:Git -C .. add %
:Git rm --cached %

Git blame

:Git blame

Use virtual text to display blame info like :Fugit2Blame.

commit

git commit

will :vsplit a diff window like committia.

inoremap <buffer> <M-N> <C-O>:lua require('git2.nvim.committia').scroll_window('<C-V><C-F>')<CR>
inoremap <buffer> <M-P> <C-O>:lua require('git2.nvim.committia').scroll_window('<C-V><C-B>')<CR>

Integrations

let g:airline#extensions#hunks#enabled = 1
if has('nvim')
  function! s:gitModified() abort
    let files = v:lua.require'git2.status'.ls(v:true)
    return map(files, "{'line': v:val, 'path': v:val}")
  endfunction

  function! s:gitUntracked() abort
    let files = v:lua.require'git2.status'.ls(v:false, v:true)
    return map(files, "{'line': v:val, 'path': v:val}")
  endfunction

  let g:startify_lists = [
        \ {'type': function('s:gitModified'),  'header': ['! Git Modified']},
        \ {'type': function('s:gitUntracked'), 'header': ['? Git Untracked']},
        \ ]
endif
! Git Modified

   [.a] README.md

? Git Untracked

   [.b] a.txt

if has('nvim')
  nnoremap <buffer><nowait> << :<C-U>call fern#scheme#file#mapping#git#stage()<CR>
  nnoremap <buffer><nowait> >> :<C-U>call fern#scheme#file#mapping#git#unstage()<CR>
endif
if has('nvim')
  call fern#scheme#file#hook#git#init()
endif
 git2.nvim/
  autoload/ [ -]
   fern/ [ -]
    scheme/ [ -]
     file/ [ -]
      hook/ [ -]
       git.vim [ M]
      mapping/
  bin/
  lua/
  packages/
  plugin/
  scripts/
  spec/
  git2.nvim-scm-1.rockspec
  LICENSE
 󰂺 README.md [ M]
  shell.nix

TODO

  • full CLI APIs. wait upstream
  • UI for CLI's git commit, vim's :Git blame, :Git status, ...

About

Use luagit2 to realize a :Git in neovim

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages