iceberg icon indicating copy to clipboard operation
iceberg copied to clipboard

Update a branch without working copy changes

Open pavel-krivanek opened this issue 2 years ago • 0 comments

Let's say you work on a feature branch and you want to merge the latest master into it. For that, you should have the local master branch updated which, if done from Pharo, requires switching to the master branch, doing pull and switching back. It is slow and sometimes dangerous.

The other way is to do it from the command line:

git fetch origin master:master

There is a way how to do the same from Pharo, take inspiration from this code.

repo := IceRepositoryCreator new
		repository: nil;
		location: repositoryPath asFileReference;
		createRepository.
"IceRepository registerRepository: repo."

repo fetch.
	
branchName := 'master'.

localBranch := repo branchNamed: branchName ifPresent: [ :b | b ] ifAbsent: [].

aRemote := localBranch upstream remote.
aRemote fetchBranch: localBranch.
remoteBranch := aRemote
		remoteBranchNamed: branchName
		ifAbsent: [ ^ self ].
localHead := localBranch commit.
remoteHead := remoteBranch commit.		

(localHead isAncestorOf: remoteHead) ifFalse: [ self error: 'Cannot do fast-forward' ].

localBranch commit: remoteHead.

It would be really cool to have it as the feature accessible from the UI

pavel-krivanek avatar Dec 28 '21 09:12 pavel-krivanek