5: 58da8e9555 ! 4: dc4674d222 sequencer: make file exists check more efficient
@@ Commit message
check `errno` to see if the file doesn't exist.
## sequencer.c ##
-@@ sequencer.c: static int write_message(const void *buf, size_t len, const char *filename,
- static int read_oneliner(struct strbuf *buf,
- const char *path, int skip_if_empty)
+@@ sequencer.c: static int read_oneliner(struct strbuf *buf,
{
--
+ int orig_len = buf->len;
+
- if (!file_exists(path))
- return 0;
-
- strbuf_reset(buf);
if (strbuf_read_file(buf, path, 0) < 0) {
- warning_errno(_("could not read '%s'"), path);
+ if (errno != ENOENT && errno != ENOTDIR)
6: 6ead240957 ! 5: f30ad07823 sequencer: make read_oneliner() accept flags
@@ sequencer.c: static int write_message(const void *buf, size_t len, const char *f
+#define READ_ONELINER_SKIP_IF_EMPTY (1 << 0)
+
/*
- * Resets a strbuf then reads a file that was presumably written by a shell
- * script, i.e. with an end-of-line marker that needs to be stripped.
+ * Reads a file that was presumably written by a shell script, i.e. with an
+ * end-of-line marker that needs to be stripped.
@@ sequencer.c: static int write_message(const void *buf, size_t len, const char *filename,
* Returns 1 if the file was read, 0 if it could not be read or does not exist.
*/
@@ sequencer.c: static int write_message(const void *buf, size_t len, const char *f
- const char *path, int skip_if_empty)
+ const char *path, unsigned flags)
{
- strbuf_reset(buf);
- if (strbuf_read_file(buf, path, 0) < 0) {
+ int orig_len = buf->len;
+
@@ sequencer.c: static int read_oneliner(struct strbuf *buf,
+ buf->buf[buf->len] = '\0';
+ }
- strbuf_trim(buf);
-
-- if (skip_if_empty && !buf->len)
-+ if ((flags & READ_ONELINER_SKIP_IF_EMPTY) && !buf->len)
+- if (skip_if_empty && buf->len == orig_len)
++ if ((flags & READ_ONELINER_SKIP_IF_EMPTY) && buf->len == orig_len)
return 0;
return 1;
@@ sequencer.c: static int read_populate_opts(struct replay_opts *opts)
strbuf_reset(&buf);
else {
@@ sequencer.c: static int read_populate_opts(struct replay_opts *opts)
- }
+ strbuf_reset(&buf);
}
- if (read_oneliner(&buf, rebase_path_allow_rerere_autoupdate(), 1)) {
@@ sequencer.c: static int read_populate_opts(struct replay_opts *opts)
opts->allow_rerere_auto = RERERE_AUTOUPDATE;
else if (!strcmp(buf.buf, "--no-rerere-autoupdate"))
@@ sequencer.c: static int read_populate_opts(struct replay_opts *opts)
- read_strategy_opts(opts, &buf);
+ strbuf_reset(&buf);
if (read_oneliner(&opts->current_fixups,
- rebase_path_current_fixups(), 1)) {
7: 2e7922b259 ! 6: 8c504427e3 sequencer: configurably warn on non-existent files
@@ Commit message
read_oneliner() will want the ability to output warnings in the event
that the `path` doesn't exist. Introduce the
`READ_ONELINER_WARN_MISSING` flag which, if active, would issue a
- warning when a file doesn't exist by skipping the `!file_exists()` check
- and letting `strbuf_read_file()` handle that case.
+ warning when a file doesn't exist by always executing warning_errno()
+ in the case where strbuf_read_file() fails.
## sequencer.c ##
@@ sequencer.c: static int write_message(const void *buf, size_t len, const char *filename,
@@ sequencer.c: static int write_message(const void *buf, size_t len, const char *f
+#define READ_ONELINER_WARN_MISSING (1 << 1)
/*
- * Resets a strbuf then reads a file that was presumably written by a shell
+ * Reads a file that was presumably written by a shell script, i.e. with an
@@ sequencer.c: static int read_oneliner(struct strbuf *buf,
- {
- strbuf_reset(buf);
+ int orig_len = buf->len;
+
if (strbuf_read_file(buf, path, 0) < 0) {
- if (errno != ENOENT && errno != ENOTDIR)
+ if ((flags & READ_ONELINER_WARN_MISSING) ||
-+ (errno != ENOENT && errno != ENOTDIR))
++ (errno != ENOENT && errno != ENOTDIR))
warning_errno(_("could not read '%s'"), path);
return 0;
}
8: 6c3c37994b ! 7: 50e93770e7 sequencer: make read_oneliner() extern
@@ sequencer.c: static int write_message(const void *buf, size_t len, const char *f
-#define READ_ONELINER_WARN_MISSING (1 << 1)
-
-/*
-- * Resets a strbuf then reads a file that was presumably written by a shell
-- * script, i.e. with an end-of-line marker that needs to be stripped.
+- * Reads a file that was presumably written by a shell script, i.e. with an
+- * end-of-line marker that needs to be stripped.
- *
- * Note that only the last end-of-line marker is stripped, consistent with the
- * behavior of "$(cat path)" in a shell script.
@@ sequencer.c: static int write_message(const void *buf, size_t len, const char *f
+int read_oneliner(struct strbuf *buf,
const char *path, unsigned flags)
{
- strbuf_reset(buf);
+ int orig_len = buf->len;
## sequencer.h ##
@@ sequencer.h: void print_commit_summary(struct repository *repo,
@@ sequencer.h: void print_commit_summary(struct repository *repo,
+#define READ_ONELINER_WARN_MISSING (1 << 1)
+
+/*
-+ * Resets a strbuf then reads a file that was presumably written by a shell
-+ * script, i.e. with an end-of-line marker that needs to be stripped.
++ * Reads a file that was presumably written by a shell script, i.e. with an
++ * end-of-line marker that needs to be stripped.
+ *
+ * Note that only the last end-of-line marker is stripped, consistent with the
+ * behavior of "$(cat path)" in a shell script.
9: 689f34a2a5 ! 8: 0cc279fc14 rebase: use read_oneliner()
@@ Commit message
Since in sequencer.c, read_one() basically duplicates the functionality
of read_oneliner(), reduce code duplication by replacing read_one() with
- read_oneliner(). Also, delete strbuf_reset() calls prior to
- read_oneliner() as read_oneliner() already resets the strbuf.
+ read_oneliner().
- This was mostly done with the following Coccinelle script
+ This was done with the following Coccinelle script
@@
expression a, b;
@@ Commit message
- read_one(a, b)
+ !read_oneliner(b, a, READ_ONELINER_WARN_NON_EXISTENCE)
- Long lines were broken up and strbuf_reset()s were deleted manually.
+ and long lines were manually broken up.
## builtin/rebase.c ##
@@ builtin/rebase.c: static const char *state_dir_path(const char *filename, struct rebase_options *o
@@ builtin/rebase.c: static int read_basic_state(struct rebase_options *opts)
opts->head_name = starts_with(head_name.buf, "refs/") ?
xstrdup(head_name.buf) : NULL;
@@ builtin/rebase.c: static int read_basic_state(struct rebase_options *opts)
- * head. Fall back to reading from head to cover for the case that the
- * user upgraded git with an ongoing interactive rebase.
*/
-- strbuf_reset(&buf);
+ strbuf_reset(&buf);
if (file_exists(state_dir_path("orig-head", opts))) {
- if (read_one(state_dir_path("orig-head", opts), &buf))
+ if (!read_oneliner(&buf, state_dir_path("orig-head", opts),
@@ builtin/rebase.c: static int read_basic_state(struct rebase_options *opts)
if (get_oid(buf.buf, &opts->orig_head))
return error(_("invalid orig-head: '%s'"), buf.buf);
@@ builtin/rebase.c: static int read_basic_state(struct rebase_options *opts)
- }
if (file_exists(state_dir_path("allow_rerere_autoupdate", opts))) {
-- strbuf_reset(&buf);
+ strbuf_reset(&buf);
- if (read_one(state_dir_path("allow_rerere_autoupdate", opts),
- &buf))
+ if (!read_oneliner(&buf, state_dir_path("allow_rerere_autoupdate", opts),
@@ builtin/rebase.c: static int read_basic_state(struct rebase_options *opts)
if (!strcmp(buf.buf, "--rerere-autoupdate"))
opts->allow_rerere_autoupdate = RERERE_AUTOUPDATE;
@@ builtin/rebase.c: static int read_basic_state(struct rebase_options *opts)
- }
if (file_exists(state_dir_path("gpg_sign_opt", opts))) {
-- strbuf_reset(&buf);
+ strbuf_reset(&buf);
- if (read_one(state_dir_path("gpg_sign_opt", opts),
- &buf))
+ if (!read_oneliner(&buf, state_dir_path("gpg_sign_opt", opts),
@@ builtin/rebase.c: static int read_basic_state(struct rebase_options *opts)
return -1;
free(opts->gpg_sign_opt);
opts->gpg_sign_opt = xstrdup(buf.buf);
- }
+@@ builtin/rebase.c: static int read_basic_state(struct rebase_options *opts)
if (file_exists(state_dir_path("strategy", opts))) {
-- strbuf_reset(&buf);
+ strbuf_reset(&buf);
- if (read_one(state_dir_path("strategy", opts), &buf))
+ if (!read_oneliner(&buf, state_dir_path("strategy", opts),
+ READ_ONELINER_WARN_MISSING))
return -1;
free(opts->strategy);
opts->strategy = xstrdup(buf.buf);
- }
+@@ builtin/rebase.c: static int read_basic_state(struct rebase_options *opts)
if (file_exists(state_dir_path("strategy_opts", opts))) {
-- strbuf_reset(&buf);
+ strbuf_reset(&buf);
- if (read_one(state_dir_path("strategy_opts", opts), &buf))
+ if (!read_oneliner(&buf, state_dir_path("strategy_opts", opts),
+ READ_ONELINER_WARN_MISSING))