changeset 10:7d16084d40a9

Added 'Hgcommit' command (and this very change is committed with it!).
author Ludovic Chabant <ludovic@chabant.com>
date Mon, 12 Dec 2011 13:29:24 -0800
parents 82a49134a85c
children b4baab0a4a92
files plugin/lawrencium.vim
diffstat 1 files changed, 77 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/plugin/lawrencium.vim	Mon Dec 12 10:59:46 2011 -0800
+++ b/plugin/lawrencium.vim	Mon Dec 12 13:29:24 2011 -0800
@@ -245,16 +245,6 @@
 
 " Hgstatus {{{
 
-let s:hg_status_messages = { 
-    \'M': 'modified',
-    \'A': 'added',
-    \'R': 'removed',
-    \'C': 'clean',
-    \'!': 'missing',
-    \'?': 'not tracked',
-    \'I': 'ignored',
-    \}
-
 function! s:HgStatus() abort
     " Get the repo and the `hg status` output.
     let l:repo = s:hg_repo()
@@ -404,6 +394,83 @@
 
 " }}}
 
+" Hgcommit {{{
+
+function! s:HgCommit(bang, vertical) abort
+    " Get the repo we'll be committing into.
+    let l:repo = s:hg_repo()
+
+    " Open a commit message file.
+    let l:commit_path = tempname()
+    let l:split = a:vertical ? 'vsplit' : 'split'
+    execute l:split . ' ' . l:commit_path
+    call append(0, ['', ''])
+    call append(2, split(s:HgCommit_GenerateMessage(l:repo), '\n'))
+
+    " Setup the auto-command that will actually commit on write/exit,
+    " and make the buffer delete itself on exit.
+    let b:mercurial_dir = l:repo.root_dir
+    setlocal bufhidden=delete
+    if a:bang
+        autocmd BufDelete <buffer> call s:HgCommit_Execute(expand('<afile>:p'), 1)
+    else
+        autocmd BufDelete <buffer> call s:HgCommit_Execute(expand('<afile>:p'), 0)
+    endif
+endfunction
+
+let s:hg_status_messages = { 
+    \'M': 'modified',
+    \'A': 'added',
+    \'R': 'removed',
+    \'C': 'clean',
+    \'!': 'missing',
+    \'?': 'not tracked',
+    \'I': 'ignored',
+    \' ': '',
+    \}
+
+function! s:HgCommit_GenerateMessage(repo) abort
+    let l:msg  = "HG: Enter commit message. Lines beginning with 'HG:' are removed.\n"
+    let l:msg .= "HG: Leave message empty to abort commit.\n"
+    let l:msg .= "HG: Write and quit buffer to proceed.\n"
+    let l:msg .= "HG: --\n"
+    let l:msg .= "HG: user: " . split(a:repo.RunCommand('showconfig ui.username'), '\n')[0] . "\n"
+    let l:msg .= "HG: branch '" . split(a:repo.RunCommand('branch'), '\n')[0] . "'\n"
+
+    let l:status_lines = split(a:repo.RunCommand('status'), "\n")
+    for l:line in l:status_lines
+        if l:line ==# ''
+            continue
+        endif
+        let l:type = matchstr(l:line, '^[MARC\!\?I ]')
+        let l:path = l:line[2:]
+        let l:msg .= "HG: " . s:hg_status_messages[l:type] . ' ' . l:path . "\n"
+    endfor
+
+    return l:msg
+endfunction
+
+function! s:HgCommit_Execute(log_file, show_output) abort
+    " Check if the user actually saved a commit message.
+    if !filereadable(a:log_file)
+        call s:trace("Commit message was not saved... abort commit.")
+        return
+    endif
+
+    " Get the repo and commit with the given message.
+    call s:trace("Committing with log file: " . a:log_file)
+    let l:repo = s:hg_repo()
+    let l:output = l:repo.RunCommand('commit', '-l', a:log_file)
+    if a:show_output
+        echom l:output
+    endif
+endfunction
+
+call s:AddMainCommand("-bang Hgcommit :execute s:HgCommit(<bang>0, 0)")
+call s:AddMainCommand("-bang Hgvcommit :execute s:HgCommit(<bang>0, 1)")
+
+" }}}
+
 " }}}
 
 " Autoload Functions {{{