view src/main/java/org/icedrobot/ika/runtime/scm/GITRepository.java @ 27:17afcf8034a6

Deja assimilate vu Ika can now assimilate already assimilated dest dirs. * ika/plugins/borg/IcedRobotCloner.java: Updated to handle re-runs. * ika/runtime/scm/GITRepository.java: Likewise. * ika/runtime/scm/HGRepository.java: Likewise. * ika/runtime/scm/Repository.java: Likewise.
author Xerxes R?nby <xerxes@zafena.se>
date Mon, 11 Apr 2011 01:50:51 +0200
parents 1366f29ac6fa
children 3b0d6002605a
line wrap: on
line source

/*
 * IKA - IcedRobot Kiosk Application
 * Copyright (C) 2011  IcedRobot team
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.icedrobot.ika.runtime.scm;

import java.io.File;
import java.io.IOException;
import org.icedrobot.ika.plugins.IkaPluginException;
import org.icedrobot.ika.runtime.IkaRuntime;

public class GITRepository extends Repository {
    
    /**
     * Creates a new git Repository with the give name as ID, the
     * base directory as the location where the clone will be performed, the
     * relativePath as the name of the cloned repository, the remotePath as the
     * location of the source repository and branch as the version of the
     * repository to clone.
     */
    public GITRepository(String name, File baseDirectory, String relativePath,
                         String remotePath, String branch) {

        this.name = name;
        this.baseDirectory = baseDirectory;
        this.relativePath = relativePath;
        this.remotePath = remotePath;

        this.branch = branch;

        String canonicalPath;
        try {
             canonicalPath = baseDirectory.getCanonicalPath();
        } catch (IOException ex) {
            throw new IkaPluginException(ex.getMessage(), ex);
        }

        this.fullPath = new File(canonicalPath + File.separator + relativePath);
    }

    /**
     * Clone the git Repository.
     * If the git Repository allready exist then pop local patches and
     * then fetch and merge new changesets from the remotePath.
     */
    @Override
    public void makeClone() {
        File git = new File(fullPath + File.separator + name +
                                       File.separator + ".git" );
        if(!git.exists()) {
            String command = "git clone --progress -b " +
                              getBranch() + " " + remotePath;
            System.err.println(command);
            IkaRuntime.exec(getFullPath(), "git", "clone", "--progress", "-b",
                            getBranch(), remotePath);
        } else {
            File patchQueue = new File(fullPath + File.separator + name +
                                                  File.separator + ".git" +
                                                  File.separator + "patches" );
            if(patchQueue.exists()) {
                String command = "guilt pop --all";
                System.err.println(command);
                IkaRuntime.exec(new File(getFullPath() + File.separator + name),
                                "guilt", "pop", "--all");
            }

            String command = "git fetch --progress " + remotePath;
            System.err.println(command);
            IkaRuntime.exec(new File(getFullPath() + File.separator + name),
                            "git", "fetch", "--progress", remotePath);

            command = "git merge HEAD";
            System.err.println(command);
            IkaRuntime.exec(new File(getFullPath() + File.separator + name),
                            "git", "merge", "HEAD");
        }
    }

    /**
     * Create new git and initialize it to the prefered branch.
     * Skipped if the git branch already exist.
     */
    @Override
    public void create() {

        File repoBranch = new File(fullPath + File.separator + ".git" +
                                              File.separator + "refs" +
                                              File.separator + "heads" +
                                              File.separator + getBranch());
        if(!repoBranch.exists()) {

            String command = "git init .";
            System.err.println(command);
            IkaRuntime.exec(getFullPath(), "git", "init");

            command = "git add --all";
            System.err.println(command);
            IkaRuntime.exec(getFullPath(), "git", "add", "--all");

            command = "git commit -m initial_repository --verbose";
            System.err.println(command);
            IkaRuntime.exec(getFullPath(), "git", "commit", "-m",
                            "initial_repository", "--verbose");

            command = "git branch " + getBranch();
            System.err.println(command);
            IkaRuntime.exec(getFullPath(), "git", "branch", getBranch());

            command = "git checkout " + getBranch();
            System.err.println(command);
            IkaRuntime.exec(getFullPath(), "git", "checkout", getBranch());

            System.err.println(">>>>>>>> done: " + this + " <<<<<<<<");
        } else {
            System.err.println(">>>>>>>> already done: " + this + " <<<<<<<<");
        }
    }

    /**
     *
     */
    @Override
    public Repository makeSubRepository(String name, String relativePath,
                                        String branch) {

        if (name.equals(".")) {
            name = this.getName();
        }
        Repository subRepository =
            new GITRepository(name, new File(getFullPath() + File.separator +
                                             getName()),
                              relativePath, null, branch);
        subRepository.create();
        return subRepository;
    }

    @Override
    public void applyPatchQueue() {
        String command = "guilt push --all";
        System.err.println(command);
        IkaRuntime.exec(getFullPath(), "guilt", "push", "--all");
    }

    @Override
    public void initPatchQueue() {

        String command = "guilt init";
        System.err.println(command);
        IkaRuntime.exec(getFullPath(), "guilt", "init");
    }
}