Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apply: make git apply respect core.fileMode settings #1620

Closed
wants to merge 1 commit into from

Conversation

Chand-ra
Copy link

@Chand-ra Chand-ra commented Dec 10, 2023

Regarding this:

I am wondering if we want to be more strict about hiding error
return code from "git ls-files" and "git ls-tree" behind pipes
like these. Usually we encourage using a temporary file, e.g.,
...
git ls-files -s script.sh >ls-files-output &&
test_grep "^100755" ls-files-output &&
...

I have modified the patch so that the output of git ls-files and
git ls-tree are stored in a temporary file instead of being directly
piped to grep but also noticed similar working in other test cases
in the same test file. For example,

test_expect_success FILEMODE 'same mode (index only)' '
....
....
....
git ls-files -s file | grep "^100755"

and

test_expect_success FILEMODE 'mode update (index only)' '
...
...
...
git ls-files -s file | grep "^100755"

Would we want to modify these scripts as well so they follow the
same convention as above or is it okay to let them be as is?

cc: Torsten Bögershausen tboegi@web.de
cc: Johannes Schindelin Johannes.Schindelin@gmx.de

Copy link

gitgitgadget bot commented Dec 10, 2023

Welcome to GitGitGadget

Hi @Chand-ra, and welcome to GitGitGadget, the GitHub App to send patch series to the Git mailing list from GitHub Pull Requests.

Please make sure that your Pull Request has a good description, as it will be used as cover letter. You can CC potential reviewers by adding a footer to the PR description with the following syntax:

CC: Revi Ewer <revi.ewer@example.com>, Ill Takalook <ill.takalook@example.net>

Also, it is a good idea to review the commit messages one last time, as the Git project expects them in a quite specific form:

  • the lines should not exceed 76 columns,
  • the first line should be like a header and typically start with a prefix like "tests:" or "revisions:" to state which subsystem the change is about, and
  • the commit messages' body should be describing the "why?" of the change.
  • Finally, the commit messages should end in a Signed-off-by: line matching the commits' author.

It is in general a good idea to await the automated test ("Checks") in this Pull Request before contributing the patches, e.g. to avoid trivial issues such as unportable code.

Contributing the patches

Before you can contribute the patches, your GitHub username needs to be added to the list of permitted users. Any already-permitted user can do that, by adding a comment to your PR of the form /allow. A good way to find other contributors is to locate recent pull requests where someone has been /allowed:

Both the person who commented /allow and the PR author are able to /allow you.

An alternative is the channel #git-devel on the Libera Chat IRC network:

<newcontributor> I've just created my first PR, could someone please /allow me? https://github.com/gitgitgadget/git/pull/12345
<veteran> newcontributor: it is done
<newcontributor> thanks!

Once on the list of permitted usernames, you can contribute the patches to the Git mailing list by adding a PR comment /submit.

If you want to see what email(s) would be sent for a /submit request, add a PR comment /preview to have the email(s) sent to you. You must have a public GitHub email address for this. Note that any reviewers CC'd via the list in the PR description will not actually be sent emails.

After you submit, GitGitGadget will respond with another comment that contains the link to the cover letter mail in the Git mailing list archive. Please make sure to monitor the discussion in that thread and to address comments and suggestions (while the comments and suggestions will be mirrored into the PR by GitGitGadget, you will still want to reply via mail).

If you do not want to subscribe to the Git mailing list just to be able to respond to a mail, you can download the mbox from the Git mailing list archive (click the (raw) link), then import it into your mail program. If you use GMail, you can do this via:

curl -g --user "<EMailAddress>:<Password>" \
    --url "imaps://imap.gmail.com/INBOX" -T /path/to/raw.txt

To iterate on your change, i.e. send a revised patch or patch series, you will first want to (force-)push to the same branch. You probably also want to modify your Pull Request description (or title). It is a good idea to summarize the revision by adding something like this to the cover letter (read: by editing the first comment on the PR, i.e. the PR description):

Changes since v1:
- Fixed a typo in the commit message (found by ...)
- Added a code comment to ... as suggested by ...
...

