forked from Dieterbe/git-scripts
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-record
More file actions
Latest commit
executable file
·260 lines (224 loc) · 6.73 KB
/
Copy pathgit-record
File metadata and controls
executable file
·260 lines (224 loc) · 6.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/env python
# coding: utf-8
# git-darcs-record, emulate "darcs record" interface on top of a git repository
#
# Usage:
# git-darcs-record first asks for any new file (previously
# untracked) to be added to the index.
# git-darcs-record then asks for each hunk to be recorded in
# the next commit. File deletion and binary blobs are supported
# git-darcs-record finally asks for a small commit message and
# executes the 'git commit' command with the newly created
# changeset in the index
# Copyright (C) 2007 Raphaël Slinckx <raphael@slinckx.net>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
importre, pprint, sys, os
BINARY=re.compile("GIT binary patch")
HEADER=re.compile("diff --git a/(.*) b/(.*)")
classHunk:
def__init__(self, lines, binary):
self.diff=None
self.lines=lines
self.keep=False
self.binary=binary
defformat(self):
output=self.diff.header.modified+"\n"
ifself.diff.header.deleted:
output="Removed file: "+output
ifself.binary:
output="Binary file changed: "+output
ifnotself.binary:
output+="\n".join(self.lines) +"\n"
returnoutput
classHeader:
def__init__(self, lines):
self.lines=lines
self.modified=None
self.deleted=False
# Extract useful info from header from git
forlineinlines:
ifHEADER.match(line):
match=HEADER.match(line)
self.modified=match.group(1)
ifline.startswith("deleted "):
self.deleted=True
# Make sure we know what file we are modifying
assertself.modified
classDiff:
def__init__(self, header, hunks):
self.header=header
self.hunks=hunks
# Put a reference to ourselves in the hunks
forhunkinself.hunks:
hunk.diff=self
self.keep=False
deffilter(self):
output='\n'.join(self.header.lines) +"\n"
forhunkinself.hunks:
ifnothunk.keep:
continue
output+='\n'.join(hunk.lines) +"\n"
returnoutput
@classmethod
deffilter_diffs(kls, diffs):
output=""
fordiffindiffs:
ifnotdiff.keep:
continue
output+=diff.filter()
returnoutput
@classmethod
defparse(kls, lines):
in_header=True
binary=False
header= []
hunks= []
current_hunk= []
forlineinlines:
ifin_headerand (line[0] notin (" ", "@", "\\") orline.startswith("+++ ") orline.startswith("--- ")) andnotBINARY.match(line):
header.append(line)
elifBINARY.match(line):
in_header=False
binary=True
header.append(line)
eliflen(line) >=1andline[0] =="@":
in_header=False
ifcurrent_hunk:
hunks.append(Hunk(current_hunk, binary))
current_hunk= []
current_hunk.append(line)
else:
current_hunk.append(line)
ifcurrent_hunk:
hunks.append(Hunk(current_hunk, binary))
returnDiff(Header(header), hunks)
@classmethod
defsplit(kls, lines):
diffs= []
current_diff= []
forlineinlines:
ifline.startswith("diff --git "):
ifcurrent_diff:
diffs.append(current_diff)
current_diff= []
current_diff.append(line)
else:
current_diff.append(line)
ifcurrent_diff:
diffs.append(current_diff)
return [Diff.parse(lines) forlinesindiffs]
defread_answer(question, allowed_responses=["Y", "n", "d", "a"]):
#Make sure there is alway a default selection
assert [rforrinallowed_responsesifr.isupper()]
whileTrue:
resp=raw_input("%s [%s] : "% (question, "".join(allowed_responses)))
ifrespin [r.lower() forrinallowed_responses]:
break
elifresp=="":
resp= [rforrinallowed_responsesifr.isupper()][0].lower()
break
print'Unexpected answer: %r'%resp
returnresp
defsetup_git_dir():
globalGIT_DIR
GIT_DIR=os.getcwd()
whilenotos.path.exists(os.path.join(GIT_DIR, ".git")):
GIT_DIR=os.path.dirname(GIT_DIR)
ifGIT_DIR=="/":
returnFalse
os.chdir(GIT_DIR)
returnTrue
defgit_get_untracked_files():
return [f.strip() forfinos.popen("git ls-files --others --exclude-from='%s' --exclude-per-directory=.gitignore"% (os.path.join(GIT_DIR, ".git", "info", "exclude"))).readlines()]
defgit_track_file(f):
os.spawnvp(os.P_WAIT, "git", ["git", "add", f])
defgit_diff():
returnos.popen("git diff -u --no-color --binary").readlines()
defgit_apply(patch):
stdin, stdout=os.popen2(["git", "apply", "--cached", "-"])
stdin.write(patch)
stdin.close()
output=stdout.read()
stdout.close()
os.wait()
returnoutput
defgit_status():
os.spawnvp(os.P_WAIT, "git", ["git", "status"])
defgit_commit(msg):
os.spawnvp(os.P_WAIT, "git", ["git", "commit", "-m", patch_name])
# Main loop ------------------------
ifnotsetup_git_dir():
print"Must be in a git (sub-)directory! Exiting..."
sys.exit()
# Ask for new files ----------------
git_untracked_files=git_get_untracked_files()
git_track_files= []
all=False
done=False
fori, finenumerate(git_untracked_files):
ifnotall:
print"Add file: ", f
resp=read_answer("Shall I add this file? (%d/%d)"% (i+1, len(git_untracked_files)))
else:
resp="y"
ifresp=="y":
git_track_files.append(f)
elifresp=="a":
git_track_files.append(f)
all=True
elifresp=="d":
done=True
break
# Ask for each hunk of the diff
diffs=Diff.split([line[:-1] forlineingit_diff()])
total_hunks=sum([len(diff.hunks) fordiffindiffs])
n_hunk=1
fordiffindiffs:
ifdone:
break
forhunkindiff.hunks:
# Check if we are in override mode
ifnotall:
print
printhunk.format()
resp=read_answer('Shall I record this change? (%d/%d)'% (n_hunk, total_hunks))
# Otherwise say 'y' to all remaining patches
else:
resp="y"
ifresp=="y":
diff.keep=True
hunk.keep=True
elifresp=="a":
diff.keep=True
hunk.keep=True
all=True
elifresp=="d":
done=True
break
n_hunk+=1
# Add new files to track
forfingit_track_files:
git_track_file(f)
# Generate a new patch to be used with git apply
new_patch=Diff.filter_diffs(diffs)
ifnew_patch:
printgit_apply(new_patch)
ifnew_patchorgit_track_files:
git_status()
patch_name=raw_input("What is the patch name? ")
git_commit(patch_name)
else:
print"Ok, if you don't want to record anything, that's fine!"