From: Paul Tan <pyokagan@gmail.com>
To: Stefan Beller <sbeller@google.com>
Cc: Git List <git@vger.kernel.org>,
Junio C Hamano <gitster@pobox.com>,
Johannes Schindelin <johannes.schindelin@gmx.de>,
Duy Nguyen <pclouds@gmail.com>,
Sam Halliday <sam.halliday@gmail.com>
Subject: Re: [PATCH/RFC/GSoC 05/17] rebase-options: implement rebase_options_load() and rebase_options_save()
Date: Wed, 16 Mar 2016 20:04:07 +0800 [thread overview]
Message-ID: <CACRoPnS7WWWVay9hAjXYgyeB=1A1gfARerKJe25APa-6u5cGaA@mail.gmail.com> (raw)
In-Reply-To: <CAGZ79kYeYzi=J=dY27FqXp72BRe-Vmm4MR5Q6dFTMUP9CxYZcg@mail.gmail.com>
Hi Stefan,
On Tue, Mar 15, 2016 at 4:30 AM, Stefan Beller <sbeller@google.com> wrote:
> On Sat, Mar 12, 2016 at 2:46 AM, Paul Tan <pyokagan@gmail.com> wrote:
>> These functions can be used for loading and saving common rebase options
>> into a state directory.
>>
>> Signed-off-by: Paul Tan <pyokagan@gmail.com>
>> ---
>> rebase-common.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>> rebase-common.h | 4 ++++
>> 2 files changed, 73 insertions(+)
>>
>> diff --git a/rebase-common.c b/rebase-common.c
>> index 5a49ac4..1835f08 100644
>> --- a/rebase-common.c
>> +++ b/rebase-common.c
>> @@ -26,3 +26,72 @@ void rebase_options_swap(struct rebase_options *dst, struct rebase_options *src)
>> *dst = *src;
>> *src = tmp;
>> }
>> +
>> +static int state_file_exists(const char *dir, const char *file)
>> +{
>> + return file_exists(mkpath("%s/%s", dir, file));
>> +}
>
> How is this specific to the state file? All it does is create the
> leading directory
> if it doesn't exist? (So I'd expect file_exists(concat(dir, file)) to
> have the same
> result without actually creating the directory if it doesn't exist as
> a side effect?
I don't quite understand, AFAIK mkpath() does not create any
directories as a side-effect. And yes, I just wanted a short way to
say file_exists(concat(dir, file)) or file_exists(mkpath("%s/%s", dir,
file)) without cluttering up the code.
> If the dir doesn't exist it can be created in rebase_options_load explicitly?
I don't intend to create any directories if they do not exist.
>> +
>> +static int read_state_file(struct strbuf *sb, const char *dir, const char *file)
>> +{
>> + const char *path = mkpath("%s/%s", dir, file);
>> + strbuf_reset(sb);
>> + if (strbuf_read_file(sb, path, 0) >= 0)
>> + return sb->len;
>> + else
>> + return error(_("could not read '%s'"), path);
>> +}
>> +
>> +int rebase_options_load(struct rebase_options *opts, const char *dir)
>> +{
>> + struct strbuf sb = STRBUF_INIT;
>> + const char *filename;
>> +
>> + /* opts->orig_refname */
>> + if (read_state_file(&sb, dir, "head-name") < 0)
>> + return -1;
>> + strbuf_trim(&sb);
>> + if (starts_with(sb.buf, "refs/heads/"))
>> + opts->orig_refname = strbuf_detach(&sb, NULL);
>> + else if (!strcmp(sb.buf, "detached HEAD"))
>> + opts->orig_refname = NULL;
>> + else
>> + return error(_("could not parse %s"), mkpath("%s/%s", dir, "head-name"));
>> +
>> + /* opts->onto */
>> + if (read_state_file(&sb, dir, "onto") < 0)
>> + return -1;
>> + strbuf_trim(&sb);
>> + if (get_oid_hex(sb.buf, &opts->onto) < 0)
>> + return error(_("could not parse %s"), mkpath("%s/%s", dir, "onto"));
>> +
>> + /*
>> + * We always write to orig-head, but interactive rebase used to write
>> + * to head. Fall back to reading from head to cover for the case that
>> + * the user upgraded git with an ongoing interactive rebase.
>> + */
>> + filename = state_file_exists(dir, "orig-head") ? "orig-head" : "head";
>> + if (read_state_file(&sb, dir, filename) < 0)
>> + return -1;
>
> So from here on we always use "orig-head" instead of "head" for
> interactive rebase.
> Would people ever rely on the (internal) file name and have e.g.
> scripts which operate
> on the "head" file ?
This backwards-compatibility code is just a straight port from the
code in git-rebase.sh.
The usage of orig-head has been around since 2011 with 84df456
(rebase: extract code for writing basic state, 2011-02-06), so I guess
if people had issues with it, it would have been reported.
>
>
>> + strbuf_trim(&sb);
>> + if (get_oid_hex(sb.buf, &opts->orig_head) < 0)
>> + return error(_("could not parse %s"), mkpath("%s/%s", dir, filename));
>> +
>> + strbuf_release(&sb);
>> + return 0;
>> +}
>> +
>> +static int write_state_text(const char *dir, const char *file, const char *string)
>> +{
>> + return write_file(mkpath("%s/%s", dir, file), "%s", string);
>> +}
>
> Same comment as on checking the state files existence. I'm not sure if the side
> effect of creating the dir is better done explicitly where it is used.
> The concat of dir and
> file name can still be done in the helper though? (If the helper is
> needed at all then)
Same as above -- AFAIK I don't think mkpath() creates any directories
as a side-effect.
>
>> +
>> +void rebase_options_save(const struct rebase_options *opts, const char *dir)
>> +{
>> + const char *head_name = opts->orig_refname;
>> + if (!head_name)
>> + head_name = "detached HEAD";
>> + write_state_text(dir, "head-name", head_name);
>> + write_state_text(dir, "onto", oid_to_hex(&opts->onto));
>> + write_state_text(dir, "orig-head", oid_to_hex(&opts->orig_head));
>> +}
>> diff --git a/rebase-common.h b/rebase-common.h
>> index db5146a..051c056 100644
>> --- a/rebase-common.h
>> +++ b/rebase-common.h
>> @@ -20,4 +20,8 @@ void rebase_options_release(struct rebase_options *);
>>
>> void rebase_options_swap(struct rebase_options *dst, struct rebase_options *src);
>>
>> +int rebase_options_load(struct rebase_options *, const char *dir);
>> +
>> +void rebase_options_save(const struct rebase_options *, const char *dir);
>> +
>> #endif /* REBASE_COMMON_H */
>> --
>> 2.7.0
Thanks,
Paul
next prev parent reply other threads:[~2016-03-16 12:04 UTC|newest]
Thread overview: 59+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-03-12 10:46 [PATCH/RFC/GSoC 00/17] A barebones git-rebase in C Paul Tan
2016-03-12 10:46 ` [PATCH/RFC/GSoC 01/17] perf: introduce performance tests for git-rebase Paul Tan
2016-03-16 7:58 ` Johannes Schindelin
2016-03-16 11:51 ` Paul Tan
2016-03-16 15:59 ` Johannes Schindelin
2016-03-18 11:01 ` Thomas Gummerer
2016-03-18 16:00 ` Johannes Schindelin
2016-03-20 14:00 ` Thomas Gummerer
2016-03-21 7:54 ` Johannes Schindelin
2016-03-12 10:46 ` [PATCH/RFC/GSoC 02/17] sha1_name: implement get_oid() and friends Paul Tan
2016-03-12 10:46 ` [PATCH/RFC/GSoC 03/17] builtin-rebase: implement skeletal builtin rebase Paul Tan
2016-03-14 18:31 ` Stefan Beller
2016-03-15 8:01 ` Johannes Schindelin
2016-03-12 10:46 ` [PATCH/RFC/GSoC 04/17] builtin-rebase: parse rebase arguments into a common rebase_options struct Paul Tan
2016-03-14 20:05 ` Stefan Beller
2016-03-15 10:54 ` Johannes Schindelin
2016-03-12 10:46 ` [PATCH/RFC/GSoC 05/17] rebase-options: implement rebase_options_load() and rebase_options_save() Paul Tan
2016-03-14 20:30 ` Stefan Beller
2016-03-16 8:04 ` Johannes Schindelin
2016-03-16 12:28 ` Paul Tan
2016-03-16 17:11 ` Johannes Schindelin
2016-03-21 14:55 ` Paul Tan
2016-03-16 12:04 ` Paul Tan [this message]
2016-03-16 17:10 ` Stefan Beller
2016-03-12 10:46 ` [PATCH/RFC/GSoC 06/17] rebase-am: introduce am backend for builtin rebase Paul Tan
2016-03-16 13:21 ` Johannes Schindelin
2016-03-12 10:46 ` [PATCH/RFC/GSoC 07/17] rebase-common: implement refresh_and_write_cache() Paul Tan
2016-03-14 21:10 ` Junio C Hamano
2016-03-16 12:56 ` Paul Tan
2016-03-12 10:46 ` [PATCH/RFC/GSoC 08/17] rebase-common: let refresh_and_write_cache() take a flags argument Paul Tan
2016-03-12 10:46 ` [PATCH/RFC/GSoC 09/17] rebase-common: implement cache_has_unstaged_changes() Paul Tan
2016-03-14 20:54 ` Johannes Schindelin
2016-03-14 21:52 ` Junio C Hamano
2016-03-15 11:51 ` Johannes Schindelin
2016-03-15 11:07 ` Duy Nguyen
2016-03-15 14:15 ` Johannes Schindelin
2016-03-12 10:46 ` [PATCH/RFC/GSoC 10/17] rebase-common: implement cache_has_uncommitted_changes() Paul Tan
2016-03-12 10:46 ` [PATCH/RFC/GSoC 11/17] rebase-merge: introduce merge backend for builtin rebase Paul Tan
2016-03-12 10:46 ` [PATCH/RFC/GSoC 12/17] rebase-todo: introduce rebase_todo_item Paul Tan
2016-03-14 13:43 ` Christian Couder
2016-03-14 20:33 ` Johannes Schindelin
2016-03-16 12:54 ` Paul Tan
2016-03-16 15:55 ` Johannes Schindelin
2016-03-12 10:46 ` [PATCH/RFC/GSoC 13/17] rebase-todo: introduce rebase_todo_list Paul Tan
2016-03-12 10:46 ` [PATCH/RFC/GSoC 14/17] status: use rebase_todo_list Paul Tan
2016-03-12 10:46 ` [PATCH/RFC/GSoC 15/17] wrapper: implement append_file() Paul Tan
2016-03-12 10:46 ` [PATCH/RFC/GSoC 16/17] editor: implement git_sequence_editor() and launch_sequence_editor() Paul Tan
2016-03-15 7:00 ` Johannes Schindelin
2016-03-16 13:06 ` Paul Tan
2016-03-16 18:21 ` Johannes Schindelin
2016-03-12 10:46 ` [PATCH/RFC/GSoC 17/17] rebase-interactive: introduce interactive backend for builtin rebase Paul Tan
2016-03-15 7:57 ` Johannes Schindelin
2016-03-15 16:48 ` Paul Tan
2016-03-15 19:45 ` Johannes Schindelin
2016-03-14 12:15 ` [PATCH/RFC/GSoC 00/17] A barebones git-rebase in C Duy Nguyen
2016-03-14 17:32 ` Stefan Beller
2016-03-14 18:43 ` Junio C Hamano
2016-03-16 12:46 ` Paul Tan
2016-03-14 20:44 ` Johannes Schindelin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CACRoPnS7WWWVay9hAjXYgyeB=1A1gfARerKJe25APa-6u5cGaA@mail.gmail.com' \
--to=pyokagan@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=johannes.schindelin@gmx.de \
--cc=pclouds@gmail.com \
--cc=sam.halliday@gmail.com \
--cc=sbeller@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).