update-changelog.el 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. ;;; update-changelog.el --- stitch rcs2log output to ChangeLog
  2. ;;; Copyright (C) 2001, 2006 Free Software Foundation, Inc.
  3. ;;;; This library is free software; you can redistribute it and/or
  4. ;;;; modify it under the terms of the GNU Lesser General Public
  5. ;;;; License as published by the Free Software Foundation; either
  6. ;;;; version 3 of the License, or (at your option) any later version.
  7. ;;;;
  8. ;;;; This library is distributed in the hope that it will be useful,
  9. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;;;; Lesser General Public License for more details.
  12. ;;;;
  13. ;;;; You should have received a copy of the GNU Lesser General Public
  14. ;;;; License along with this library; if not, write to the Free
  15. ;;;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. ;;;; 02111-1307 USA
  17. ;;; Commentary:
  18. ;; Usage: emacs -batch -l update-changelog.el
  19. ;;
  20. ;; This program is basically a wrapper around rcs2log, and inherits rcs2log's
  21. ;; weaknesses, namely, the requirement that there be a checked out (working
  22. ;; directory) copy. It would be nice if rcs2log grokked with the repository
  23. ;; directly, but until then, we work around it by requiring the environment
  24. ;; var `LOCAL_WORK_ROOT' to be defined. This should be a directory under
  25. ;; which cvs modules are checked out.
  26. ;;
  27. ;; Flash! Newer versions of rcs2log do indeed understand the repository,
  28. ;; and can be invoked with "-R" therein. We infer this if `LOCAL_WORK_ROOT'
  29. ;; is not set, and use instead `CVSROOT'. At least one of these must be set.
  30. ;;
  31. ;; You can pass additional options to rcs2log using env var `RCS2LOG_OPTS'.
  32. ;;
  33. ;; Usage from a Lisp program:
  34. ;; (ucl-update filename) -- Update FILENAME, a Change Log file
  35. ;;; Code:
  36. ;;;---------------------------------------------------------------------------
  37. ;;; Variables
  38. (defvar ucl-o (or (getenv "RCS2LOG_OPTS") "")
  39. "Additional options to pass to rcs2log.")
  40. ;;;---------------------------------------------------------------------------
  41. ;;; Cleanup functions
  42. (defun ucl-stitch-new-old (new-old &rest ignore)
  43. "In a changelog buffer, remove redundancy around NEW-OLD point.
  44. The new text is before NEW-OLD point, and the old after."
  45. (goto-char new-old)
  46. (or (= new-old (point-max)) ; no old
  47. (let ((last-new
  48. (save-excursion
  49. (buffer-substring (re-search-backward "^[0-9]+") new-old))))
  50. (let ((has-diff (string-match "\n\tdiff.*-r" last-new))) ; ugh
  51. (and has-diff (setq last-new (substring last-new 0 has-diff))))
  52. (let ((overlap (search-forward last-new (point-max) t)))
  53. (and overlap (delete-region new-old overlap))))))
  54. ;; Sometimes wannabe developers append diffs to their log entries.
  55. (defun ucl-omit-diffs (&rest ignore)
  56. "In a changelog buffer, delete diffs (assumed at end of entry)."
  57. (goto-char (point-min))
  58. (while (re-search-forward "^\tdiff .*-r" (point-max) t)
  59. (beginning-of-line)
  60. (delete-region (point)
  61. (save-excursion
  62. (if (re-search-forward "^[0-9]+" (point-max))
  63. (- (point) 4)
  64. (point-max))))))
  65. (defun ucl-space-out-entries (&rest ignore)
  66. "In a changelog buffer, ensure proper spacing between entries."
  67. (goto-char (point-max))
  68. (while (re-search-backward "^[0-9]+" (point-min) t)
  69. (unless (= (point) (point-min))
  70. (open-line 3) ; yuk
  71. (delete-blank-lines))))
  72. (defun ucl-kill-eol-white-space (&rest ignore)
  73. "In a changelog buffer, delete end-of-line white space."
  74. (goto-char (point-min))
  75. (while (re-search-forward "[ \t]+$" (point-max) t)
  76. (delete-region
  77. (match-beginning 0) (match-end 0))))
  78. (defvar ucl-cleanup-hook '(ucl-stitch-new-old
  79. ucl-omit-diffs
  80. ucl-space-out-entries
  81. ucl-kill-eol-white-space)
  82. "Hook run after combining the new fragment with the old changelog. These
  83. are called with the argument NEW-OLD, which is the buffer position at the
  84. boundary of the two pieces of text. This is suboptimal; we should use a
  85. marker so that munges on the text do not lose this position. The result is
  86. that currently, `ucl-stitch-new-old' must be called first because it depends
  87. on NEW-OLD, while the other cleanup funcs ignore it. (Sigh.)")
  88. ;;;---------------------------------------------------------------------------
  89. ;;; Update functions
  90. (defun ucl-root ()
  91. (let ((lwr (getenv "LOCAL_WORK_ROOT"))
  92. (cr (getenv "CVSROOT")))
  93. (concat (or lwr
  94. (and cr (progn
  95. (setq ucl-o (concat "-R " ucl-o)) ; hmm
  96. cr))
  97. (error "Must set env var LOCAL_WORK_ROOT or CVSROOT"))
  98. "/")))
  99. (defun ucl-update (filename)
  100. (interactive "fChangeLog: ")
  101. (let* ((ofile (expand-file-name filename))
  102. (cmd (concat "rcs2log " ucl-o " -c " ofile))
  103. (obuf "*ucl-work*"))
  104. (when (and (file-exists-p ofile)
  105. (progn
  106. (shell-command cmd obuf)
  107. (get-buffer obuf)))
  108. (save-excursion ; prevent default-directory hosing
  109. (set-buffer obuf)
  110. (unless (= 0 (buffer-size))
  111. (let ((new-old-boundary (point-max)))
  112. (goto-char new-old-boundary)
  113. (insert-file ofile)
  114. (run-hook-with-args 'ucl-cleanup-hook new-old-boundary))
  115. (or (= (buffer-size) (nth 7 (file-attributes ofile)))
  116. (let (make-backup-files) ; less clutter
  117. (write-file ofile))))
  118. (kill-buffer (current-buffer))))))
  119. ;;;---------------------------------------------------------------------------
  120. ;;; Load-time actions
  121. (when noninteractive ; only when `-batch'
  122. (or (ucl-update "ChangeLog")
  123. (message "Sorry, could not update ChangeLog in %s" default-directory)))
  124. (provide 'update-changelog)
  125. ;;; update-changelog.el ends here