From patchwork Tue Sep 25 22:33:24 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul-Sebastian Ungureanu X-Patchwork-Id: 10614877 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4AF223E9D for ; Tue, 25 Sep 2018 22:34:37 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3BD0B298BF for ; Tue, 25 Sep 2018 22:34:37 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2FE202AC9F; Tue, 25 Sep 2018 22:34:37 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9E7D3298DC for ; Tue, 25 Sep 2018 22:34:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726463AbeIZEoS (ORCPT ); Wed, 26 Sep 2018 00:44:18 -0400 Received: from mail-ed1-f67.google.com ([209.85.208.67]:46628 "EHLO mail-ed1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726250AbeIZEoS (ORCPT ); Wed, 26 Sep 2018 00:44:18 -0400 Received: by mail-ed1-f67.google.com with SMTP id k14-v6so156593edr.13 for ; Tue, 25 Sep 2018 15:34:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=+O4emAM1zoizqNUSo0x090r7p62ARn+IwSd9pPL8pyY=; b=AJIqWZC8YezDpjfQcGApsRGvPQr2Hm4bVGzy53g94bWd4/yhc8xvuRA7b+2thWCJh+ kqcnosPqC2ztriJujU7k6iflV+yqWngy2j1wExXkjUF+cvuBR1w87fvCHmLpat+GFVih X2KHM9cxKHrlN8lYrVws9u30OvpBJ5vv9jbDZ8Xnf0kR0/AFkFlagUymBiwZ1N8MQecB Dph7CF+jiXoLwavsSWc8LPruPE+0SJ6+mvovPhkrPE7csFa4HVQ9/E6SMhATUv0snp9G GoYOoTE0/sfexJBhjPbD83LVWFurK4qaIH86qEUTHBj9hJSFyV7p1Do1MyWti7emRE61 sh1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=+O4emAM1zoizqNUSo0x090r7p62ARn+IwSd9pPL8pyY=; b=TgreKC6/nyPZrr1KlXN+YztLg/ipvcP3UsS3Y3x2Sws4p70nB7JX7QScUrrW2kFgI0 hNvO3jrIpj4DJ+/cZsQMCUh3wP0YSCgDXbYKtMSnKpkM3nSJCe6vw632FRhWg4JXBP6v zLuwiAS3IdIT8FIx5HgTETZN1lECBtxuGJNu2V+skVJy/PahX3WSUXkwhGpXaae5SMLT cvsH1FxBbq3vrYAeQEG2dOxbxT00ZXVCxyQS15S4H1l0rWVYuPN8uS1ravqePmpNDaWB HieelYm0xxVlNSO0Mv5NtaOqA+g7WYuZwtdk8lOuWQPTD4MT521S1VpdjvXU3t4GCgU6 XH9w== X-Gm-Message-State: ABuFfoiL/3WjVREZrXgm7yK2JfD5M71uSZTacIOXykkepviH/5AO1nu4 k6iTmSJy0owMnmejd2IhAo/tUHu4 X-Google-Smtp-Source: ACcGV61PX44ppUeFBaoiiXtKrrRhyP1e0OejOZ23CM68hpJUfyRYxXjG2WIjJpJMBu0KEHjyTpML0w== X-Received: by 2002:a50:ed1a:: with SMTP id j26-v6mr4640013eds.29.1537914871616; Tue, 25 Sep 2018 15:34:31 -0700 (PDT) Received: from localhost.localdomain ([92.55.154.13]) by smtp.gmail.com with ESMTPSA id o37-v6sm33553edo.71.2018.09.25.15.34.30 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 25 Sep 2018 15:34:30 -0700 (PDT) From: Paul-Sebastian Ungureanu To: git@vger.kernel.org Subject: [PATCH v9 08/21] stash: convert drop and clear to builtin Date: Wed, 26 Sep 2018 01:33:24 +0300 Message-Id: X-Mailer: git-send-email 2.19.0.rc0.23.g1fb9f40d88.dirty In-Reply-To: References: MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Joel Teichroeb Add the drop and clear commands to the builtin helper. These two are each simple, but are being added together as they are quite related. We have to unfortunately keep the drop and clear functions in the shell script as functions are called with parameters internally that are not valid when the commands are called externally. Once pop is converted they can both be removed. Signed-off-by: Joel Teichroeb Signed-off-by: Paul-Sebastian Ungureanu --- builtin/stash--helper.c | 116 ++++++++++++++++++++++++++++++++++++++++ git-stash.sh | 4 +- 2 files changed, 118 insertions(+), 2 deletions(-) diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c index 7819dae332..72472eaeb7 100644 --- a/builtin/stash--helper.c +++ b/builtin/stash--helper.c @@ -12,7 +12,14 @@ #include "rerere.h" static const char * const git_stash_helper_usage[] = { + N_("git stash--helper drop [-q|--quiet] []"), N_("git stash--helper apply [--index] [-q|--quiet] []"), + N_("git stash--helper clear"), + NULL +}; + +static const char * const git_stash_helper_drop_usage[] = { + N_("git stash--helper drop [-q|--quiet] []"), NULL }; @@ -21,6 +28,11 @@ static const char * const git_stash_helper_apply_usage[] = { NULL }; +static const char * const git_stash_helper_clear_usage[] = { + N_("git stash--helper clear"), + NULL +}; + static const char *ref_stash = "refs/stash"; static struct strbuf stash_index_path = STRBUF_INIT; @@ -139,6 +151,31 @@ static int get_stash_info(struct stash_info *info, int argc, const char **argv) return !(ret == 0 || ret == 1); } +static int do_clear_stash(void) +{ + struct object_id obj; + if (get_oid(ref_stash, &obj)) + return 0; + + return delete_ref(NULL, ref_stash, &obj, 0); +} + +static int clear_stash(int argc, const char **argv, const char *prefix) +{ + struct option options[] = { + OPT_END() + }; + + argc = parse_options(argc, argv, prefix, options, + git_stash_helper_clear_usage, + PARSE_OPT_STOP_AT_NON_OPTION); + + if (argc) + return error(_("git stash clear with parameters is unimplemented")); + + return do_clear_stash(); +} + static int reset_tree(struct object_id *i_tree, int update, int reset) { int nr_trees = 1; @@ -424,6 +461,81 @@ static int apply_stash(int argc, const char **argv, const char *prefix) return ret; } +static int do_drop_stash(const char *prefix, struct stash_info *info, int quiet) +{ + int ret; + struct child_process cp_reflog = CHILD_PROCESS_INIT; + struct child_process cp = CHILD_PROCESS_INIT; + + /* + * reflog does not provide a simple function for deleting refs. One will + * need to be added to avoid implementing too much reflog code here + */ + + cp_reflog.git_cmd = 1; + argv_array_pushl(&cp_reflog.args, "reflog", "delete", "--updateref", + "--rewrite", NULL); + argv_array_push(&cp_reflog.args, info->revision.buf); + ret = run_command(&cp_reflog); + if (!ret) { + if (!quiet) + printf_ln(_("Dropped %s (%s)"), info->revision.buf, + oid_to_hex(&info->w_commit)); + } else { + return error(_("%s: Could not drop stash entry"), + info->revision.buf); + } + + /* + * This could easily be replaced by get_oid, but currently it will throw + * a fatal error when a reflog is empty, which we can not recover from. + */ + cp.git_cmd = 1; + /* Even though --quiet is specified, rev-parse still outputs the hash */ + cp.no_stdout = 1; + argv_array_pushl(&cp.args, "rev-parse", "--verify", "--quiet", NULL); + argv_array_pushf(&cp.args, "%s@{0}", ref_stash); + ret = run_command(&cp); + + /* do_clear_stash if we just dropped the last stash entry */ + if (ret) + do_clear_stash(); + + return 0; +} + +static void assert_stash_ref(struct stash_info *info) +{ + if (!info->is_stash_ref) { + free_stash_info(info); + error(_("'%s' is not a stash reference"), info->revision.buf); + exit(128); + } +} + +static int drop_stash(int argc, const char **argv, const char *prefix) +{ + int ret; + int quiet = 0; + struct stash_info info; + struct option options[] = { + OPT__QUIET(&quiet, N_("be quiet, only report errors")), + OPT_END() + }; + + argc = parse_options(argc, argv, prefix, options, + git_stash_helper_drop_usage, 0); + + if (get_stash_info(&info, argc, argv)) + return -1; + + assert_stash_ref(&info); + + ret = do_drop_stash(prefix, &info, quiet); + free_stash_info(&info); + return ret; +} + int cmd_stash__helper(int argc, const char **argv, const char *prefix) { pid_t pid = getpid(); @@ -446,6 +558,10 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix) usage_with_options(git_stash_helper_usage, options); if (!strcmp(argv[0], "apply")) return !!apply_stash(argc, argv, prefix); + else if (!strcmp(argv[0], "clear")) + return !!clear_stash(argc, argv, prefix); + else if (!strcmp(argv[0], "drop")) + return !!drop_stash(argc, argv, prefix); usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]), git_stash_helper_usage, options); diff --git a/git-stash.sh b/git-stash.sh index 809b1c2d1d..a99d5dc9e5 100755 --- a/git-stash.sh +++ b/git-stash.sh @@ -653,7 +653,7 @@ apply) ;; clear) shift - clear_stash "$@" + git stash--helper clear "$@" ;; create) shift @@ -665,7 +665,7 @@ store) ;; drop) shift - drop_stash "$@" + git stash--helper drop "$@" ;; pop) shift