{- |
Copyright  : Will Thompson, Iñaki García Etxebarria and Jonas Platte
License    : LGPL-2.1
Maintainer : Iñaki García Etxebarria (inaki@blueleaf.cc)

Represents a patch object.
-}

#define ENABLE_OVERLOADING (MIN_VERSION_haskell_gi_overloading(1,0,0) \
       && !defined(__HADDOCK_VERSION__))

module GI.Ggit.Structs.Patch
    (

-- * Exported types
    Patch(..)                               ,
    noPatch                                 ,


 -- * Methods
-- ** getDelta #method:getDelta#

#if ENABLE_OVERLOADING
    PatchGetDeltaMethodInfo                 ,
#endif
    patchGetDelta                           ,


-- ** getHunk #method:getHunk#

#if ENABLE_OVERLOADING
    PatchGetHunkMethodInfo                  ,
#endif
    patchGetHunk                            ,


-- ** getLineStats #method:getLineStats#

#if ENABLE_OVERLOADING
    PatchGetLineStatsMethodInfo             ,
#endif
    patchGetLineStats                       ,


-- ** getNumHunks #method:getNumHunks#

#if ENABLE_OVERLOADING
    PatchGetNumHunksMethodInfo              ,
#endif
    patchGetNumHunks                        ,


-- ** getNumLinesInHunk #method:getNumLinesInHunk#

#if ENABLE_OVERLOADING
    PatchGetNumLinesInHunkMethodInfo        ,
#endif
    patchGetNumLinesInHunk                  ,


-- ** newFromBlobs #method:newFromBlobs#

    patchNewFromBlobs                       ,


-- ** newFromDiff #method:newFromDiff#

    patchNewFromDiff                        ,


-- ** ref #method:ref#

#if ENABLE_OVERLOADING
    PatchRefMethodInfo                      ,
#endif
    patchRef                                ,


-- ** toStream #method:toStream#

#if ENABLE_OVERLOADING
    PatchToStreamMethodInfo                 ,
#endif
    patchToStream                           ,


-- ** toString #method:toString#

#if ENABLE_OVERLOADING
    PatchToStringMethodInfo                 ,
#endif
    patchToString                           ,


-- ** unref #method:unref#

#if ENABLE_OVERLOADING
    PatchUnrefMethodInfo                    ,
#endif
    patchUnref                              ,




    ) where

import Data.GI.Base.ShortPrelude
import qualified Data.GI.Base.ShortPrelude as SP
import qualified Data.GI.Base.Overloading as O
import qualified Prelude as P

import qualified Data.GI.Base.Attributes as GI.Attributes
import qualified Data.GI.Base.ManagedPtr as B.ManagedPtr
import qualified Data.GI.Base.GClosure as B.GClosure
import qualified Data.GI.Base.GError as B.GError
import qualified Data.GI.Base.GVariant as B.GVariant
import qualified Data.GI.Base.GValue as B.GValue
import qualified Data.GI.Base.GParamSpec as B.GParamSpec
import qualified Data.GI.Base.CallStack as B.CallStack
import qualified Data.GI.Base.Properties as B.Properties
import qualified Data.Text as T
import qualified Data.ByteString.Char8 as B
import qualified Data.Map as Map
import qualified Foreign.Ptr as FP
import qualified GHC.OverloadedLabels as OL

