{-# LANGUAGE TypeApplications #-}


-- | Copyright  : Will Thompson, Iñaki García Etxebarria and Jonas Platte
-- License    : LGPL-2.1
-- Maintainer : Iñaki García Etxebarria
-- 
-- Represents a patch object.

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

module GI.Ggit.Structs.Patch
    (

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


 -- * Methods
-- ** Overloaded methods #method:Overloaded methods#

#if defined(ENABLE_OVERLOADING)
    ResolvePatchMethod                      ,
#endif


-- ** getDelta #method:getDelta#

#if defined(ENABLE_OVERLOADING)
    PatchGetDeltaMethodInfo                 ,
#endif
    patchGetDelta                           ,


-- ** getHunk #method:getHunk#

#if defined(ENABLE_OVERLOADING)
    PatchGetHunkMethodInfo                  ,
#endif
    patchGetHunk                            ,


-- ** getLineStats #method:getLineStats#

#if defined(ENABLE_OVERLOADING)
    PatchGetLineStatsMethodInfo             ,
#endif
    patchGetLineStats                       ,


-- ** getNumHunks #method:getNumHunks#

#if defined(ENABLE_OVERLOADING)
    PatchGetNumHunksMethodInfo              ,
#endif
    patchGetNumHunks                        ,


-- ** getNumLinesInHunk #method:getNumLinesInHunk#

#if defined(ENABLE_OVERLOADING)
    PatchGetNumLinesInHunkMethodInfo        ,
#endif
    patchGetNumLinesInHunk                  ,


-- ** newFromBlobs #method:newFromBlobs#

    patchNewFromBlobs                       ,


-- ** newFromDiff #method:newFromDiff#

    patchNewFromDiff                        ,


-- ** ref #method:ref#

#if defined(ENABLE_OVERLOADING)
    PatchRefMethodInfo                      ,
#endif
    patchRef                                ,


-- ** toStream #method:toStream#

#if defined(ENABLE_OVERLOADING)
    PatchToStreamMethodInfo                 ,
#endif
    patchToStream                           ,


-- ** toString #method:toString#

#if defined(ENABLE_OVERLOADING)
    PatchToStringMethodInfo                 ,
#endif
    patchToString                           ,


-- ** unref #method:unref#

#if defined(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.GI.Base.Signals as B.Signals
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)
    deriving (Eq)
foreign import ccall "ggit_patch_get_type" c_ggit_patch_get_type ::
    IO GType

instance BoxedObject Patch where
    boxedType _ = c_ggit_patch_get_type

-- | Convert 'Patch' to and from 'Data.GI.Base.GValue.GValue' with 'Data.GI.Base.GValue.toGValue' and 'Data.GI.Base.GValue.fromGValue'.
instance B.GValue.IsGValue Patch where
    toGValue o = do
        gtype <- c_ggit_patch_get_type
        B.ManagedPtr.withManagedPtr o (B.GValue.buildGValue gtype B.GValue.set_boxed)

    fromGValue gv = do
        ptr <- B.GValue.get_boxed gv :: IO (Ptr Patch)
        B.ManagedPtr.newBoxed Patch ptr



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


#if defined(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 t'GI.Ggit.Objects.Blob.Blob' to diff from.
    -> Maybe (T.Text)
    -- ^ /@oldAsPath@/: treat /@oldBlob@/ as if it had this filename, or 'P.Nothing',
    -> Maybe (b)
    -- ^ /@newBlob@/: a t'GI.Ggit.Objects.Blob.Blob' to diff to.
    -> Maybe (T.Text)
    -- ^ /@newAsPath@/: treat /@newBlob@/ as if it had this filename, or 'P.Nothing',
    -> Maybe (c)
    -- ^ /@diffOptions@/: a t'GI.Ggit.Objects.DiffOptions.DiffOptions', or 'P.Nothing'.
    -> m (Maybe Patch)
    -- ^ __Returns:__ a newly created t'GI.Ggit.Structs.Patch.Patch' or 'P.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 defined(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 TUInt32
--           , 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"})
    Word32 ->                               -- idx : TBasicType TUInt32
    Ptr (Ptr GError) ->                     -- error
    IO (Ptr Patch)

-- | The t'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 t'GI.Ggit.Objects.Diff.Diff'.
    -> Word32
    -- ^ /@idx@/: index into diff list.
    -> m (Maybe Patch)
    -- ^ __Returns:__ a newly created t'GI.Ggit.Structs.Patch.Patch' or 'P.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 defined(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 t'GI.Ggit.Structs.Patch.Patch'.
    -> m (Maybe Ggit.DiffDelta.DiffDelta)
    -- ^ __Returns:__ the t'GI.Ggit.Structs.DiffDelta.DiffDelta' of the patch or 'P.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 defined(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 TUInt32
--           , 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"})
    Word32 ->                               -- idx : TBasicType TUInt32
    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 t'GI.Ggit.Structs.Patch.Patch'
    -> Word32
    -- ^ /@idx@/: the hunk index.
    -> m (Maybe Ggit.DiffHunk.DiffHunk)
    -- ^ __Returns:__ a new t'GI.Ggit.Structs.DiffHunk.DiffHunk' or 'P.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 defined(ENABLE_OVERLOADING)
data PatchGetHunkMethodInfo
instance (signature ~ (Word32 -> 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 TUInt32
--           , 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 TUInt32
--           , 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 TUInt32
--           , 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 Word32 ->                           -- total_context : TBasicType TUInt32
    Ptr Word32 ->                           -- total_additions : TBasicType TUInt32
    Ptr Word32 ->                           -- total_deletions : TBasicType TUInt32
    Ptr (Ptr GError) ->                     -- error
    IO CInt

-- | Get the line statistics of the patch.
patchGetLineStats ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Patch
    -- ^ /@patch@/: a t'GI.Ggit.Structs.Patch.Patch'.
    -> m ((Word32, Word32, Word32))
    -- ^ /(Can throw 'Data.GI.Base.GError.GError')/
patchGetLineStats patch = liftIO $ do
    patch' <- unsafeManagedPtrGetPtr patch
    totalContext <- allocMem :: IO (Ptr Word32)
    totalAdditions <- allocMem :: IO (Ptr Word32)
    totalDeletions <- allocMem :: IO (Ptr Word32)
    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 defined(ENABLE_OVERLOADING)
data PatchGetLineStatsMethodInfo
instance (signature ~ (m ((Word32, Word32, Word32))), 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 TUInt32)
-- 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 Word32

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

#if defined(ENABLE_OVERLOADING)
data PatchGetNumHunksMethodInfo
instance (signature ~ (m Word32), 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 TUInt32
--           , 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"})
    Word32 ->                               -- hunk : TBasicType TUInt32
    IO Int32

-- | Get the number of lines in /@hunk@/.
patchGetNumLinesInHunk ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Patch
    -- ^ /@patch@/: a t'GI.Ggit.Structs.Patch.Patch'.
    -> Word32
    -- ^ /@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 defined(ENABLE_OVERLOADING)
data PatchGetNumLinesInHunkMethodInfo
instance (signature ~ (Word32 -> 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 t'GI.Ggit.Structs.Patch.Patch'.
    -> m (Maybe Patch)
    -- ^ __Returns:__ a t'GI.Ggit.Structs.Patch.Patch' or 'P.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 defined(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 t'GI.Ggit.Structs.Patch.Patch'.
    -> a
    -- ^ /@stream@/: a t'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 defined(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 t'GI.Ggit.Structs.Patch.Patch'.
    -> m (Maybe T.Text)
    -- ^ __Returns:__ the content of a patch as a single diff text or 'P.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 defined(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 t'GI.Ggit.Structs.Patch.Patch'.
    -> m ()
patchUnref patch = liftIO $ do
    patch' <- unsafeManagedPtrGetPtr patch
    ggit_patch_unref patch'
    touchManagedPtr patch
    return ()

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

#endif

#if defined(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 @info
#else
    fromLabel _ = O.overloadedMethod @info
#endif

#endif