To send a new iteration, just add another PR comment with the contents: /submit.

Need help?

New contributors who want advice are encouraged to join git-mentoring@googlegroups.com, where volunteers who regularly contribute to Git are willing to answer newbie questions, give advice, or otherwise provide mentoring to interested contributors. You must join in order to post or view messages, but anyone can join.

You may also be able to find help in real time in the developer IRC channel, #git-devel on Libera Chat. Remember that IRC does not support offline messaging, so if you send someone a private message and log out, they cannot respond to you. The scrollback of #git-devel is archived, though.

@Ikke
Copy link

Ikke commented Dec 10, 2023

/allow

Copy link

gitgitgadget bot commented Dec 10, 2023

User Chand-ra is now allowed to use GitGitGadget.

WARNING: Chand-ra has no public email address set on GitHub;
GitGitGadget needs an email address to Cc: you on your contribution, so that you receive any feedback on the Git mailing list. Go to https://github.com/settings/profile to make your preferred email public to let GitGitGadget know which email address to use.

Copy link
Member

@dscho dscho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Chand-ra thank you for working on this!

It would probably make sense to refer to #1555 in the PR description (something like "This closes #1555").

Also, could you integrate the script I provided in #1555 (comment) into the patch so that a new test case runs every time the test suite runs and thereby avoids regressions? t/t4129-apply-samemode.sh strikes me as a natural home for this new test case.

apply.c Outdated Show resolved Hide resolved
@dscho
Copy link
Member

dscho commented Dec 10, 2023

could you integrate the script I provided in #1555 (comment)

I should note that the part setting up the repository won't be necessary in the t4129, as it is done automatically by the test suite framework. Further, you will want to turn git config into test_config (which instructs the test framework to unset the key/value after the test case concluded), and you will want to insert a test_tick && before each git commit call (which will make the commit hashes stable by forcing the committer/author name/time to repeatable values).

@Chand-ra Chand-ra marked this pull request as draft December 10, 2023 13:08
@Chand-ra Chand-ra marked this pull request as ready for review December 10, 2023 16:07
@Chand-ra
Copy link
Author

@dscho thanks a lot for the informative review! I have made the changes you suggested, let me know if there is anything else that needs to be done before I submit this patch.

Copy link
Member

@dscho dscho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great progress! Just a couple of finishing touches, then the patch is ready for the Git mailing list!

apply.c Show resolved Hide resolved
t/t4129-apply-samemode.sh Outdated Show resolved Hide resolved
t/t4129-apply-samemode.sh Outdated Show resolved Hide resolved
@Chand-ra Chand-ra changed the title Make git apply respect core.fileMode settings apply: make git apply respect core.fileMode settings Dec 15, 2023
Copy link
Member

@dscho dscho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you do a last touch-up of the commit message? I'd like you to turn the CC: into a Reviewed-by: and fill the body of the commit message with some background along the lines of https://github.blog/2022-06-30-write-better-commits-build-better-projects/: Explain intent (I guess the commit title does a good job of that already) and context (what does core.fileMode do and in what scenarios is that relevant?), then implementation (anything that's not obvious from the diff is a good thing to add here) and justification ("the warning is annoying and not helpful", maybe?).

@dscho
Copy link
Member

dscho commented Dec 17, 2023

@Chand-ra excellent, thank you for addressing my feedback! I left a few ideas how to make the commit message more compelling (which always helps patches on the Git mailing list to go through more smoothly, seeing as reviewers pay a lot of attention to the care put into commit messages), but apart from that, this patch is good to go from my side!

@Chand-ra
Copy link
Author

/preview

Copy link

gitgitgadget bot commented Dec 18, 2023

Preview email sent as pull.1620.git.1702906703811.gitgitgadget@gmail.com

@Chand-ra
Copy link
Author

/submit

Copy link

gitgitgadget bot commented Dec 18, 2023

Submitted as pull.1620.git.1702908568890.gitgitgadget@gmail.com

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-1620/Chand-ra/devel-v1

