How can I use JGit to do the equivalent of "git push -mirror ..."?
I am trying to create a simple application that does a "git push -mirror" operation from a Java domain.
The JGit library, in particular the PushCommand class , does not seem to support the "-mirror" option, even though it does support "--all" and "--tags".
Am I missing something? How do we use JGit to do "git push -mirror ..."?
source to share
There is no exact equivalent in JGit yet --mirror
, but you should emulate this behavior. To force push all local links, you can configure the PushCommand with
PushCommand pushCommand = git.push();
pushCommand.setForce( true );
pushCommand.add( "refs/*:refs/*" );
This will leave links that were removed locally. Therefore, you can get a list of remote links to determine what was removed locally and publish those deletions to the remote machine:
Collection<Ref> remoteRefs = git.lsRemote().setRemote( "origin" ).setHeads( true ).setTags( true ).call();
Collection<String> deletedRefs = new ArrayList<String>();
for( Ref remoteRef : remoteRefs ) {
if( git.getRepository().getRef( remoteRef.getName() ) == null ) {
deletedRefs.add( remoteRef.getName() );
}
}
for( String deletedRef : deletedRefs ) {
pushCommand.add( ":" + deletedRef );
}
The variable git
refers to the repository you want to push from, i.e. from the first block. LsRemoteCommand
returns all chapters and tags from a remote repository that is configured as origin
in the local repository configuration. In the usual case, you cloned.
Note that there is a slight gap regarding the approach in which remote local links are propagated. LsRemoteCommand
returns refs only under heads
and tags
(e.g. no custom refs , for example pulls
), so you won't find local deletions like. refs/foo/bar
...
Does this work for you?
source to share