git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Michael Rappazzo <rappazzo@gmail.com>
To: gitster@pobox.com
Cc: git@vger.kernel.org, mhagger@alum.mit.edu, peff@peff.net,
	dturner@twopensource.com, pclouds@gmail.com,
	sunshine@sunshineco.com, Michael Rappazzo <rappazzo@gmail.com>
Subject: [PATCH 2/5] ff-refs: update each updatable ref
Date: Tue, 10 Nov 2015 21:11:22 -0500	[thread overview]
Message-ID: <1447207885-10911-3-git-send-email-rappazzo@gmail.com> (raw)
In-Reply-To: <1447207885-10911-1-git-send-email-rappazzo@gmail.com>

If an updatable ref is checked out in any worktree, emulate merge
--ff-only to also update the local work tree (including executing the
post-merge hook).

If an updatable ref is not checked out in any worktree, the ref is
simply updated.

If a ref update is successful, that ref is reported as 'UPDATED'.  If
it is not successful, the ref is reported as 'UNABLE-TO-UPDATE'.

Signed-off-by: Michael Rappazzo <rappazzo@gmail.com>
---
 builtin/ff-refs.c | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/builtin/ff-refs.c b/builtin/ff-refs.c
index 94a4649..f14afc5 100644
--- a/builtin/ff-refs.c
+++ b/builtin/ff-refs.c
@@ -59,6 +59,43 @@ static const char *result_type_str(enum ff_result_type result_type)
 }
 
 /**
+ * Do the ref update.
+ *  - If the ref is checked out in any worktree, emulate merge --ff-only to
+ *    also update the local work tree (including executing the post-merge hook).
+ *
+ *  - If the ref is not checked out in any worktree, update it
+ *
+ *  - If any of the ref updates fails, the result_type is set to UNABLE-TO-UPDATE
+ */
+static void do_ref_update(struct ff_ref_data *data, struct ff_ref_details *details)
+{
+	const char *refname = details->branch->refname;
+
+	if (details->wt) {
+		struct strbuf path = STRBUF_INIT;
+
+		strbuf_getcwd(&path);
+		chdir(details->wt->path);
+		set_git_dir(details->wt->git_dir);
+		read_index(&the_index);
+
+		if (checkout_fast_forward(details->branch_commit->object.sha1,
+				details->upstream_commit->object.sha1, 1))
+			details->result_type = NON_FAST_FORWARD;
+		else if (update_ref("ff-refs", refname, details->upstream_commit->object.sha1,
+				details->branch_commit->object.sha1, 0, UPDATE_REFS_QUIET_ON_ERR)) {
+			details->result_type = UNABLE_TO_UPDATE;
+			run_hook_le(NULL, "post-merge", "0", NULL);
+		}
+		discard_index(&the_index);
+		chdir(path.buf);
+		strbuf_release(&path);
+	} else if (update_ref("ff-refs", refname, details->upstream_commit->object.sha1,
+			details->branch_commit->object.sha1, 0, UPDATE_REFS_QUIET_ON_ERR))
+		details->result_type = UNABLE_TO_UPDATE;
+}
+
+/**
  * return the worktree with the given refname checked out, or NULL if that
  * ref is not checked out in any branch.
  *
@@ -98,6 +135,9 @@ static void process_refs(struct ff_ref_data *data)
 
 		printf("     %s -> %s%*.*s",
 			details->branch->name, details->shortened_upstream, padLen, padLen, padding);
+		if (details->result_type == UPDATABLE)
+			do_ref_update(data, details);
+
 		printf("[%s]\n", result_type_str(details->result_type));
 	}
 }
-- 
2.6.2

  parent reply	other threads:[~2015-11-11  2:13 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-11  2:11 [PATCH 0/5] ff-refs: builtin command to fast-forward local refs Michael Rappazzo
2015-11-11  2:11 ` [PATCH 1/5] ff-refs: builtin cmd to check and fast forward local refs to their upstream Michael Rappazzo
2015-11-11  2:11 ` Michael Rappazzo [this message]
2015-11-11  2:11 ` [PATCH 3/5] ff-refs: add --dry-run and --skip-worktree options Michael Rappazzo
2015-11-11  2:11 ` [PATCH 4/5] ff-refs: Add documentation Michael Rappazzo
2015-11-11  2:11 ` [PATCH 5/5] ff-refs: Add tests Michael Rappazzo
2015-11-11 10:41 ` [PATCH 0/5] ff-refs: builtin command to fast-forward local refs Michael J Gruber
2015-11-11 12:32   ` Mike Rappazzo
     [not found]     ` <CANoM8SWxMeDjwy-GwVc+En8D7N8LyzzsBKtX_MbiS4Z49DjD7g@mail.gmail.com>
2015-11-17 15:28       ` Michael J Gruber
2015-11-17 15:36         ` Mike Rappazzo
2015-11-18  9:56           ` Johannes Schindelin
2015-11-24 22:39             ` Jeff King
2015-12-01  0:24               ` Junio C Hamano

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1447207885-10911-3-git-send-email-rappazzo@gmail.com \
    --to=rappazzo@gmail.com \
    --cc=dturner@twopensource.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=mhagger@alum.mit.edu \
    --cc=pclouds@gmail.com \
    --cc=peff@peff.net \
    --cc=sunshine@sunshineco.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.
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 NNTP newsgroup(s).