sacha/fix-tla-log {{04.04.21,EmacsHacks}} 11:17

(defun sacha/fix-tla-log ()
  "Correct a wrong commit.
Run this inside the arch subdirectory for the patch in your
repository, not your project tree."
  ;; Copy the log file
   (let*
       ((directory (car (file-expand-wildcards "*.patches")))
        modes
        (log-file
         (car
          (file-expand-wildcards
           (concat (file-name-as-directory directory)
                   "new-files-archive/{arch}/*/*/*/[email protected]/patch-log/patch-*"))))
        (tar-file
         (car (file-expand-wildcards "*.tar.gz")))
        log-md5 tar-md5)
     (when directory
       (if (file-newer-than-file-p "log" log-file)
           (progn
             (delete-file log-file)
             (copy-file "log" log-file))
         (delete-file "log")
         (copy-file log-file "log"))
       ;; Recreate the tar.gz
       (delete-file tar-file)
       (call-process "tar" nil nil nil "zcvf" tar-file directory)
       ;; Calculate checksums
       (with-temp-buffer
         (call-process "md5sum" nil t nil "log" tar-file)
         (goto-char (point-min))
         (re-search-forward "^\\([^ ]+\\)\\s-+log" nil t)
         (setq log-md5 (match-string 1))
         (re-search-forward "^\\([^ ]+\\)" nil t)
         (setq tar-md5 (match-string 1)))
       (with-temp-buffer
         (setq modes (file-modes "checksum"))
         (insert-file-contents "checksum")
         (goto-char (point-min))
         (re-search-forward "^Signature-for")
         (delete-region (point-min) (match-beginning 0))
         (re-search-forward "^md5\\s-+log\\s-+\\([^ ]+\\)$")
         (replace-match log-md5 t t nil 1)
         (re-search-forward "^md5\\s-+.+?\\.tar\\.gz\\s-+\\([^ ]+\\)$")
         (replace-match tar-md5 t t nil 1)
         (when (re-search-forward "BEGIN PGP SIGNATURE" nil t)
           (delete-region (line-beginning-position) (point-max)))
         (let ((pgg-output-buffer (current-buffer)))
           (pgg-sign t))
         (delete-file "checksum")
         ;; Sign the checksum
         (write-file "checksum")
         (set-file-modes "checksum" modes)))))