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

Re: [PATCH 6/7] sequencer: Expose API to cherry-picking machinery

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Aug 14, 2011, 15:22 UTC
Message-ID
<20110814152204.GJ18466@elie.gateway.2wire.net>
In-Reply-To
<CALkWK0=zqyvL8zo9wvBGUXyf3RWSZB7dY=WaC9TN6YXnThag0Q@mail.gmail.com>
Ramkumar Ramachandra wrote:
> Jonathan Nieder writes:
>> git_path() calls vsnprintf which clobbers errno, so depending on the
>> platform this can print messages like
>>
>>        fatal: Could not open '.git/CHERRY_PICK_HEAD' for writing: Success
[...]
> Ugh, yet another "bugfix patch" to queue near the beginning of the
> series.  Thanks for catching this.

It's not a bug you introduced, and ideally it would be a separate patch on its own, against the js/cherry-pick-usability branch.

>> Ramkumar Ramachandra wrote:
Show 7 quoted lines
>>> +static struct tree *empty_tree(void)
>>> [...]
>>
>> This tree is leaked (for example if you cherry-pick a sequence of
>> root commits).
>
> This is not something I introduced -- it can wait until later, no?
Yep, it's Christian's fault (for introducing multiple-cherry-pick).
Patch below.  The things I do... :)
[...]
> It is a simple code movement.  Is there something I can do to help?

Part of the review was about examples where it was not simple code movement. Maybe if there is another round the thing to do would be to send a patch generated with -B -M to allow others to more easily check it.

Thanks.
-- >8 --
Subject: revert: plug memory leak in "cherry-pick root commit" codepath

The empty tree passed as common ancestor to merge_trees() when cherry-picking a parentless commit is allocated on the heap and never freed. Leaking such a small one-time allocation is not a very big problem, but now that "git cherry-pick" can cherry-pick multiple commits it can start to add up.

Avoid the leak by storing the fake tree exactly once in the BSS section (i.e., use a static). While at it, let's add a test to make sure cherry-picking multiple parentless commits continues to work.

Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
Patch is against cc/cherry-pick-series (86c7bb47).
 builtin/revert.c            |   10 +++++-----
 t/t3503-cherry-pick-root.sh |   27 ++++++++++++++++++++++++++-
 2 files changed, 31 insertions(+), 6 deletions(-)
diff --git a/builtin/revert.c b/builtin/revert.c
index 853e9e40..c1b0fb3d 100644
--- a/builtin/revert.c
+++ b/builtin/revert.c
@@ -273,12 +273,12 @@ static void write_message(struct strbuf *msgbuf, const char *filename)
 
 static struct tree *empty_tree(void)
 {
-	struct tree *tree = xcalloc(1, sizeof(struct tree));
+	static struct tree tree;
 
-	tree->object.parsed = 1;
-	tree->object.type = OBJ_TREE;
-	pretend_sha1_file(NULL, 0, OBJ_TREE, tree->object.sha1);
-	return tree;
+	tree.object.parsed = 1;
+	tree.object.type = OBJ_TREE;
+	pretend_sha1_file(NULL, 0, OBJ_TREE, tree.object.sha1);
+	return &tree;
 }
 
 static NORETURN void die_dirty_index(const char *me)
diff --git a/t/t3503-cherry-pick-root.sh b/t/t3503-cherry-pick-root.sh
index b0faa299..472e5b80 100755
--- a/t/t3503-cherry-pick-root.sh
+++ b/t/t3503-cherry-pick-root.sh
@@ -16,15 +16,40 @@ test_expect_success setup '
 	echo second > file2 &&
 	git add file2 &&
 	test_tick &&
-	git commit -m "second"
+	git commit -m "second" &&
+
+	git symbolic-ref HEAD refs/heads/third &&
+	rm .git/index file2 &&
+	echo third > file3 &&
+	git add file3 &&
+	test_tick &&
+	git commit -m "third"
 
 '
 
 test_expect_success 'cherry-pick a root commit' '
 
+	git checkout second^0 &&
 	git cherry-pick master &&
 	test first = $(cat file1)
 
 '
 
+test_expect_success 'cherry-pick two root commits' '
+
+	echo first >expect.file1 &&
+	echo second >expect.file2 &&
+	echo third >expect.file3 &&
+
+	git checkout second^0 &&
+	git cherry-pick master third &&
+
+	test_cmp expect.file1 file1 &&
+	test_cmp expect.file2 file2 &&
+	test_cmp expect.file3 file3 &&
+	git rev-parse --verify HEAD^^ &&
+	test_must_fail git rev-parse --verify HEAD^^^
+
+'
+
 test_done
-- 
1.7.6
Previous: Ramkumar RamachandraNext: Junio C Hamano
Message 20 of 43 in “Generalized sequencer foundations”
  1. 0/7 Generalized sequencer foundationsRamkumar Ramachandra, Aug 14, 2011
  2. 1/7 revert: Free memory after get_message callRamkumar Ramachandra, Aug 14, 2011
  3. Jonathan NiederAug 14, 2011
  4. 2/7 revert: Fix buffer overflow in insn sheet parserRamkumar Ramachandra, Aug 14, 2011
  5. Jonathan NiederAug 14, 2011
  6. Ramkumar RamachandraAug 14, 2011
  7. 3/7 revert: Make commit descriptions in insn sheet optionalRamkumar Ramachandra, Aug 14, 2011
  8. Jonathan NiederAug 14, 2011
  9. Ramkumar RamachandraAug 14, 2011
  10. 4/7 revert: Allow mixed pick and revert instructionsRamkumar Ramachandra, Aug 14, 2011
  11. Jonathan NiederAug 14, 2011
  12. Ramkumar RamachandraAug 14, 2011
  13. Jonathan NiederAug 14, 2011
  14. 5/7 revert: Make the argument parser responsible for setup_revisionsRamkumar Ramachandra, Aug 14, 2011
  15. Jonathan NiederAug 14, 2011
  16. Ramkumar RamachandraAug 14, 2011
  17. 6/7 sequencer: Expose API to cherry-picking machineryRamkumar Ramachandra, Aug 14, 2011
  18. Jonathan NiederAug 14, 2011
  19. Ramkumar RamachandraAug 14, 2011
  20. Jonathan NiederAug 14, 2011
  21. Junio C HamanoAug 16, 2011
  22. revert: plug memory leak in "cherry-pick root commit" codepathJonathan Nieder, Aug 16, 2011
  23. Jonathan NiederAug 16, 2011
  24. Jeff KingAug 16, 2011
  25. Jonathan NiederAug 16, 2011
  26. 7/7 sequencer: Remove sequencer state after final commitRamkumar Ramachandra, Aug 14, 2011
  27. Jonathan NiederAug 14, 2011
  28. Ramkumar RamachandraAug 14, 2011
  29. Jonathan NiederAug 14, 2011
  30. Junio C HamanoAug 14, 2011
  31. Jonathan NiederAug 14, 2011
  32. Junio C HamanoAug 14, 2011
  33. Junio C HamanoAug 15, 2011
  34. Ramkumar RamachandraAug 17, 2011
  35. Ramkumar RamachandraAug 18, 2011
  36. Jonathan NiederAug 18, 2011
  37. Ramkumar RamachandraAug 18, 2011
  38. Jonathan NiederAug 18, 2011
  39. Ramkumar RamachandraAug 18, 2011
  40. Jonathan NiederAug 18, 2011
  41. Junio C HamanoAug 18, 2011
  42. Junio C HamanoAug 18, 2011
  43. Ramkumar RamachandraAug 19, 2011

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.