import {-# SOURCE #-} qualified GI.Ggit.Objects.Blob as Ggit.Blob
import {-# SOURCE #-} qualified GI.Ggit.Objects.Diff as Ggit.Diff
import {-# SOURCE #-} qualified GI.Ggit.Objects.DiffOptions as Ggit.DiffOptions
import {-# SOURCE #-} qualified GI.Ggit.Structs.DiffDelta as Ggit.DiffDelta
import {-# SOURCE #-} qualified GI.Ggit.Structs.DiffHunk as Ggit.DiffHunk
import qualified GI.Gio.Objects.OutputStream as Gio.OutputStream

-- | Memory-managed wrapper type.
newtype Patch = Patch (ManagedPtr Patch)
foreign import ccall "ggit_patch_get_type" c_ggit_patch_get_type ::
    IO GType

instance BoxedObject Patch where
    boxedType _ = c_ggit_patch_get_type

-- | A convenience alias for `Nothing` :: `Maybe` `Patch`.
noPatch :: Maybe Patch
noPatch = Nothing


#if ENABLE_OVERLOADING
instance O.HasAttributeList Patch
type instance O.AttributeList Patch = PatchAttributeList
type PatchAttributeList = ('[ ] :: [(Symbol, *)])
#endif

-- method Patch::new_from_blobs
-- method type : Constructor
-- Args : [Arg {argCName = "old_blob", argType = TInterface (Name {namespace = "Ggit", name = "Blob"}), direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "a #GgitBlob to diff from.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "old_as_path", argType = TBasicType TUTF8, direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "treat @old_blob as if it had this filename, or %NULL,", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "new_blob", argType = TInterface (Name {namespace = "Ggit", name = "Blob"}), direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "a #GgitBlob to diff to.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "new_as_path", argType = TBasicType TUTF8, direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "treat @new_blob as if it had this filename, or %NULL,", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "diff_options", argType = TInterface (Name {namespace = "Ggit", name = "DiffOptions"}), direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "a #GgitDiffOptions, or %NULL.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "Ggit", name = "Patch"}))
-- throws : True
-- Skip return : False

foreign import ccall "ggit_patch_new_from_blobs" ggit_patch_new_from_blobs ::
    Ptr Ggit.Blob.Blob ->                   -- old_blob : TInterface (Name {namespace = "Ggit", name = "Blob"})
    CString ->                              -- old_as_path : TBasicType TUTF8
    Ptr Ggit.Blob.Blob ->                   -- new_blob : TInterface (Name {namespace = "Ggit", name = "Blob"})
    CString ->                              -- new_as_path : TBasicType TUTF8
    Ptr Ggit.DiffOptions.DiffOptions ->     -- diff_options : TInterface (Name {namespace = "Ggit", name = "DiffOptions"})
    Ptr (Ptr GError) ->                     -- error
    IO (Ptr Patch)

{- |
Directly generate a patch from the difference between two blobs.

This is just like 'GI.Ggit.Objects.Diff.diffBlobs' except it generates a patch object
for the difference instead of directly making callbacks.  You can use the
standard ggit_patch accessor functions to read the patch data, and
you must call ggit_patch_unref on the patch when done.
-}
patchNewFromBlobs ::
    (B.CallStack.HasCallStack, MonadIO m, Ggit.Blob.IsBlob a, Ggit.Blob.IsBlob b, Ggit.DiffOptions.IsDiffOptions c) =>
    Maybe (a)
    {- ^ /@oldBlob@/: a 'GI.Ggit.Objects.Blob.Blob' to diff from. -}
    -> Maybe (T.Text)
    {- ^ /@oldAsPath@/: treat /@oldBlob@/ as if it had this filename, or 'Nothing', -}
    -> Maybe (b)
    {- ^ /@newBlob@/: a 'GI.Ggit.Objects.Blob.Blob' to diff to. -}
    -> Maybe (T.Text)
    {- ^ /@newAsPath@/: treat /@newBlob@/ as if it had this filename, or 'Nothing', -}
    -> Maybe (c)
    {- ^ /@diffOptions@/: a 'GI.Ggit.Objects.DiffOptions.DiffOptions', or 'Nothing'. -}
    -> m (Maybe Patch)
    {- ^ __Returns:__ a newly created 'GI.Ggit.Structs.Patch.Patch' or 'Nothing'. /(Can throw 'Data.GI.Base.GError.GError')/ -}
patchNewFromBlobs oldBlob oldAsPath newBlob newAsPath diffOptions = liftIO $ do
    maybeOldBlob <- case oldBlob of
        Nothing -> return nullPtr
        Just jOldBlob -> do
            jOldBlob' <- unsafeManagedPtrCastPtr jOldBlob
            return jOldBlob'
    maybeOldAsPath <- case oldAsPath of
        Nothing -> return nullPtr
        Just jOldAsPath -> do
            jOldAsPath' <- textToCString jOldAsPath
            return jOldAsPath'
    maybeNewBlob <- case newBlob of
        Nothing -> return nullPtr
        Just jNewBlob -> do
            jNewBlob' <- unsafeManagedPtrCastPtr jNewBlob
            return jNewBlob'
    maybeNewAsPath <- case newAsPath of
        Nothing -> return nullPtr
        Just jNewAsPath -> do
            jNewAsPath' <- textToCString jNewAsPath
            return jNewAsPath'
    maybeDiffOptions <- case diffOptions of
        Nothing -> return nullPtr
        Just jDiffOptions -> do
            jDiffOptions' <- unsafeManagedPtrCastPtr jDiffOptions
            return jDiffOptions'
    onException (do
        result <- propagateGError $ ggit_patch_new_from_blobs maybeOldBlob maybeOldAsPath maybeNewBlob maybeNewAsPath maybeDiffOptions
        maybeResult <- convertIfNonNull result $ \result' -> do
            result'' <- (wrapBoxed Patch) result'
            return result''
        whenJust oldBlob touchManagedPtr
        whenJust newBlob touchManagedPtr
        whenJust diffOptions touchManagedPtr
        freeMem maybeOldAsPath
        freeMem maybeNewAsPath
        return maybeResult
     ) (do
        freeMem maybeOldAsPath
        freeMem maybeNewAsPath
     )

#if ENABLE_OVERLOADING
#endif

-- method Patch::new_from_diff
-- method type : Constructor
-- Args : [Arg {argCName = "diff", argType = TInterface (Name {namespace = "Ggit", name = "Diff"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GgitDiff.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "idx", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "index into diff list.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "Ggit", name = "Patch"}))
-- throws : True
-- Skip return : False

foreign import ccall "ggit_patch_new_from_diff" ggit_patch_new_from_diff ::
    Ptr Ggit.Diff.Diff ->                   -- diff : TInterface (Name {namespace = "Ggit", name = "Diff"})
    Word64 ->                               -- idx : TBasicType TUInt64
    Ptr (Ptr GError) ->                     -- error
    IO (Ptr Patch)

{- |
The 'GI.Ggit.Structs.Patch.Patch' is a newly created object contains the text diffs
for the delta.  You have to call 'GI.Ggit.Structs.Patch.patchUnref' when you are
done with it.  You can use the patch object to loop over all the hunks
and lines in the diff of the one delta.
-}
patchNewFromDiff ::
    (B.CallStack.HasCallStack, MonadIO m, Ggit.Diff.IsDiff a) =>
    a
    {- ^ /@diff@/: a 'GI.Ggit.Objects.Diff.Diff'. -}
    -> Word64
    {- ^ /@idx@/: index into diff list. -}
    -> m (Maybe Patch)
    {- ^ __Returns:__ a newly created 'GI.Ggit.Structs.Patch.Patch' or 'Nothing'. /(Can throw 'Data.GI.Base.GError.GError')/ -}
patchNewFromDiff diff idx = liftIO $ do
    diff' <- unsafeManagedPtrCastPtr diff
    onException (do
        result <- propagateGError $ ggit_patch_new_from_diff diff' idx
        maybeResult <- convertIfNonNull result $ \result' -> do
            result'' <- (wrapBoxed Patch) result'
            return result''
        touchManagedPtr diff
        return maybeResult
     ) (do
        return ()
     )

#if ENABLE_OVERLOADING
#endif

-- method Patch::get_delta
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "patch", argType = TInterface (Name {namespace = "Ggit", name = "Patch"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GgitPatch.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "Ggit", name = "DiffDelta"}))
-- throws : False
-- Skip return : False

foreign import ccall "ggit_patch_get_delta" ggit_patch_get_delta ::
    Ptr Patch ->                            -- patch : TInterface (Name {namespace = "Ggit", name = "Patch"})
    IO (Ptr Ggit.DiffDelta.DiffDelta)

{- |
Get the diff delta corresponding to the patch.
-}
patchGetDelta ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Patch
    {- ^ /@patch@/: a 'GI.Ggit.Structs.Patch.Patch'. -}
    -> m (Maybe Ggit.DiffDelta.DiffDelta)
    {- ^ __Returns:__ the 'GI.Ggit.Structs.DiffDelta.DiffDelta' of the patch or 'Nothing'. -}
patchGetDelta patch = liftIO $ do
    patch' <- unsafeManagedPtrGetPtr patch
    result <- ggit_patch_get_delta patch'
    maybeResult <- convertIfNonNull result $ \result' -> do
        result'' <- (wrapBoxed Ggit.DiffDelta.DiffDelta) result'
        return result''
    touchManagedPtr patch
    return maybeResult

#if ENABLE_OVERLOADING
data PatchGetDeltaMethodInfo
instance (signature ~ (m (Maybe Ggit.DiffDelta.DiffDelta)), MonadIO m) => O.MethodInfo PatchGetDeltaMethodInfo Patch signature where
    overloadedMethod _ = patchGetDelta

#endif

-- method Patch::get_hunk
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "patch", argType = TInterface (Name {namespace = "Ggit", name = "Patch"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GgitPatch", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "idx", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the hunk index.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "Ggit", name = "DiffHunk"}))
-- throws : True
-- Skip return : False

foreign import ccall "ggit_patch_get_hunk" ggit_patch_get_hunk ::
    Ptr Patch ->                            -- patch : TInterface (Name {namespace = "Ggit", name = "Patch"})
    Word64 ->                               -- idx : TBasicType TUInt64
    Ptr (Ptr GError) ->                     -- error
    IO (Ptr Ggit.DiffHunk.DiffHunk)

{- |
Get the /@idx@/\'th hunk in the patch.
-}
patchGetHunk ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Patch
    {- ^ /@patch@/: a 'GI.Ggit.Structs.Patch.Patch' -}
    -> Word64
    {- ^ /@idx@/: the hunk index. -}
    -> m (Maybe Ggit.DiffHunk.DiffHunk)
    {- ^ __Returns:__ a new 'GI.Ggit.Structs.DiffHunk.DiffHunk' or 'Nothing' on error. /(Can throw 'Data.GI.Base.GError.GError')/ -}
patchGetHunk patch idx = liftIO $ do
    patch' <- unsafeManagedPtrGetPtr patch
    onException (do
        result <- propagateGError $ ggit_patch_get_hunk patch' idx
        maybeResult <- convertIfNonNull result $ \result' -> do
            result'' <- (wrapBoxed Ggit.DiffHunk.DiffHunk) result'
            return result''
        touchManagedPtr patch
        return maybeResult
     ) (do
        return ()
     )

#if ENABLE_OVERLOADING
data PatchGetHunkMethodInfo
instance (signature ~ (Word64 -> m (Maybe Ggit.DiffHunk.DiffHunk)), MonadIO m) => O.MethodInfo PatchGetHunkMethodInfo Patch signature where
    overloadedMethod _ = patchGetHunk

#endif

-- method Patch::get_line_stats
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "patch", argType = TInterface (Name {namespace = "Ggit", name = "Patch"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GgitPatch.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "total_context", argType = TBasicType TUInt64, direction = DirectionOut, mayBeNull = False, argDoc = Documentation {rawDocText = Just "return value for the number of context lines.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferEverything},Arg {argCName = "total_additions", argType = TBasicType TUInt64, direction = DirectionOut, mayBeNull = False, argDoc = Documentation {rawDocText = Just "return value for the number of added lines.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferEverything},Arg {argCName = "total_deletions", argType = TBasicType TUInt64, direction = DirectionOut, mayBeNull = False, argDoc = Documentation {rawDocText = Just "return value for the number of deleted lines.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferEverything}]
-- Lengths : []
-- returnType : Just (TBasicType TBoolean)
-- throws : True
-- Skip return : False

foreign import ccall "ggit_patch_get_line_stats" ggit_patch_get_line_stats ::
    Ptr Patch ->                            -- patch : TInterface (Name {namespace = "Ggit", name = "Patch"})
    Ptr Word64 ->                           -- total_context : TBasicType TUInt64
    Ptr Word64 ->                           -- total_additions : TBasicType TUInt64
    Ptr Word64 ->                           -- total_deletions : TBasicType TUInt64
    Ptr (Ptr GError) ->                     -- error
    IO CInt

{- |
Get the line statistics of the patch.
-}
patchGetLineStats ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Patch
    {- ^ /@patch@/: a 'GI.Ggit.Structs.Patch.Patch'. -}
    -> m ((Word64, Word64, Word64))
    {- ^ /(Can throw 'Data.GI.Base.GError.GError')/ -}
patchGetLineStats patch = liftIO $ do
    patch' <- unsafeManagedPtrGetPtr patch
    totalContext <- allocMem :: IO (Ptr Word64)
    totalAdditions <- allocMem :: IO (Ptr Word64)
    totalDeletions <- allocMem :: IO (Ptr Word64)
    onException (do
        _ <- propagateGError $ ggit_patch_get_line_stats patch' totalContext totalAdditions totalDeletions
        totalContext' <- peek totalContext
        totalAdditions' <- peek totalAdditions
        totalDeletions' <- peek totalDeletions
        touchManagedPtr patch
        freeMem totalContext
        freeMem totalAdditions
        freeMem totalDeletions
        return (totalContext', totalAdditions', totalDeletions')
     ) (do
        freeMem totalContext
        freeMem totalAdditions
        freeMem totalDeletions
     )

#if ENABLE_OVERLOADING
data PatchGetLineStatsMethodInfo
instance (signature ~ (m ((Word64, Word64, Word64))), MonadIO m) => O.MethodInfo PatchGetLineStatsMethodInfo Patch signature where
    overloadedMethod _ = patchGetLineStats

#endif

-- method Patch::get_num_hunks
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "patch", argType = TInterface (Name {namespace = "Ggit", name = "Patch"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GgitPatch.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TBasicType TUInt64)
-- throws : False
-- Skip return : False

foreign import ccall "ggit_patch_get_num_hunks" ggit_patch_get_num_hunks ::
    Ptr Patch ->                            -- patch : TInterface (Name {namespace = "Ggit", name = "Patch"})
    IO Word64

{- |
Get the number of hunks in the patch.
-}
patchGetNumHunks ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Patch
    {- ^ /@patch@/: a 'GI.Ggit.Structs.Patch.Patch'. -}
    -> m Word64
    {- ^ __Returns:__ the number of hunks. -}
patchGetNumHunks patch = liftIO $ do
    patch' <- unsafeManagedPtrGetPtr patch
    result <- ggit_patch_get_num_hunks patch'
    touchManagedPtr patch
    return result

#if ENABLE_OVERLOADING
data PatchGetNumHunksMethodInfo
instance (signature ~ (m Word64), MonadIO m) => O.MethodInfo PatchGetNumHunksMethodInfo Patch signature where
    overloadedMethod _ = patchGetNumHunks

#endif

-- method Patch::get_num_lines_in_hunk
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "patch", argType = TInterface (Name {namespace = "Ggit", name = "Patch"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GgitPatch.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "hunk", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the hunk index.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TBasicType TInt)
-- throws : False
-- Skip return : False

foreign import ccall "ggit_patch_get_num_lines_in_hunk" ggit_patch_get_num_lines_in_hunk ::
    Ptr Patch ->                            -- patch : TInterface (Name {namespace = "Ggit", name = "Patch"})
    Word64 ->                               -- hunk : TBasicType TUInt64
    IO Int32

{- |
Get the number of lines in /@hunk@/.
-}
patchGetNumLinesInHunk ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Patch
    {- ^ /@patch@/: a 'GI.Ggit.Structs.Patch.Patch'. -}
    -> Word64
    {- ^ /@hunk@/: the hunk index. -}
    -> m Int32
    {- ^ __Returns:__ the number of lines. -}
patchGetNumLinesInHunk patch hunk = liftIO $ do
    patch' <- unsafeManagedPtrGetPtr patch
    result <- ggit_patch_get_num_lines_in_hunk patch' hunk
    touchManagedPtr patch
    return result

#if ENABLE_OVERLOADING
data PatchGetNumLinesInHunkMethodInfo
instance (signature ~ (Word64 -> m Int32), MonadIO m) => O.MethodInfo PatchGetNumLinesInHunkMethodInfo Patch signature where
    overloadedMethod _ = patchGetNumLinesInHunk

#endif

-- method Patch::ref
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "patch", argType = TInterface (Name {namespace = "Ggit", name = "Patch"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GgitPatch.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "Ggit", name = "Patch"}))
-- throws : False
-- Skip return : False

foreign import ccall "ggit_patch_ref" ggit_patch_ref ::
    Ptr Patch ->                            -- patch : TInterface (Name {namespace = "Ggit", name = "Patch"})
    IO (Ptr Patch)

{- |
Atomically increments the reference count of /@patch@/ by one.
This function is MT-safe and may be called from any thread.
-}
patchRef ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Patch
    {- ^ /@patch@/: a 'GI.Ggit.Structs.Patch.Patch'. -}
    -> m (Maybe Patch)
    {- ^ __Returns:__ a 'GI.Ggit.Structs.Patch.Patch' or 'Nothing'. -}
patchRef patch = liftIO $ do
    patch' <- unsafeManagedPtrGetPtr patch
    result <- ggit_patch_ref patch'
    maybeResult <- convertIfNonNull result $ \result' -> do
        result'' <- (newBoxed Patch) result'
        return result''
    touchManagedPtr patch
    return maybeResult

#if ENABLE_OVERLOADING
data PatchRefMethodInfo
instance (signature ~ (m (Maybe Patch)), MonadIO m) => O.MethodInfo PatchRefMethodInfo Patch signature where
    overloadedMethod _ = patchRef

#endif

-- method Patch::to_stream
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "patch", argType = TInterface (Name {namespace = "Ggit", name = "Patch"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GgitPatch.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "stream", argType = TInterface (Name {namespace = "Gio", name = "OutputStream"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GOutputStream.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TBasicType TBoolean)
-- throws : True
-- Skip return : False

foreign import ccall "ggit_patch_to_stream" ggit_patch_to_stream ::
    Ptr Patch ->                            -- patch : TInterface (Name {namespace = "Ggit", name = "Patch"})
    Ptr Gio.OutputStream.OutputStream ->    -- stream : TInterface (Name {namespace = "Gio", name = "OutputStream"})
    Ptr (Ptr GError) ->                     -- error
    IO CInt

{- |
Write the contents of a patch to the provided stream.
-}
patchToStream ::
    (B.CallStack.HasCallStack, MonadIO m, Gio.OutputStream.IsOutputStream a) =>
    Patch
    {- ^ /@patch@/: a 'GI.Ggit.Structs.Patch.Patch'. -}
    -> a
    {- ^ /@stream@/: a 'GI.Gio.Objects.OutputStream.OutputStream'. -}
    -> m ()
    {- ^ /(Can throw 'Data.GI.Base.GError.GError')/ -}
patchToStream patch stream = liftIO $ do
    patch' <- unsafeManagedPtrGetPtr patch
    stream' <- unsafeManagedPtrCastPtr stream
    onException (do
        _ <- propagateGError $ ggit_patch_to_stream patch' stream'
        touchManagedPtr patch
        touchManagedPtr stream
        return ()
     ) (do
        return ()
     )

#if ENABLE_OVERLOADING
data PatchToStreamMethodInfo
instance (signature ~ (a -> m ()), MonadIO m, Gio.OutputStream.IsOutputStream a) => O.MethodInfo PatchToStreamMethodInfo Patch signature where
    overloadedMethod _ = patchToStream

#endif

-- method Patch::to_string
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "patch", argType = TInterface (Name {namespace = "Ggit", name = "Patch"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GgitPatch.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TBasicType TUTF8)
-- throws : True
-- Skip return : False

foreign import ccall "ggit_patch_to_string" ggit_patch_to_string ::
    Ptr Patch ->                            -- patch : TInterface (Name {namespace = "Ggit", name = "Patch"})
    Ptr (Ptr GError) ->                     -- error
    IO CString

{- |
Gets the content of a patch as a single diff text.
-}
patchToString ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Patch
    {- ^ /@patch@/: a 'GI.Ggit.Structs.Patch.Patch'. -}
    -> m (Maybe T.Text)
    {- ^ __Returns:__ the content of a patch as a single diff text or 'Nothing'. /(Can throw 'Data.GI.Base.GError.GError')/ -}
patchToString patch = liftIO $ do
    patch' <- unsafeManagedPtrGetPtr patch
    onException (do
        result <- propagateGError $ ggit_patch_to_string patch'
        maybeResult <- convertIfNonNull result $ \result' -> do
            result'' <- cstringToText result'
            freeMem result'
            return result''
        touchManagedPtr patch
        return maybeResult
     ) (do
        return ()
     )

#if ENABLE_OVERLOADING
data PatchToStringMethodInfo
instance (signature ~ (m (Maybe T.Text)), MonadIO m) => O.MethodInfo PatchToStringMethodInfo Patch signature where
    overloadedMethod _ = patchToString

#endif

-- method Patch::unref
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "patch", argType = TInterface (Name {namespace = "Ggit", name = "Patch"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GgitPatch.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Nothing
-- throws : False
-- Skip return : False

foreign import ccall "ggit_patch_unref" ggit_patch_unref ::
    Ptr Patch ->                            -- patch : TInterface (Name {namespace = "Ggit", name = "Patch"})
    IO ()

{- |
Atomically decrements the reference count of /@patch@/ by one.
If the reference count drops to 0, /@patch@/ is freed.
-}
patchUnref ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Patch
    {- ^ /@patch@/: a 'GI.Ggit.Structs.Patch.Patch'. -}
    -> m ()
patchUnref patch = liftIO $ do
    patch' <- unsafeManagedPtrGetPtr patch
    ggit_patch_unref patch'
    touchManagedPtr patch
    return ()

#if ENABLE_OVERLOADING
data PatchUnrefMethodInfo
instance (signature ~ (m ()), MonadIO m) => O.MethodInfo PatchUnrefMethodInfo Patch signature where
    overloadedMethod _ = patchUnref

#endif

#if ENABLE_OVERLOADING
type family ResolvePatchMethod (t :: Symbol) (o :: *) :: * where
    ResolvePatchMethod "ref" o = PatchRefMethodInfo
    ResolvePatchMethod "toStream" o = PatchToStreamMethodInfo
    ResolvePatchMethod "toString" o = PatchToStringMethodInfo
    ResolvePatchMethod "unref" o = PatchUnrefMethodInfo
    ResolvePatchMethod "getDelta" o = PatchGetDeltaMethodInfo
    ResolvePatchMethod "getHunk" o = PatchGetHunkMethodInfo
    ResolvePatchMethod "getLineStats" o = PatchGetLineStatsMethodInfo
    ResolvePatchMethod "getNumHunks" o = PatchGetNumHunksMethodInfo
    ResolvePatchMethod "getNumLinesInHunk" o = PatchGetNumLinesInHunkMethodInfo
    ResolvePatchMethod l o = O.MethodResolutionFailed l o

instance (info ~ ResolvePatchMethod t Patch, O.MethodInfo info Patch p) => OL.IsLabel t (Patch -> p) where
#if MIN_VERSION_base(4,10,0)
    fromLabel = O.overloadedMethod (O.MethodProxy :: O.MethodProxy info)
#else
    fromLabel _ = O.overloadedMethod (O.MethodProxy :: O.MethodProxy info)
#endif

#endif