# subtle bug in git-am

4 messages from 2020-04-07 to 2020-04-30. Participants: Andy Shevchenko, brian m. carlson.
Thread: https://gitlist.dev/t/53182

## Andy Shevchenko, 2020-04-07 18:48

Subject: subtle bug in git-am
Message-ID: <20200407184823.GA3932679@smile.fi.intel.com>
URL: https://gitlist.dev/e/20200407184823.GA3932679%40smile.fi.intel.com

```
Hi!

What I would like to do is to apply patch from one repository to another with
same files but *different directory structure*.

When I try to change directory in the target repo to the folder of files, I run
git-am -p5 my_cool_patch.patch.

Instead of the expected result (files and their contents is the same!) I got
fileXXX is not in index.

So, I think this is a bug, because -p<n> use in git-am makes little to no sense
without above feature.

Perhaps I didn't understand something.
Please, advise.

$ git version
git version 2.25.1


-- 
With Best Regards,
Andy Shevchenko



```

## brian m. carlson, 2020-04-08 01:18

Subject: Re: subtle bug in git-am
Message-ID: <20200408011830.GA6549@camp.crustytoothpaste.net>
URL: https://gitlist.dev/e/20200408011830.GA6549%40camp.crustytoothpaste.net
In-Reply-To: <20200407184823.GA3932679@smile.fi.intel.com>

```
On 2020-04-07 at 18:48:23, Andy Shevchenko wrote:
> Hi!
> 
> What I would like to do is to apply patch from one repository to another with
> same files but *different directory structure*.
> 
> When I try to change directory in the target repo to the folder of files, I run
> git-am -p5 my_cool_patch.patch.
> 
> Instead of the expected result (files and their contents is the same!) I got
> fileXXX is not in index.
> 
> So, I think this is a bug, because -p<n> use in git-am makes little to no sense
> without above feature.

So if I understand correctly, you're expecting git am to apply relative
to the current directory in the repository.  I have also expected that
behavior in the past, and found it surprising when it did not.

What git am does is apply relative to the root of the repository.  If
you'd instead like to apply to a specific subdirectory of the
repository, you can use the --directory option to specify to which
directory your patch should apply.

This is the behavior of git apply, which underpins git am.  However,
outside of a repository, it _does_ apply relative to the current
directory, since there's no repository root to consider.  I, at least,
found this confusing, but that's how it works.
-- 
brian m. carlson: Houston, Texas, US
OpenPGP: https://keybase.io/bk2204

```

## Andy Shevchenko, 2020-04-08 09:29

Subject: Re: subtle bug in git-am
Message-ID: <CAHp75Vfuuth05wn-0x_AZa_LFSmQMHB6LGM5b4KNMoaxce1S-g@mail.gmail.com>
URL: https://gitlist.dev/e/CAHp75Vfuuth05wn-0x_AZa_LFSmQMHB6LGM5b4KNMoaxce1S-g%40mail.gmail.com
In-Reply-To: <20200408011830.GA6549@camp.crustytoothpaste.net>

```
On Wed, Apr 8, 2020 at 4:19 AM brian m. carlson
<sandals@crustytoothpaste.net> wrote:
>
> On 2020-04-07 at 18:48:23, Andy Shevchenko wrote:
> > Hi!
> >
> > What I would like to do is to apply patch from one repository to another with
> > same files but *different directory structure*.
> >
> > When I try to change directory in the target repo to the folder of files, I run
> > git-am -p5 my_cool_patch.patch.
> >
> > Instead of the expected result (files and their contents is the same!) I got
> > fileXXX is not in index.
> >
> > So, I think this is a bug, because -p<n> use in git-am makes little to no sense
> > without above feature.
>
> So if I understand correctly, you're expecting git am to apply relative
> to the current directory in the repository.  I have also expected that
> behavior in the past, and found it surprising when it did not.
>
> What git am does is apply relative to the root of the repository.  If
> you'd instead like to apply to a specific subdirectory of the
> repository, you can use the --directory option to specify to which
> directory your patch should apply.
>
> This is the behavior of git apply, which underpins git am.  However,
> outside of a repository, it _does_ apply relative to the current
> directory, since there's no repository root to consider.  I, at least,
> found this confusing, but that's how it works.

Thank you for the prompt reply with useful information. I will try it
whenever I will need similar flow again.


-- 
With Best Regards,
Andy Shevchenko

```

## Andy Shevchenko, 2020-04-30 14:26

Subject: Re: subtle bug in git-am
Message-ID: <CAHp75VceVXMsTRAd9iW8D6-uqVR1Z6A9Crgsvyv_-KaSx7JR9Q@mail.gmail.com>
URL: https://gitlist.dev/e/CAHp75VceVXMsTRAd9iW8D6-uqVR1Z6A9Crgsvyv_-KaSx7JR9Q%40mail.gmail.com
In-Reply-To: <CAHp75Vfuuth05wn-0x_AZa_LFSmQMHB6LGM5b4KNMoaxce1S-g@mail.gmail.com>

```
On Wed, Apr 8, 2020 at 12:29 PM Andy Shevchenko
<andy.shevchenko@gmail.com> wrote:
> On Wed, Apr 8, 2020 at 4:19 AM brian m. carlson
> <sandals@crustytoothpaste.net> wrote:
> > On 2020-04-07 at 18:48:23, Andy Shevchenko wrote:

> > > What I would like to do is to apply patch from one repository to another with
> > > same files but *different directory structure*.
> > >
> > > When I try to change directory in the target repo to the folder of files, I run
> > > git-am -p5 my_cool_patch.patch.
> > >
> > > Instead of the expected result (files and their contents is the same!) I got
> > > fileXXX is not in index.
> > >
> > > So, I think this is a bug, because -p<n> use in git-am makes little to no sense
> > > without above feature.
> >
> > So if I understand correctly, you're expecting git am to apply relative
> > to the current directory in the repository.  I have also expected that
> > behavior in the past, and found it surprising when it did not.
> >
> > What git am does is apply relative to the root of the repository.  If
> > you'd instead like to apply to a specific subdirectory of the
> > repository, you can use the --directory option to specify to which
> > directory your patch should apply.
> >
> > This is the behavior of git apply, which underpins git am.  However,
> > outside of a repository, it _does_ apply relative to the current
> > directory, since there's no repository root to consider.  I, at least,
> > found this confusing, but that's how it works.
>
> Thank you for the prompt reply with useful information. I will try it
> whenever I will need similar flow again.

Today I have needed something similar and I checked proposed w/a and it works!
Thanks!

-- 
With Best Regards,
Andy Shevchenko

```
