mercurial/HGMergeTool.st
author Patrik Svestka <patrik.svestka@gmail.com>
Mon, 10 Jan 2022 14:21:17 +0100
changeset 938 2bb53758015c
parent 916 02d7b7f1ea08
permissions -rw-r--r--
Enable support for Mercurial 6.x

"
COPYRIGHT (c) 2020 LabWare

stx:libscm - a new source code management library for Smalltalk/X

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License. 

This library 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
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
"
"{ Package: 'stx:libscm/mercurial' }"

"{ NameSpace: Smalltalk }"

SCMMergeTool subclass:#HGMergeTool
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'SCM-Mercurial-StX-Tools'
!

!HGMergeTool class methodsFor:'documentation'!

copyright
"
COPYRIGHT (c) 2020 LabWare

stx:libscm - a new source code management library for Smalltalk/X

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License. 

This library 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
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
"
! !

!HGMergeTool methodsFor:'merging'!

merge: local revision: otherRevString
    | repo localRev baseRev otherRev localPath base other |

    repo := HGRepository discover: local.
    repo isNil ifTrue: [
        self error: 'Cannot find Mercurial repository for: ', local pathName.
        ^ self.
    ].
    repo := HGRepository on: repo.
    otherRev := repo changesetsMatching: otherRevString.
    otherRev isEmpty ifTrue:[
        self error: 'No revisions matching ', otherRevString.
        ^ self.
    ].
    otherRev size > 1 ifTrue: [
        self error: 'Multiple matching ', otherRevString.
        ^ self.
    ].
    otherRev := otherRev first.

    localRev := repo workingCopy changeset.

    baseRev := repo changesetsMatching: ('children(ancestor(%1,%2)) and ancestors(%1)' bindWith: localRev id printStringWithoutNumber with: otherRev id printStringWithoutNumber).
    baseRev isEmpty ifTrue:[
        self error: 'Cannot find base revision'.
        ^ self.
    ].
    baseRev size > 1 ifTrue: [
        self error: 'Multiple base revisions found, probably a bug!!'.
        ^ self.
    ].
    baseRev := baseRev first.

    localPath := local pathName copyFrom: repo pathName size + 2.
    
    base := baseRev / localPath.
    other := otherRev / localPath.

    "/ Save merge info to include in commit message
    (local pathName , '-merge-info.txt') asFilename writingFileDo: [:s |
        | mergedRevs |

        mergedRevs := repo changesetsMatching: ('parents(%1):%2 and file(%3)' bindWith: baseRev id printStringWithoutNumber with: otherRev id printStringWithoutNumber with: localPath).
        s nextPutAll: ('cherry-picked %1 from %2:' bindWith: localPath with: otherRev id printStringWithoutNumber).
        s cr; cr.
        mergedRevs do: [:r | 
            s nextPutLine: ('    * %1: %2, %3' bindWith: r id printStringWithoutNumber with: r summary with: r author ).
        ].
    ].

    ^ self base: base local: local other: other output: local.

    "
    HGMergeTool new merge:'/home/jv/Projects/SmalltalkX/sources/jv1_x32_lin/build/stx/libbasic/String.st' asFilename revision: 'default'
    "

    "Created: / 27-08-2020 / 09:34:57 / Jan Vrany <jan.vrany@labware.com>"
    "Modified: / 26-10-2020 / 22:02:39 / Jan Vrany <jan.vrany@labware.com>"
! !

!HGMergeTool class methodsFor:'documentation'!

version_HG

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