To fetch this version to local tag pr-1620/Chand-ra/devel-v1:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-1620/Chand-ra/devel-v1

Copy link

gitgitgadget bot commented Dec 18, 2023

On the Git mailing list, Junio C Hamano wrote (reply to this):

"Chandra Pratap via GitGitGadget" <gitgitgadget@gmail.com> writes:

> From: Chandra Pratap <chandrapratap3519@gmail.com>
>

This part goes in the final commit when the patch gets applied.
Everything between the three-dash line and the patch (i.e., the
first "diff --get" line) are discarded.  Move what you wrote below
here to make it the proposed log message for this patch.

Assuming that gets done, let's review what will become the proposed
log message.

> CC: Johannes Schindelin <johannes.schindelin@gmail.com>
> Signed-off-by: Chandra Pratap <chandrapratap3519@gmail.com>
> ---
>     apply: make git apply respect core.fileMode settings
>     
>     When applying a patch that adds an executable file, git apply ignores
>     the core.fileMode setting (core.fileMode in git config specifies whether
>     the executable bit on files in the working tree
>     should be honored or not) resulting in warnings like:
>     
>     warning: script.sh has type 100644, expected 100755
>     
>     even when core.fileMode is set to false, which is undesired. This is
>     extra true for systems like Windows which don't rely on lsat().

"lstat()" you mean.  Add "," between "Windows" and " which".

>     Fix this by inferring the correct file mode from the existing index
>     entry when core.filemode is set to false. The added test case helps
>     verify the change and prevents future regression.

Perfect.

>     
>     Reviewed-by: Johannes Schindelin johannes.schindelin@gmail.com
>     Signed-off-by: Chandra Pratap chandrapratap3519@gmail.com

The e-mail addresses somehow lost <angle brakets> around them.

> diff --git a/apply.c b/apply.c
> index 3d69fec836d..56790f515e0 100644
> --- a/apply.c
> +++ b/apply.c
> @@ -3778,8 +3778,11 @@ static int check_preimage(struct apply_state *state,
>  		return error_errno("%s", old_name);
>  	}
>  
> -	if (!state->cached && !previous)
> -		st_mode = ce_mode_from_stat(*ce, st->st_mode);
> +	if (!state->cached && !previous) {
> +		if (!trust_executable_bit && patch->old_mode)
> +			st_mode = patch->old_mode;
> +		else st_mode = ce_mode_from_stat(*ce, st->st_mode);
> +	}

Write the body of the "else" clause on a separate line.

More importantly, even though we know we cannot trust st->st_mode on
such a filesystem (that is what !trust_executable_bit is about),
once we have a cache entry in the in-core index, shouldn't we trust
ce->ce_mode more than what the incoming patch says?  Or is the
executable bit of a cache-entry totally hosed on a platform with
!trust_executable_bit?

I thought the way things should work was

 (1) "--chmod=+x", which you used in the test, should mark the added
     path executable in the index.  Writing that to a tree (by
     making a commit) should record script.sh as an executable
     (i.e., "git ls-tree -r" should show 100755 not 100644).

 (2) if you read such a tree, then the index will have the "correct"
     executable bit in the cache entry (i.e., "git ls-files -s"
     should show 100755 not 100644).

IOW, I am wondering if the above should look more like

	if (!state->cached && !previous) {
		if (!trust_executable_bit) {
			if (*ce)
				st_mode = (*ce)->ce_mode;
			else
				st_mode = patch->old_mode;
		} else {
			st_mode = ce_mode_from_stat(*ce, st->st_mode);
		}
	}

As setting patch->old_mode to st_mode is equivalent to saying "we
blindly trust the data on the patch much more than what we know
about the current repository state", which goes directly against
what "check_preimage()" wants to achieve.

>  
> diff --git a/t/t4129-apply-samemode.sh b/t/t4129-apply-samemode.sh
> index e7a7295f1b6..95917fee128 100755
> --- a/t/t4129-apply-samemode.sh
> +++ b/t/t4129-apply-samemode.sh
> @@ -101,4 +101,19 @@ test_expect_success POSIXPERM 'do not use core.sharedRepository for working tree
>  	)
>  '
>  
> +test_expect_success FILEMODE 'ensure git apply respects core.fileMode' '
> +	test_config core.fileMode false &&
> +	echo true >script.sh &&
> +	git add --chmod=+x script.sh &&

