diff mbox series

[RFC,1/3] branch: add new 'tail' concept

Message ID 20230310214515.39154-2-felipe.contreras@gmail.com (mailing list archive)
State New, archived
Headers show
Series Support for tail (branch point) experiment | expand

Commit Message

Felipe Contreras March 10, 2023, 9:45 p.m. UTC
The 'tail' of a branch points to the first parent commit of the branch
when it was created.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 branch.c                  | 12 ++++++++++++
 branch.h                  |  1 +
 builtin/clone.c           |  1 +
 t/t1514-rev-parse-tail.sh | 23 +++++++++++++++++++++++
 4 files changed, 37 insertions(+)
 create mode 100755 t/t1514-rev-parse-tail.sh
diff mbox series

Patch

diff --git a/branch.c b/branch.c
index e5614b53b3..e43a164ddb 100644
--- a/branch.c
+++ b/branch.c
@@ -234,6 +234,16 @@  static int inherit_tracking(struct tracking *tracking, const char *orig_ref)
 	return 0;
 }
 
+void install_branch_base(const char *local, const struct object_id *tail)
+{
+	struct strbuf ref = STRBUF_INIT;
+
+	strbuf_addf(&ref, "refs/tails/%s", local);
+	update_ref(NULL, ref.buf, tail, NULL, 0, UPDATE_REFS_DIE_ON_ERR);
+
+	strbuf_release(&ref);
+}
+
 /*
  * Used internally to set the branch.<new_ref>.{remote,merge} config
  * settings so that branch 'new_ref' tracks 'orig_ref'. Unlike
@@ -623,6 +633,8 @@  void create_branch(struct repository *r,
 	if (real_ref && track)
 		setup_tracking(ref.buf + 11, real_ref, track, quiet);
 
+	install_branch_base(ref.buf + 11, &oid);
+
 cleanup:
 	strbuf_release(&ref);
 	free(real_ref);
diff --git a/branch.h b/branch.h
index ef56103c05..f020d5be5d 100644
--- a/branch.h
+++ b/branch.h
@@ -142,6 +142,7 @@  void remove_branch_state(struct repository *r, int verbose);
  */
 #define BRANCH_CONFIG_VERBOSE 01
 int install_branch_config(int flag, const char *local, const char *origin, const char *remote);
+void install_branch_base(const char *local, const struct object_id *tail);
 
 /*
  * Read branch description
diff --git a/builtin/clone.c b/builtin/clone.c
index 65b5b7db6d..62afcff2ba 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -624,6 +624,7 @@  static void update_head(const struct ref *our, const struct ref *remote,
 			update_ref(msg, "HEAD", &our->old_oid, NULL, 0,
 				   UPDATE_REFS_DIE_ON_ERR);
 			install_branch_config(0, head, remote_name, our->name);
+			install_branch_base(head, &our->old_oid);
 		}
 	} else if (our) {
 		struct commit *c = lookup_commit_reference(the_repository,
diff --git a/t/t1514-rev-parse-tail.sh b/t/t1514-rev-parse-tail.sh
new file mode 100755
index 0000000000..da8e9ceef1
--- /dev/null
+++ b/t/t1514-rev-parse-tail.sh
@@ -0,0 +1,23 @@ 
+#!/bin/sh
+
+test_description='test <branch>@{upstream} syntax'
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+	echo one > content &&
+	git add content &&
+	git commit -m one &&
+	git checkout -b test master &&
+	echo two > new &&
+	git add new &&
+	git commit -a -m two
+'
+
+test_expect_success 'test tail creation' '
+	git rev-parse refs/tails/test > actual &&
+	git rev-parse master > expect &&
+	test_cmp expect actual
+'
+
+test_done