/* * Determine whether we can simply reuse the file in the worktree. */ static int use_wt_file(const char *workdir, const char *name, struct object_id *oid) { struct strbuf buf = STRBUF_INIT; struct stat st; int use = 0; strbuf_addstr(&buf, workdir); add_path(&buf, buf.len, name); if (!lstat(buf.buf, &st) && !S_ISLNK(st.st_mode)) { struct object_id wt_oid; int fd = open(buf.buf, O_RDONLY); if (fd >= 0 && !index_fd(wt_oid.hash, fd, &st, OBJ_BLOB, name, 0)) { if (is_null_oid(oid)) { oidcpy(oid, &wt_oid); use = 1; } else if (!oidcmp(oid, &wt_oid)) use = 1; } } strbuf_release(&buf); return use; }
static void hash_fd(int fd, const char *type, int write_object, const char *path) { struct stat st; unsigned char sha1[20]; if (fstat(fd, &st) < 0 || index_fd(sha1, fd, &st, write_object, type_from_string(type), path)) die(write_object ? "Unable to add %s to database" : "Unable to hash %s", path); printf("%s\n", sha1_to_hex(sha1)); maybe_flush_or_die(stdout, "hash to stdout"); }
static void hash_fd(int fd, const char *type, const char *path, unsigned flags, int literally) { struct stat st; unsigned char sha1[20]; if (fstat(fd, &st) < 0 || (literally ? hash_literally(sha1, fd, type, flags) : index_fd(sha1, fd, &st, type_from_string(type), path, flags))) die((flags & HASH_WRITE_OBJECT) ? "Unable to add %s to database" : "Unable to hash %s", path); printf("%s\n", sha1_to_hex(sha1)); maybe_flush_or_die(stdout, "hash to stdout"); }
static void hash_fd(int fd, const char *type, const char *path, unsigned flags, int literally) { struct stat st; struct object_id oid; if (fstat(fd, &st) < 0 || (literally ? hash_literally(&oid, fd, type, flags) : index_fd(the_repository->index, &oid, fd, &st, type_from_string(type), path, flags))) die((flags & HASH_WRITE_OBJECT) ? "Unable to add %s to database" : "Unable to hash %s", path); printf("%s\n", oid_to_hex(&oid)); maybe_flush_or_die(stdout, "hash to stdout"); }
static int add_file_to_cache(char *path) { int size, namelen; struct cache_entry *ce; struct stat st; int fd; fd = open(path, O_RDONLY); if (fd < 0) { if (errno == ENOENT) return remove_file_from_cache(path); return -1; } if (fstat(fd, &st) < 0) { close(fd); return -1; } namelen = strlen(path); size = cache_entry_size(namelen); ce = malloc(size); memset(ce, 0, size); memcpy(ce->name, path, namelen); ce->ctime.sec = st.st_ctime; ce->ctime.nsec = st.st_ctim.tv_nsec; ce->mtime.sec = st.st_mtime; ce->mtime.nsec = st.st_mtim.tv_nsec; ce->st_dev = st.st_dev; ce->st_ino = st.st_ino; ce->st_mode = st.st_mode; ce->st_uid = st.st_uid; ce->st_gid = st.st_gid; ce->st_size = st.st_size; ce->namelen = namelen; if (index_fd(path, namelen, ce, fd, &st) < 0) return -1; return add_cache_entry(ce); }
/* * Read a previously-exported (and possibly edited) object back from "filename", * interpreting it as "type", and writing the result to the object database. * The sha1 of the written object is returned via sha1. */ static int import_object(struct object_id *oid, enum object_type type, int raw, const char *filename) { int fd; fd = open(filename, O_RDONLY); if (fd < 0) return error_errno("unable to open %s for reading", filename); if (!raw && type == OBJ_TREE) { const char *argv[] = { "mktree", NULL }; struct child_process cmd = CHILD_PROCESS_INIT; struct strbuf result = STRBUF_INIT; cmd.argv = argv; cmd.git_cmd = 1; cmd.in = fd; cmd.out = -1; if (start_command(&cmd)) { close(fd); return error("unable to spawn mktree"); } if (strbuf_read(&result, cmd.out, 41) < 0) { error_errno("unable to read from mktree"); close(fd); close(cmd.out); return -1; } close(cmd.out); if (finish_command(&cmd)) { strbuf_release(&result); return error("mktree reported failure"); } if (get_oid_hex(result.buf, oid) < 0) { strbuf_release(&result); return error("mktree did not return an object name"); } strbuf_release(&result); } else { struct stat st; int flags = HASH_FORMAT_CHECK | HASH_WRITE_OBJECT; if (fstat(fd, &st) < 0) { error_errno("unable to fstat %s", filename); close(fd); return -1; } if (index_fd(oid, fd, &st, type, NULL, flags) < 0) return error("unable to write object to database"); /* index_fd close()s fd for us */ } /* * No need to close(fd) here; both run-command and index-fd * will have done it for us. */ return 0; }