gitpython commit not writing to disk

56 Views Asked by At

I am trying to write a basic script that is pulling replicating the contents of one directory into another one, but making some modifications along the way. The items written in the new directory are then committed to a git repo.

import git

repo = git.Repo("/path/to/repo")
repo.index.add(["list","of","files"])
commit = repo.index.commit=(message="foo Bar")

But when I navigate to that destination directory and run git status it shows that all the newly written files are untracked. I have previously pushed the commit generate in the method above to a remote successfully, but cannot figure out why the commit appears, for lack of a better way of saying, to have not been written to disk.

I have tried passing the message with and without an explicit argument assignment, as well as with and without author information.

1

There are 1 best solutions below

0
Panduar On

I figured it out. The paths being added need to be relative to the CWD or else absolute. I had thought they were supposed to be relative to the root directory of the repository, since all of the paths of the blobs in the repository object are.

import git, os

# setup paths
repo_path = "/path/to/repo"
relative_paths = ["CHANGELOG","README.md","VERSION"]
absolute_paths = [os.path.join(repo_path, path) for path in relative_paths]

# create Repo obj
repo = git.Repo(repo_path)

# add files to repo
repo.index.add(absolute_paths)

# commit changes
commit = repo.commit("commit message goes here")