GitPython icon indicating copy to clipboard operation
GitPython copied to clipboard

Fails to commit files one by one

Open NikSWE opened this issue 5 years ago • 1 comments

I'm trying to add files one by one to my git repo (with custom git messages depending on some conditions) the problem is GitPython creates the commit but also adds some of these files to the staging area (and all the files are already committed).

When I have a single file to commit then I'm not facing any issues, this only surfaces with multiple files to commit.

Images

Screen Shot 2019-08-24 at 1 35 35 PM Screen Shot 2019-08-24 at 1 35 52 PM Screen Shot 2019-08-24 at 1 35 59 PM

Code

add_file = repo.index.add
commit_file = repo.index.commit
....
....
for f in untracked_files:
    is_file_private = False
    solution_type = None
    commit_emoji = None
    commit_msg = None

    with open(Path(repo.working_tree_dir).joinpath(f)) as target:
        solution_type = target.readline().strip("#").split()[0]
        if solution_type == 'PVT':
            is_file_private = True

    if not is_file_private:
        count_untracked_files += 1
        add_file([f])
        .......
        .......
        commit_file("{} {}".format(commit_emoji, commit_msg))

NikSWE avatar Aug 24 '19 08:08 NikSWE

I'm have created this workaround, for now, if anybody needs it

But If you invoke it from any other directory you will get an error fatal: not a git repository (or any of the parent directories): .git

Code

from subprocess import call
....
....
commit_file = call
....
....
commit_file(["git", "commit", "-m", f"{commit_emoji} {commit_msg}"])

NikSWE avatar Aug 24 '19 10:08 NikSWE