update-changelog.el 5.4 KB

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