;;; Guile-Git --- GNU Guile bindings of libgit2
;;; Copyright © 2025 Enrico Tolotto <etolotto@gmail.com>
;;;
;;; This file is part of Guile-Git.
;;;
;;; Guile-Git is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; Guile-Git is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with Guile-Git.  If not, see <http://www.gnu.org/licenses/>.

(define-module (git apply)
  #:use-module (system foreign)
  #:use-module (git bindings)
  #:use-module (git structs)
  #:use-module (git tree)
  #:use-module (git types)
  #:export (APPLY-LOCATION-WORKDIR
            APPLY-LOCATION-INDEX
            APPLY-LOCATION-BOTH

            make-apply-options
            apply-diff
            apply-diff-to-tree))

(define APPLY-OPTIONS-VERSION 1)

;; Apply the patch to the workdir, leaving the index untouched.  This is the
;; equivalent of `git apply` with no location argument.
(define APPLY-LOCATION-WORKDIR 0)

;; Apply the patch to the index, leaving the working directory untouched.
;; This is the equivalent of `git apply --cached`.
(define APPLY-LOCATION-INDEX 1)

;; Apply the patch to both the working directory and the index.  This is the
;; equivalent of `git apply --index`.
(define APPLY-LOCATION-BOTH 2)

(define make-apply-options
  (let ((proc (libgit2->procedure* "git_apply_options_init"
                                   `(* ,unsigned-int))))
    (lambda ()
      "Return a fresh <apply-options> structure for use with the 'apply-'
procedures."
      (let ((apply-options (make-apply-options-bytestructure)))
        (proc (apply-options->pointer apply-options) APPLY-OPTIONS-VERSION)
        apply-options))))

(define apply-diff
  (let ((proc (libgit2->procedure* "git_apply" `(* * ,int *))))
    (lambda* (repository diff apply-location
                              #:optional (apply-options (make-apply-options)))
      "Apply DIFF to the given REPOSITORY, making changes directly in the
APPLY-LOCATION (one of the 'APPLY-LOCATION-' constants)."
      (proc (repository->pointer repository)
            (diff->pointer diff)
            apply-location
            (apply-options->pointer apply-options)))))

(define apply-diff-to-tree
  (let ((proc (libgit2->procedure* "git_apply_to_tree" `(* * * * *))))
    (lambda* (repository tree diff
                         #:optional (apply-options (make-apply-options)))
      "Apply a DIFF to the TREE, a tree of REPOSITORY, and return the resulting
image as an index."
      (let ((out (make-double-pointer)))
        (proc out
            (repository->pointer repository)
            (tree->pointer tree)
            (diff->pointer diff)
            (apply-options->pointer apply-options))
          (pointer->index (dereference-pointer out))))))
