comparison autoload/lawrencium/commit.vim @ 139:065625e1bb31

Split plugin file into multiple extensions.
author Ludovic Chabant <ludovic@chabant.com>
date Mon, 13 Jun 2016 09:32:34 -0700
parents
children 652a6f5df0f3
comparison
equal deleted inserted replaced
138:a2d823c82e5f 139:065625e1bb31
1
2 function! lawrencium#commit#init() abort
3 call lawrencium#add_command("-bang -nargs=* -complete=customlist,lawrencium#list_repo_files Hgcommit :call lawrencium#commit#HgCommit(<bang>0, 0, 0, <f-args>)")
4 call lawrencium#add_command("-bang -nargs=* -complete=customlist,lawrencium#list_repo_files Hgvcommit :call lawrencium#commit#HgCommit(<bang>0, 1, 0, <f-args>)")
5 endfunction
6
7 function! lawrencium#commit#HgCommit(bang, vertical, callback, ...) abort
8 " Get the repo we'll be committing into.
9 let l:repo = lawrencium#hg_repo()
10
11 " Get the list of files to commit.
12 " It can either be several files passed as extra parameters, or an
13 " actual list passed as the first extra parameter.
14 let l:filenames = []
15 if a:0
16 let l:filenames = a:000
17 if a:0 == 1 && type(a:1) == type([])
18 let l:filenames = a:1
19 endif
20 endif
21
22 " Open a commit message file.
23 let l:commit_path = s:tempname('hg-editor-', '.txt')
24 let l:split = a:vertical ? 'vsplit' : 'split'
25 execute l:split . ' ' . l:commit_path
26 call append(0, ['', ''])
27 call append(2, split(s:HgCommit_GenerateMessage(l:repo, l:filenames), '\n'))
28 call cursor(1, 1)
29
30 " Setup the auto-command that will actually commit on write/exit,
31 " and make the buffer delete itself on exit.
32 let b:mercurial_dir = l:repo.root_dir
33 let b:lawrencium_commit_files = l:filenames
34 if type(a:callback) == type([])
35 let b:lawrencium_commit_pre_callback = a:callback[0]
36 let b:lawrencium_commit_post_callback = a:callback[1]
37 let b:lawrencium_commit_abort_callback = a:callback[2]
38 else
39 let b:lawrencium_commit_pre_callback = 0
40 let b:lawrencium_commit_post_callback = a:callback
41 let b:lawrencium_commit_abort_callback = 0
42 endif
43 setlocal bufhidden=delete
44 setlocal filetype=hgcommit
45 if a:bang
46 autocmd BufDelete <buffer> call s:HgCommit_Execute(expand('<afile>:p'), 0)
47 else
48 autocmd BufDelete <buffer> call s:HgCommit_Execute(expand('<afile>:p'), 1)
49 endif
50 " Make commands available.
51 call lawrencium#define_commands()
52 endfunction
53
54 let s:hg_status_messages = {
55 \'M': 'modified',
56 \'A': 'added',
57 \'R': 'removed',
58 \'C': 'clean',
59 \'!': 'missing',
60 \'?': 'not tracked',
61 \'I': 'ignored',
62 \' ': '',
63 \}
64
65 function! s:HgCommit_GenerateMessage(repo, filenames) abort
66 let l:msg = "HG: Enter commit message. Lines beginning with 'HG:' are removed.\n"
67 let l:msg .= "HG: Leave message empty to abort commit.\n"
68 let l:msg .= "HG: Write and quit buffer to proceed.\n"
69 let l:msg .= "HG: --\n"
70 let l:msg .= "HG: user: " . split(a:repo.RunCommand('showconfig ui.username'), '\n')[0] . "\n"
71 let l:msg .= "HG: branch '" . split(a:repo.RunCommand('branch'), '\n')[0] . "'\n"
72
73 execute 'lcd ' . fnameescape(a:repo.root_dir)
74 if len(a:filenames)
75 let l:status_lines = split(a:repo.RunCommand('status', a:filenames), "\n")
76 else
77 let l:status_lines = split(a:repo.RunCommand('status'), "\n")
78 endif
79 for l:line in l:status_lines
80 if l:line ==# ''
81 continue
82 endif
83 let l:type = matchstr(l:line, '\v^[MARC\!\?I ]')
84 let l:path = l:line[2:]
85 let l:msg .= "HG: " . s:hg_status_messages[l:type] . ' ' . l:path . "\n"
86 endfor
87
88 return l:msg
89 endfunction
90
91 function! s:HgCommit_Execute(log_file, show_output) abort
92 " Check if the user actually saved a commit message.
93 if !filereadable(a:log_file)
94 call lawrencium#error("abort: Commit message not saved")
95 if exists('b:lawrencium_commit_abort_callback') &&
96 \type(b:lawrencium_commit_abort_callback) == type("") &&
97 \b:lawrencium_commit_abort_callback != ''
98 call lawrencium#trace("Executing abort callback: ".b:lawrencium_commit_abort_callback)
99 execute b:lawrencium_commit_abort_callback
100 endif
101 return
102 endif
103
104 " Execute a pre-callback if there is one.
105 if exists('b:lawrencium_commit_pre_callback') &&
106 \type(b:lawrencium_commit_pre_callback) == type("") &&
107 \b:lawrencium_commit_pre_callback != ''
108 call lawrencium#trace("Executing pre callback: ".b:lawrencium_commit_pre_callback)
109 execute b:lawrencium_commit_pre_callback
110 endif
111
112 call lawrencium#trace("Committing with log file: " . a:log_file)
113
114 " Clean all the 'HG: ' lines.
115 let l:is_valid = s:clean_commit_file(a:log_file)
116 if !l:is_valid
117 call lawrencium#error("abort: Empty commit message")
118 return
119 endif
120
121 " Get the repo and commit with the given message.
122 let l:repo = lawrencium#hg_repo()
123 let l:hg_args = ['-l', a:log_file]
124 call extend(l:hg_args, b:lawrencium_commit_files)
125 let l:output = l:repo.RunCommand('commit', l:hg_args)
126 if a:show_output && l:output !~# '\v%^\s*%$'
127 call lawrencium#trace("Output from hg commit:", 1)
128 for l:output_line in split(l:output, '\n')
129 echom l:output_line
130 endfor
131 endif
132
133 " Execute a post-callback if there is one.
134 if exists('b:lawrencium_commit_post_callback') &&
135 \type(b:lawrencium_commit_post_callback) == type("") &&
136 \b:lawrencium_commit_post_callback != ''
137 call lawrencium#trace("Executing post callback: ".b:lawrencium_commit_post_callback)
138 execute b:lawrencium_commit_post_callback
139 endif
140 endfunction
141