@@ -188,10 +188,12 @@ static int replace_object(const char *object_ref, const char *replace_ref, int f
188
188
}
189
189
190
190
/*
191
- * Write the contents of the object named by "sha1" to the file "filename",
192
- * pretty-printed for human editing based on its type.
191
+ * Write the contents of the object named by "sha1" to the file "filename".
192
+ * If "raw" is true, then the object's raw contents are printed according to
193
+ * "type". Otherwise, we pretty-print the contents for human editing.
193
194
*/
194
- static void export_object (const unsigned char * sha1 , const char * filename )
195
+ static void export_object (const unsigned char * sha1 , enum object_type type ,
196
+ int raw , const char * filename )
195
197
{
196
198
struct child_process cmd = { NULL };
197
199
int fd ;
@@ -202,7 +204,10 @@ static void export_object(const unsigned char *sha1, const char *filename)
202
204
203
205
argv_array_push (& cmd .args , "--no-replace-objects" );
204
206
argv_array_push (& cmd .args , "cat-file" );
205
- argv_array_push (& cmd .args , "-p" );
207
+ if (raw )
208
+ argv_array_push (& cmd .args , typename (type ));
209
+ else
210
+ argv_array_push (& cmd .args , "-p" );
206
211
argv_array_push (& cmd .args , sha1_to_hex (sha1 ));
207
212
cmd .git_cmd = 1 ;
208
213
cmd .out = fd ;
@@ -217,15 +222,15 @@ static void export_object(const unsigned char *sha1, const char *filename)
217
222
* The sha1 of the written object is returned via sha1.
218
223
*/
219
224
static void import_object (unsigned char * sha1 , enum object_type type ,
220
- const char * filename )
225
+ int raw , const char * filename )
221
226
{
222
227
int fd ;
223
228
224
229
fd = open (filename , O_RDONLY );
225
230
if (fd < 0 )
226
231
die_errno ("unable to open %s for reading" , filename );
227
232
228
- if (type == OBJ_TREE ) {
233
+ if (! raw && type == OBJ_TREE ) {
229
234
const char * argv [] = { "mktree" , NULL };
230
235
struct child_process cmd = { argv };
231
236
struct strbuf result = STRBUF_INIT ;
@@ -265,7 +270,7 @@ static void import_object(unsigned char *sha1, enum object_type type,
265
270
*/
266
271
}
267
272
268
- static int edit_and_replace (const char * object_ref , int force )
273
+ static int edit_and_replace (const char * object_ref , int force , int raw )
269
274
{
270
275
char * tmpfile = git_pathdup ("REPLACE_EDITOBJ" );
271
276
enum object_type type ;
@@ -281,10 +286,10 @@ static int edit_and_replace(const char *object_ref, int force)
281
286
282
287
check_ref_valid (old , prev , ref , sizeof (ref ), force );
283
288
284
- export_object (old , tmpfile );
289
+ export_object (old , type , raw , tmpfile );
285
290
if (launch_editor (tmpfile , NULL , NULL ) < 0 )
286
291
die ("editing object file failed" );
287
- import_object (new , type , tmpfile );
292
+ import_object (new , type , raw , tmpfile );
288
293
289
294
free (tmpfile );
290
295
@@ -297,6 +302,7 @@ static int edit_and_replace(const char *object_ref, int force)
297
302
int cmd_replace (int argc , const char * * argv , const char * prefix )
298
303
{
299
304
int force = 0 ;
305
+ int raw = 0 ;
300
306
const char * format = NULL ;
301
307
enum {
302
308
MODE_UNSPECIFIED = 0 ,
@@ -310,6 +316,7 @@ int cmd_replace(int argc, const char **argv, const char *prefix)
310
316
OPT_CMDMODE ('d' , "delete" , & cmdmode , N_ ("delete replace refs" ), MODE_DELETE ),
311
317
OPT_CMDMODE ('e' , "edit" , & cmdmode , N_ ("edit existing object" ), MODE_EDIT ),
312
318
OPT_BOOL ('f' , "force" , & force , N_ ("replace the ref if it exists" )),
319
+ OPT_BOOL (0 , "raw" , & raw , N_ ("do not pretty-print contents for --edit" )),
313
320
OPT_STRING (0 , "format" , & format , N_ ("format" ), N_ ("use this format" )),
314
321
OPT_END ()
315
322
};
@@ -329,6 +336,10 @@ int cmd_replace(int argc, const char **argv, const char *prefix)
329
336
usage_msg_opt ("-f only makes sense when writing a replacement" ,
330
337
git_replace_usage , options );
331
338
339
+ if (raw && cmdmode != MODE_EDIT )
340
+ usage_msg_opt ("--raw only makes sense with --edit" ,
341
+ git_replace_usage , options );
342
+
332
343
switch (cmdmode ) {
333
344
case MODE_DELETE :
334
345
if (argc < 1 )
@@ -346,7 +357,7 @@ int cmd_replace(int argc, const char **argv, const char *prefix)
346
357
if (argc != 1 )
347
358
usage_msg_opt ("-e needs exactly one argument" ,
348
359
git_replace_usage , options );
349
- return edit_and_replace (argv [0 ], force );
360
+ return edit_and_replace (argv [0 ], force , raw );
350
361
351
362
case MODE_LIST :
352
363
if (argc > 1 )
0 commit comments