summaryrefslogtreecommitdiffstats
path: root/src/GitDeploy/GitDeploy.php
blob: d1c2cf68c7b0da905a73725e11dfb23907be5bba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php

namespace GitDeploy;

use GitWrapper\GitWrapper;
use GitWrapper\GitWorkingCopy;

class GitDeploy
{
    protected $repositories;

    public function __construct($repositories = array())
    {
        $this->repositories = $repositories;
    }

    public static function fromFile($file = 'git-deploy.json')
    {
        $results = array();

        if (is_file($file)) {
            $contents = file_get_contents($file);
            $results = json_decode($contents);
        }

        return new GitDeploy($results);
    }

    public function update()
    {
        // Create the wrapper.
        $wrapper = new GitWrapper();
        $wrapper->streamOutput();

        foreach ($this->repositories as $dir => $repo) {
            // Build our git interface.
            $git = null;
            if (!is_dir($dir)) {
                $git = $wrapper->cloneRepository($repo, $dir);
            }
            else {
                $git = new GitWorkingCopy($wrapper, $dir);
            }

            // Fetch all the latest.
            $git->fetch('--all');

            // Reset over to the gh-pages branch.
            $git->reset('origin/gh-pages', array('hard' => true));

            // Remove any extra files.
            $git->clean('-d', '-f', '-x');
        }
    }

    public function getRepositories()
    {
        return $this->repositories;
    }
}