chiark / gitweb /
c989b5d087c979a37d0f2b1e301b11b915a3727b
[stgit] / stgit / commands / pull.py
1 __copyright__ = """
2 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License version 2 as
6 published by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 """
17
18 import sys, os
19 from stgit.argparse import opt
20 from stgit.commands.common import *
21 from stgit.utils import *
22 from stgit.out import *
23 from stgit.config import GitConfigException
24 from stgit import stack, git
25
26 help = 'Pull changes from a remote repository'
27 kind = 'stack'
28 usage = ['[options] [<repository>]']
29 description = """
30 Pull the latest changes from the given remote repository (defaulting
31 to branch.<name>.remote, or 'origin' if not set). This command works
32 by popping all the patches from the stack, pulling the changes in the
33 parent repository, setting the base of the stack to the latest parent
34 HEAD and pushing the patches back (unless '--nopush' is specified).
35 The 'push' operation can fail if there are conflicts. They need to be
36 resolved and the patch pushed again.
37
38 Check the 'git fetch' documentation for the <repository> format."""
39
40 options = [
41     opt('-n', '--nopush', action = 'store_true',
42         short = 'Do not push the patches back after pulling'),
43     opt('-m', '--merged', action = 'store_true',
44         short = 'Check for patches merged upstream')]
45
46 directory = DirectoryGotoToplevel()
47
48 def func(parser, options, args):
49     """Pull the changes from a remote repository
50     """
51     policy = config.get('branch.%s.stgit.pull-policy' % crt_series.get_name()) or \
52              config.get('stgit.pull-policy')
53
54     if policy == 'rebase':
55         # parent is local
56         if len(args) == 1:
57             parser.error('specifying a repository is meaningless for policy="%s"' % policy)
58         if len(args) > 0:
59             parser.error('incorrect number of arguments')
60
61     else:
62         # parent is remote
63         if len(args) > 1:
64             parser.error('incorrect number of arguments')
65
66         if len(args) >= 1:
67             repository = args[0]
68         else:
69             repository = crt_series.get_parent_remote()
70
71     if crt_series.get_protected():
72         raise CmdException, 'This branch is protected. Pulls are not permitted'
73
74     check_local_changes()
75     check_conflicts()
76     check_head_top_equal(crt_series)
77
78     if policy not in ['pull', 'fetch-rebase', 'rebase']:
79         raise GitConfigException, 'Unsupported pull-policy "%s"' % policy
80
81     applied = prepare_rebase(crt_series)
82
83     # pull the remote changes
84     if policy == 'pull':
85         out.info('Pulling from "%s"' % repository)
86         git.pull(repository)
87     elif policy == 'fetch-rebase':
88         out.info('Fetching from "%s"' % repository)
89         git.fetch(repository)
90         try:
91             target = git.fetch_head()
92         except git.GitException:
93             out.error('Could not find the remote head to rebase onto - fix branch.%s.merge in .git/config' % crt_series.get_name())
94             out.error('Pushing any patches back...')
95             post_rebase(crt_series, applied, False, False)
96             raise
97
98         rebase(crt_series, target)
99     elif policy == 'rebase':
100         rebase(crt_series, crt_series.get_parent_branch())
101
102     post_rebase(crt_series, applied, options.nopush, options.merged)
103
104     # maybe tidy up
105     if config.get('stgit.keepoptimized') == 'yes':
106         git.repack()
107
108     print_crt_patch(crt_series)