Example usage for org.eclipse.jgit.api Git branchCreate

List of usage examples for org.eclipse.jgit.api Git branchCreate

Introduction

In this page you can find the example usage for org.eclipse.jgit.api Git branchCreate.

Prototype

public CreateBranchCommand branchCreate() 

Source Link

Document

Return a command object used to create branches

Usage

From source file:com.door43.translationstudio.tasks.PullTargetTranslationTask.java

private void createBackupBranch(Repo repo) {
    try {/*www .j  a  va2  s.co  m*/
        Git git = repo.getGit();
        DeleteBranchCommand deleteBranchCommand = git.branchDelete();
        deleteBranchCommand.setBranchNames("backup-master").setForce(true).call();
        CreateBranchCommand createBranchCommand = git.branchCreate();
        createBranchCommand.setName("backup-master").setForce(true).call();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.gitblit.servlet.GitServletTest.java

License:Apache License

private void testRefChange(AccessPermission permission, Status expectedCreate, Status expectedDelete,
        Status expectedRewind) throws Exception {

    final String originName = "ticgit.git";
    final String forkName = "refchecks/ticgit.git";
    final String workingCopy = "refchecks/ticgit-wc";

    // lower access restriction on origin repository
    RepositoryModel origin = repositories().getRepositoryModel(originName);
    origin.accessRestriction = AccessRestrictionType.NONE;
    repositories().updateRepositoryModel(origin.name, origin, false);

    UserModel user = getUser();//w w w. ja va2 s. com
    delete(user);

    CredentialsProvider cp = new UsernamePasswordCredentialsProvider(user.username, user.password);

    // fork from original to a temporary bare repo
    File refChecks = new File(GitBlitSuite.REPOSITORIES, forkName);
    if (refChecks.exists()) {
        FileUtils.delete(refChecks, FileUtils.RECURSIVE);
    }
    CloneCommand clone = Git.cloneRepository();
    clone.setURI(url + "/" + originName);
    clone.setDirectory(refChecks);
    clone.setBare(true);
    clone.setCloneAllBranches(true);
    clone.setCredentialsProvider(cp);
    GitBlitSuite.close(clone.call());

    // elevate repository to clone permission
    RepositoryModel model = repositories().getRepositoryModel(forkName);
    switch (permission) {
    case VIEW:
        model.accessRestriction = AccessRestrictionType.CLONE;
        break;
    case CLONE:
        model.accessRestriction = AccessRestrictionType.CLONE;
        break;
    default:
        model.accessRestriction = AccessRestrictionType.PUSH;
    }
    model.authorizationControl = AuthorizationControl.NAMED;

    // grant user specified
    user.setRepositoryPermission(model.name, permission);

    gitblit().addUser(user);
    repositories().updateRepositoryModel(model.name, model, false);

    // clone temp bare repo to working copy
    File local = new File(GitBlitSuite.REPOSITORIES, workingCopy);
    if (local.exists()) {
        FileUtils.delete(local, FileUtils.RECURSIVE);
    }
    clone = Git.cloneRepository();
    clone.setURI(MessageFormat.format("{0}/{1}", url, model.name));
    clone.setDirectory(local);
    clone.setBare(false);
    clone.setCloneAllBranches(true);
    clone.setCredentialsProvider(cp);

    try {
        GitBlitSuite.close(clone.call());
    } catch (GitAPIException e) {
        if (permission.atLeast(AccessPermission.CLONE)) {
            throw e;
        } else {
            // close serving repository
            GitBlitSuite.close(refChecks);

            // user does not have clone permission
            assertTrue(e.getMessage(), e.getMessage().contains("not permitted"));
            return;
        }
    }

    Git git = Git.open(local);

    // commit a file and push it
    File file = new File(local, "PUSHCHK");
    OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true), Constants.CHARSET);
    BufferedWriter w = new BufferedWriter(os);
    w.write("// " + new Date().toString() + "\n");
    w.close();
    git.add().addFilepattern(file.getName()).call();
    git.commit().setMessage("push test").call();
    Iterable<PushResult> results = null;
    try {
        results = git.push().setCredentialsProvider(cp).setRemote("origin").call();
    } catch (GitAPIException e) {
        if (permission.atLeast(AccessPermission.PUSH)) {
            throw e;
        } else {
            // close serving repository
            GitBlitSuite.close(refChecks);

            // user does not have push permission
            assertTrue(e.getMessage(), e.getMessage().contains("not permitted"));
            GitBlitSuite.close(git);
            return;
        }
    }

    for (PushResult result : results) {
        RemoteRefUpdate ref = result.getRemoteUpdate("refs/heads/master");
        Status status = ref.getStatus();
        if (permission.atLeast(AccessPermission.PUSH)) {
            assertTrue("User failed to push commit?! " + status.name(), Status.OK.equals(status));
        } else {
            // close serving repository
            GitBlitSuite.close(refChecks);

            assertTrue("User was able to push commit! " + status.name(),
                    Status.REJECTED_OTHER_REASON.equals(status));
            GitBlitSuite.close(git);
            // skip delete test
            return;
        }
    }

    // create a local branch and push the new branch back to the origin
    git.branchCreate().setName("protectme").call();
    RefSpec refSpec = new RefSpec("refs/heads/protectme:refs/heads/protectme");
    results = git.push().setCredentialsProvider(cp).setRefSpecs(refSpec).setRemote("origin").call();
    for (PushResult result : results) {
        RemoteRefUpdate ref = result.getRemoteUpdate("refs/heads/protectme");
        Status status = ref.getStatus();
        if (Status.OK.equals(expectedCreate)) {
            assertTrue("User failed to push creation?! " + status.name(), status.equals(expectedCreate));
        } else {
            // close serving repository
            GitBlitSuite.close(refChecks);

            assertTrue("User was able to push ref creation! " + status.name(), status.equals(expectedCreate));
            GitBlitSuite.close(git);
            // skip delete test
            return;
        }
    }

    // delete the branch locally
    git.branchDelete().setBranchNames("protectme").call();

    // push a delete ref command
    refSpec = new RefSpec(":refs/heads/protectme");
    results = git.push().setCredentialsProvider(cp).setRefSpecs(refSpec).setRemote("origin").call();
    for (PushResult result : results) {
        RemoteRefUpdate ref = result.getRemoteUpdate("refs/heads/protectme");
        Status status = ref.getStatus();
        if (Status.OK.equals(expectedDelete)) {
            assertTrue("User failed to push ref deletion?! " + status.name(), status.equals(Status.OK));
        } else {
            // close serving repository
            GitBlitSuite.close(refChecks);

            assertTrue("User was able to push ref deletion?! " + status.name(), status.equals(expectedDelete));
            GitBlitSuite.close(git);
            // skip rewind test
            return;
        }
    }

    // rewind master by two commits
    git.reset().setRef("HEAD~2").setMode(ResetType.HARD).call();

    // commit a change on this detached HEAD
    file = new File(local, "REWINDCHK");
    os = new OutputStreamWriter(new FileOutputStream(file, true), Constants.CHARSET);
    w = new BufferedWriter(os);
    w.write("// " + new Date().toString() + "\n");
    w.close();
    git.add().addFilepattern(file.getName()).call();
    RevCommit commit = git.commit().setMessage("rewind master and new commit").call();

    // Reset master to our new commit now we our local branch tip is no longer
    // upstream of the remote branch tip.  It is an alternate tip of the branch.
    JGitUtils.setBranchRef(git.getRepository(), "refs/heads/master", commit.getName());

    // Try pushing our new tip to the origin.
    // This requires the server to "rewind" it's master branch and update it
    // to point to our alternate tip.  This leaves the original master tip
    // unreferenced.
    results = git.push().setCredentialsProvider(cp).setRemote("origin").setForce(true).call();
    for (PushResult result : results) {
        RemoteRefUpdate ref = result.getRemoteUpdate("refs/heads/master");
        Status status = ref.getStatus();
        if (Status.OK.equals(expectedRewind)) {
            assertTrue("User failed to rewind master?! " + status.name(), status.equals(expectedRewind));
        } else {
            assertTrue("User was able to rewind master?! " + status.name(), status.equals(expectedRewind));
        }
    }
    GitBlitSuite.close(git);

    // close serving repository
    GitBlitSuite.close(refChecks);

    delete(user);
}

