/*
 * Copyright (C) 2017 Dennis Neufeld
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package space.npstr.wolfia.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.Properties;

/**
 * Created by napster on 05.05.17.
 * <p>
 * Provides access to the values of the property file generated by whatever git info plugin we're using
 */
@Slf4j
public class GitRepoState {

    public static GitRepoState getGitRepositoryState() {
        return GitRepoStateHolder.INSTANCE;
    }

    //holder pattern
    private static final class GitRepoStateHolder {
        private static final GitRepoState INSTANCE = new GitRepoState("git.properties");
    }

    public final String branch;
    public final String commitId;
    public final String commitIdAbbrev;
    public final String commitUserName;
    public final String commitUserEmail;
    public final String commitMessageFull;
    public final String commitMessageShort;
    public final long commitTime;

    @SuppressWarnings("ConstantConditions")
    public GitRepoState(final String propsName) {

        final Properties properties = new Properties();
        try {
            properties.load(GitRepoState.class.getClassLoader().getResourceAsStream(propsName));
        } catch (final NullPointerException | IOException e) {
            log.info("Failed to load git repo information", e); //need to build with build tool to get them
        }

        this.branch = String.valueOf(properties.getOrDefault("git.branch", ""));
        this.commitId = String.valueOf(properties.getOrDefault("git.commit.id", ""));
        this.commitIdAbbrev = String.valueOf(properties.getOrDefault("git.commit.id.abbrev", ""));
        this.commitUserName = String.valueOf(properties.getOrDefault("git.commit.user.name", ""));
        this.commitUserEmail = String.valueOf(properties.getOrDefault("git.commit.user.email", ""));
        this.commitMessageFull = String.valueOf(properties.getOrDefault("git.commit.message.full", ""));
        this.commitMessageShort = String.valueOf(properties.getOrDefault("git.commit.message.short", ""));
        this.commitTime = Long.parseLong(String.valueOf(properties.getOrDefault("git.commit.time", "0"))); //epoch seconds
    }
}
@@ -806,16 +806,92 @@
 eption;%0A
+import java.time.OffsetDateTime;%0Aimport java.time.format.DateTimeFormatter;%0A
 import j
@@ -911,16 +911,16 @@
 erties;%0A
-
 %0A/**%0A * 
@@ -1206,24 +1206,193 @@
 NCE;%0A    %7D%0A%0A
+    // https://github.com/n0mer/gradle-git-properties/issues/71%0A    private static final DateTimeFormatter DTF = DateTimeFormatter.ofPattern(%22yyyy-MM-dd'T'HH:mm:ssZ%22);%0A%0A
     //holder
@@ -1855,16 +1855,32 @@
 mitTime;
+ //epoch seconds
 %0A%0A    @S
@@ -3007,41 +3007,28 @@
     
-this.commitTime = Long.parseLong(
+final String time = 
 Stri
@@ -3048,33 +3048,24 @@
 operties.get
-OrDefault
 (%22git.commit
@@ -3074,33 +3074,183 @@
 ime%22
-, %220%22))); //e
+));%0A        if (time == null) %7B%0A            this.commitTime = 0;%0A        %7D else %7B%0A            this.commitTime = OffsetDateTime.from(DTF.parse(time)).toE
 poch
- s
+S
 econd
-s
+();%0A        %7D
 %0A   

Fix commit time parsing in git plugin