-
Notifications
You must be signed in to change notification settings - Fork 0
/
tfs.el
554 lines (478 loc) · 23.6 KB
/
tfs.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
;;; tfs.el --- MS Team Foundation Server commands for Emacs.
;; Author : Dino Chiesa <[email protected]>
;; Version : 0.2.6
;; X-URL : http://cheeso.members.winisp.net/srcview.aspx?dir=emacs&file=tfs.el
;; Last saved : <2011-May-03 10:49:03>
;;
;; Copyright 2009-2010 Dino Chiesa
;; Microsoft Public License (Ms-PL)
;;
;; This license governs use of the accompanying software, the tfs.el
;; library ("the software"). If you use the software, you accept this
;; license. If you do not accept the license, do not use the software.
;;
;; 1. Definitions
;;
;; The terms "reproduce," "reproduction," "derivative works," and
;; "distribution" have the same meaning here as under U.S. copyright
;; law.
;;
;; A "contribution" is the original software, or any additions or
;; changes to the software.
;;
;; A "contributor" is any person that distributes its contribution under
;; this license.
;;
;; "Licensed patents" are a contributor's patent claims that read
;; directly on its contribution.
;;
;; 2. Grant of Rights
;;
;; (A) Copyright Grant- Subject to the terms of this license, including
;; the license conditions and limitations in section 3, each contributor
;; grants you a non-exclusive, worldwide, royalty-free copyright license
;; to reproduce its contribution, prepare derivative works of its
;; contribution, and distribute its contribution or any derivative works
;; that you create.
;;
;; (B) Patent Grant- Subject to the terms of this license, including the
;; license conditions and limitations in section 3, each contributor
;; grants you a non-exclusive, worldwide, royalty-free license under its
;; licensed patents to make, have made, use, sell, offer for sale,
;; import, and/or otherwise dispose of its contribution in the software
;; or derivative works of the contribution in the software.
;;
;; 3. Conditions and Limitations
;;
;; (A) No Trademark License- This license does not grant you rights to
;; use any contributors' name, logo, or trademarks.
;;
;; (B) If you bring a patent claim against any contributor over patents
;; that you claim are infringed by the software, your patent license
;; from such contributor to the software ends automatically.
;;
;; (C) If you distribute any portion of the software, you must retain
;; all copyright, patent, trademark, and attribution notices that are
;; present in the software.
;;
;; (D) If you distribute any portion of the software in source code
;; form, you may do so only under this license by including a complete
;; copy of this license with your distribution. If you distribute any
;; portion of the software in compiled or object code form, you may only
;; do so under a license that complies with this license.
;;
;; (E) The software is licensed "as-is." You bear the risk of using
;; it. The contributors give no express warranties, guarantees or
;; conditions. You may have additional consumer rights under your local
;; laws which this license cannot change. To the extent permitted under
;; your local laws, the contributors exclude the implied warranties of
;; merchantability, fitness for a particular purpose and
;; non-infringement.
;;; Commentary:
;;
;; Basic steps to setup:
;; 1. Place `tfs.el' in your `load-path'.
;; 2. In your .emacs file:
;; (require 'tfs)
;; (setq tfs/tf-exe "c:\\vs2008\\common7\\ide\\tf.exe")
;; (setq tfs/login "/login:domain\\userid,password")
;; -or-
;; (setq tfs/login (getenv "TFSLOGIN"))
;; 3. also in your .emacs file:
;; set local or global key bindings for tfs commands. like so:
;;
;; (global-set-key "\C-xvo" 'tfs/checkout)
;; (global-set-key "\C-xvi" 'tfs/checkin)
;; (global-set-key "\C-xvp" 'tfs/properties)
;; (global-set-key "\C-xvr" 'tfs/rename)
;; (global-set-key "\C-xvg" 'tfs/get)
;; (global-set-key "\C-xvh" 'tfs/history)
;; (global-set-key "\C-xvu" 'tfs/undo)
;; (global-set-key "\C-xvd" 'tfs/diff)
;; (global-set-key "\C-xv-" 'tfs/delete)
;; (global-set-key "\C-xv+" 'tfs/add)
;; (global-set-key "\C-xvs" 'tfs/status)
;; (global-set-key "\C-xva" 'tfs/annotate)
;; (global-set-key "\C-xvw" 'tfs/workitem)
;;
;;
(defvar tfs/tf-exe "c:\\Program Files\\Microsoft Visual Studio 9.0\\common7\\ide\\tf.exe"
"location of the tf.exe command. Defaults to \"c:\\Program Files\\Microsoft Visual Studio 9.0\\common7\\ide\\tf.exe\"")
(defvar tfs/tfpt-exe "c:\\Program Files\\Microsoft Team Foundation Server 2008 Power Tools\\TFPT.exe"
"location of the tfpt.exe command. Defaults to \"c:\\Program Files\\Microsoft Team Foundation Server 2008 Power Tools\\TFPT.exe\"")
(defvar tfs/login "/login:domain\\user,password"
"/login option for all TFS activity.")
(defvar tfs/buffer-name "*TFS Messages*"
"name of buffer for TFS Messages")
;; -------------------------------------------------------
;; tfs/checkout
;; performs a TFS checkout on the file being visited by the current buffer.
(defun tfs/checkout ()
"Performs a tf checkout (edit) on the file being visited by the current buffer. Checkout happens only if the file is non-writable now. In other words checkout will fail if the local file is currently writable."
(interactive)
(if buffer-file-name
(if (not (file-writable-p buffer-file-name))
(let* ((exitcode nil)
(shortname (file-name-nondirectory buffer-file-name))
(command (list tfs/tf-exe "checkout" shortname)))
(tfs/append-to-message-buffer (concat "checkout " shortname ": "
(prin1-to-string command) "\n"))
(setq exitcode (apply 'call-process
(car command)
nil
tfs/buffer-name
nil
(append (cdr command) (list tfs/login))))
(if (equal exitcode 0)
(let ((is-flymake-enabled
(and (fboundp 'flymake-mode)
flymake-mode)))
;; disable
(if is-flymake-enabled
(flymake-mode-off))
;; get the checked-out version - read from the disk file
(revert-buffer t t)
(if is-flymake-enabled
(flymake-mode-on)))
(error "Checkout of %s was unsuccessful (%S)" buffer-file-name exitcode))))
(error "tfs/checkout: No file")))
;; -------------------------------------------------------
;; tfs/checkin
;; performs a TFS checkin on the file being visited by the current buffer.
(defun tfs/checkin ()
"perform a tf checkin on the file being visited by the current buffer. Checkin happens only if the file is writable now. This function allows you to specify a checkin comment. It checks in only the current file being visited - pending changes for any other files will not be checked in."
(interactive)
(if buffer-file-name
(if (file-writable-p buffer-file-name)
(let* ((exitcode nil)
(shortname (file-name-nondirectory buffer-file-name))
(comment (read-string (format "Comment for %s: " shortname) nil nil nil))
(command (list tfs/tf-exe "checkin" (format "/comment:%s" comment)
buffer-file-name)))
(tfs/append-to-message-buffer (concat "checkin " shortname ": "
(prin1-to-string command) "\n"))
(setq exitcode (apply 'call-process
(car command)
nil
tfs/buffer-name
nil
(append (cdr command) (list tfs/login))))
(if (equal exitcode 0)
;; revert to the (now) readonly version
(revert-buffer t t)
(error "Checkin of %s was unsuccessful (%S)" buffer-file-name exitcode)))
(error "Cannot checkin %s : the file is not writable" buffer-file-name))
(error "tfs/checkin: No file")))
;; -------------------------------------------------------
;; tfs/rename
;; performs a TFS rename on the file being visited by the current buffer.
(defun tfs/rename ()
"perform a tf rename on the file being visited by the current buffer. If successful, it also renames the buffer to the new name.
"
(interactive)
(if buffer-file-name
(let* (
(exitcode nil)
(shortname (file-name-nondirectory buffer-file-name))
(newname (read-string (format "New name for %s: " shortname) nil nil nil))
(command (list tfs/tf-exe "rename" shortname newname)))
(tfs/append-to-message-buffer (concat "rename " shortname " " newname ": "
(prin1-to-string command) "\n"))
(setq exitcode (apply 'call-process
(car command)
nil
tfs/buffer-name
nil
(append (cdr command) (list tfs/login))))
(if (equal exitcode 0)
(set-visited-file-name newname)
(error "Rename of %s was unsuccessful (%S)" buffer-file-name exitcode)))
(error "tfs/rename: No file")))
;; -------------------------------------------------------
;; tfs/add
;; performs a TFS add on a file
(defun tfs/add ()
"perform a tf add on the file being visited by the current buffer."
(interactive)
(if buffer-file-name
(let* ((shortname (file-name-nondirectory buffer-file-name))
(command (list tfs/tf-exe "add" shortname))
(exitcode nil))
(tfs/append-to-message-buffer (concat "add " shortname ": "
(prin1-to-string command) "\n"))
(setq exitcode (apply 'call-process
(car command)
nil
tfs/buffer-name
nil
(append (cdr command) (list tfs/login))))
(if (equal exitcode 0)
;; TODO: make this conditional on a verbose setting
;; After using this package for a while, the Add is sort of
;; opaque. Hard to know when it's done. It's nice to get
;; a confirmation message. The warm and fuzzy factor.
(message (format "Successful add of %s" buffer-file-name))
(error "Add of %s was unsuccessful (%S)" buffer-file-name exitcode)))
(error "tfs/add: No file")))
;; -------------------------------------------------------
;; tfs/delete
;; performs a TFS delete on a file.
(defun tfs/delete ()
"perform a tf delete on the file being visited by the current buffer. Kills the buffer if the delete is successful."
(interactive)
(if buffer-file-name
(let ((command)
(exitcode nil)
(shortname (file-name-nondirectory buffer-file-name)))
(if (y-or-n-p (concat "Really delete " shortname "? "))
(progn
(setq command (list tfs/tf-exe
"delete"
shortname))
(tfs/append-to-message-buffer (concat "delete " shortname ": "
(prin1-to-string command) "\n"))
(setq exitcode (apply 'call-process
(car command)
nil
tfs/buffer-name
nil
(append (cdr command) (list tfs/login))))
(if (equal exitcode 0)
(kill-buffer)
(error "Delete of %s was unsuccessful (%S)" buffer-file-name exitcode)))))
(error "tfs/delete: No file")))
;; -------------------------------------------------------
;; tfs/get
;; performs a TFS get: retrieve a readonly copy of the specified file.
;;
(defun tfs/get ()
"perform a tf get on the specified file. Happens only when the file is not writable. "
(interactive)
(if buffer-file-name
(let ((command (list tfs/tf-exe "get" buffer-file-name))
(exitcode nil)
(shortname (file-name-nondirectory buffer-file-name)))
(if (not (file-writable-p buffer-file-name))
(progn
;;(tfs/prep-message-buffer)
(tfs/append-to-message-buffer (concat "get " shortname ": "
(prin1-to-string command) "\n"))
(setq exitcode (apply 'call-process
(car command)
nil
tfs/buffer-name
nil
(append (cdr command) (list tfs/login))))
(if (equal exitcode 0)
;; get the latest version
(revert-buffer t t)
(error "Get of %s was unsuccessful (%S)" buffer-file-name exitcode)))
(error "Will not get %s : the file is writable." shortname)))
(error "tfs/get: No file")))
;; -------------------------------------------------------
;; tfs/undo
;; performs a TFS undo: discards pending changes for the specified file. Happens only when writable.
(defun tfs/undo ()
"perform a tf undo on the specified file. Happens only when the file is writable. Confirms before discarding edits."
(interactive)
(if buffer-file-name
(let ((command (list tfs/tf-exe "undo" buffer-file-name))
(exitcode nil)
(shortname (file-name-nondirectory buffer-file-name)))
(if (file-writable-p buffer-file-name)
(if (y-or-n-p (concat "Discard current changes for " shortname "? "))
(progn
(tfs/append-to-message-buffer (concat "undo " shortname ": "
(prin1-to-string command) "\n"))
(setq exitcode (apply 'call-process
(car command)
nil
tfs/buffer-name
nil
(append (cdr command) (list tfs/login))))
(if (equal exitcode 0)
;; get the checked-out (reverted) version
(revert-buffer t t)
(error "undo on %s was unsuccessful (%S)"
buffer-file-name exitcode))))
(error "cannot undo %s : the file is not writable" shortname)))
(error "tfs/undo: No file")))
;; -------------------------------------------------------
;; tfs/history
;; performs a TFS history: retrieve and display the TFS history of specified file
(defun tfs/history ()
"perform a tf history on the specified file."
(interactive)
(if buffer-file-name
(let* ((command (list tfs/tf-exe "history" "/format:detailed"
buffer-file-name))
(exitcode nil)
(history-bufname (concat "*TFS-history* " buffer-file-name))
(shortname (file-name-nondirectory buffer-file-name))
(buffer (get-buffer-create history-bufname)))
(save-excursion (set-buffer buffer) (erase-buffer))
(tfs/append-to-message-buffer (concat "history " shortname ": "
(prin1-to-string command) "\n"))
(setq exitcode (apply 'call-process
(car command)
nil
history-bufname
nil
(append (cdr command) (list tfs/login))))
(if (equal exitcode 0)
(display-buffer history-bufname t)
(error "tf history of %s was unsuccessful (%S)" shortname exitcode)))
(error "tfs/history: No file")))
;; -------------------------------------------------------
;; tfs/properties
;; gets information on the file being visited by the current buffer.
;; displays that information in a new temp buffer.
(defun tfs/properties ()
"Performs a tf properties: gets TFS properties of the current file. "
(interactive)
(tfs/action "properties" nil))
;; -------------------------------------------------------
;; tfs/action
;; gets information on the file being visited by the current buffer.
;; diff, properties, etc
;; displays that information in a new temp buffer.
(defun tfs/action (verb retcode)
"Performs a tf \"action\": gets a tf query for the current file. "
(interactive)
(if buffer-file-name
(let* ((command (list tfs/tf-exe verb buffer-file-name))
(exitcode nil)
(info-bufname (concat "*TFS-" verb "* " buffer-file-name))
(buffer (get-buffer-create info-bufname))
(shortname (file-name-nondirectory buffer-file-name)))
(save-excursion (set-buffer buffer) (erase-buffer))
(tfs/append-to-message-buffer (concat verb shortname ": "
(prin1-to-string command) "\n"))
(setq exitcode (apply 'call-process
(car command)
nil
info-bufname
nil
(append (cdr command) (list tfs/login))))
(if (or (equal exitcode 0) (not (numberp retcode)) (equal exitcode retcode))
(display-buffer info-bufname t)
(error (concat "Get TFS " verb " for %s was unsuccessful (%S)")
buffer-file-name exitcode)))
(error "tfs/%s: No file" verb)))
;; -------------------------------------------------------
;; tfs/annotate
(defun tfs/annotate ()
"Gets line-by-line annotation for the file being visited by the current buffer. Displays that information in the annotation viewer. This requires the TFPT.exe tool. See 'tfs/tfpt-exe'."
(interactive)
(if (file-exists-p tfs/tfpt-exe)
(if buffer-file-name
(let* ((exitcode nil)
(shortname (file-name-nondirectory buffer-file-name))
(command (list tfs/tfpt-exe "annotate" "/noprompt"
shortname))
(annotation-bufname (concat "*TFS annotation* " shortname))
(buffer (get-buffer-create annotation-bufname)))
(save-excursion (set-buffer buffer) (erase-buffer))
(message "computing...")
;;(message (apply 'concat command))
(tfs/append-to-message-buffer (concat "annotate " shortname ": "
(prin1-to-string command) "\n"))
(setq exitcode (apply 'call-process
(car command)
nil
annotation-bufname
nil
(append (cdr command) (list tfs/login))))
(if (equal exitcode 0)
(progn
(display-buffer annotation-bufname t)
(beginning-of-buffer-other-window 0))
(error "Get TFS properties for %s was unsuccessful (%S)"
buffer-file-name exitcode)))
(error "tfs/annotate: No file"))
(error "%s does not exist. (have you set tfs/tfpt-exe?)" tfs/tfpt-exe)))
;; -------------------------------------------------------
;; tfs/thinginfo
(defun tfs/thinginfo (exe thing)
"Gets info on a workitem or changeset. This requires the TFPT.exe tool. See 'tfs/tfpt-exe'."
(if (file-exists-p exe)
(let* ((exitcode nil)
(guess (thing-at-point 'word))
(item-number (read-string (concat thing ": ") guess nil nil))
(command (list exe thing item-number))
(bufname (concat "*TFS " thing "* " item-number))
(buffer (get-buffer-create bufname)))
(save-excursion (set-buffer buffer) (erase-buffer))
;;(message (apply 'concat command))
(tfs/append-to-message-buffer (concat thing " " item-number ": "
(prin1-to-string command) "\n"))
(setq exitcode (apply 'call-process
(car command)
nil
bufname
nil
(append (cdr command) (list tfs/login))))
(if (equal exitcode 0)
(progn
(display-buffer bufname t)
(beginning-of-buffer-other-window 0))
(error (concat "Get TFS " thing "%s was unsuccessful (%S)"
item-number exitcode))))
(error "%s does not exist. (have you set tfs/tfpt-exe or tfs/tf-exe?)" exe)))
;; -------------------------------------------------------
;; tfs/workitem
(defun tfs/workitem ()
"Gets info on a workitem. This requires the TFPT.exe tool. See 'tfs/tfpt-exe'."
(interactive)
(tfs/thinginfo tfs/tfpt-exe "workitem"))
;; -------------------------------------------------------
;; tfs/changeset
(defun tfs/changeset ()
"Gets info on a changeset. This requires the TFPT.exe tool. See 'tfs/tfpt-exe'."
(interactive)
(tfs/thinginfo tfs/tf-exe "changeset"))
;; -------------------------------------------------------
;; tfs/diff
;; diff on the file being visited by the current buffer.
(defun tfs/diff()
"Performs a tf diff on the current file. "
(interactive)
(tfs/action "diff" 100))
;; -------------------------------------------------------
;; tfs/status
;; tf status.
(defun tfs/status ()
"Performs a tf status. Displays the result in a buffer."
(interactive)
(let* ((command (list tfs/tf-exe "status"))
(exitcode nil)
(status-bufname "*TFS-status*")
(buffer (get-buffer-create status-bufname)))
(save-excursion (set-buffer buffer) (erase-buffer))
(tfs/append-to-message-buffer (concat "status" ": "
(prin1-to-string command) "\n"))
(setq exitcode (apply 'call-process
(car command)
nil
status-bufname
nil
(append (cdr command) (list tfs/login))))
(if (equal exitcode 0)
(display-buffer status-bufname t)
(error "Get TFS status was unsuccessful (%S)" exitcode))))
(defun tfs/prep-message-buffer ()
"scrolls the TFS Messages buffer to the end. Intended to be used by the tfs.el module internally, before appending content to the messages buffer."
(let ((buf (current-buffer))
(tfsbuffer (get-buffer-create tfs/buffer-name)))
(set-buffer tfsbuffer)
(goto-char (point-max))
(set-buffer buf)))
(defun tfs/append-to-message-buffer (text)
"Append text to the TFS Messages buffer. Intended for internal use only."
(let ((buf (current-buffer))
(tfsbuffer (get-buffer-create tfs/buffer-name)))
(set-buffer tfsbuffer)
(goto-char (point-max))
(insert text)
(set-buffer buf)))
(provide 'tfs)