Check git pull remote before defaulting to 'origin'
[stgit] / stgit / commands / pull.py
... / ...
CommitLineData
1__copyright__ = """
2Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
3
4This program is free software; you can redistribute it and/or modify
5it under the terms of the GNU General Public License version 2 as
6published by the Free Software Foundation.
7
8This program is distributed in the hope that it will be useful,
9but WITHOUT ANY WARRANTY; without even the implied warranty of
10MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11GNU General Public License for more details.
12
13You should have received a copy of the GNU General Public License
14along with this program; if not, write to the Free Software
15Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16"""
17
18import sys, os
19from optparse import OptionParser, make_option
20
21from stgit.commands.common import *
22from stgit.utils import *
23from stgit import stack, git
24
25
26help = 'pull the changes from the remote repository'
27usage = """%prog [options] [<repository>] [<refspec>]
28
29Pull the latest changes from the given repository (defaulting to
30'origin'). This command works by popping all the patches from the
31stack, pulling the changes in the parent repository, setting the base
32of the stack to the latest parent HEAD and pushing the patches back
33(unless '--nopush' is specified). The 'push' operation can fail if
34there are conflicts. They need to be resolved and the patch pushed
35again.
36
37Check the 'git pull' documentation for the <repository> and <refspec>
38format."""
39
40options = [make_option('-n', '--nopush',
41 help = 'do not push the patches back after pulling',
42 action = 'store_true'),
43 make_option('-m', '--merged',
44 help = 'check for patches merged upstream',
45 action = 'store_true')]
46
47def func(parser, options, args):
48 """Pull the changes from a remote repository
49 """
50 if len(args) > 2:
51 parser.error('incorrect number of arguments')
52
53 if len(args) >= 1:
54 repository = args[0]
55 else:
56 section = 'branch "%s"' % git.get_head_file()
57 if config.has_option(section, 'remote'):
58 repository = config.get(section, 'remote')
59 else:
60 repository = 'origin'
61
62 refspec = None
63 if len(args) == 2:
64 refspec = args[1]
65
66 if crt_series.get_protected():
67 raise CmdException, 'This branch is protected. Pulls are not permitted'
68
69 check_local_changes()
70 check_conflicts()
71 check_head_top_equal()
72
73 # pop all patches
74 applied = crt_series.get_applied()
75 if len(applied) > 0:
76 print 'Popping all applied patches...',
77 sys.stdout.flush()
78 crt_series.pop_patch(applied[0])
79 print 'done'
80
81 # pull the remote changes
82 print 'Pulling from "%s"...' % repository
83 git.pull(repository, refspec)
84 print 'done'
85
86 # push the patches back
87 if not options.nopush:
88 push_patches(applied, options.merged)
89
90 # maybe tidy up
91 repack = config.get('stgit', 'keepoptimized')
92 if repack == 'yes':
93 git.repack()
94
95 print_crt_patch()