Fix the t1201-pull-trailing.sh test
[stgit] / stgit / git.py
CommitLineData
41a6d859
CM
1"""Python GIT interface
2"""
3
4__copyright__ = """
5Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License version 2 as
9published by the Free Software Foundation.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19"""
20
3659ef88 21import sys, os, popen2, re, gitmergeonefile
41a6d859 22
170f576b 23from stgit import basedir
41a6d859
CM
24from stgit.utils import *
25
26# git exception class
27class GitException(Exception):
28 pass
29
30
41a6d859 31
41a6d859
CM
32#
33# Classes
34#
35class Commit:
36 """Handle the commit objects
37 """
38 def __init__(self, id_hash):
39 self.__id_hash = id_hash
41a6d859 40
26dba451 41 lines = _output_lines('git-cat-file commit %s' % id_hash)
37a4d1bf 42 self.__parents = []
26dba451
BL
43 for i in range(len(lines)):
44 line = lines[i]
41a6d859
CM
45 if line == '\n':
46 break
47 field = line.strip().split(' ', 1)
48 if field[0] == 'tree':
49 self.__tree = field[1]
50 elif field[0] == 'parent':
37a4d1bf 51 self.__parents.append(field[1])
41a6d859
CM
52 if field[0] == 'author':
53 self.__author = field[1]
dad310d0 54 if field[0] == 'committer':
41a6d859 55 self.__committer = field[1]
0618ea9c 56 self.__log = ''.join(lines[i+1:])
41a6d859
CM
57
58 def get_id_hash(self):
59 return self.__id_hash
60
61 def get_tree(self):
62 return self.__tree
63
64 def get_parent(self):
37a4d1bf
CM
65 return self.__parents[0]
66
67 def get_parents(self):
68 return self.__parents
41a6d859
CM
69
70 def get_author(self):
71 return self.__author
72
73 def get_committer(self):
74 return self.__committer
75
37a4d1bf
CM
76 def get_log(self):
77 return self.__log
78
8e29bcd2
CM
79# dictionary of Commit objects, used to avoid multiple calls to git
80__commits = dict()
41a6d859
CM
81
82#
83# Functions
84#
bae29ddd 85
8e29bcd2
CM
86def get_commit(id_hash):
87 """Commit objects factory. Save/look-up them in the __commits
88 dictionary
89 """
3237b6e4
CM
90 global __commits
91
8e29bcd2
CM
92 if id_hash in __commits:
93 return __commits[id_hash]
94 else:
95 commit = Commit(id_hash)
96 __commits[id_hash] = commit
97 return commit
98
41a6d859
CM
99def get_conflicts():
100 """Return the list of file conflicts
101 """
170f576b 102 conflicts_file = os.path.join(basedir.get(), 'conflicts')
41a6d859
CM
103 if os.path.isfile(conflicts_file):
104 f = file(conflicts_file)
105 names = [line.strip() for line in f.readlines()]
106 f.close()
107 return names
108 else:
109 return None
110
0d2cd1e4 111def _input(cmd, file_desc):
741f2784 112 p = popen2.Popen3(cmd, True)
6fe6b1bd
CM
113 while True:
114 line = file_desc.readline()
115 if not line:
116 break
0d2cd1e4
CM
117 p.tochild.write(line)
118 p.tochild.close()
119 if p.wait():
120 raise GitException, '%s failed' % str(cmd)
121
d0bfda1a
CM
122def _input_str(cmd, string):
123 p = popen2.Popen3(cmd, True)
124 p.tochild.write(string)
125 p.tochild.close()
126 if p.wait():
127 raise GitException, '%s failed' % str(cmd)
128
26dba451 129def _output(cmd):
741f2784 130 p=popen2.Popen3(cmd, True)
7cc615f3 131 output = p.fromchild.read()
26dba451
BL
132 if p.wait():
133 raise GitException, '%s failed' % str(cmd)
7cc615f3 134 return output
26dba451 135
d3cf7d86 136def _output_one_line(cmd, file_desc = None):
741f2784 137 p=popen2.Popen3(cmd, True)
d3cf7d86
PBG
138 if file_desc != None:
139 for line in file_desc:
140 p.tochild.write(line)
141 p.tochild.close()
7cc615f3 142 output = p.fromchild.readline().strip()
26dba451
BL
143 if p.wait():
144 raise GitException, '%s failed' % str(cmd)
7cc615f3 145 return output
41a6d859 146
26dba451 147def _output_lines(cmd):
741f2784 148 p=popen2.Popen3(cmd, True)
26dba451
BL
149 lines = p.fromchild.readlines()
150 if p.wait():
151 raise GitException, '%s failed' % str(cmd)
152 return lines
153
154def __run(cmd, args=None):
155 """__run: runs cmd using spawnvp.
156
157 Runs cmd using spawnvp. The shell is avoided so it won't mess up
158 our arguments. If args is very large, the command is run multiple
159 times; args is split xargs style: cmd is passed on each
160 invocation. Unlike xargs, returns immediately if any non-zero
161 return code is received.
162 """
163
164 args_l=cmd.split()
165 if args is None:
166 args = []
167 for i in range(0, len(args)+1, 100):
168 r=os.spawnvp(os.P_WAIT, args_l[0], args_l + args[i:min(i+100, len(args))])
169 if r:
170 return r
171 return 0
172
9216b602 173def __tree_status(files = None, tree_id = 'HEAD', unknown = False,
be24d874 174 noexclude = True):
41a6d859
CM
175 """Returns a list of pairs - [status, filename]
176 """
f8fb5747 177 refresh_index()
41a6d859 178
9216b602
CL
179 if not files:
180 files = []
41a6d859
CM
181 cache_files = []
182
183 # unknown files
184 if unknown:
170f576b 185 exclude_file = os.path.join(basedir.get(), 'info', 'exclude')
be24d874
CM
186 base_exclude = ['--exclude=%s' % s for s in
187 ['*.[ao]', '*.pyc', '.*', '*~', '#*', 'TAGS', 'tags']]
188 base_exclude.append('--exclude-per-directory=.gitignore')
189
41a6d859 190 if os.path.exists(exclude_file):
3c6fbd2c 191 extra_exclude = ['--exclude-from=%s' % exclude_file]
be24d874
CM
192 else:
193 extra_exclude = []
4d4c0e3a
PBG
194 if noexclude:
195 extra_exclude = base_exclude = []
be24d874 196
2c02c3b7
PBG
197 lines = _output_lines(['git-ls-files', '--others', '--directory']
198 + base_exclude + extra_exclude)
26dba451 199 cache_files += [('?', line.strip()) for line in lines]
41a6d859
CM
200
201 # conflicted files
202 conflicts = get_conflicts()
203 if not conflicts:
204 conflicts = []
205 cache_files += [('C', filename) for filename in conflicts]
206
207 # the rest
fec7f658 208 for line in _output_lines(['git-diff-index', tree_id] + files):
26dba451 209 fs = tuple(line.rstrip().split(' ',4)[-1].split('\t',1))
41a6d859
CM
210 if fs[1] not in conflicts:
211 cache_files.append(fs)
41a6d859
CM
212
213 return cache_files
214
215def local_changes():
216 """Return true if there are local changes in the tree
217 """
218 return len(__tree_status()) != 0
219
aa01a285
CM
220# HEAD value cached
221__head = None
222
41a6d859 223def get_head():
3097799d 224 """Verifies the HEAD and returns the SHA1 id that represents it
41a6d859 225 """
aa01a285
CM
226 global __head
227
228 if not __head:
229 __head = rev_parse('HEAD')
230 return __head
41a6d859
CM
231
232def get_head_file():
233 """Returns the name of the file pointed to by the HEAD link
234 """