git/GitRepository.st
author vranyj1@bd9d3459-6c23-4dd9-91de-98eeebb81177
Wed, 19 Sep 2012 01:08:21 +0000
changeset 8 4275f357d3e8
parent 5 57c20a77e549
child 9 2783c9882d30
permissions -rw-r--r--
- API fixes, more to come

"{ Package: 'stx:libscm/git' }"

GitModel subclass:#GitRepository
	instanceVariableNames:'path workdir'
	classVariableNames:''
	poolDictionaries:'GitObjectType'
	category:'SCM-Git-Model'
!


!GitRepository class methodsFor:'instance creation'!

open: aString
    "Opens an existing git repository on given path"

    | ref |

    ref := ByteArray new: ExternalBytes sizeofPointer.
    GitError raiseIfError: (GitPrimitives prim_git_repository_open: ref path: aString).
    ^self new 
        setHandleFromRef:ref;
        setPath: aString;
        yourself

    "Created: / 07-09-2012 / 23:45:19 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!GitRepository class methodsFor:'accessing'!

structSize
    "Returns size of undelaying structure in bytes"

    ^0
! !

!GitRepository methodsFor:'accessing'!

workdir
    "Get the path of the working directory for this repository or nil, if
     repository is bare"

    self isBare ifTrue: [ ^ nil ].
    workdir isNil ifTrue:[
        workdir := GitPrimitives prim_git_repository_workdir: self.
        workdir := workdir asFilename.
    ].
    ^workdir

    "Created: / 10-09-2012 / 19:07:25 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

workdir: aStringOrFilename update: aBoolean
    "Set the path of the working directory for this repository.
     if update is true, then create/update gitlink in workdir and set 
     config 'core.worktree' (if workdir is not the parent of the 
     .git directory)"

    GitError raiseIfError: 
        (GitPrimitives prim_git_repository_set_workdir: self 
                                               workdir: aStringOrFilename asString
                                        update_gitlink: (aBoolean ifTrue:[1] ifFalse:[0])).
    workdir := aStringOrFilename asFilename.

    "Created: / 10-09-2012 / 19:19:40 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!GitRepository methodsFor:'copying'!

shallowCopy

    ^self class open: path

    "Created: / 10-09-2012 / 19:04:41 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!GitRepository methodsFor:'initialization'!

setPath: aString
    path := aString

    "Created: / 10-09-2012 / 19:05:27 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!GitRepository methodsFor:'initialization & release'!

free
    handle notNil ifTrue:[
        GitPrimitives prim_git_object_free: handle. 
        handle := nil.
    ].

    "Created: / 17-09-2012 / 21:16:44 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!GitRepository methodsFor:'lookup'!

lookup: oid
    "Lookup an object with given OID"
    ^self lookup: oid type: OBJ_ANY

    "Created: / 10-09-2012 / 10:53:13 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

lookup: oid type: typeId
    "Lookup an object with given OID"

    | ref err type obj |

    oid class == GitOid ifFalse:[
        self error: 'Passed oid is not a GitOid'.
        ^nil.
    ].
    ref := ByteArray new: ExternalBytes sizeofPointer.
    err := GitPrimitives prim_git_object_lookup: ref repo: handle id: oid type: typeId.
    GitError raiseIfError: err.

    typeId == OBJ_ANY ifTrue:[
        obj := ExternalAddress new setAddressFromBytes:ref.
        type := GitPrimitives prim_git_object_type: obj.
    ] ifFalse:[
        type := typeId.
    ].
    obj := GitObject newForType: type.
    ^obj
        setHandleFromRef: ref;
        setOid: oid;
        setRepository: self;
        yourself

    "Created: / 10-09-2012 / 11:01:25 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!GitRepository methodsFor:'private-accessing'!

getHandleClass
    ^GitRepositoryHandle

    "Created: / 17-09-2012 / 21:20:59 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!GitRepository methodsFor:'testing'!

isBare
    "Check if a repository is bare"

    ^(GitPrimitives prim_git_repository_is_bare: self) == 1

    "Created: / 10-09-2012 / 19:11:57 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

isEmpty
    "An empty repository has just been initialized and contains
     no commits."

    ^(GitPrimitives prim_git_repository_is_empty: self) == 1

    "Created: / 10-09-2012 / 19:12:24 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!GitRepository class methodsFor:'documentation'!

version_SVN
    ^ '$Id::                                                                                                                        $'
! !