git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/5] ff-refs: update each updatable ref

From
Michael Rappazzo <rappazzo@gmail.com>
Date
Nov 11, 2015, 02:11 UTC
Message-ID
<1447207885-10911-3-git-send-email-rappazzo@gmail.com>
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
Previous: Michael RappazzoNext: Michael Rappazzo
Message 3 of 13 in “ff-refs: builtin command to fast-forward local refs”
  1. 0/5 ff-refs: builtin command to fast-forward local refsMichael Rappazzo, Nov 11, 2015
  2. 1/5 ff-refs: builtin cmd to check and fast forward local refs to their upstreamMichael Rappazzo, Nov 11, 2015
  3. 2/5 ff-refs: update each updatable refMichael Rappazzo, Nov 11, 2015
  4. 3/5 ff-refs: add --dry-run and --skip-worktree optionsMichael Rappazzo, Nov 11, 2015
  5. 4/5 ff-refs: Add documentationMichael Rappazzo, Nov 11, 2015
  6. 5/5 ff-refs: Add testsMichael Rappazzo, Nov 11, 2015
  7. Michael J GruberNov 11, 2015
  8. Mike RappazzoNov 11, 2015
  9. Michael J GruberNov 17, 2015
  10. Mike RappazzoNov 17, 2015
  11. Johannes SchindelinNov 18, 2015
  12. Jeff KingNov 24, 2015
  13. Junio C HamanoDec 1, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.