libgit2 / libgit2/pygit2

[question] How to apply a patch in a cloned repository

Open
#944 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.7k
Forks
408
Avg merge
2d 57m
Merged PRs (30d)
7

Description

Hi!

I would like to apply a regular patch file, but without a git repo. Is it possible using this project? For instance:

$ cd myproject/
$ patch < 0001-regular-fix.patch

I found two methods, but I'm not sure if they are what I want:

pygit2.Patch.create_from
pygit2.Diff.parse_diff

Actually I didn't find some easy to apply them. My idea was something like:

def apply_patch(working_folder, patch_file):
    with open(patch_file, 'rb') as fd:
        patch_content = fd.read()
    
        diff = pygit2.Diff.parse_diff(patch_content)
        repo = pygit2.init_repository(working_folder, bare=True)
        repo.apply(diff)

Regards!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by comparing the regular patch workflow in the issue with pygit2.Patch.create_from, pygit2.Diff.parse_diff, pygit2.init_repository, and repo.apply. Check the corresponding pygit2 documentation and behavior to determine whether a patch can be applied without a Git repository; done means the supported workflow or limitation is clearly established.

Written by the indexing model from the issue text.

Assessment

Tech stack
git, python
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.