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

So it doesn't work when not using Git? #115

Closed
OnkelTem opened this issue Mar 8, 2017 · 6 comments
Closed

So it doesn't work when not using Git? #115

OnkelTem opened this issue Mar 8, 2017 · 6 comments

Comments

@OnkelTem
Copy link

OnkelTem commented Mar 8, 2017

I created a patch for a released version of a package and was expecting composer-patches would work but from the output I see that it doesn't:

- Applying patches for drush/drush
    patches/drush/DrupalBoot8-invalidateContainer.patch (Disable invalidateContainer() on every Drush run)
cd '/home/fantomas/777/vendor/drush/drush' && GIT_DIR=. git apply --check '-p1' '/home/fantomas/777/patches/drush/DrupalBoot8-invalidateContainer.patch'

so it runs git to apply patch instead of patch. Is there a way to apply patch to a released (dist) version instead of sources, cuz I don't need any .git repos under vendor/ (as it is distributed with the application)?

@cweagans
Copy link
Owner

cweagans commented Mar 8, 2017

It will work without git. Try it and let me know if it doesn't.

@cweagans cweagans closed this as completed Mar 8, 2017
@OnkelTem
Copy link
Author

OnkelTem commented Mar 8, 2017

Let me explain.

From the project's main page it's clear that packages are expected to be installed as sources:

 "config": {
    "preferred-install": "source"
  }

and when I enabled verbose output and seen that list of git apply errors I started to investigate on this. It didn't say by itself - dude, that's fine, I'm not just a regular fatal error, I could be ignored. And that's confusing.

After some debugging I've found the code which really executes after all attempts to apply a patch i.e. when git apply has been failed:

    // In some rare cases, git will fail to apply a patch, fallback to using
    // the 'patch' command.
    if (!$patched) {
      foreach ($patch_levels as $patch_level) {
        // --no-backup-if-mismatch here is a hack that fixes some
        // differences between how patch works on windows and unix.
        if ($patched = $this->executeCommand("patch %s --no-backup-if-mismatch -d %s < %s", $patch_level, $install_path, $filename)) {
          break;
        }
      }
    }

so it falls back to using patch utility and that's fine. I mean that's fine now, after I've seen that in code.

Still I have two questions:

  1. Why do you call patch utility case — a "rare" case? To my mind (and I can be wrong) it's a natural thing to apply patches on released versions, had I a git repo - I'd just create a fork/branch/whatever. Also, "in some cases" always sounds like a black magic you know. Like ok, git didn't work, let's try patch, darn no way, let's play chess, ok, let's try git again - still doesn't work. Aghr... :)

  2. How do you consider selecting patch method depending on $package->installationSource value? If I'm not mistaking its value can be either 'dist' or 'source'. If it's source - use git, if it's dist - then patch? Just a wild guess.

@cweagans
Copy link
Owner

cweagans commented Mar 8, 2017

The reason preferred-install is source is that git is better at resolving patch conflicts when it has some history to go off of, rather than just the current state of the file. Even if preferred install is dist though, git apply will still work as long as you set the GIT_DIR env variable (or pass the --git-dir argument) to the root of the project that you want to apply a patch to. In the rare case where git apply cannot figure out the patch, we try patch as a last-ditch effort. It's extremely unlikely that it will work, but there have been cases where git can't figure it out and patch can. There's no need to switch between git apply vs patch because git apply provides a superset of the functionality that patch provides. The only reason that patch is even used at all is just as a last safety net to hopefully get the patch applied properly.

@cweagans
Copy link
Owner

cweagans commented Mar 8, 2017

(also, preferred-install: source doesn't really hurt anything. it takes more disk space, but other than that, it should be fine)

@OnkelTem
Copy link
Author

OnkelTem commented Mar 8, 2017

@cweagans

Thank you for the quick reply. It's really big news to me - didn't know that git can run in such a unattended mode, it explains everything then! And that it couldn't apply a patch in my case means probably a bad patch formatting idk, I'll check it. Thank you very much, Cameron!

Btw, GIT_DIR seems to be removed in c76046e
And no --git-dir argument is added. So, it won't work anymore like how you've described?

The only reason that patch is even used at all is just as a last safety net to hopefully get the patch applied properly.

Hehe, that was my case. But as I said - I'll see what's wrong.

@cweagans
Copy link
Owner

cweagans commented Mar 9, 2017

In theory, cd'ing to a dir should have worked, but it wasn't well tested, so we're adding --git-dir back in #108.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants