git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] blame: allow --contents to work with bare repo
@ 2023-07-19 10:48 韩仰
  2023-07-19 21:21 ` Junio C Hamano
  2023-07-21  3:57 ` [PATCH v2] " Han Young
  0 siblings, 2 replies; 4+ messages in thread
From: 韩仰 @ 2023-07-19 10:48 UTC (permalink / raw)
  To: git@vger.kernel.org; +Cc: jacob.keller@gmail.com

The --contents option can be used with git blame to blame the file as if
it had the contents from the specified file. Since
1a3119ed06c8fbb1c00a6aa3615299252575abab ("blame: allow --contents to
work with non-HEAD commit"), the --contents option can work with
non-HEAD commit. However, if you try to use --contents in a bare
repository, you get the following error:

    fatal: this operation must be run in a work tree

This is because before trying to generate a fake working tree commit,
we always call setup_work_tree. But in a bare repo, work tree is not
available. The call to setup_work_tree is used to prepare the reading
of the blamed file in the work tree, which isn't neccessary if we are
reading the contents from the specific file instead of the file in the
work tree.

Add a check in setup_scoreboard to skip setup_work_tree if we are
reading from the file specified in --contents.

This enables us to use --contents in a bare repo. This is a nice
addtion on top of 1a3119ed06c8fbb1c00a6aa3615299252575abab, having a
working tree to use --contents is optional.

Add test for the --contents option with bare repo to the
annotate-tests.sh test script.

Signed-off-by: Han Young <hanyang.tony@bytedance.com>
---
Before commit 1a3119ed06c8fbb1c00a6aa3615299252575abab using
--contents in a bare repo will lead to

        fatal: cannot use --contents with final commit object name

even without specify a revision, this is because in a bare repo, HEAD
is seem as a passed revision. After
1a3119ed06c8fbb1c00a6aa3615299252575abab, git will try to
setup_work_tree as long as --contents is passed. So we still cannot
use --contents on a bare repo.

I was comparing files between a bare repo on the server and my local
partial cloned repo. It is convenient for me to be able to use
--contents option on a bare repo, because the project is very large
and would occupy a lot of disk space if fully cloned on local machine.

With this change I can upload my local file to the server and get the
blame result without keeping a full clone of the repo on my local
machine.

 blame.c             | 4 +++-
 t/annotate-tests.sh | 9 +++++++++
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/blame.c b/blame.c
index d12bd9f97b..0b6703d636 100644
--- a/blame.c
+++ b/blame.c
@@ -2806,7 +2806,9 @@ void setup_scoreboard(struct blame_scoreboard *sb,
                         parent_oid = &head_oid;
                 }

-                setup_work_tree();
+                if (!sb->contents_from) {
+                        setup_work_tree();
+                }
                 sb->final = fake_working_tree_commit(sb->repo,
                                                      &sb->revs->diffopt,
                                                      sb->path,
sb->contents_from,
diff --git a/t/annotate-tests.sh b/t/annotate-tests.sh
index 2ef70235b1..5e21e84f38 100644
--- a/t/annotate-tests.sh
+++ b/t/annotate-tests.sh
@@ -83,6 +83,15 @@ test_expect_success 'blame with --contents' '
         check_count --contents=file A 2
 '

+test_expect_success 'blame with --contents in a bare repo' '
+        git clone --bare . bare-contents.git &&
+        (
+                cd bare-contents.git &&
+                echo "1A quick brown fox jumps over the" >contents &&
+                check_count --contents=contents A 1
+        )
+'
+
 test_expect_success 'blame with --contents changed' '
         echo "1A quick brown fox jumps over the" >contents &&
         echo "another lazy dog" >>contents &&
--
2.40.0

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] blame: allow --contents to work with bare repo
  2023-07-19 10:48 [PATCH] blame: allow --contents to work with bare repo 韩仰
@ 2023-07-19 21:21 ` Junio C Hamano
  2023-07-21  3:57 ` [PATCH v2] " Han Young
  1 sibling, 0 replies; 4+ messages in thread
From: Junio C Hamano @ 2023-07-19 21:21 UTC (permalink / raw)
  To: 韩仰; +Cc: git@vger.kernel.org, jacob.keller@gmail.com

韩仰 <hanyang.tony@bytedance.com> writes:

> -                setup_work_tree();
> +                if (!sb->contents_from) {
> +                        setup_work_tree();
> +                }

Unwanted {braces} around a single statement.

The patch is severely whitespace damaged and does not apply.

Please check your outgoing mail route and make sure it does not
happen when you send out an updated patch.  Sending an e-mail to
yourself and checking what you received may also work as a way to
sanity-check without bombarding other people and the list with a
test message.

Thanks.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2] blame: allow --contents to work with bare repo
  2023-07-19 10:48 [PATCH] blame: allow --contents to work with bare repo 韩仰
  2023-07-19 21:21 ` Junio C Hamano
@ 2023-07-21  3:57 ` Han Young
  2023-07-21 14:32   ` Junio C Hamano
  1 sibling, 1 reply; 4+ messages in thread
From: Han Young @ 2023-07-21  3:57 UTC (permalink / raw)
  To: git; +Cc: Han Young

The --contents option can be used with git blame to blame the file as if
it had the contents from the specified file. Since 1a3119ed06c8fbb1c00a6aa3615299252575abab ("blame: allow --contents to work with non-HEAD commit"), the --contents option can work with non-HEAD commit. However, if you try to use --contents in a bare repository, you get the following error:

    fatal: this operation must be run in a work tree

This is because before trying to generate a fake working tree commit, we always call setup_work_tree. But in a bare repo, work tree is not available. The call to setup_work_tree is used to prepare the reading of the blamed file in the work tree, which isn't necessary if we are reading the contents from the specific file instead of the file in the work tree.

Add a check in setup_scoreboard to skip setup_work_tree if we are reading from the file specified in --contents.

This enables us to use --contents in a bare repo. This is a nice addition on top of 1a3119ed06c8fbb1c00a6aa3615299252575abab, having a working tree to use --contents is optional.

Add test for the --contents option with bare repo to the annotate-tests.sh test script.

Signed-off-by: Han Young <hanyang.tony@bytedance.com>
---
Changes since v1:
* Removed unwanted {braces} around a single statement
* Sent the email properly with git-send-email so the tabs are preserved
* Corrected typos in commit message

Before commit 1a3119ed06c8fbb1c00a6aa3615299252575abab using --contents in a bare repo will lead to

	fatal: cannot use --contents with final commit object name

even without specify a revision, this is because in a bare repo, HEAD is seem as a passed revision. After 1a3119ed06c8fbb1c00a6aa3615299252575abab, git will try to setup_work_tree as long as --contents is passed. So we still cannot use --contents on a bare repo.

I was comparing files between a bare repo on the server and my local partial cloned repo. It is convenient for me to be able to use --contents option on a bare repo, because the project is very large and would occupy a lot of disk space if fully cloned on local machine.

With this change I can upload my local file to the server and get the blame result without keeping a full clone of the repo on my local machine.

 blame.c             | 4 +++-
 t/annotate-tests.sh | 9 +++++++++
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/blame.c b/blame.c
index d12bd9f97..141756975 100644
--- a/blame.c
+++ b/blame.c
@@ -2806,7 +2806,9 @@ void setup_scoreboard(struct blame_scoreboard *sb,
 			parent_oid = &head_oid;
 		}
 
-		setup_work_tree();
+		if (!sb->contents_from)
+			setup_work_tree();
+
 		sb->final = fake_working_tree_commit(sb->repo,
 						     &sb->revs->diffopt,
 						     sb->path, sb->contents_from,
diff --git a/t/annotate-tests.sh b/t/annotate-tests.sh
index 2ef70235b..5e21e84f3 100644
--- a/t/annotate-tests.sh
+++ b/t/annotate-tests.sh
@@ -83,6 +83,15 @@ test_expect_success 'blame with --contents' '
 	check_count --contents=file A 2
 '
 
+test_expect_success 'blame with --contents in a bare repo' '
+	git clone --bare . bare-contents.git &&
+	(
+		cd bare-contents.git &&
+		echo "1A quick brown fox jumps over the" >contents &&
+		check_count --contents=contents A 1
+	)
+'
+
 test_expect_success 'blame with --contents changed' '
 	echo "1A quick brown fox jumps over the" >contents &&
 	echo "another lazy dog" >>contents &&
-- 
2.40.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] blame: allow --contents to work with bare repo
  2023-07-21  3:57 ` [PATCH v2] " Han Young
