Added 'svn commit' support.
[mp-5.x.git] / mp_vcs.mpsl
blob90465a56b2f4892dd04d87cf167d410d345608a4
1 /*
3     Minimum Profit 5.x
4     A Programmer's Text Editor
6     Version Control System support
8     Copyright (C) 1991-2012 Angel Ortega <angel@triptico.com>
10     This program is free software; you can redistribute it and/or
11     modify it under the terms of the GNU General Public License
12     as published by the Free Software Foundation; either version 2
13     of the License, or (at your option) any later version.
15     This program is distributed in the hope that it will be useful,
16     but WITHOUT ANY WARRANTY; without even the implied warranty of
17     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18     GNU General Public License for more details.
20     You should have received a copy of the GNU General Public License
21     along with this program; if not, write to the Free Software
22     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24     http://www.triptico.com
28 /** data **/
30 mp.vcs = [
31     {
32         id:     'git',
33         check:  [ '.git', '../.git', '../../.git' ],
34         opts:   {
35             'git diff'          => NULL,
36             'git status'        => NULL,
37             'git log'           => NULL,
38             "git diff '%s'"     => NULL,
39             "git log '%s'"      => NULL,
40             "git add '%s'"      => NULL,
41             "git blame '%s'"    => NULL,
42             "git checkout '%s'" => NULL,
43             "git stash"         => NULL,
44             "git stash apply"   => NULL,
45             "git push"          => NULL,
46             "git pull"          => NULL,
47             "git commit -a"     => {
48                 pre:    sub (doc) {
49                     mp.vcs_commit_on_close(doc, "git commit -a -F '%s'");
50                 }
51             }
52         }
53     },
54     {
55         id:     'SVN',
56         check:  [ '.svn' ],
57         opts:   {
58             'svn diff'          => NULL,
59             'svn status'        => NULL,
60             'svn log'           => NULL,
61             "svn diff '%s'"     => NULL,
62             "svn log '%s'"      => NULL,
63             "svn add '%s'"      => NULL,
64             "svn annotate '%s'" => NULL,
65             "svn update"        => NULL,
66             "svn commit"        => {
67                 pre:    sub (doc) {
68                     mp.vcs_commit_on_close(doc, "svn commit -F '%s'");
69                 }
70             }
71         }
72     }
75 /** editor actions **/
77 mp.actions['vcs'] = sub (d) {
78     local v = NULL;
80     foreach (n, mp.vcs) {
81         foreach (d, n.check) {
82             if (stat(d) != NULL) {
83                 v = n;
84                 break;
85             }
86         }
88         if (v != NULL)
89             break;
90     }
92     if (v == NULL)
93         mp.alert(L("This directory is not under a supported version control system"));
94     else {
95         local l = sort(keys(v.opts));
97         local t = mp.form(
98             [
99                 {
100                     label:  sprintf(L("Available %s commands"), v.id),
101                     type:   'list',
102                     list:   l,
103                     value:  mp.vcs_target
104                 }
105             ]
106         );
108         if (t != NULL) {
109             mp.vcs_target = t[0];
111             local cmd = sprintf(l[mp.vcs_target], d.name);
113             /* if a 'pre' hook exists, run it and get the
114                cmd to be executed (NULL if none) */
115             if (v.opts[cmd].pre) {
116                 cmd = v.opts[cmd].pre(d);
117             }
119             if (cmd != NULL) {
120                 local log = mp.open(sprintf(L("<%s output>"), cmd));
122                 log.txt.lines   = [ '' ];
123                 log.txt.y       = 0;
125                 if (v.opts[cmd] == NULL) {
126                     mp.actions.exec_command(log, cmd);
127                 }
129                 mp.detect_syntax(log);
130                 mp.move_bof(log);
131                 log.txt.mod     = 0;
132                 log.undo        = [];
133                 log.read_only   = 1;
134             }
135         }
136     }
139 /** default key bindings **/
141 mp.keycodes['ctrl-p'] = 'vcs';
143 /** action descriptions **/
145 mp.keycodes['vcs'] = LL("Version Control...");
147 /** code **/
149 sub mp.vcs_commit_on_close(doc, cmd)
150 /* on_close handler for commit message documents */
152     /* create a commit message */
153     local ci = mp.open('.COMMIT_MESSAGE');
155     ci.commit_cmd = cmd;
157     /* and add an on_close handler */
158     push(ci.on_close,
159         sub (doc) {
160             local ret = 1;
162             if (stat(doc.name)) {
163                 doc.txt.lines   = [ '' ];
164                 doc.txt.y       = 0;
165                 mp.actions.exec_command(doc, sprintf(doc.commit_cmd, doc.name));
167                 mp.detect_syntax(doc);
168                 mp.move_bof(doc);
169                 doc.txt.mod     = 0;
170                 doc.undo        = [];
171                 doc.read_only   = 1;
173                 doc.name = sprintf(L("<%s output>"), doc.commit_cmd);
175                 /* reject closing (file has been modified) */
176                 ret = 0;
177             }
179             unlink('.COMMIT_MESSAGE');
181             /* delete this on_close handler */
182             pop(doc.on_close);
184             return ret;
185         }
186     );
188     return NULL;