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