How do you delete a file with gitPython - gitpython

How do you delete a file with gitPython?
repo.delete([file_to_delete])
says that there is no delete method.

The problem I had was that I was deleting the file from the repository but not the working tree. The answer is to do the following:
repo.index.remove([file_to_delete],working_tree = True)
Without the working_tree argument the file is left in the working tree even though it is not in the repository.

Related

How to get updated files via gitpython?

I'm trying to get the paths of all the changed files in this current branch, compared to master.
I've tried
repo = Repo("path/to/repo")
files_changed = [item.a_path for item in repo.index.diff("master").iter_change_type("D")]
for f in files_changed:
print(f)
and I have few questions about the results:
When using R iter_change_type to get the files that are renamed, it only gives the new path. How do I get the original path of the file?
When using D iter_change_type, it gives all the files that are added instead of deleted. And A iter_change_type gives all the files that are deleted instead of added. Shouldn't this be the other way around?

Create a directory structure from a path in gradle/groovy

I am implementing a diff package generation task in my project's gradle build from the git command line output. Currently I have a method which will give me a list of changed files from git diff --name-only. What I would like to do is create a directory structure in a new directory which matches the paths of each file. For example: inputting the string repo/dir/file.java would create in an output directory if not already created and inside it the directories head/repo/dir with the current file.java and prev/repo/dir with the previous file.java.
My current plan is to split the string repo/dir/file.java on the forward slash, and create directories until the last element of the split result, then write the file there. but nothing I have been able to come up with in gradle is nice or clean. I am wondering if there is a nicer way to create directories from a string like that.
My current plan is to split the string repo/dir/file.java on the forward slash, and create directories until the last element of the split result
Rather than splitting your string manually, you could try using File.mkdirs():
File newDirectoryStructureParent = new File('some/path/to/parent/dir')
def s = 'repo/dir/file.java'
def newContainer = new File(s, newDirectoryStructureParent).getParent()
newContainer.mkdirs()
everyone
In this part of my code you can just work around Path not File!
At the first you can define Path and second need check that path exist or not, if not mkdirs can make it ;)
Its help when you unknown about that path exist or not /
File fullPath = new File('/tmp/Test1')
if (!fullPath.exists())
fullPath.mkdirs()

gitignore file pattern not working

I have dynamic directory structure like,
dependency
a
b
c
d e
f
g
h
I want to ignore all files under dependency folder recursively except .xml files.
I am using below pattern to achieve the same.
dependencies/**
!dependencies/**/*.xml
But it seems it's not working for me. It's ignoring all the files but accepting only .xml files which are directly inside the dependency folder, not recursively. :(
I am using below environment.
OS : Windows 7(64 bit)
Git : 2.6.1.windows.1
Can anyone help me?
This should work:
You ignore everything but white-list the parent folders.
Then you can white-list files.
dependencies
!dependencies/**/
!dependencies/**/*.xml
As I mentioned in "How do I add files without dots in them (all extension-less files) to the gitignore file?", there is mainly one rule to remember with .gitignore:
It is not possible to re-include a file if a parent directory of that file is excluded.
That means, when you exclude everything ('*'), you have to white-list folders, before being able to white-list files.
Check if this is working with git check-ignore -v -- afile to see if it is ignored (and by which rule) or not.

Overwriting a file that already exists in the directory during shutil.move operation

I have a Source File which I'm moving to an Archive directory using
shutil.move(srcfile, dstdir)
But when the same file already exists in the archive destination directory it throws an error saying can't move the file exists already. So I would like to overwrite the existing file. Is there a way to do that?
I had this same question. In case anyone else is looking for a solution, here's what I did.
According to the shutil documentation, there isn't a direct way to do this. However, there is an easy fix using os.remove(). Assuming that you are in the source directory and you are moving the file 'srcfile' to 'dstdir':
import shutil, os
try:
os.remove(dstdir+'srcfile')
except OSError:
pass
else:
shutil.move(srcfile, dstdir)`
This tries to clear 'dstdir' of 'srcfile' before it moves the file.

Directory dependencies with rake

I'm using rake to copy a directory as so:
file copied_directory => original_directory do
#copy directory
end
This works fine, except when something inside of original_directory changes. The problem is that the mod date doesn't change on the enclosing directory, so rake doesn't know to copy the directory again. Is there any way to handle this? Unfortunately my current setup does not allow me to set up individual dependencies for each individual file inside of original_directory.
You could use rsync to keep the 2 directories in sync as shown here: http://asciicasts.com/episodes/149-rails-engines
You don't need to know the files to depend on them:
file copied_directory => FileList[original_directory, original_directory + "/**/*"]

Resources