1
1
#include "builtin.h"
2
2
#include "abspath.h"
3
+ #include "hex.h"
4
+ #include "object-name.h"
5
+ #include "object-store.h"
3
6
#include "config.h"
4
7
#include "gettext.h"
5
8
#include "setup.h"
@@ -31,10 +34,11 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
31
34
mmfile_t mmfs [3 ] = { 0 };
32
35
mmbuffer_t result = { 0 };
33
36
xmparam_t xmp = { 0 };
34
- int ret = 0 , i = 0 , to_stdout = 0 ;
37
+ int ret = 0 , i = 0 , to_stdout = 0 , object_id = 0 ;
35
38
int quiet = 0 ;
36
39
struct option options [] = {
37
40
OPT_BOOL ('p' , "stdout" , & to_stdout , N_ ("send results to standard output" )),
41
+ OPT_BOOL (0 , "object-id" , & object_id , N_ ("use object IDs instead of filenames" )),
38
42
OPT_SET_INT (0 , "diff3" , & xmp .style , N_ ("use a diff3 based merge" ), XDL_MERGE_DIFF3 ),
39
43
OPT_SET_INT (0 , "zdiff3" , & xmp .style , N_ ("use a zealous diff3 based merge" ),
40
44
XDL_MERGE_ZEALOUS_DIFF3 ),
@@ -71,21 +75,35 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
71
75
return error_errno ("failed to redirect stderr to /dev/null" );
72
76
}
73
77
78
+ if (object_id )
79
+ setup_git_directory ();
80
+
74
81
for (i = 0 ; i < 3 ; i ++ ) {
75
82
char * fname ;
83
+ struct object_id oid ;
76
84
mmfile_t * mmf = mmfs + i ;
77
85
78
86
if (!names [i ])
79
87
names [i ] = argv [i ];
80
88
81
89
fname = prefix_filename (prefix , argv [i ]);
82
90
83
- if (read_mmfile (mmf , fname ))
91
+ if (object_id ) {
92
+ if (repo_get_oid (the_repository , argv [i ], & oid ))
93
+ ret = error (_ ("object '%s' does not exist" ),
94
+ argv [i ]);
95
+ else if (!oideq (& oid , the_hash_algo -> empty_blob ))
96
+ read_mmblob (mmf , & oid );
97
+ else
98
+ read_mmfile (mmf , "/dev/null" );
99
+ } else if (read_mmfile (mmf , fname )) {
84
100
ret = -1 ;
85
- else if (mmf -> size > MAX_XDIFF_SIZE ||
86
- buffer_is_binary (mmf -> ptr , mmf -> size ))
101
+ }
102
+ if (ret != -1 && (mmf -> size > MAX_XDIFF_SIZE ||
103
+ buffer_is_binary (mmf -> ptr , mmf -> size ))) {
87
104
ret = error ("Cannot merge binary files: %s" ,
88
105
argv [i ]);
106
+ }
89
107
90
108
free (fname );
91
109
if (ret )
@@ -99,20 +117,32 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
99
117
ret = xdl_merge (mmfs + 1 , mmfs + 0 , mmfs + 2 , & xmp , & result );
100
118
101
119
if (ret >= 0 ) {
102
- const char * filename = argv [0 ];
103
- char * fpath = prefix_filename (prefix , argv [0 ]);
104
- FILE * f = to_stdout ? stdout : fopen (fpath , "wb" );
105
-
106
- if (!f )
107
- ret = error_errno ("Could not open %s for writing" ,
108
- filename );
109
- else if (result .size &&
110
- fwrite (result .ptr , result .size , 1 , f ) != 1 )
111
- ret = error_errno ("Could not write to %s" , filename );
112
- else if (fclose (f ))
113
- ret = error_errno ("Could not close %s" , filename );
120
+ if (object_id && !to_stdout ) {
121
+ struct object_id oid ;
122
+ if (result .size ) {
123
+ if (write_object_file (result .ptr , result .size , OBJ_BLOB , & oid ) < 0 )
124
+ ret = error (_ ("Could not write object file" ));
125
+ } else {
126
+ oidcpy (& oid , the_hash_algo -> empty_blob );
127
+ }
128
+ if (ret >= 0 )
129
+ printf ("%s\n" , oid_to_hex (& oid ));
130
+ } else {
131
+ const char * filename = argv [0 ];
132
+ char * fpath = prefix_filename (prefix , argv [0 ]);
133
+ FILE * f = to_stdout ? stdout : fopen (fpath , "wb" );
134
+
135
+ if (!f )
136
+ ret = error_errno ("Could not open %s for writing" ,
137
+ filename );
138
+ else if (result .size &&
139
+ fwrite (result .ptr , result .size , 1 , f ) != 1 )
140
+ ret = error_errno ("Could not write to %s" , filename );
141
+ else if (fclose (f ))
142
+ ret = error_errno ("Could not close %s" , filename );
143
+ free (fpath );
144
+ }
114
145
free (result .ptr );
115
- free (fpath );
116
146
}
117
147
118
148
if (ret > 127 )
0 commit comments