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

[PATCH 2/6] revision: factor out get_commit()

From
René Scharfe <l.s.r@web.de>
Date
Sep 15, 2022, 14:52 UTC
Message-ID
<90bdf408-9bad-6816-06c1-66ac64ea9c3b@web.de>
In-Reply-To
<ba6eea28-fb3a-b376-2529-351727c02f1a@web.de>

Split out the first half of add_parents_only() to obtain a function that finds and returns the commit object. It allows checking the validity of the child separately from adding its parents.

Signed-off-by: René Scharfe <l.s.r@web.de>
---
 revision.c | 35 +++++++++++++++++++++++------------
 1 file changed, 23 insertions(+), 12 deletions(-)
diff --git a/revision.c b/revision.c
index 0b8d48f94c..4f896b4992 100644
--- a/revision.c
+++ b/revision.c
@@ -1822,35 +1822,46 @@ static void add_alternate_refs_to_pending(struct rev_info *revs,
 	for_each_alternate_ref(add_one_alternate_ref, &data);
 }

-static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
-			    int exclude_parent)
+static struct commit *get_commit(struct rev_info *revs, const char *arg_)
 {
 	struct object_id oid;
 	struct object *it;
-	struct commit *commit;
-	struct commit_list *parents;
-	int parent_number;
 	const char *arg = arg_;

-	if (*arg == '^') {
-		flags ^= UNINTERESTING | BOTTOM;
+	if (*arg == '^')
 		arg++;
-	}
 	if (get_oid_committish(arg, &oid))
-		return 0;
+		return NULL;
 	while (1) {
 		it = get_reference(revs, arg, &oid, 0);
 		if (!it && revs->ignore_missing)
-			return 0;
+			return NULL;
 		if (it->type != OBJ_TAG)
 			break;
 		if (!((struct tag*)it)->tagged)
-			return 0;
+			return NULL;
 		oidcpy(&oid, &((struct tag*)it)->tagged->oid);
 	}
 	if (it->type != OBJ_COMMIT)
+		return NULL;
+	return (struct commit *)it;
+}
+
+static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
+			    int exclude_parent)
+{
+	struct object *it;
+	struct commit *commit = get_commit(revs, arg_);
+	struct commit_list *parents;
+	int parent_number;
+	const char *arg = arg_;
+
+	if (*arg == '^') {
+		flags ^= UNINTERESTING | BOTTOM;
+		arg++;
+	}
+	if (!commit)
 		return 0;
-	commit = (struct commit *)it;
 	if (exclude_parent &&
 	    exclude_parent > commit_list_count(commit->parents))
 		return 0;
--
2.37.3
Previous: Chris TorekNext: René Scharfe
Message 4 of 16 in “revision: fix order of revs for ^!”
  1. 0/6 revision: fix order of revs for ^!René Scharfe, Sep 15, 2022
  2. 1/6 revision: use strtol_i() for exclude_parentRené Scharfe, Sep 15, 2022
  3. Chris TorekSep 17, 2022
  4. 2/6 revision: factor out get_commit()René Scharfe, Sep 15, 2022
  5. 3/6 revision: factor out add_parent()René Scharfe, Sep 15, 2022
  6. 4/6 revision: factor out add_parents()René Scharfe, Sep 15, 2022
  7. 5/6 revision: rename add_parents_only() to add_nth_parent()René Scharfe, Sep 15, 2022
  8. 6/6 revision: add parents after child for ^!René Scharfe, Sep 15, 2022
  9. Junio C HamanoSep 15, 2022
  10. René ScharfeSep 16, 2022
  11. Junio C HamanoSep 16, 2022
  12. René ScharfeSep 18, 2022
  13. 0/3 diff: support ^! for mergesRené Scharfe, Oct 1, 2022
  14. 1/3 revision: use strtol_i() for exclude_parentRené Scharfe, Oct 1, 2022
  15. 2/3 revisions.txt: unspecify order of resolved parts of ^!René Scharfe, Oct 1, 2022
  16. 3/3 diff: support ^! for mergesRené Scharfe, Oct 1, 2022

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.