How can I use git python get diffs - python

I am trying to use GitPython to get me diffs in the format I want.
For every file I would like 3 lists. A list of lines that were changed, added, and removed.
I've been looking online and at the documentation and can't seem to figure out how to do this.

As far as I can tell, there is actually no way to do this.
You'd think they'd implement this functionality, but as far as I can tell file-level diffs are not implemented directly in GitPython.
The only way to retrieve changed lines within GitPython is using the git command directly, like so:
from git import Repo
repo = Repo('path/to/repo')
filename = 'path/to/repo/subdir/myfile'
diff_output = repo.git.diff(filename)
With diff_output now containing the same content you'd see if you did:
$ cd path/to/repo
$ git diff subdir/myfile
So it's not very useful. You're going to probably have to write your own code to parse the diff file to get the information you need.
Frustrating, because I'd like a similar functionality and it looks like my only option really is manually parsing git diffs.
I have to assume that GitPython is primarily used for managing changes to branches, etc and not inspecting or dealing with source code changes on the file level.

See this documentation. It could be useful.

Related

Get method names and lines from git commit - python

I am wondering if there is a way to get the list of method names that are modified along with modified lines and file path from git commit or if there are any better solution for python files
I have been going through few answers in stackoverflow where the suggestion was to edit the gitconfig file and .gitattributes but I couldn't get the desired output
Git has no notion of the semantics of the files it tracks. It can just give you the lines added or removed from a file or the bytes changed in binary files.
How these maps to your methods is beyond Git's understanding. However there might be tools and/or scripts that will try and heuristically determine which methods are affected by a (range of) commits.

Is it possible to generate a single .pot file from Sphinx documentation?

I'm undergoing the task of i18n / l10n the documentation of a largish project. The documentation is done with Sphinx, that has off-the shelf basic support for i18n.
My problem is similar to that of this other question: namely the fact a large chunk of the strings for each pot file is the same, and I would like my translators not to re-enter the same translation over and again. I would rather have a single template file.
My problem is not really merging the files (that is just a msgcat *.pot > all.pot away), but rather the fact that - for the domains to work when building the documentation in a particular language - I have to copy and rename all.pot back to the original file names. So my workaroundish way of working is:
Generate fileA.pot, fileB.pot
Merge the two into all.pot
cp all.pot fileA.pot + cp all.pot fileB.pot
Is there a cleaner way to do the same? gettext_compact brings me only half-way through my goal...
After over 7 months, extensive research and a couple of attempted answers, it seems safe to say that no - at least with the current version 1.1.3 - it is not possible to generate a single .pot file from Sphinx documentation.
The workaround described in the original question is also the most straightforward for automatically removing duplicate strings when merging the different .pot files into a single one.
I see two possibilities here:
One is to hack Sphinx to not use domains at all… which I guess you won’t want to do for several reasons.
The other is: since you split by domains, the -M option of msggrep looks like it does what you need to do. Otherwise, the -N option can still be used to filter by source file(s).
That is, if the obvious solution doesn't work:
generate fileA.pot, fileB.pot (also look at msguniq here)
merge them into all.pot and give that to translators
for x in file*.pot; do msgmerge -o ${x%t} all-translated.po $x; done
According to TFM, msgmerge only takes the (supposedly newer) translations from its first argument (the translated po file) that match the up-to-date source locations of the second argument (po file with old translations, or pot file = template with only empty msgstrings).
Add the following to your conf.py :
gettext_compact = "docs"

Python: How to create diff/patch files between 2 revisions for a single URL?

I would like to create a diff (patch) file between two revisions for a single SVN URL, including lines of unified context.
Basically I need to provide a Python method to achieve the following:
URL to the SVN repository
number for first (before) revision
number for second (after) revision
The output I require is as follows:
number of lines of code in head revision
number of changed files
actual diff files.
How can I do this using Python? I see many similar questions here but none specifically on how to achieve this in Python. Can anyone suggest some libraries/code to help accomplish this?
I noticed pysvn offers diff method. I think this is exactly what you need.

CLI git log statistics

I'm being faced with the task of generating statistics about the history of a Git project, and I need to produce some specific numbers and representations for various metrics - things like commits per author, commits-over-time/date histograms, that sort of thing.
The trouble is that I need all this data generated in a format that can be dealt with via a script or similar - the output has to be text, and if I can get the numbers into a Python (or similar) script, so much the better.
My question is this: are there any existing frameworks or projects that will provide such an interface? I've seen GitStats, and it does a lot of what I want, but then it dumps the results into a HTML structure instead of just providing textual or programmatic representations back to me. Are there (for example) Python bindings for a Git log parser, or even a Git statistics generator that returns a big text dump of data?
I realize it's a very specific need, and I'm willing to do some serious coding to get the precise format I want, but I'd like to think there's a starting point out there somewhere. Ideas?
How about using XML logs instead, and then you can parse the xml in python relativily easily and build your stats
see this answer for how to get an xml log from git

How do I get hgweb to actually display the repository I want?

I am having an infuriating experience with IIS7, Python 2.6, Mercurial 1.7.2, and hgweb.cgi.
After battling for an afternoon getting hgweb.cgi to work, I finally got it to render in the browser using hgweb.cgi and IIS7. I can now see a blank rendering of the web server, that is, a header with no repositories listed.
Now, according to the multipe sites I've read after scouring through Google results, I know that I have to update my hgweb.config file to point to some repositories.
However, for the life of me, I can't get it to list my repository using either the [paths] or [collections] entries.
I have the following directory structure, (simplified but illustrative...):
c:\code
c:\code\htmlwriter
c:\code\CommandLineProjects\Clean
The latter two directories have mercurial repositories in them.
I am trying to publish the repository in c:\code\htmlwriter
Now, if I make this entry in hgweb.config
[paths]
htmlwriter = c:\code\htmlwriter
I get nothing listed in my output.
If I put
[paths]
htmlwriter = c:\code\*
I get something, but not what I want, i.e. this:
htmlwriter/CommandLineProjects/Clean
(Note that the about drills down one directory level farther than I want it to).
I can't seem to find any combination of paths, asterisks, or anything else that will serve up the repository in c:\code\htmlwriter. It appears to always want to go one level deeper than I want it to, or to show nothing.
I know that my hgweb.config file is being read because I can change the style tag in it and it changes what is rendered.
I have read and re-read multiple time a number of resources on the web, but they all say what I'm trying should be working. For instance, I followed this instructions to the letter with no good results:
http://www.jeremyskinner.co.uk/mercurial-on-iis7/
Anyone have any suggestions?
I had about the same luck with hgweb.cgi, and ended up going a different route with wsgi and a "pure python" mercurial install.
I wrote a pretty comprehensive answer here.
I'll answer my own question:
The solution is that the path listed in the [paths] section is relative to the directory where the hgweb.config file is residing.
So, if you have your repository in:
c:\code\myrepo
and your hgweb.config file is in:
C:\inetpub\hgcgi
then the entry in your hgweb.config file needs to be:
/myrepo = ../../code/myrepo
That was the trick -- to put the correct relative path.
I was never able to get hgweb.cgi to work with a repo on a different drive.

Categories