summaryrefslogtreecommitdiff
path: root/morphlib/plugins/push_pull_plugin.py
blob: ddc9a8af94cebbea8414e84205a19886572f996f (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
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
# Copyright (C) 2014  Codethink Limited
#
# 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; version 2 of the License.
#
# 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import cliapp
import logging
import os

import morphlib


class PushPullPlugin(cliapp.Plugin):

    '''Add subcommands to wrap the git push and pull commands.'''

    def enable(self):
        self.app.add_subcommand(
            'push', self.push, arg_synopsis='REPO TARGET')
        self.app.add_subcommand('pull', self.pull, arg_synopsis='[REMOTE]')

    def disable(self):
        pass

    def push(self, args):
        '''Push a branch to a remote repository.

        Command line arguments:

        * `REPO` is the repository to push your changes to.

        * `TARGET` is the branch to push to the repository.

        This is a wrapper for the `git push` command. It also deals with
        pushing any binary files that have been added using git-fat.

        Example:

            morph push origin jrandom/new-feature

        '''
        if len(args) != 2:
            raise morphlib.Error('push must get exactly two arguments')

        gd = morphlib.gitdir.GitDirectory(os.getcwd(), search_for_root=True)
        remote, branch = args
        rs = morphlib.gitdir.RefSpec(branch)
        gd.get_remote(remote).push(rs)
        if gd.has_fat():
            gd.fat_init()
            gd.fat_push()

    def pull(self, args):
        '''Pull changes to the current branch from a repository.

        Command line arguments:

        * `REMOTE` is the remote branch to pull from. By default this is the
          branch being tracked by your current git branch (ie origin/master
          for branch master)

        This is a wrapper for the `git pull` command. It also deals with
        pulling any binary files that have been added to the repository using
        git-fat.

        Example:

            morph pull

        '''
        if len(args) > 1:
            raise morphlib.Error('pull takes at most one argument')

        gd = morphlib.gitdir.GitDirectory(os.getcwd(), search_for_root=True)
        remote = gd.get_remote('origin')
        if args:
            branch = args[0]
            remote.pull(branch)
        else:
            remote.pull()
        if gd.has_fat():
            gd.fat_init()
            gd.fat_pull()