chiark / gitweb /
Infrastructure for current directory handling
[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 optparse import OptionParser, make_option
20
21 from stgit.commands.common import *
22 from stgit.utils import *
23 from stgit.out import *
24 from stgit.config import GitConfigException
25 from stgit import stack, git
26
27
28 help = 'pull the changes from the remote repository'
29 usage = """%prog [options] [<repository>]
30
31 Pull the latest changes from the given remote repository (defaulting
32 to branch.<name>.remote, or 'origin' if not set). This command works
33 by popping all the patches from the stack, pulling the changes in the
34 parent repository, setting the base of the stack to the latest parent
35 HEAD and pushing the patches back (unless '--nopush' is specified).
36 The 'push' operation can fail if there are conflicts. They need to be
37 resolved and the patch pushed again.
38
39 Check the 'git fetch' documentation for the <repository> format."""
40
41 directory = DirectoryHasRepository()
42 options = [make_option('-n', '--nopush',
43                        help = 'do not push the patches back after pulling',
44                        action = 'store_true'),
45            make_option('-m', '--merged',
46                        help = 'check for patches merged upstream',
47                        action = 'store_true')]
48
49 def func(parser, options, args):
50     """Pull the changes from a remote repository
51     """
52     policy = config.get('branch.%s.stgit.pull-policy' % crt_series.get_name()) or \
53              config.get('stgit.pull-policy')
54
55     if policy == 'rebase':
56         # parent is local
57         if len(args) == 1:
58             parser.error('specifying a repository is meaningless for policy="%s"' % policy)
59         if len(args) > 0:
60             parser.error('incorrect number of arguments')
61
62     else:
63         # parent is remote
64         if len(args) > 1:
65             parser.error('incorrect number of arguments')
66
67         if len(args) >= 1:
68             repository = args[0]
69         else:
70             repository = crt_series.get_parent_remote()
71
72     if crt_series.get_protected():
73         raise CmdException, 'This branch is protected. Pulls are not permitted'
74
75     check_local_changes()
76     check_conflicts()
77     check_head_top_equal()
78
79     if policy not in ['pull', 'fetch-rebase', 'rebase']:
80         raise GitConfigException, 'Unsupported pull-policy "%s"' % policy
81
82     applied = prepare_rebase()
83
84     # pull the remote changes
85     if policy == 'pull':
86         out.info('Pulling from "%s"' % repository)
87         git.pull(repository)
88     elif policy == 'fetch-rebase':
89         out.info('Fetching from "%s"' % repository)
90         git.fetch(repository)
91         try:
92             target = git.fetch_head()
93         except git.GitException:
94             out.error('Could not find the remote head to rebase onto, pushing any patches back...')
95             post_rebase(applied, False, False)
96             raise CmdException, 'Could not find the remote head to rebase onto - fix branch.%s.merge in .git/config' % crt_series.get_name()
97
98         rebase(target)
99     elif policy == 'rebase':
100         rebase(crt_series.get_parent_branch())
101
102     post_rebase(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()