-- "Git Mapping function GitPullAndNotify() vim.notify('Pull Processing...', vim.log.levels.INFO, { title = 'Git', timeout = 5000, -- 5 seconds instead of 10 hours }) vim.fn.jobstart('git fetch --all && git pull --rebase', { on_stdout = function(id, data, e) notif(id, data, e, 4000) end, on_stderr = function(id, data, e) notif(id, data, e, 4000) end, on_exit = function(id, data, e) notif(id, data, e, 4000) end, }) end function GitPushAndNotify() vim.notify('Push Processing...', vim.log.levels.INFO, { title = 'Git', timeout = 5000, -- 5 seconds instead of 10 hours }) vim.fn.jobstart('git pull --rebase && git push', { on_stdout = function(id, data, e) notif(id, data, e, 4000) end, on_stderr = function(id, data, e) notif(id, data, e, 4000) end, on_exit = function(id, data, e) notif(id, data, e, 4000) end, }) end function OpenGitStatus() -- Check if neogit is already open local neogit_open = false for _, buf in ipairs(vim.api.nvim_list_bufs()) do local name = vim.api.nvim_buf_get_name(buf) if name:match 'NeogitStatus' then neogit_open = true vim.api.nvim_buf_delete(buf, { force = true }) break end end if not neogit_open then vim.cmd [[Neotree close]] vim.cmd [[Neogit]] end end function CreateBranchAndPush(branchName) RunCommandAndNotify('git checkout -b ' .. branchName .. ' && git push -u origin ' .. branchName) end vim.keymap.set('n', '', OpenGitStatus, { desc = '[G]it [S]tatus', }) function GitCommit(commitMessage) RunCommandAndNotify('git add . && git commit -m "' .. commitMessage .. '"') end function RevertToCommitUnderCursor() local commit = vim.fn.expand '' if commit == nil or commit == '' then print 'No word under cursor.' return end local answer = vim.fn.input("Reset to commit '" .. commit .. "' and force push? (yes/no): ") if answer ~= 'yes' then print 'Cancelled.' return end local cmd = string.format('git reset --hard %s && git push --force', commit) RunCommandInNewTab(cmd) end function UndoCommit() local n = vim.fn.input 'Enter number of commits to undo: ' RunCommandAndNotify('git reset --soft HEAD~' .. n) end vim.keymap.set('n', 'gx', UndoCommit, { desc = 'Discard changes in last commit (Undo Commit)' }) vim.keymap.set('n', 'gr', RevertToCommitUnderCursor, { desc = 'Reset HEAD to commit under cursor + force push' }) vim.keymap.set('n', 'ga', function() local branchName = vim.fn.input 'Enter commit message: ' if branchName == '' then return end CreateBranchAndPush(branchName) end, { desc = '[G]it [A]dd Branch', noremap = true, silent = false, }) vim.keymap.set('n', 'gc', function() local commitMessage = vim.fn.input 'Enter commit message: ' if commitMessage == '' then return end GitCommit(commitMessage) end, { desc = '[G]it [C]ommit', noremap = true, silent = false, }) vim.keymap.set('n', 'gh', ':Telescope git_bcommits', { desc = '[G]it File [H]istory', noremap = true, silent = true, }) vim.keymap.set('n', 'gp', GitPushAndNotify, { desc = '[G]it [P]ush', noremap = true, silent = true, }) vim.keymap.set('n', 'gu', GitPullAndNotify, { desc = '[G]it P[u]ll', noremap = true, silent = true, }) vim.keymap.set('n', 'gd', 'Neogit diff', { desc = '[G]it [D]iff', noremap = true, silent = true, }) vim.keymap.set('n', 'gb', 'Neogit branch', { desc = '[G]it [B]ranch', noremap = true, silent = true, }) vim.keymap.set('n', 'gv', 'DiffviewOpen', { desc = '[G]it [V]iew Diff (current file)', noremap = true, silent = true, }) vim.keymap.set('n', 'gl', ':Telescope git_commits', { desc = '[G]it [L]og', noremap = true, silent = true, }) vim.api.nvim_create_user_command('GitInitPush', function() local username = 'yokowasis' local repo = vim.fn.fnamemodify(vim.loop.cwd(), ':t') if repo == '' then print 'Could not determine repository name from current directory' return end local remote = 'https://github.com/' .. username .. '/' .. repo .. '.git' local cmd = 'git init && git add . && git commit -m "Initial commit" && git branch -M main && gh repo create ' .. repo .. ' --private --source=. --remote=origin --push' vim.cmd('terminal ' .. cmd) end, {}) vim.keymap.set('n', 'gi', ':GitInitPush', { desc = '[G]it [I]nit and Push', noremap = true, silent = true, }) vim.keymap.set('n', 'coo', function() -- Get current line and extract branch name, then checkout with neogit local line = vim.api.nvim_get_current_line() local branch = line:match 'origin/(.+)' if branch then vim.cmd('Neogit branch checkout ' .. branch) else vim.notify('No origin branch found on current line', vim.log.levels.WARN) end end, { desc = 'Checkout Origin Branch', noremap = true, silent = true, }) return { { 'NeogitOrg/neogit', dependencies = { 'nvim-lua/plenary.nvim', -- required 'sindrets/diffview.nvim', -- optional - for diff view 'nvim-telescope/telescope.nvim', -- optional - for telescope integration }, config = function() require('neogit').setup { -- Neogit configuration integrations = { telescope = true, diffview = true, }, sections = { untracked = { folded = false, }, unstaged = { folded = false, }, staged = { folded = false, }, }, } -- Branch-specific keymaps vim.keymap.set('n', 'gb', 'Neogit branch', { desc = '[G]it [B]ranch menu', noremap = true, silent = true, }) end, }, }