Fix sparse warnings
Fix warnings from 'make check'.
- These files don't include 'builtin.h' causing sparse to complain that
cmd_* isn't declared:
builtin/clone.c:364, builtin/fetch-pack.c:797,
builtin/fmt-merge-msg.c:34, builtin/hash-object.c:78,
builtin/merge-index.c:69, builtin/merge-recursive.c:22
builtin/merge-tree.c:341, builtin/mktag.c:156, builtin/notes.c:426
builtin/notes.c:822, builtin/pack-redundant.c:596,
builtin/pack-refs.c:10, builtin/patch-id.c:60, builtin/patch-id.c:149,
builtin/remote.c:1512, builtin/remote-ext.c:240,
builtin/remote-fd.c:53, builtin/reset.c:236, builtin/send-pack.c:384,
builtin/unpack-file.c:25, builtin/var.c:75
- These files have symbols which should be marked static since they're
only file scope:
submodule.c:12, diff.c:631, replace_object.c:92, submodule.c:13,
submodule.c:14, trace.c:78, transport.c:195, transport-helper.c:79,
unpack-trees.c:19, url.c:3, url.c:18, url.c:104, url.c:117, url.c:123,
url.c:129, url.c:136, thread-utils.c:21, thread-utils.c:48
- These files redeclare symbols to be different types:
builtin/index-pack.c:210, parse-options.c:564, parse-options.c:571,
usage.c:49, usage.c:58, usage.c:63, usage.c:72
- These files use a literal integer 0 when they really should use a NULL
pointer:
daemon.c:663, fast-import.c:2942, imap-send.c:1072, notes-merge.c:362
While we're in the area, clean up some unused #includes in builtin files
(mostly exec_cmd.h).
Signed-off-by: Stephen Boyd <bebarino@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2011-03-22 07:51:05 +00:00
|
|
|
#include "builtin.h"
|
2017-06-14 18:07:36 +00:00
|
|
|
#include "config.h"
|
2018-05-15 23:42:15 +00:00
|
|
|
#include "object-store.h"
|
2005-04-18 21:11:01 +00:00
|
|
|
|
2017-07-13 23:49:25 +00:00
|
|
|
static char *create_temp_file(struct object_id *oid)
|
2005-04-18 21:11:01 +00:00
|
|
|
{
|
|
|
|
static char path[50];
|
|
|
|
void *buf;
|
2007-02-26 19:55:59 +00:00
|
|
|
enum object_type type;
|
2005-04-18 21:11:01 +00:00
|
|
|
unsigned long size;
|
|
|
|
int fd;
|
|
|
|
|
sha1_file: convert read_sha1_file to struct object_id
Convert read_sha1_file to take a pointer to struct object_id and rename
it read_object_file. Do the same for read_sha1_file_extended.
Convert one use in grep.c to use the new function without any other code
change, since the pointer being passed is a void pointer that is already
initialized with a pointer to struct object_id. Update the declaration
and definitions of the modified functions, and apply the following
semantic patch to convert the remaining callers:
@@
expression E1, E2, E3;
@@
- read_sha1_file(E1.hash, E2, E3)
+ read_object_file(&E1, E2, E3)
@@
expression E1, E2, E3;
@@
- read_sha1_file(E1->hash, E2, E3)
+ read_object_file(E1, E2, E3)
@@
expression E1, E2, E3, E4;
@@
- read_sha1_file_extended(E1.hash, E2, E3, E4)
+ read_object_file_extended(&E1, E2, E3, E4)
@@
expression E1, E2, E3, E4;
@@
- read_sha1_file_extended(E1->hash, E2, E3, E4)
+ read_object_file_extended(E1, E2, E3, E4)
Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-03-12 02:27:53 +00:00
|
|
|
buf = read_object_file(oid, &type, &size);
|
2007-02-26 19:55:59 +00:00
|
|
|
if (!buf || type != OBJ_BLOB)
|
2017-07-13 23:49:25 +00:00
|
|
|
die("unable to read blob object %s", oid_to_hex(oid));
|
2005-04-18 21:11:01 +00:00
|
|
|
|
2015-09-24 21:06:08 +00:00
|
|
|
xsnprintf(path, sizeof(path), ".merge_file_XXXXXX");
|
2007-08-14 19:45:58 +00:00
|
|
|
fd = xmkstemp(path);
|
avoid "write_in_full(fd, buf, len) != len" pattern
The return value of write_in_full() is either "-1", or the
requested number of bytes[1]. If we make a partial write
before seeing an error, we still return -1, not a partial
value. This goes back to f6aa66cb95 (write_in_full: really
write in full or return error on disk full., 2007-01-11).
So checking anything except "was the return value negative"
is pointless. And there are a couple of reasons not to do
so:
1. It can do a funny signed/unsigned comparison. If your
"len" is signed (e.g., a size_t) then the compiler will
promote the "-1" to its unsigned variant.
This works out for "!= len" (unless you really were
trying to write the maximum size_t bytes), but is a
bug if you check "< len" (an example of which was fixed
recently in config.c).
We should avoid promoting the mental model that you
need to check the length at all, so that new sites are
not tempted to copy us.
2. Checking for a negative value is shorter to type,
especially when the length is an expression.
3. Linus says so. In d34cf19b89 (Clean up write_in_full()
users, 2007-01-11), right after the write_in_full()
semantics were changed, he wrote:
I really wish every "write_in_full()" user would just
check against "<0" now, but this fixes the nasty and
stupid ones.
Appeals to authority aside, this makes it clear that
writing it this way does not have an intentional
benefit. It's a historical curiosity that we never
bothered to clean up (and which was undoubtedly
cargo-culted into new sites).
So let's convert these obviously-correct cases (this
includes write_str_in_full(), which is just a wrapper for
write_in_full()).
[1] A careful reader may notice there is one way that
write_in_full() can return a different value. If we ask
write() to write N bytes and get a return value that is
_larger_ than N, we could return a larger total. But
besides the fact that this would imply a totally broken
version of write(), it would already invoke undefined
behavior. Our internal remaining counter is an unsigned
size_t, which means that subtracting too many byte will
wrap it around to a very large number. So we'll instantly
begin reading off the end of the buffer, trying to write
gigabytes (or petabytes) of data.
Signed-off-by: Jeff King <peff@peff.net>
Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2017-09-13 17:16:03 +00:00
|
|
|
if (write_in_full(fd, buf, size) < 0)
|
2009-06-27 15:58:47 +00:00
|
|
|
die_errno("unable to write temp-file");
|
2005-04-18 21:11:01 +00:00
|
|
|
close(fd);
|
2022-11-08 18:17:43 +00:00
|
|
|
free(buf);
|
2005-04-18 21:11:01 +00:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2010-01-22 15:38:03 +00:00
|
|
|
int cmd_unpack_file(int argc, const char **argv, const char *prefix)
|
2005-04-18 21:11:01 +00:00
|
|
|
{
|
2017-07-13 23:49:25 +00:00
|
|
|
struct object_id oid;
|
2005-04-18 21:11:01 +00:00
|
|
|
|
2009-11-09 15:04:56 +00:00
|
|
|
if (argc != 2 || !strcmp(argv[1], "-h"))
|
2022-10-13 15:39:10 +00:00
|
|
|
usage("git unpack-file <blob>");
|
2017-07-13 23:49:25 +00:00
|
|
|
if (get_oid(argv[1], &oid))
|
2006-05-08 21:43:38 +00:00
|
|
|
die("Not a valid object name %s", argv[1]);
|
2005-04-18 21:11:01 +00:00
|
|
|
|
2008-05-14 17:46:53 +00:00
|
|
|
git_config(git_default_config, NULL);
|
2005-11-26 08:50:02 +00:00
|
|
|
|
2017-07-13 23:49:25 +00:00
|
|
|
puts(create_temp_file(&oid));
|
2005-04-18 21:11:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|