From source file:com.tasktop.c2c.server.scm.service.GitServiceBean.java

License:Open Source License

@Secured({ Role.User })
@Override//w  ww  . j av  a  2 s. c  om
public String createBranch(String repoName, String branchName) throws EntityNotFoundException {
    try {
        Repository r = findRepositoryByName(repoName);
        Git git = Git.wrap(r);
        CreateBranchCommand command = git.branchCreate();
        command.setName(branchName);

        /*
         * command.setStartPoint(getStartPoint().name()); if (upstreamMode != null)
         * command.setUpstreamMode(upstreamMode); command.call();
         */

        command.call();
        r.close();
        return branchName;
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (GitAPIException e) {
        throw new RuntimeException(e);
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.worldline.easycukes.scm.utils.GitHelper.java

License:Open Source License

/**
* Create a new branch in the local git repository
* (git checkout -b branchname) and finally pushes new branch on the remote repository (git push)
*
* @param directory the directory in which the local git repository is located
* @param username  the username to be used while pushing
* @param password  the password matching with the provided username to be used
*                  for authentication/*  w  w  w.ja v  a 2 s.c om*/
* @param message   the commit message to be used    
*/
public static void createBranch(@NonNull File directory, String branchName, String username, String password,
        String message) throws GitAPIException {

    try {
        final Git git = Git.open(directory);

        final UsernamePasswordCredentialsProvider userCredential = new UsernamePasswordCredentialsProvider(
                username, password);

        CreateBranchCommand branchCommand = git.branchCreate();
        branchCommand.setName(branchName);
        branchCommand.call();

        // and then commit
        final PersonIdent author = new PersonIdent(username, "");
        git.commit().setCommitter(author).setMessage(message).setAuthor(author).call();
        log.info(message);

        git.push().setCredentialsProvider(userCredential).call();
        log.info("Pushed the changes in remote Git repository...");
    } catch (final GitAPIException | IOException e) {
        log.error(e.getMessage(), e);
    }
}

From source file:de.blizzy.documentr.page.CherryPicker.java

License:Open Source License

private List<CommitCherryPickResult> cherryPick(String projectName, String branchName, String path,
        List<String> commits, String targetBranch, Set<CommitCherryPickConflictResolve> conflictResolves,
        boolean dryRun, User user, Locale locale) throws IOException, GitAPIException {

    ILockedRepository repo = null;//from  ww  w  . j  av a  2  s. c o m
    List<CommitCherryPickResult> cherryPickResults = Lists.newArrayList();
    boolean hadConflicts = false;
    boolean failed = false;
    try {
        repo = globalRepositoryManager.getProjectBranchRepository(projectName, targetBranch);

        String tempBranchName = "_temp_" + String.valueOf((long) (Math.random() * Long.MAX_VALUE)); //$NON-NLS-1$
        Git git = Git.wrap(repo.r());

        git.branchCreate().setName(tempBranchName).setStartPoint(targetBranch).call();

        git.checkout().setName(tempBranchName).call();

        for (String commit : commits) {
            PageVersion pageVersion = PageUtil.toPageVersion(CommitUtils.getCommit(repo.r(), commit));
            if (!hadConflicts) {
                CommitCherryPickResult singleCherryPickResult = cherryPick(repo, branchName, path, pageVersion,
                        targetBranch, conflictResolves, user, locale);
                if (singleCherryPickResult != null) {
                    cherryPickResults.add(singleCherryPickResult);
                    if (singleCherryPickResult.getStatus() == CommitCherryPickResult.Status.CONFLICT) {
                        hadConflicts = true;
                    }
                } else {
                    failed = true;
                    break;
                }
            } else {
                cherryPickResults
                        .add(new CommitCherryPickResult(pageVersion, CommitCherryPickResult.Status.UNKNOWN));
            }
        }

        if (hadConflicts || failed) {
            git.reset().setMode(ResetCommand.ResetType.HARD).call();
        }

        git.checkout().setName(targetBranch).call();

        if (!dryRun && !hadConflicts && !failed) {
            git.merge().include(repo.r().resolve(tempBranchName)).call();
        }

        git.branchDelete().setBranchNames(tempBranchName).setForce(true).call();

        if (failed) {
            throw new IOException("cherry-picking failed"); //$NON-NLS-1$
        }
    } finally {
        Closeables.closeQuietly(repo);
    }

    if (!dryRun && !hadConflicts && !failed) {
        eventBus.post(new PageChangedEvent(projectName, targetBranch, path));
    }

    return cherryPickResults;
}

From source file:de.blizzy.documentr.page.PageStore.java

License:Open Source License

private MergeConflict savePageInternal(String projectName, String branchName, String path, String suffix,
        Page page, String baseCommit, String rootDir, User user, ILockedRepository repo, boolean push)
        throws IOException, GitAPIException {

    Git git = Git.wrap(repo.r());

    String headCommit = CommitUtils.getHead(repo.r()).getName();
    if ((baseCommit != null) && headCommit.equals(baseCommit)) {
        baseCommit = null;//from   www  .  j ava 2 s. c om
    }

    String editBranchName = "_edit_" + String.valueOf((long) (Math.random() * Long.MAX_VALUE)); //$NON-NLS-1$
    if (baseCommit != null) {
        git.branchCreate().setName(editBranchName).setStartPoint(baseCommit).call();

        git.checkout().setName(editBranchName).call();
    }

    Map<String, Object> metaMap = new HashMap<String, Object>();
    metaMap.put(TITLE, page.getTitle());
    metaMap.put(CONTENT_TYPE, page.getContentType());
    if (!page.getTags().isEmpty()) {
        metaMap.put(TAGS, page.getTags());
    }
    metaMap.put(VIEW_RESTRICTION_ROLE, page.getViewRestrictionRole());
    Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create();
    String json = gson.toJson(metaMap);
    File workingDir = RepositoryUtil.getWorkingDir(repo.r());
    File pagesDir = new File(workingDir, rootDir);
    File workingFile = Util.toFile(pagesDir, path + DocumentrConstants.META_SUFFIX);
    FileUtils.write(workingFile, json, Charsets.UTF_8);

    PageData pageData = page.getData();
    if (pageData != null) {
        workingFile = Util.toFile(pagesDir, path + suffix);
        FileUtils.writeByteArrayToFile(workingFile, pageData.getData());
    }

    AddCommand addCommand = git.add().addFilepattern(rootDir + "/" + path + DocumentrConstants.META_SUFFIX); //$NON-NLS-1$
    if (pageData != null) {
        addCommand.addFilepattern(rootDir + "/" + path + suffix); //$NON-NLS-1$
    }
    addCommand.call();

    PersonIdent ident = new PersonIdent(user.getLoginName(), user.getEmail());
    git.commit().setAuthor(ident).setCommitter(ident).setMessage(rootDir + "/" + path + suffix).call(); //$NON-NLS-1$

    MergeConflict conflict = null;

    if (baseCommit != null) {
        git.rebase().setUpstream(branchName).call();

        if (repo.r().getRepositoryState() != RepositoryState.SAFE) {
            String text = FileUtils.readFileToString(workingFile, Charsets.UTF_8);
            conflict = new MergeConflict(text, headCommit);

            git.rebase().setOperation(RebaseCommand.Operation.ABORT).call();
        }

        git.checkout().setName(branchName).call();

        if (conflict == null) {
            git.merge().include(repo.r().resolve(editBranchName)).call();
        }

        git.branchDelete().setBranchNames(editBranchName).setForce(true).call();
    }

    if (push && (conflict == null)) {
        git.push().call();
    }

    page.setParentPagePath(getParentPagePath(path, repo.r()));

    if (conflict == null) {
        PageUtil.updateProjectEditTime(projectName);
    }

    return conflict;
}

From source file:de.blizzy.documentr.repository.ProjectRepositoryManager.java

License:Open Source License

ILockedRepository createBranchRepository(String branchName, String startingBranch)
        throws IOException, GitAPIException {
    Assert.hasLength(branchName);/*from  ww w .  j ava2  s  .com*/
    if (startingBranch != null) {
        Assert.hasLength(startingBranch);
    }

    File repoDir = new File(reposDir, branchName);
    if (repoDir.isDirectory()) {
        throw new IllegalStateException("repository already exists: " + repoDir.getAbsolutePath()); //$NON-NLS-1$
    }

    List<String> branches = listBranches();
    if (branches.contains(branchName)) {
        throw new IllegalArgumentException("branch already exists: " + branchName); //$NON-NLS-1$
    }

    if ((startingBranch == null) && !branches.isEmpty()) {
        throw new IllegalArgumentException("must specify a starting branch"); //$NON-NLS-1$
    }

    ILock lock = lockManager.lockAll();
    try {
        Repository centralRepo = null;
        File centralRepoGitDir;
        try {
            centralRepo = getCentralRepositoryInternal(true);
            centralRepoGitDir = centralRepo.getDirectory();
        } finally {
            RepositoryUtil.closeQuietly(centralRepo);
            centralRepo = null;
        }

        Repository repo = null;
        try {
            repo = Git.cloneRepository().setURI(centralRepoGitDir.toURI().toString()).setDirectory(repoDir)
                    .call().getRepository();

            try {
                centralRepo = getCentralRepositoryInternal(true);
                if (!RepositoryUtils.getBranches(centralRepo).contains(branchName)) {
                    CreateBranchCommand createBranchCommand = Git.wrap(centralRepo).branchCreate();
                    if (startingBranch != null) {
                        createBranchCommand.setStartPoint(startingBranch);
                    }
                    createBranchCommand.setName(branchName).call();
                }
            } finally {
                RepositoryUtil.closeQuietly(centralRepo);
            }

            Git git = Git.wrap(repo);
            RefSpec refSpec = new RefSpec("refs/heads/" + branchName + ":refs/remotes/origin/" + branchName); //$NON-NLS-1$ //$NON-NLS-2$
            git.fetch().setRemote("origin").setRefSpecs(refSpec).call(); //$NON-NLS-1$
            git.branchCreate().setName(branchName).setStartPoint("origin/" + branchName).call(); //$NON-NLS-1$
            git.checkout().setName(branchName).call();
        } finally {
            RepositoryUtil.closeQuietly(repo);
        }
    } finally {
        lockManager.unlock(lock);
    }

    return getBranchRepository(branchName);
}

From source file:de.blizzy.documentr.repository.ProjectRepositoryManager.java

License:Open Source License

public void importSampleContents() throws IOException, GitAPIException {
    // TODO: synchronization is not quite correct here, but should be okay in this edge case
    if (listBranches().isEmpty()) {
        ILock lock = lockManager.lockAll();
        List<String> branches;
        try {//from  w  ww .j av  a2  s  .c  o  m
            File gitDir = new File(centralRepoDir, ".git"); //$NON-NLS-1$
            FileUtils.forceDelete(gitDir);

            Git.cloneRepository().setURI(DocumentrConstants.SAMPLE_REPO_URL).setDirectory(gitDir).setBare(true)
                    .call();

            Repository centralRepo = null;
            File centralRepoGitDir;
            try {
                centralRepo = getCentralRepositoryInternal(true);
                centralRepoGitDir = centralRepo.getDirectory();
                StoredConfig config = centralRepo.getConfig();
                config.unsetSection("remote", "origin"); //$NON-NLS-1$ //$NON-NLS-2$
                config.unsetSection("branch", "master"); //$NON-NLS-1$ //$NON-NLS-2$
                config.save();
            } finally {
                RepositoryUtil.closeQuietly(centralRepo);
            }

            branches = listBranches();
            for (String branchName : branches) {
                File repoDir = new File(reposDir, branchName);
                Repository repo = null;
                try {
                    repo = Git.cloneRepository().setURI(centralRepoGitDir.toURI().toString())
                            .setDirectory(repoDir).call().getRepository();

                    Git git = Git.wrap(repo);
                    RefSpec refSpec = new RefSpec(
                            "refs/heads/" + branchName + ":refs/remotes/origin/" + branchName); //$NON-NLS-1$ //$NON-NLS-2$
                    git.fetch().setRemote("origin").setRefSpecs(refSpec).call(); //$NON-NLS-1$
                    git.branchCreate().setName(branchName).setStartPoint("origin/" + branchName).call(); //$NON-NLS-1$
                    git.checkout().setName(branchName).call();
                } finally {
                    RepositoryUtil.closeQuietly(repo);
                }
            }
        } finally {
            lockManager.unlock(lock);
        }

        for (String branch : branches) {
            eventBus.post(new BranchCreatedEvent(projectName, branch));
        }
    }
}

From source file:io.fabric8.git.zkbridge.Bridge.java

License:Apache License

private void updateLocal(Git git, CuratorFramework zookeeper, CredentialsProvider credentialsProvider)
        throws Exception {
    String remoteName = "origin";

    try {/*from  w w w. j  a v  a  2s  .  co  m*/
        git.fetch().setCredentialsProvider(credentialsProvider).setRemote(remoteName).call();
    } catch (Exception e) {
        // Ignore fetch exceptions
        return;
    }

    // Get local and remote branches
    Map<String, Ref> localBranches = new HashMap<String, Ref>();
    Map<String, Ref> remoteBranches = new HashMap<String, Ref>();
    Set<String> gitVersions = new HashSet<String>();
    for (Ref ref : git.branchList().setListMode(ListBranchCommand.ListMode.ALL).call()) {
        if (ref.getName().startsWith("refs/remotes/" + remoteName + "/")) {
            String name = ref.getName().substring(("refs/remotes/" + remoteName + "/").length());
            if (!"master".equals(name) && !name.endsWith("-tmp")) {
                remoteBranches.put(name, ref);
                gitVersions.add(name);
            }
        } else if (ref.getName().startsWith("refs/heads/")) {
            String name = ref.getName().substring(("refs/heads/").length());
            if (!name.equals("master") && !name.endsWith("-tmp")) {
                localBranches.put(name, ref);
                gitVersions.add(name);
            }
        }
    }

    // Check git commmits
    for (String version : gitVersions) {
        // Delete unneeded local branches
        if (!remoteBranches.containsKey(version)) {
            git.branchDelete().setBranchNames(localBranches.get(version).getName()).setForce(true).call();
        }
        // Create new local branches
        else if (!localBranches.containsKey(version)) {
            git.branchCreate().setName(version).call();
            git.reset().setMode(ResetCommand.ResetType.HARD).setRef(remoteBranches.get(version).getName())
                    .call();
        } else {
            String localCommit = localBranches.get(version).getObjectId().getName();
            String remoteCommit = remoteBranches.get(version).getObjectId().getName();
            if (!localCommit.equals(remoteCommit)) {
                git.clean().setCleanDirectories(true).call();
                git.checkout().setName("HEAD").setForce(true).call();
                git.checkout().setName(version).setForce(true).call();
                MergeResult result = git.merge().setStrategy(MergeStrategy.THEIRS)
                        .include(remoteBranches.get(version).getObjectId()).call();
                // TODO: handle conflicts
            }
        }
    }
}

From source file:io.fabric8.git.zkbridge.Bridge.java

License:Apache License

private static void update(Git git, CuratorFramework zookeeper, CredentialsProvider credentialsProvider)
        throws Exception {
    String remoteName = "origin";

    boolean remoteAvailable = false;
    try {//  w w w.j a v  a  2  s  .c  o m
        git.fetch().setCredentialsProvider(credentialsProvider).setRemote(remoteName).call();
        remoteAvailable = true;
    } catch (Exception e) {
        // Ignore fetch exceptions
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("Unable to fetch master", e);
        } else if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Unable to fetch master: " + e.getClass().getName() + ": " + e.getMessage());
        }
    }

    // Handle versions in git and not in zookeeper
    Map<String, Ref> localBranches = new HashMap<String, Ref>();
    Map<String, Ref> remoteBranches = new HashMap<String, Ref>();
    Set<String> gitVersions = new HashSet<String>();
    for (Ref ref : git.branchList().setListMode(ListBranchCommand.ListMode.ALL).call()) {
        if (ref.getName().startsWith("refs/remotes/" + remoteName + "/")) {
            String name = ref.getName().substring(("refs/remotes/" + remoteName + "/").length());
            if (!"master".equals(name) && !name.endsWith("-tmp")) {
                remoteBranches.put(name, ref);
                gitVersions.add(name);
            }
        } else if (ref.getName().startsWith("refs/heads/")) {
            String name = ref.getName().substring(("refs/heads/").length());
            if (!name.equals("master") && !name.endsWith("-tmp")) {
                localBranches.put(name, ref);
                gitVersions.add(name);
            }
        }
    }
    List<String> zkVersions = getChildren(zookeeper, ZkPath.CONFIG_VERSIONS.getPath());
    createDefault(zookeeper, "/fabric/configs/git", null);
    Properties versionsMetadata = loadProps(zookeeper, "/fabric/configs/git");

    boolean allDone = true;
    // Check no modifs in zookeeper
    String lastModified = Long.toString(lastModified(zookeeper, ZkPath.CONFIG_VERSIONS.getPath()));
    if (!lastModified.equals(versionsMetadata.get("zk-lastmodified"))) {
        allDone = false;
    }
    // Check the versions in zk and git are the same
    if (zkVersions.size() != gitVersions.size() || !zkVersions.containsAll(gitVersions)) {
        allDone = false;
    }
    // Check all local and remote branches exists
    if (gitVersions.size() != localBranches.size() || !localBranches.keySet().containsAll(gitVersions)) {
        allDone = false;
    }
    // If remote is available, check that all remote branches exist
    if (remoteAvailable && !remoteBranches.keySet().containsAll(gitVersions)) {
        allDone = false;
    }
    // Check git commmits
    if (allDone) {
        for (String version : zkVersions) {
            String zkCommit = versionsMetadata.get(version);
            String localCommit = localBranches.get(version).getObjectId().getName();
            String remoteCommit = remoteAvailable ? remoteBranches.get(version).getObjectId().getName() : null;
            if (!localCommit.equals(zkCommit) || remoteCommit != null && !localCommit.equals(remoteCommit)) {
                allDone = false;
                break;
            }
        }
    }
    if (allDone) {
        return;
    }

    // ZooKeeper -> Git changes
    for (String version : zkVersions) {
        String zkNode = ZkPath.CONFIG_VERSION.getPath(version);

        // Checkout updated version
        List<Ref> allBranches = git.branchList().setListMode(ListBranchCommand.ListMode.ALL).call();
        Ref local = null;
        Ref remote = null;
        Ref tmp = null;
        for (Ref ref : allBranches) {
            if (ref.getName().equals("refs/remotes/" + remoteName + "/" + version)) {
                remote = ref;
            } else if (ref.getName().equals("refs/heads/" + version)) {
                local = ref;
            } else if (ref.getName().equals("refs/heads/" + version + "-tmp")) {
                tmp = ref;
            }
        }
        if (local == null) {
            git.branchCreate().setName(version).call();
        }
        if (tmp == null) {
            git.branchCreate().setName(version + "-tmp").call();
        }
        git.clean().setCleanDirectories(true).call();
        git.checkout().setName("HEAD").setForce(true).call();
        git.checkout().setName(version).setForce(true).call();
        if (remoteAvailable && remote != null) {
            MergeResult result = git.merge().setStrategy(MergeStrategy.THEIRS).include(remote.getObjectId())
                    .call();
            // TODO: check merge conflicts
        }
        git.checkout().setName(version + "-tmp").setForce(true).call();
        String gitCommit = versionsMetadata.get(version);
        if (gitCommit != null) {
            try {
                git.reset().setMode(ResetCommand.ResetType.HARD).setRef(gitCommit).call();
            } catch (Exception e) {
                // Ignore, we did our best
                if (LOGGER.isTraceEnabled()) {
                    LOGGER.trace("Unable to reset branch to commit", e);
                } else if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Unable to reset branch to commit " + gitCommit + ": " + e.getClass().getName()
                            + ": " + e.getMessage());
                }
            }
        }

        // Apply changes to git
        syncVersionFromZkToGit(git, zookeeper, zkNode);

        if (git.status().call().isClean()) {
            git.checkout().setName(version).setForce(true).call();
        } else {
            ObjectId rev = git.commit().setMessage("Merge zookeeper updates in version " + version).call()
                    .getId();
            git.checkout().setName(version).setForce(true).call();
            MergeResult result = git.merge().setStrategy(MergeStrategy.OURS).include(rev).call();
            // TODO: check merge conflicts
        }
        if (remoteAvailable) {
            git.push().setCredentialsProvider(credentialsProvider).setRefSpecs(new RefSpec(version)).call();
        }

        // Apply changes to zookeeper
        syncVersionFromGitToZk(git, zookeeper, zkNode);

        versionsMetadata.put(version, git.getRepository().getRef("HEAD").getObjectId().getName());
    }
    // Iterate through known git versions
    for (String version : gitVersions) {
        String state = versionsMetadata.get(version);
        if (zkVersions.contains(version)) {
            continue;
        }
        // The version is not known to zookeeper, so create it
        if (state == null) {
            if (localBranches.containsKey(version)) {
                if (remoteAvailable) {
                    git.push().setRefSpecs(new RefSpec(version)).call();
                }
            } else {
                git.branchCreate().setName(version).call();
                git.reset().setMode(ResetCommand.ResetType.HARD).setRef(remoteBranches.get(version).getName())
                        .call();
            }
            git.checkout().setName(version).setForce(true).call();
            // Sync zookeeper
            String zkNode = ZkPath.CONFIG_VERSION.getPath(version);
            create(zookeeper, zkNode);
            create(zookeeper, ZkPath.CONFIG_VERSIONS_PROFILES.getPath(version));
            create(zookeeper, ZkPath.CONFIG_VERSIONS_CONTAINERS.getPath(version));
            syncVersionFromGitToZk(git, zookeeper, zkNode);
            // Flag version as active
            versionsMetadata.put(version, git.getRepository().getRef("HEAD").getObjectId().getName());
        }
        // The version has been deleted from zookeeper so delete it in git
        else {
            git.checkout().setName("master").setForce(true).call();
            git.branchDelete().setBranchNames(version, version + "-tmp").setForce(true).call();
            git.push().setRefSpecs(new RefSpec(version + ":")).call();
            versionsMetadata.remove(version);
        }
    }
    versionsMetadata.put("zk-lastmodified",
            Long.toString(lastModified(zookeeper, ZkPath.CONFIG_VERSIONS.getPath())));
    setPropertiesAsMap(zookeeper, "/fabric/configs/git", versionsMetadata);
}