Perhaps we would want to check with "git ls-files -s script.sh" what
its mode bits are (hopefully it would be executable).

> +	test_tick && git commit -m "Add script" &&

Similarly, check with "git ls-tree -r HEAD script.sh" what its mode
bits are?

> +	echo true >>script.sh &&
> +	test_tick && git commit -m "Modify script" script.sh &&

Ditto.

> +	git format-patch -1 --stdout >patch &&

Check that the patch expects script.sh to have its executable bit
here, too?

> +	git switch -c branch HEAD^ &&
> +	git apply patch 2>err &&

We may also want to check "git apply --cached" and "git apply --index"
here, not just the "poor-man's GNU patch emulation" mode.

> +	! test_grep "has type 100644, expected 100755" err

If you use test_grep, the correct negation is not like that, but

	test_grep ! "has type 100644, expected 100755" err

Giving a better diagnosis when the expectation is violated is the
whole point of using "test_grep" not a vanilla "grep", so we need to
tell it that we are reversing our expectations.

Thanks.

> +'
> +
>  test_done
>
> base-commit: 1a87c842ece327d03d08096395969aca5e0a6996

Copy link

gitgitgadget bot commented Dec 18, 2023

On the Git mailing list, Junio C Hamano wrote (reply to this):

Junio C Hamano <gitster@pobox.com> writes:

>> +test_expect_success FILEMODE 'ensure git apply respects core.fileMode' '

Forgot to point out the most important thing.

The code change in this patch is primarily about making the code
work better for folks without trustworthy filemode support.
Emulating what happens by setting core.fileMode to false on a
platform with capable filesystems may be a way to test the code, but
we should have a test specific to folks without FILEMODE
prerequisites and make sure it works well, no?

IOW, shouldn't we drom FILEMODE prerequisite from this test?  How
does it break on say Windows if this test is added without FILEMODE
prerequisite?

Copy link

gitgitgadget bot commented Dec 19, 2023

On the Git mailing list, Chandra Pratap wrote (reply to this):

Thanks for the review, Junio. I have considered your feedback and
will adjust the patch as such. The mail formatting issues seem to
have arisen from my invigilant use of GitGitGadget.

> Junio C Hamano <gitster@pobox.com> writes:
>
> IOW, I am wondering if the above should look more like
>
>        if (!state->cached && !previous) {
>                if (!trust_executable_bit) {
>                        if (*ce)
>                                st_mode = (*ce)->ce_mode;
>                        else
>                                st_mode = patch->old_mode;
>                } else {
>                       st_mode = ce_mode_from_stat(*ce, st->st_mode);
>                }
>      }

You're right, we should prioritise the file mode info from the
existing cache entry (if one exists) instead of blindly assigning
the one from the incoming patch. It's more robust that way.

> Perhaps we would want to check with "git ls-files -s script.sh" what
> its mode bits are (hopefully it would be executable).
>
> Similarly, check with "git ls-tree -r HEAD script.sh" what its mode>
> bits are?
>
> Check that the patch expects script.sh to have its executable bit
> here, too?

I assume we're doing all this filemode checking to ensure that the
executable bit doesn't get lost due to any other git command?

> The code change in this patch is primarily about making the code
> work better for folks without trustworthy filemode support.
> Emulating what happens by setting core.fileMode to false on a
> platform with capable filesystems may be a way to test the code, but
> we should have a test specific to folks without FILEMODE
> prerequisites and make sure it works well, no?
>
> IOW, shouldn't we drom FILEMODE prerequisite from this test?

I will keep that in mind.

Copy link

gitgitgadget bot commented Dec 19, 2023

User Chandra Pratap <chandrapratap3519@gmail.com> has been added to the cc: list.

@Chand-ra
Copy link
Author

/preview

Copy link

