// Path: cicomponents-api/src/main/java/org/cicomponents/ResourceHolder.java
// public interface ResourceHolder<T extends Resource> {
//     T acquire();
// }
// 
// Path: cicomponents-common/src/main/java/org/cicomponents/common/SimpleResourceHolder.java
// public class SimpleResourceHolder<T extends Resource> implements ResourceHolder<T> {
// 
//     private final T resource;
// 
//     public SimpleResourceHolder(T resource) {this.resource = resource;}
// 
//     @Override public T acquire() {
//         resource.acquire();
//         return resource;
//     }
// }
// 
// Path: cicomponents-fs-api/src/main/java/org/cicomponents/fs/WorkingDirectory.java
// public interface WorkingDirectory extends Resource {
//     String getDirectory();
// }
// 
// Path: cicomponents-git-api/src/main/java/org/cicomponents/git/GitRevision.java
// public interface GitRevision extends File, Resource {
//     Git getRepository();
//     ObjectId getRef();
// }


import lombok.SneakyThrows;
import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;
import org.cicomponents.ResourceHolder;
import org.cicomponents.common.SimpleResourceHolder;
import org.cicomponents.fs.WorkingDirectory;
import org.cicomponents.git.GitRevision;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.events.RefsChangedEvent;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.osgi.framework.FrameworkUtil;
import java.io.File;
import java.io.IOException;
import java.util.Dictionary;
import java.util.Map;
/**
 * Copyright (c) 2016, All Contributors (see CONTRIBUTORS file)
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package org.cicomponents.git.impl;



@Slf4j
public class LatestRevisionGitBranchMonitor extends AbstractLocalGitMonitor {

    private final Map<String, Object> persistentMap;
    private ObjectId head;
    private final String branch;

    private boolean initialized = false;

    @SneakyThrows
    public LatestRevisionGitBranchMonitor(Environment environment, Dictionary<String, ?> dictionary) {
        super(environment, dictionary);
        branch = (String) dictionary.get("branch");
        persistentMap = environment.getPersistentMap();
    }

    @SneakyThrows
    @Override void resume() {
        super.resume();
    }

    @SneakyThrows
    @Override protected void onGitReady() {
        checkLatest();
    }

    private void checkLatest() throws IOException {
        synchronized (git) {
            String headRefName = git.getRepository().findRef("refs/heads/" + branch).getObjectId().getName();
            String knownHead = (String) persistentMap.get(headRefName);
// Path: cicomponents-api/src/main/java/org/cicomponents/ResourceHolder.java
// public interface ResourceHolder<T extends Resource> {
//     T acquire();
// }
// 
// Path: cicomponents-common/src/main/java/org/cicomponents/common/SimpleResourceHolder.java
// public class SimpleResourceHolder<T extends Resource> implements ResourceHolder<T> {
// 
//     private final T resource;
// 
//     public SimpleResourceHolder(T resource) {this.resource = resource;}
// 
//     @Override public T acquire() {
//         resource.acquire();
//         return resource;
//     }
// }
// 
// Path: cicomponents-fs-api/src/main/java/org/cicomponents/fs/WorkingDirectory.java
// public interface WorkingDirectory extends Resource {
//     String getDirectory();
// }
// 
// Path: cicomponents-git-api/src/main/java/org/cicomponents/git/GitRevision.java
// public interface GitRevision extends File, Resource {
//     Git getRepository();
//     ObjectId getRef();
// }


// Path: cicomponents-git/src/main/java/org/cicomponents/git/impl/LatestRevisionGitBranchMonitor.java
import lombok.SneakyThrows;
import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;
import org.cicomponents.ResourceHolder;
import org.cicomponents.common.SimpleResourceHolder;
import org.cicomponents.fs.WorkingDirectory;
import org.cicomponents.git.GitRevision;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.events.RefsChangedEvent;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.osgi.framework.FrameworkUtil;
import java.io.File;
import java.io.IOException;
import java.util.Dictionary;
import java.util.Map;

/**
 * Copyright (c) 2016, All Contributors (see CONTRIBUTORS file)
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package org.cicomponents.git.impl;



@Slf4j
public class LatestRevisionGitBranchMonitor extends AbstractLocalGitMonitor {

    private final Map<String, Object> persistentMap;
    private ObjectId head;
    private final String branch;

    private boolean initialized = false;

    @SneakyThrows
    public LatestRevisionGitBranchMonitor(Environment environment, Dictionary<String, ?> dictionary) {
        super(environment, dictionary);
        branch = (String) dictionary.get("branch");
        persistentMap = environment.getPersistentMap();
    }

    @SneakyThrows
    @Override void resume() {
        super.resume();
    }

    @SneakyThrows
    @Override protected void onGitReady() {
        checkLatest();
    }

    private void checkLatest() throws IOException {
        synchronized (git) {
            String headRefName = git.getRepository().findRef("refs/heads/" + branch).getObjectId().getName();
            String knownHead = (String) persistentMap.get(headRefName);
            if (knownHead != null) {