0

I want to run git update-index --no-skip-worktree C:\dev\src\Folder\MyFile.cs from python script", but I encountered 2 problems:

  1. My script doesn't run from the git folder, so I need to somehow make it understand this is a git repo. I saw git -C should help but wasn't able to make it work.
  2. I tried using subprocess.run and subprocess.Popen, but I get return code 0 and the file is not ignored.

How can I run it from my script? Thanks!

Toto
  • 89,455
  • 62
  • 89
  • 125
Zapdor
  • 35
  • 1
  • 5
  • 1
    If you're trying to ignore a tracked file, you'll probably want to read the [Git FAQ entry that says that what you're doing won't work](https://git-scm.com/docs/gitfaq#ignore-tracked-files). – bk2204 Jul 24 '21 at 16:27
  • I read it already, thank you. I am using it for a very special case and this is exactly what I need. The only problem is I can't seem to make it happen from my python script – Zapdor Jul 24 '21 at 17:27

2 Answers2

0

If the problem is just that the command is not being run in the right folder, have you tried adding the cwd keyword argument to subprocess.run? So if your current call is

subprocess.run(['git', 'update-index', '--no-skip-worktree', 'C:\\dev\\src\\Folder\\MyFile.cs'])

then change it to

subprocess.run(['git', 'update-index', '--no-skip-worktree', 'C:\\dev\\src\\Folder\\MyFile.cs'], cwd='path\\to\\the\\relevant\\folder')

Also, make sure to escape any backslashed in file paths, as I did here - otherwise they won't be understood as normal backslashes by Python.

David Husz
  • 48
  • 4
0

So apparently my issue was the fact I used a relative path instead of an absolute one as in here. Now I managed to get it to work both ways:

  1. subprocess.run(f"git -C {path_to_repo_folder} update-index --no-skip-worktree {path_to_file}").
  2. subprocess.run(['git', 'update-index', '--no-skip-worktree', '{path_to_file}'], cwd='{path_to_repo_folder}') I ended up using (2) as it is more elegant. Thanks David Husz!
Zapdor
  • 35
  • 1
  • 5