gitgitgadget bot commented Dec 19, 2023

Preview email sent as pull.1620.v2.git.1703009578723.gitgitgadget@gmail.com

@Chand-ra
Copy link
Author

/preview

Copy link

gitgitgadget bot commented Dec 19, 2023

Preview email sent as pull.1620.v2.git.1703009963932.gitgitgadget@gmail.com

@Chand-ra
Copy link
Author

/preview

Copy link

gitgitgadget bot commented Feb 8, 2024

This patch series was integrated into seen via git@b96ffa4.

Copy link

gitgitgadget bot commented Feb 9, 2024

This patch series was integrated into seen via git@63fc4ad.

Copy link

gitgitgadget bot commented Feb 9, 2024

There was a status update in the "Cooking" section about the branch cp/apply-core-filemode on the Git mailing list:

"git apply" on a filesystem without filemode support have learned
to take a hint from what is in the index for the path, even when
not working with the "--index" or "--cached" option, when checking
the executable bit match what is required by the preimage in the
patch.

Will cook in 'next'.
cf. <xmqqzfwb53a9.fsf@gitster.g>
source: <20231226233218.472054-1-gitster@pobox.com>

Copy link

gitgitgadget bot commented Feb 10, 2024

This patch series was integrated into seen via git@3719a69.

Copy link

gitgitgadget bot commented Feb 12, 2024

This patch series was integrated into seen via git@ac5ea45.

Copy link

gitgitgadget bot commented Feb 12, 2024

This patch series was integrated into seen via git@3449aac.

Copy link

gitgitgadget bot commented Feb 13, 2024

This patch series was integrated into seen via git@f4c06b9.

Copy link

gitgitgadget bot commented Feb 14, 2024

There was a status update in the "Cooking" section about the branch cp/apply-core-filemode on the Git mailing list:

"git apply" on a filesystem without filemode support have learned
to take a hint from what is in the index for the path, even when
not working with the "--index" or "--cached" option, when checking
the executable bit match what is required by the preimage in the
patch.

Will cook in 'next'.
cf. <xmqqzfwb53a9.fsf@gitster.g>
source: <20231226233218.472054-1-gitster@pobox.com>

Copy link

gitgitgadget bot commented Feb 14, 2024

This patch series was integrated into seen via git@044aaa6.

Copy link

gitgitgadget bot commented Feb 15, 2024

This patch series was integrated into seen via git@0b5eb50.

Copy link

gitgitgadget bot commented Feb 15, 2024

This patch series was integrated into seen via git@9a521d4.

Copy link

gitgitgadget bot commented Feb 16, 2024

There was a status update in the "Cooking" section about the branch cp/apply-core-filemode on the Git mailing list:

"git apply" on a filesystem without filemode support have learned
to take a hint from what is in the index for the path, even when
not working with the "--index" or "--cached" option, when checking
the executable bit match what is required by the preimage in the
patch.

Will cook in 'next'.
cf. <xmqqzfwb53a9.fsf@gitster.g>
source: <20231226233218.472054-1-gitster@pobox.com>

Copy link

gitgitgadget bot commented Feb 18, 2024

On the Git mailing list, Johannes Schindelin wrote (reply to this):

Hi Junio,

On Wed, 7 Feb 2024, Junio C Hamano wrote:

> Junio C Hamano <gitster@pobox.com> writes:
>
> > Chandra Pratap noticed that "git apply" on a filesystem without
> > executable bit support gives a warning when applying a patch that
> > expects the preimage file to have executable bit on.  Dscho noticed
> > that the initial fix by Chandra did not work well when applying a
> > patch in reverse.  It turns out that apply.c:reverse_patches()
> > invalidates the "a patch that does not change mode bits have the
> > mode bits in .old_mode member and not in .new_mode member" invariant
> > we rely on.
> >
> > Here is the result of concerted effort.
> >
> > Chandra Pratap (1):
> >   apply: ignore working tree filemode when !core.filemode
> >
> > Junio C Hamano (2):
> >   apply: correctly reverse patch's pre- and post-image mode bits
> >   apply: code simplification
> >
> >  apply.c                   | 16 +++++++++++++---
> >  t/t4129-apply-samemode.sh | 27 +++++++++++++++++++++++++++
> >  2 files changed, 40 insertions(+), 3 deletions(-)
>
> Anybody wants to offer a review on this?  I actually am fairly
> confortable with these without any additional review, but since I am
> sweeping the "Needs review" topics in the What's cooking report, I
> thought I would ask for this one, too.

