From: Junio C Hamano Date: Thu, 28 Sep 2017 05:47:56 +0000 (+0900) Subject: Merge branch 'rs/resolve-ref-optional-result' X-Git-Tag: v2.15.0-rc0~41 X-Git-Url: https://git.lorimer.id.au/gitweb.git/diff_plain/73ecdc606eedbfd98ec66d50d44b3374425fd13b?ds=inline;hp=-c Merge branch 'rs/resolve-ref-optional-result' Code clean-up. * rs/resolve-ref-optional-result: refs: pass NULL to resolve_ref_unsafe() if hash is not needed refs: pass NULL to refs_resolve_ref_unsafe() if hash is not needed refs: make sha1 output parameter of refs_resolve_ref_unsafe() optional --- 73ecdc606eedbfd98ec66d50d44b3374425fd13b diff --combined builtin/receive-pack.c index dd06b3fb4f,65d89078ab..29a0f3b75f --- a/builtin/receive-pack.c +++ b/builtin/receive-pack.c @@@ -743,7 -743,7 +743,7 @@@ static int run_and_feed_hook(const cha size_t n; if (feed(feed_state, &buf, &n)) break; - if (write_in_full(proc.in, buf, n) != n) + if (write_in_full(proc.in, buf, n) < 0) break; } close(proc.in); @@@ -1207,11 -1207,10 +1207,10 @@@ static void check_aliased_update(struc const char *dst_name; struct string_list_item *item; struct command *dst_cmd; - unsigned char sha1[GIT_MAX_RAWSZ]; int flag; strbuf_addf(&buf, "%s%s", get_git_namespace(), cmd->ref_name); - dst_name = resolve_ref_unsafe(buf.buf, 0, sha1, &flag); + dst_name = resolve_ref_unsafe(buf.buf, 0, NULL, &flag); strbuf_release(&buf); if (!(flag & REF_ISSYMREF)) diff --combined builtin/submodule--helper.c index f6a5e1af5d,ea49710893..06ed02f994 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@@ -17,9 -17,8 +17,8 @@@ static char *get_default_remote(void) { char *dest = NULL, *ret; - unsigned char sha1[20]; struct strbuf sb = STRBUF_INIT; - const char *refname = resolve_ref_unsafe("HEAD", 0, sha1, NULL); + const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL); if (!refname) die(_("No such ref: %s"), "HEAD"); @@@ -1089,8 -1088,7 +1088,7 @@@ static const char *remote_submodule_bra return "master"; if (!strcmp(branch, ".")) { - unsigned char sha1[20]; - const char *refname = resolve_ref_unsafe("HEAD", 0, sha1, NULL); + const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL); if (!refname) die(_("No such ref: %s"), "HEAD"); @@@ -1189,7 -1187,6 +1187,7 @@@ static int push_check(int argc, const c break; die("HEAD does not match the named branch in the superproject"); } + /* fallthrough */ default: die("src refspec '%s' must name a ref", rs->src); diff --combined http-backend.c index e51c7805c8,b8ce960a35..f3dc218b2a --- a/http-backend.c +++ b/http-backend.c @@@ -358,7 -358,7 +358,7 @@@ static void inflate_request(const char die("zlib error inflating request, result %d", ret); n = stream.total_out - cnt; - if (write_in_full(out, out_buf, n) != n) + if (write_in_full(out, out_buf, n) < 0) die("%s aborted reading request", prog_name); cnt += n; @@@ -379,7 -379,7 +379,7 @@@ static void copy_request(const char *pr ssize_t n = read_request(0, &buf); if (n < 0) die_errno("error reading request body"); - if (write_in_full(out, buf, n) != n) + if (write_in_full(out, buf, n) < 0) die("%s aborted reading request", prog_name); close(out); free(buf); @@@ -486,10 -486,9 +486,9 @@@ static int show_head_ref(const char *re struct strbuf *buf = cb_data; if (flag & REF_ISSYMREF) { - struct object_id unused; const char *target = resolve_ref_unsafe(refname, RESOLVE_REF_READING, - unused.hash, NULL); + NULL, NULL); if (target) strbuf_addf(buf, "ref: %s\n", strip_namespace(target)); diff --combined refs.c index 83f0e68785,97e33fdc7b..6042645c40 --- a/refs.c +++ b/refs.c @@@ -239,8 -239,7 +239,7 @@@ int read_ref(const char *refname, unsig int ref_exists(const char *refname) { - unsigned char sha1[20]; - return !!resolve_ref_unsafe(refname, RESOLVE_REF_READING, sha1, NULL); + return !!resolve_ref_unsafe(refname, RESOLVE_REF_READING, NULL, NULL); } static int filter_refs(const char *refname, const struct object_id *oid, @@@ -286,12 -285,11 +285,11 @@@ static int warn_if_dangling_symref(cons { struct warn_if_dangling_data *d = cb_data; const char *resolves_to; - struct object_id junk; if (!(flags & REF_ISSYMREF)) return 0; - resolves_to = resolve_ref_unsafe(refname, 0, junk.hash, NULL); + resolves_to = resolve_ref_unsafe(refname, 0, NULL, NULL); if (!resolves_to || (d->refname ? strcmp(resolves_to, d->refname) @@@ -609,7 -607,7 +607,7 @@@ static int write_pseudoref(const char * } } - if (write_in_full(fd, buf.buf, buf.len) != buf.len) { + if (write_in_full(fd, buf.buf, buf.len) < 0) { strbuf_addf(err, "could not write to '%s'", filename); rollback_lock_file(&lock); goto done; @@@ -939,8 -937,6 +937,8 @@@ int ref_transaction_update(struct ref_t return -1; } + flags &= REF_TRANSACTION_UPDATE_ALLOWED_FLAGS; + flags |= (new_sha1 ? REF_HAVE_NEW : 0) | (old_sha1 ? REF_HAVE_OLD : 0); ref_transaction_add_update(transaction, refname, flags, @@@ -1359,7 -1355,7 +1357,7 @@@ int for_each_replace_ref(each_ref_fn fn return do_for_each_ref(get_main_ref_store(), git_replace_ref_base, fn, strlen(git_replace_ref_base), - 0, cb_data); + DO_FOR_EACH_INCLUDE_BROKEN, cb_data); } int for_each_namespaced_ref(each_ref_fn fn, void *cb_data) @@@ -1398,9 -1394,12 +1396,12 @@@ const char *refs_resolve_ref_unsafe(str unsigned char *sha1, int *flags) { static struct strbuf sb_refname = STRBUF_INIT; + struct object_id unused_oid; int unused_flags; int symref_count; + if (!sha1) + sha1 = unused_oid.hash; if (!flags) flags = &unused_flags; diff --combined refs.h index ae33a5f74d,996d4ea54d..e1c5803f9a --- a/refs.h +++ b/refs.h @@@ -10,10 -10,11 +10,11 @@@ struct worktree /* * Resolve a reference, recursively following symbolic refererences. * - * Store the referred-to object's name in sha1 and return the name of - * the non-symbolic reference that ultimately pointed at it. The - * return value, if not NULL, is a pointer into either a static buffer - * or the input ref. + * Return the name of the non-symbolic reference that ultimately pointed + * at the resolved object name. The return value, if not NULL, is a + * pointer into either a static buffer or the input ref. + * + * If sha1 is non-NULL, store the referred-to object's name in it. * * If the reference cannot be resolved to an object, the behavior * depends on the RESOLVE_REF_READING flag: @@@ -344,14 -345,6 +345,14 @@@ int refs_pack_refs(struct ref_store *re #define REF_NODEREF 0x01 #define REF_FORCE_CREATE_REFLOG 0x40 +/* + * Flags that can be passed in to ref_transaction_update + */ +#define REF_TRANSACTION_UPDATE_ALLOWED_FLAGS \ + REF_ISPRUNING | \ + REF_FORCE_CREATE_REFLOG | \ + REF_NODEREF + /* * Setup reflog before using. Fill in err and return -1 on failure. */ diff --combined refs/files-backend.c index dac33628b3,a3134d23ab..fec77744b4 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@@ -1549,7 -1549,7 +1549,7 @@@ static int log_ref_write_fd(int fd, con written = len <= maxlen ? write_in_full(fd, logrec, len) : -1; free(logrec); - if (written != len) + if (written < 0) return -1; return 0; @@@ -1628,8 -1628,8 +1628,8 @@@ static int write_ref_to_lockfile(struc return -1; } fd = get_lock_file_fd(&lock->lk); - if (write_in_full(fd, oid_to_hex(oid), GIT_SHA1_HEXSZ) != GIT_SHA1_HEXSZ || - write_in_full(fd, &term, 1) != 1 || + if (write_in_full(fd, oid_to_hex(oid), GIT_SHA1_HEXSZ) < 0 || + write_in_full(fd, &term, 1) < 0 || close_ref_gently(lock) < 0) { strbuf_addf(err, "couldn't write '%s'", get_lock_file_path(&lock->lk)); @@@ -1676,13 -1676,12 +1676,12 @@@ static int commit_ref_update(struct fil * check with HEAD only which should cover 99% of all usage * scenarios (even 100% of the default ones). */ - struct object_id head_oid; int head_flag; const char *head_ref; head_ref = refs_resolve_ref_unsafe(&refs->base, "HEAD", RESOLVE_REF_READING, - head_oid.hash, &head_flag); + NULL, &head_flag); if (head_ref && (head_flag & REF_ISSYMREF) && !strcmp(head_ref, lock->ref_name)) { struct strbuf log_err = STRBUF_INIT; @@@ -3006,8 -3005,8 +3005,8 @@@ static int files_reflog_expire(struct r rollback_lock_file(&reflog_lock); } else if (update && (write_in_full(get_lock_file_fd(&lock->lk), - oid_to_hex(&cb.last_kept_oid), GIT_SHA1_HEXSZ) != GIT_SHA1_HEXSZ || - write_str_in_full(get_lock_file_fd(&lock->lk), "\n") != 1 || + oid_to_hex(&cb.last_kept_oid), GIT_SHA1_HEXSZ) < 0 || + write_str_in_full(get_lock_file_fd(&lock->lk), "\n") < 1 || close_ref_gently(lock) < 0)) { status |= error("couldn't write %s", get_lock_file_path(&lock->lk)); diff --combined revision.c index 1520f69d93,9582a727ca..613168d397 --- a/revision.c +++ b/revision.c @@@ -21,7 -21,6 +21,7 @@@ #include "bisect.h" #include "packfile.h" #include "worktree.h" +#include "argv-array.h" volatile show_early_output_fn_t show_early_output; @@@ -1673,15 -1672,31 +1673,15 @@@ int handle_revision_arg(const char *arg return 0; } -struct cmdline_pathspec { - int alloc; - int nr; - const char **path; -}; - -static void append_prune_data(struct cmdline_pathspec *prune, const char **av) -{ - while (*av) { - ALLOC_GROW(prune->path, prune->nr + 1, prune->alloc); - prune->path[prune->nr++] = *(av++); - } -} - static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb, - struct cmdline_pathspec *prune) + struct argv_array *prune) { - while (strbuf_getline(sb, stdin) != EOF) { - ALLOC_GROW(prune->path, prune->nr + 1, prune->alloc); - prune->path[prune->nr++] = xstrdup(sb->buf); - } + while (strbuf_getline(sb, stdin) != EOF) + argv_array_push(prune, sb->buf); } static void read_revisions_from_stdin(struct rev_info *revs, - struct cmdline_pathspec *prune) + struct argv_array *prune) { struct strbuf sb; int seen_dashdash = 0; @@@ -2248,11 -2263,10 +2248,10 @@@ static int handle_revision_pseudo_opt(c static void NORETURN diagnose_missing_default(const char *def) { - unsigned char sha1[20]; int flags; const char *refname; - refname = resolve_ref_unsafe(def, 0, sha1, &flags); + refname = resolve_ref_unsafe(def, 0, NULL, &flags); if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN)) die(_("your current branch appears to be broken")); @@@ -2271,9 -2285,10 +2270,9 @@@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt) { int i, flags, left, seen_dashdash, read_from_stdin, got_rev_arg = 0, revarg_opt; - struct cmdline_pathspec prune_data; + struct argv_array prune_data = ARGV_ARRAY_INIT; const char *submodule = NULL; - memset(&prune_data, 0, sizeof(prune_data)); if (opt) submodule = opt->submodule; @@@ -2289,7 -2304,7 +2288,7 @@@ argv[i] = NULL; argc = i; if (argv[i + 1]) - append_prune_data(&prune_data, argv + i + 1); + argv_array_pushv(&prune_data, argv + i + 1); seen_dashdash = 1; break; } @@@ -2350,14 -2365,14 +2349,14 @@@ for (j = i; j < argc; j++) verify_filename(revs->prefix, argv[j], j == i); - append_prune_data(&prune_data, argv + i); + argv_array_pushv(&prune_data, argv + i); break; } else got_rev_arg = 1; } - if (prune_data.nr) { + if (prune_data.argc) { /* * If we need to introduce the magic "a lone ':' means no * pathspec whatsoever", here is the place to do so. @@@ -2372,10 -2387,11 +2371,10 @@@ * call init_pathspec() to set revs->prune_data here. * } */ - ALLOC_GROW(prune_data.path, prune_data.nr + 1, prune_data.alloc); - prune_data.path[prune_data.nr++] = NULL; parse_pathspec(&revs->prune_data, 0, 0, - revs->prefix, prune_data.path); + revs->prefix, prune_data.argv); } + argv_array_clear(&prune_data); if (revs->def == NULL) revs->def = opt ? opt->def : NULL;