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

Re: [PATCH] worktree add: add --lock option

From
Junio C Hamano <gitster@pobox.com>
Date
Apr 15, 2017, 08:07 UTC
Message-ID
<xmqqmvbim6vi.fsf@gitster.mtv.corp.google.com>
In-Reply-To
<xmqqzifim8co.fsf@gitster.mtv.corp.google.com>
Junio C Hamano <gitster@pobox.com> writes:
Show 19 quoted lines
> Nguyễn Thái Ngọc Duy  <pclouds@gmail.com> writes:
>
>> -	unlink_or_warn(sb.buf);
>> +	if (!ret && opts->keep_locked) {
>> +		/*
>> +		 * Don't keep the confusing "initializing" message
>> +		 * after it's already over.
>> +		 */
>> +		truncate(sb.buf, 0);
>> +	} else {
>> +		unlink_or_warn(sb.buf);
>> +	}
>
> builtin/worktree.c: In function 'add_worktree':
> builtin/worktree.c:314:11: error: ignoring return value of 'truncate', declared with attribute warn_unused_result [-Werror=unused-result]
>    truncate(sb.buf, 0);
>            ^
> cc1: all warnings being treated as errors
> make: *** [builtin/worktree.o] Error 1

I wonder why we need to have "initializing" and then remove the string. Wouldn't it be simpler to do something like this instead? Does an empty lockfile have some special meaning?

 builtin/worktree.c      | 16 +++++++---------
 t/t2025-worktree-add.sh |  3 +--
 2 files changed, 8 insertions(+), 11 deletions(-)
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 3dab07c829..5ebdcce793 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -243,7 +243,10 @@ static int add_worktree(const char *path, const char *refname,
 	 * after the preparation is over.
 	 */
 	strbuf_addf(&sb, "%s/locked", sb_repo.buf);
-	write_file(sb.buf, "initializing");
+	if (!opts->keep_locked)
+		write_file(sb.buf, "initializing");
+	else
+		write_file(sb.buf, "added with --lock");
 
 	strbuf_addf(&sb_git, "%s/.git", path);
 	if (safe_create_leading_directories_const(sb_git.buf))
@@ -306,15 +309,10 @@ static int add_worktree(const char *path, const char *refname,
 done:
 	strbuf_reset(&sb);
 	strbuf_addf(&sb, "%s/locked", sb_repo.buf);
-	if (!ret && opts->keep_locked) {
-		/*
-		 * Don't keep the confusing "initializing" message
-		 * after it's already over.
-		 */
-		truncate(sb.buf, 0);
-	} else {
+	if (!ret && opts->keep_locked)
+		;
+	else
 		unlink_or_warn(sb.buf);
-	}
 	argv_array_clear(&child_env);
 	strbuf_release(&sb);
 	strbuf_release(&symref);
diff --git a/t/t2025-worktree-add.sh b/t/t2025-worktree-add.sh
index 6dce920c03..304f25fcd1 100755
--- a/t/t2025-worktree-add.sh
+++ b/t/t2025-worktree-add.sh
@@ -65,8 +65,7 @@ test_expect_success '"add" worktree' '
 
 test_expect_success '"add" worktree with lock' '
 	git rev-parse HEAD >expect &&
-	git worktree add --detach --lock here-with-lock master &&
-	test_must_be_empty .git/worktrees/here-with-lock/locked
+	git worktree add --detach --lock here-with-lock master
 '
 
 test_expect_success '"add" worktree from a subdir' '
Previous: Junio C HamanoNext: Duy Nguyen
Message 9 of 10 in “git work trees”
  1. taylor, davidApr 11, 2017
  2. Duy NguyenApr 12, 2017
  3. worktree add: add --lock optionNguyễn Thái Ngọc Duy, Apr 12, 2017
  4. Junio C HamanoApr 13, 2017
  5. Duy NguyenApr 14, 2017
  6. taylor, davidApr 24, 2017
  7. Jacob KellerApr 14, 2017
  8. Junio C HamanoApr 15, 2017
  9. Junio C HamanoApr 15, 2017
  10. Duy NguyenApr 15, 2017

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.