I just had a look over all three of the patches, and to me, they look good
to go.

Ciao,
Johannes

Copy link

gitgitgadget bot commented Feb 19, 2024

On the Git mailing list, Junio C Hamano wrote (reply to this):

Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:

>> > Chandra Pratap (1):
>> >   apply: ignore working tree filemode when !core.filemode
>> >
>> > Junio C Hamano (2):
>> >   apply: correctly reverse patch's pre- and post-image mode bits
>> >   apply: code simplification
>> >
>> >  apply.c                   | 16 +++++++++++++---
>> >  t/t4129-apply-samemode.sh | 27 +++++++++++++++++++++++++++
>> >  2 files changed, 40 insertions(+), 3 deletions(-)
>>
>> Anybody wants to offer a review on this?  I actually am fairly
>> confortable with these without any additional review, but since I am
>> sweeping the "Needs review" topics in the What's cooking report, I
>> thought I would ask for this one, too.
>
> I just had a look over all three of the patches, and to me, they look good
> to go.

Thanks.

Copy link

gitgitgadget bot commented Feb 20, 2024

This patch series was integrated into seen via git@13b7d25.

Copy link

gitgitgadget bot commented Feb 20, 2024

This patch series was integrated into seen via git@183b4bf.

Copy link

gitgitgadget bot commented Feb 20, 2024

This patch series was integrated into seen via git@796ecfe.

Copy link

gitgitgadget bot commented Feb 20, 2024

There was a status update in the "Cooking" section about the branch cp/apply-core-filemode on the Git mailing list:

"git apply" on a filesystem without filemode support have learned
to take a hint from what is in the index for the path, even when
not working with the "--index" or "--cached" option, when checking
the executable bit match what is required by the preimage in the
patch.

Will cook in 'next'.
cf. <xmqqzfwb53a9.fsf@gitster.g>
source: <20231226233218.472054-1-gitster@pobox.com>

Copy link

gitgitgadget bot commented Feb 20, 2024

This patch series was integrated into seen via git@e2b6517.

Copy link

gitgitgadget bot commented Feb 23, 2024

This patch series was integrated into seen via git@79f036a.

Copy link

gitgitgadget bot commented Feb 23, 2024

There was a status update in the "Cooking" section about the branch cp/apply-core-filemode on the Git mailing list:

"git apply" on a filesystem without filemode support have learned
to take a hint from what is in the index for the path, even when
not working with the "--index" or "--cached" option, when checking
the executable bit match what is required by the preimage in the
patch.

Will cook in 'next'.
cf. <xmqqzfwb53a9.fsf@gitster.g>
source: <20231226233218.472054-1-gitster@pobox.com>

Copy link

gitgitgadget bot commented Feb 23, 2024

This patch series was integrated into seen via git@0fcab17.

Copy link

gitgitgadget bot commented Feb 24, 2024

This patch series was integrated into seen via git@f9e6dd3.

Copy link

gitgitgadget bot commented Feb 27, 2024

This patch series was integrated into seen via git@cf47fb7.

Copy link

gitgitgadget bot commented Feb 27, 2024

This patch series was integrated into master via git@cf47fb7.

Copy link

gitgitgadget bot commented Feb 27, 2024

This patch series was integrated into next via git@cf47fb7.

@gitgitgadget gitgitgadget bot added the master label Feb 27, 2024
@gitgitgadget gitgitgadget bot closed this Feb 27, 2024
Copy link

gitgitgadget bot commented Feb 27, 2024

Closed via cf47fb7.

@Chand-ra Chand-ra deleted the devel branch May 10, 2024 14:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants