On Tue, Oct 24, 2023 at 3:58 PM brian m. carlson <sandals@xxxxxxxxxxxxxxxxxxxx> wrote: > git merge-file knows how to merge files on the file system already. It > would be helpful, however, to allow it to also merge single blobs. > Teach it an `--object-id` option which means that its arguments are > object IDs and not files to allow it to do so. > > Since we obviously won't be writing the data to the first argument, > either write to the object store and print the object ID, or honor the > -p argument and print it to standard out. > > We handle the empty blob specially since read_mmblob doesn't read it > directly, instead throwing an error, and otherwise users cannot specify > an empty ancestor. > > Signed-off-by: brian m. carlson <bk2204@xxxxxxxxxx> > --- > diff --git a/builtin/merge-file.c b/builtin/merge-file.c > @@ -99,20 +116,29 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix) > if (ret >= 0) { > - const char *filename = argv[0]; > - char *fpath = prefix_filename(prefix, argv[0]); > - FILE *f = to_stdout ? stdout : fopen(fpath, "wb"); > + if (object_id && !to_stdout) { > + struct object_id oid; > + if (result.size) > + write_object_file(result.ptr, result.size, OBJ_BLOB, &oid); Should this be caring about errors by checking the return value of write_object_file()? > + } else { > + const char *filename = argv[0]; > + char *fpath = prefix_filename(prefix, argv[0]); > + FILE *f = to_stdout ? stdout : fopen(fpath, "wb"); > + if (!f) > + ret = error_errno("Could not open %s for writing", > + filename); > + else if (result.size && > + fwrite(result.ptr, result.size, 1, f) != 1) > + ret = error_errno("Could not write to %s", filename); > + else if (fclose(f)) > + ret = error_errno("Could not close %s", filename); > + free(fpath); The non-"object-id" case cares about errors.