chiark / gitweb /
d18b0b2514d984fc4124572625d721920ae3a17e
[stgit] / stgit / commands / common.py
1 """Function/variables commmon to all the commands
2 """
3
4 __copyright__ = """
5 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 """
20
21 import sys, os
22 from optparse import OptionParser, make_option
23
24 from stgit.utils import *
25 from stgit import stack, git
26
27
28 # Command exception class
29 class CmdException(Exception):
30     pass
31
32
33 # Global variables
34 try:
35     crt_series = stack.Series()
36 except (IOError, stack.StackException, git.GitException), err:
37     print >> sys.stderr, err
38     sys.exit(2)
39
40
41 # Utility functions
42 def git_id(string):
43     """Return the GIT id
44     """
45     if not string:
46         return None
47     
48     string_list = string.split('/')
49
50     if len(string_list) == 1:
51         patch_name = None
52         git_id = string_list[0]
53
54         if git_id == 'HEAD':
55             return git.get_head()
56         if git_id == 'base':
57             return read_string(crt_series.get_base_file())
58
59         for path in [os.path.join(git.base_dir, 'refs', 'heads'),
60                      os.path.join(git.base_dir, 'refs', 'tags')]:
61             id_file = os.path.join(path, git_id)
62             if os.path.isfile(id_file):
63                 return read_string(id_file)
64     elif len(string_list) == 2:
65         patch_name = string_list[0]
66         if patch_name == '':
67             patch_name = crt_series.get_current()
68         git_id = string_list[1]
69
70         if not patch_name:
71             raise CmdException, 'No patches applied'
72         elif not (patch_name in crt_series.get_applied()
73                 + crt_series.get_unapplied()):
74             raise CmdException, 'Unknown patch "%s"' % patch_name
75
76         if git_id == 'bottom':
77             return crt_series.get_patch(patch_name).get_bottom()
78         if git_id == 'top':
79             return crt_series.get_patch(patch_name).get_top()
80
81     raise CmdException, 'Unknown id: %s' % string
82
83 def check_local_changes():
84     if git.local_changes():
85         raise CmdException, \
86               'local changes in the tree. Use "refresh" to commit them'
87
88 def check_head_top_equal():
89     if not crt_series.head_top_equal():
90         raise CmdException, \
91               'HEAD and top are not the same. You probably committed\n' \
92               '  changes to the tree ouside of StGIT. If you know what you\n' \
93               '  are doing, use the "refresh -f" command'
94
95 def check_conflicts():
96     if os.path.exists(os.path.join(git.base_dir, 'conflicts')):
97         raise CmdException, 'Unsolved conflicts. Please resolve them first'
98
99 def print_crt_patch():
100     patch = crt_series.get_current()
101     if patch:
102         print 'Now at patch "%s"' % patch
103     else:
104         print 'No patches applied'
105
106 def resolved(filename):
107     git.update_cache([filename])
108     for ext in ['.local', '.older', '.remote']:
109         fn = filename + ext
110         if os.path.isfile(fn):
111             os.remove(fn)
112
113 def resolved_all():
114     conflicts = git.get_conflicts()
115     if conflicts:
116         for filename in conflicts:
117             resolved(filename)
118         os.remove(os.path.join(git.base_dir, 'conflicts'))