aboutsummaryrefslogtreecommitdiffstats
path: root/library/kzykhys/git/src/PHPGit/Command/Remote/SetUrlCommand.php
blob: 7b7d84ff5d10022d011b43fb22783eb0eb9596f7 (plain) (blame)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php

namespace PHPGit\Command\Remote;

use PHPGit\Command;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

/**
 * Changes URL remote points to
 *
 * @author Kazuyuki Hayashi
 */
class SetUrlCommand extends Command
{

    /**
     * Alias of set()
     *
     * ``` php
     * $git = new PHPGit\Git();
     * $git->setRepository('/path/to/repo');
     * $git->remote->add('origin', 'https://github.com/kzykhys/Text.git');
     * $git->remote->url('origin', 'https://github.com/text/Text.git');
     * ```
     *
     * ##### Options
     *
     * - **push** (_boolean_) Push URLs are manipulated instead of fetch URLs
     *
     * @param string $name    The name of remote
     * @param string $newUrl  The new URL
     * @param string $oldUrl  [optional] The old URL
     * @param array  $options [optional] An array of options {@see SetUrlCommand::setDefaultOptions}
     *
     * @return bool
     */
    public function __invoke($name, $newUrl, $oldUrl = null, array $options = array())
    {
        return $this->set($name, $newUrl, $oldUrl, $options);
    }

    /**
     * Sets the URL remote to $newUrl
     *
     * ``` php
     * $git = new PHPGit\Git();
     * $git->setRepository('/path/to/repo');
     * $git->remote->add('origin', 'https://github.com/kzykhys/Text.git');
     * $git->remote->url->set('origin', 'https://github.com/text/Text.git');
     * ```
     *
     * ##### Options
     *
     * - **push** (_boolean_) Push URLs are manipulated instead of fetch URLs
     *
     * @param string $name    The name of remote
     * @param string $newUrl  The new URL
     * @param string $oldUrl  [optional] The old URL
     * @param array  $options [optional] An array of options {@see SetUrlCommand::setDefaultOptions}
     *
     * @return bool
     */
    public function set($name, $newUrl, $oldUrl = null, array $options = array())
    {
        $options = $this->resolve($options);
        $builder = $this->git->getProcessBuilder()
            ->add('remote')
            ->add('set-url');

        $this->addFlags($builder, $options);

        $builder
            ->add($name)
            ->add($newUrl);

        if ($oldUrl) {
            $builder->add($oldUrl);
        }

        $this->git->run($builder->getProcess());

        return true;
    }

    /**
     * Adds new URL to remote
     *
     * ``` php
     * $git = new PHPGit\Git();
     * $git->setRepository('/path/to/repo');
     * $git->remote->add('origin', 'https://github.com/kzykhys/Text.git');
     * $git->remote->url->add('origin', 'https://github.com/text/Text.git');
     * ```
     *
     * ##### Options
     *
     * - **push** (_boolean_) Push URLs are manipulated instead of fetch URLs
     *
     * @param string $name    The name of remote
     * @param string $newUrl  The new URL
     * @param array  $options [optional] An array of options {@see SetUrlCommand::setDefaultOptions}
     *
     * @return bool
     */
    public function add($name, $newUrl, array $options = array())
    {
        $options = $this->resolve($options);
        $builder = $this->git->getProcessBuilder()
            ->add('remote')
            ->add('set-url')
            ->add('--add');

        $this->addFlags($builder, $options);

        $builder
            ->add($name)
            ->add($newUrl);

        $this->git->run($builder->getProcess());

        return true;
    }

    /**
     * Deletes all URLs matching regex $url
     *
     * ``` php
     * $git = new PHPGit\Git();
     * $git->setRepository('/path/to/repo');
     * $git->remote->add('origin', 'https://github.com/kzykhys/Text.git');
     * $git->remote->url->delete('origin', 'https://github.com');
     * ```
     *
     * ##### Options
     *
     * - **push** (_boolean_) Push URLs are manipulated instead of fetch URLs
     *
     * @param string $name    The remote name
     * @param string $url     The URL to delete
     * @param array  $options [optional] An array of options {@see SetUrlCommand::setDefaultOptions}
     *
     * @return bool
     */
    public function delete($name, $url, array $options = array())
    {
        $options = $this->resolve($options);
        $builder = $this->git->getProcessBuilder()
            ->add('remote')
            ->add('set-url')
            ->add('--delete');

        $this->addFlags($builder, $options);

        $builder
            ->add($name)
            ->add($url);

        $this->git->run($builder->getProcess());

        return true;
    }
    
    /**
     * {@inheritdoc}
     *
     * - **push** (_boolean_) Push URLs are manipulated instead of fetch URLs
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'push' => false
        ));
    }

}