@ 2023-07-21 14:32   ` Junio C Hamano
  0 siblings, 0 replies; 4+ messages in thread
From: Junio C Hamano @ 2023-07-21 14:32 UTC (permalink / raw)
  To: Han Young; +Cc: git

Han Young <hanyang.tony@bytedance.com> writes:

> The --contents option can be used with git blame to blame the file as if
> it had the contents from the specified file. Since 1a3119ed06c8fbb1c00a6aa3615299252575abab ("blame: allow --contents to work with non-HEAD commit"), the --contents option can work with non-HEAD commit. However, if you try to use --contents in a bare repository, you get the following error:

When using the "show -s --pretty=reference --oneline", abbreviate
the commit object name to a reasonable length (like 8 to 12
hexadecimal digits).  Please wrap overlong lines.

I'll fix them up at the receiving end, if there is no other issues
in the patch.  Let's read on.

>  blame.c             | 4 +++-
>  t/annotate-tests.sh | 9 +++++++++
>  2 files changed, 12 insertions(+), 1 deletion(-)
>
> diff --git a/blame.c b/blame.c
> index d12bd9f97..141756975 100644
> --- a/blame.c
> +++ b/blame.c
> @@ -2806,7 +2806,9 @@ void setup_scoreboard(struct blame_scoreboard *sb,
>  			parent_oid = &head_oid;
>  		}
>  
> -		setup_work_tree();
> +		if (!sb->contents_from)
> +			setup_work_tree();
> +
>  		sb->final = fake_working_tree_commit(sb->repo,
>  						     &sb->revs->diffopt,
>  						     sb->path, sb->contents_from,

Thanks.

> diff --git a/t/annotate-tests.sh b/t/annotate-tests.sh
> index 2ef70235b..5e21e84f3 100644
> --- a/t/annotate-tests.sh
> +++ b/t/annotate-tests.sh
> @@ -83,6 +83,15 @@ test_expect_success 'blame with --contents' '
>  	check_count --contents=file A 2
>  '
>  
> +test_expect_success 'blame with --contents in a bare repo' '
> +	git clone --bare . bare-contents.git &&
> +	(
> +		cd bare-contents.git &&
> +		echo "1A quick brown fox jumps over the" >contents &&
> +		check_count --contents=contents A 1
> +	)
> +'
> +
>  test_expect_success 'blame with --contents changed' '
>  	echo "1A quick brown fox jumps over the" >contents &&
>  	echo "another lazy dog" >>contents &&

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-07-21 14:33 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-07-19 10:48 [PATCH] blame: allow --contents to work with bare repo 韩仰
2023-07-19 21:21 ` Junio C Hamano
2023-07-21  3:57 ` [PATCH v2] " Han Young
2023-07-21 14:32   ` Junio C Hamano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).