From mboxrd@z Thu Jan 1 00:00:00 1970 From: Martin von Zweigbergk Subject: [PATCH v2 13/19] reset.c: move lock, write and commit out of update_index_refresh() Date: Mon, 14 Jan 2013 21:47:45 -0800 Message-ID: <1358228871-7142-14-git-send-email-martinvonz@gmail.com> References: <1357719376-16406-1-git-send-email-martinvonz@gmail.com> <1358228871-7142-1-git-send-email-martinvonz@gmail.com> Cc: Junio C Hamano , Matt Kraai , Ramsay Jones , Duy Nguyen , Jeff King , Martin von Zweigbergk To: git@vger.kernel.org X-From: git-owner@vger.kernel.org Tue Jan 15 06:49:23 2013 Return-path: Envelope-to: gcvg-git-2@plane.gmane.org Received: from vger.kernel.org ([209.132.180.67]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1TuzOg-0007Y3-Jo for gcvg-git-2@plane.gmane.org; Tue, 15 Jan 2013 06:49:18 +0100 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755928Ab3AOFst (ORCPT ); Tue, 15 Jan 2013 00:48:49 -0500 Received: from mail-ye0-f201.google.com ([209.85.213.201]:36857 "EHLO mail-ye0-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755663Ab3AOFs1 (ORCPT ); Tue, 15 Jan 2013 00:48:27 -0500 Received: by mail-ye0-f201.google.com with SMTP id r11so588150yen.2 for ; Mon, 14 Jan 2013 21:48:27 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-received:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references:x-gm-message-state; bh=TVwueX3/842iuAOPhIFYFqr4qesBmp2zlJ8qqSzD+sQ=; b=UbeMjfPJyPzlgo4TP9eMPFECnlZpWO78B/xdrW1i0QYJQRvrXHrB+mdKGee7/MRd/d piYiI+YAQUvAgl5VaNMFT9K32BJLX0RVKS7LzUrOfUFKq1ZZ3Qj25NbUkhdvFBGUVKpx UW7S12Dme+ENzODIS7+hfrbcU+wSqjHnajxlJa5MZtxWQGH3IZfwuzCUNJQA55B0tX1a 3lZmYz2daBxeIUW5ogALVi1os3d05DpwLshAvLsR6qOMaC7Si+VAszrFT3qeq1kkHGDu jwzZjJMwK3REaGONC6nChmr02d95NTgcbw/Og9vqdgKiiOUJ9v5LIoByVTqWd0nmMf5l mEww== X-Received: by 10.236.120.70 with SMTP id o46mr47793571yhh.39.1358228907136; Mon, 14 Jan 2013 21:48:27 -0800 (PST) Received: from corp2gmr1-2.hot.corp.google.com (corp2gmr1-2.hot.corp.google.com [172.24.189.93]) by gmr-mx.google.com with ESMTPS id i27si818379yhe.4.2013.01.14.21.48.27 (version=TLSv1.1 cipher=AES128-SHA bits=128/128); Mon, 14 Jan 2013 21:48:27 -0800 (PST) Received: from handduk2.mtv.corp.google.com (handduk2.mtv.corp.google.com [172.18.144.137]) by corp2gmr1-2.hot.corp.google.com (Postfix) with ESMTP id E36145A4173; Mon, 14 Jan 2013 21:48:26 -0800 (PST) Received: by handduk2.mtv.corp.google.com (Postfix, from userid 151024) id C03D9101887; Mon, 14 Jan 2013 21:48:26 -0800 (PST) X-Mailer: git-send-email 1.8.1.1.454.gce43f05 In-Reply-To: <1358228871-7142-1-git-send-email-martinvonz@gmail.com> X-Gm-Message-State: ALoCoQnqbbaCc4ijx7v014HiBpfDkqDf0pUfu2hiw/92EfppNuikZgJBQNa0mw6wI3IkwLeXI2sAFG31YM3NERsZ28SXjnnFx4ixxcRczh9HjvTMfq2sI8YJrS4B1HkWWXT2bG7nwMOFnqcQMHXxsdkMkvFMm8igdKxRi4nHpLKFeC6VEs3zB2DSXVNEWHdpX+0yO0vYnwQn Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Archived-At: In preparation for the/a following patch, move the locking, writing and committing of the index file out of update_index_refresh(). The code duplication caused will soon be taken care of. What remains of update_index_refresh() is just one line, but it is still called from two places, so let's leave it for now. In the process, we expose and fix the minor UI bug that makes us print "Could not refresh index" when we fail to write the index file when invoked with a pathspec. Copy the error message from the pathspec-less codepath ("Could not write new index file."). Signed-off-by: Martin von Zweigbergk --- builtin/reset.c | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/builtin/reset.c b/builtin/reset.c index 70733c2..c1d6ef2 100644 --- a/builtin/reset.c +++ b/builtin/reset.c @@ -109,19 +109,10 @@ static void print_new_head_line(struct commit *commit) printf("\n"); } -static int update_index_refresh(int fd, struct lock_file *index_lock, int flags) +static void update_index_refresh(int flags) { - if (!index_lock) { - index_lock = xcalloc(1, sizeof(struct lock_file)); - fd = hold_locked_index(index_lock, 1); - } - refresh_index(&the_index, (flags), NULL, NULL, _("Unstaged changes after reset:")); - if (write_cache(fd, active_cache, active_nr) || - commit_locked_index(index_lock)) - return error ("Could not refresh index"); - return 0; } static void update_index_from_diff(struct diff_queue_struct *q, @@ -321,9 +312,14 @@ int cmd_reset(int argc, const char **argv, const char *prefix) if (pathspec) { struct lock_file *lock = xcalloc(1, sizeof(struct lock_file)); int index_fd = hold_locked_index(lock, 1); - return read_from_tree(pathspec, sha1) || - update_index_refresh(index_fd, lock, - quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN); + if (read_from_tree(pathspec, sha1)) + return 1; + update_index_refresh( + quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN); + if (write_cache(index_fd, active_cache, active_nr) || + commit_locked_index(lock)) + return error("Could not write new index file."); + return 0; } /* Soft reset does not touch the index file nor the working tree @@ -351,9 +347,15 @@ int cmd_reset(int argc, const char **argv, const char *prefix) if (reset_type == HARD && !update_ref_status && !quiet) print_new_head_line(commit); - else if (reset_type == MIXED) /* Report what has not been updated. */ - update_index_refresh(0, NULL, - quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN); + else if (reset_type == MIXED) { /* Report what has not been updated. */ + struct lock_file *index_lock = xcalloc(1, sizeof(struct lock_file)); + int fd = hold_locked_index(index_lock, 1); + update_index_refresh( + quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN); + if (write_cache(fd, active_cache, active_nr) || + commit_locked_index(index_lock)) + error("Could not refresh index"); + } remove_branch_state(); -- 1.8.1.1.454.gce43f05