chiark / gitweb /
6193eb0cb1e5bc6a67fc174aa849d3c5d5d6f40c
[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 crt_series = stack.Series()
35
36
37 # Utility functions
38 def git_id(string):
39     """Return the GIT id
40     """
41     if not string:
42         return None
43     
44     string_list = string.split('/')
45
46     if len(string_list) == 1:
47         patch_name = None
48         git_id = string_list[0]
49
50         if git_id == 'HEAD':
51             return git.get_head()
52         if git_id == 'base':
53             return read_string(crt_series.get_base_file())
54
55         for path in [os.path.join(git.base_dir, 'refs', 'heads'),
56                      os.path.join(git.base_dir, 'refs', 'tags')]:
57             id_file = os.path.join(path, git_id)
58             if os.path.isfile(id_file):
59                 return read_string(id_file)
60     elif len(string_list) == 2:
61         patch_name = string_list[0]
62         if patch_name == '':
63             patch_name = crt_series.get_current()
64         git_id = string_list[1]
65
66         if not patch_name:
67             raise CmdException, 'No patches applied'
68         elif not (patch_name in crt_series.get_applied()
69                 + crt_series.get_unapplied()):
70             raise CmdException, 'Unknown patch "%s"' % patch_name
71
72         if git_id == 'bottom':
73             return crt_series.get_patch(patch_name).get_bottom()
74         if git_id == 'top':
75             return crt_series.get_patch(patch_name).get_top()
76
77     raise CmdException, 'Unknown id: %s' % string
78
79 def check_local_changes():
80     if git.local_changes():
81         raise CmdException, \
82               'local changes in the tree. Use "refresh" to commit them'
83
84 def check_head_top_equal():
85     if not crt_series.head_top_equal():
86         raise CmdException, \
87               'HEAD and top are not the same. You probably committed\n' \
88               '  changes to the tree ouside of StGIT. If you know what you\n' \
89               '  are doing, use the "refresh -f" command'
90
91 def check_conflicts():
92     if os.path.exists(os.path.join(git.base_dir, 'conflicts')):
93         raise CmdException, 'Unsolved conflicts. Please resolve them first'
94
95 def print_crt_patch():
96     patch = crt_series.get_current()
97     if patch:
98         print 'Now at patch "%s"' % patch
99     else:
100         print 'No patches applied'
101
102 def resolved(filename):
103     git.update_cache([filename])
104     for ext in ['.local', '.older', '.remote']:
105         fn = filename + ext
106         if os.path.isfile(fn):
107             os.remove(fn)
108
109 def resolved_all():
110     conflicts = git.get_conflicts()
111     if conflicts:
112         for filename in conflicts:
113             resolved(filename)
114         os.remove(os.path.join(git.base_dir, 'conflicts'))