mercurial/HGConfig.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Fri, 01 Feb 2013 12:02:22 +0000
changeset 210 54a73fa50d40
parent 150 1813913f6106
child 335 7e19ab19148b
permissions -rw-r--r--
Added copyright notice.

"
 COPYRIGHT (c) 2012-2013 by Jan Vrany
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
"{ Package: 'stx:libscm/mercurial' }"

HGRepositoryObject subclass:#HGConfig
	instanceVariableNames:'root timestamp'
	classVariableNames:''
	poolDictionaries:''
	category:'SCM-Mercurial-Core'
!

Object subclass:#Entry
	instanceVariableNames:'name value'
	classVariableNames:''
	poolDictionaries:''
	privateIn:HGConfig
!

Dictionary subclass:#Section
	instanceVariableNames:'name entries'
	classVariableNames:''
	poolDictionaries:''
	privateIn:HGConfig
!

!HGConfig class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2012-2013 by Jan Vrany
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
! !

!HGConfig class methodsFor:'accessing-files'!

userConfigFiles
    "Return a list of per-user config files"

    OperatingSystem isMSWINDOWSlike ifTrue:[
        ^{
            Filename homeDirectory / '.hgrc' .
            Filename homeDirectory / 'Mercurial.ini'
        }
    ].

    OperatingSystem isUNIXlike ifTrue:[
        ^{
            Filename homeDirectory / '.hgrc' .
        }
    ].

    self error:'Unsupported operating system'

    "Created: / 06-12-2012 / 20:30:11 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified (format): / 06-12-2012 / 21:34:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!HGConfig methodsFor:'accessing'!

at: key
    ^self root at:key

    "Created: / 06-12-2012 / 20:22:41 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

at: key ifAbsent: block
    ^self root at:key ifAbsent: block

    "Created: / 06-12-2012 / 20:23:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

at: key ifAbsentPut: block
    ^self root at:key ifAbsentPut: block

    "Created: / 06-12-2012 / 20:23:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

at: key put: val
    ^self root at:key put: val

    "Created: / 06-12-2012 / 20:23:38 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

get: key
    ^self root get:key

    "Created: / 06-12-2012 / 20:22:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

get: key default: default
    ^self root get:key default: default

    "Created: / 07-12-2012 / 15:46:07 / jv"
! !

!HGConfig methodsFor:'private'!

mustReload
    root isNil ifTrue:[ ^ true ].

    (self mustReloadBecauseOf: repository path / '.hg' / 'hgrc') ifTrue:[
        ^true
    ].
    HGConfig userConfigFiles do:[:e|
        (self mustReloadBecauseOf: e) ifTrue:[ ^ true ].
    ].
    ^false

    "Created: / 06-12-2012 / 21:38:49 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 07-12-2012 / 16:28:33 / jv"
!

mustReloadBecauseOf: filenameOrString
    "Return true, if config must be reloaded because file is newer"

    | filename |

    filename := filenameOrString asFilename.
    "/compensate for lack of milliseconds in OS time ---v
    ^filename exists and:[(filename modificationTime + 1000) >= timestamp].

    "Created: / 07-12-2012 / 16:26:38 / jv"
    "Modified (comment): / 09-12-2012 / 23:15:43 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

root
    self mustReload ifTrue:[
        timestamp := Timestamp now.
        root := HGCommand showconfig
                    workingDirectory: repository pathName;
                    execute.
    ].
    ^root

    "Created: / 06-12-2012 / 20:25:14 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 09-12-2012 / 23:10:07 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!HGConfig::Entry methodsFor:'accessing'!

name
    ^ name
!

value
    ^ value
! !

!HGConfig::Entry methodsFor:'initialization'!

setName: nm value: val
    name := nm.
    value := val.

    "Created: / 06-12-2012 / 19:29:17 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!HGConfig::Entry methodsFor:'testing'!

isEntry
    ^true

    "Created: / 06-12-2012 / 19:36:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

isSection
    ^false

    "Created: / 06-12-2012 / 19:36:41 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!HGConfig::Section methodsFor:'accessing'!

get: key

    | entry |

    entry := self.
    (key isCollection and:[key isString not]) ifTrue:[
        key do:[:e|entry := entry at: e]
    ] ifFalse:[
        entry := entry at:key.
    ].
    ^entry value

    "Created: / 06-12-2012 / 20:01:03 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 06-12-2012 / 21:48:10 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

get: key default: default
    | entry |

    entry := self.
    (key isCollection and:[key isString not]) ifTrue:[
        key do:[:e|entry := entry at: e ifAbsent:[ ^ default ]]
    ] ifFalse:[
        entry := entry at:key ifAbsent:[ ^ default ].
    ].
    ^entry value

    "Created: / 06-12-2012 / 20:07:49 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 06-12-2012 / 21:49:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

name
    ^ name
! !

!HGConfig::Section methodsFor:'initialization'!

setName: nm 
    name := nm.
    entries := Dictionary new

    "Created: / 06-12-2012 / 19:57:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!HGConfig::Section methodsFor:'testing'!

isEntry
    ^false

    "Created: / 06-12-2012 / 19:36:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

isSection
    ^true

    "Created: / 06-12-2012 / 19:36:41 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!HGConfig class methodsFor:'documentation'!

version_HG

    ^ '$Changeset: <not expanded> $'
! !