Is there a way to use use python to rebase a repo from one on github, then push the result. As well as detecting if the rebase failed as a result of conflicts that need to be resolved?
Git is primarily a command-line tool. Once installed, you should be able to open-up a console, command prompt, powershell, c-shell, bash shell, etc. and just type git and get a list of available git commands.
Once you have Git setup and working this way, then from Python it would be possible to execute git commands in the same way you would execute any other shell commands. I'm not a Python expert, but ElpieKay suggests in the comments to use:
commands.getstatusoutput("git <command>")
You will need to do a separate search for git rebase specifically and figure out how the output is formatted and parse it to determine success, or possibly there is an error code or StdErr output that you can get through the .getstatusoutput or a similar command from commands in python.
Another thing that may help is looking at the man page for rebase with git rebase --help.
Summary
I recommend doing a search to find out more about the python commands library or just shell interaction in general for python, and then a separate set of searches/research to determine exactly how to implement the git rebase commands and its output format to determine what you need to parse to determine success or failure.
Related
I want to run git update-index --no-skip-worktree C:\dev\src\Folder\MyFile.cs from python script", but I encountered 2 problems:
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.
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!
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.
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:
subprocess.run(f"git -C {path_to_repo_folder} update-index --no-skip-worktree {path_to_file}").
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!
I'm trying to generate a simple Python code that:
checks if it is running inside a git folder
if so, fetch the latest commit, else skip
it should work under the three platforms: Linux, Windows, and Mac
I have this code that works correctly under Linux:
from subprocess import call, STDOUT
import os
if call(["git", "branch"], stderr=STDOUT, stdout=open(os.devnull, 'w')) != 0:
# Not a git folder
commit = ''
else:
# Inside a git folder.: fetch latest commit
commit = subprocess.check_output(['git', 'rev-parse', '{}'.format('HEAD')])
print(commit)
but I have no way of checking if itwill work under Windows and Mac.
Does it work? Is there any way of checking/knowing this sort of things when one has no access to the other operating system?
You don't want to run git branch to detect whether you're in a Git repository, because you may or may not have any branches. To detect whether you're able to use Git commands, you'll want to run something like git rev-parse --git-dir, which will exit non-zero if you're not within a Git repository.
However, there are a couple of other issues with your code. First of all, in a new repository (one created fresh with git init), there will be a .git directory and the above command will succeed, but HEAD will not point anywhere. Therefore, your git rev-parse HEAD command will fail and print HEAD and an error.
Finally, if you want parse a revision, you should usually use --verify so that you don't print the dummy HEAD value on failure. So your invocation should look like git rev-parse --verify HEAD.
Ultimately, it's up to you to figure out what you want to do in a newly initialized repository, whether that's fail or fall back to an empty string.
The behaviors I've described here are consistent across platforms; they're built into Git and well defined.
There's a method check_output in subprocess library
from subprocess import check_output
try:
# use python to parse this log for info. This is your entire last commit
logs = check_output(['git', 'log', '-1', '--stat']).decode("UTF-8")
except Exception as e:
# Do whatever you wanna do otherwise if not git repository
print(e)
Git has a command called "git log".
"-1" indicates the last commit and
--stat will give you the files that were changed, commit ID, TIME ETC
then you can use python to parse this log and retrive any information you want
Check this out for more info on git log
I am stuck since 2 days trying to set up a small automatic deployment script.
The thing is: I have been using Git for some months now, but I always used it locally just by myself, just with the purpose of easily saving version of my code. All good until here.
Now I have to find a way to "publish" the code as soon as new functionalities are implemented and I think the code is stable enough.
Searching around I've discovered these 'hooks', which are scripts that are executed by Git in certain situations. Basically the idea is to have my master branch sync'd with my published code, so that everytime I merge a branch to the master and 'push', the files are automatically copied into '/my/published/folder'.
That said, I've found this tutorial that explains to do exactly what I want using a 'hooks' post-receive script, which is written in Ruby. Since at my studio I don't have and don't want to use Ruby at this time, I've found a Python version of the same script.
I tested and tested, but I couldn't make it work. I keep getting the same error:
remote: GIT_WORK_TREE is not recognized as as internal or external command,
Consider this is based on the tutorial I've shared above. Same prj name, same structure, etc.
I even installed Ruby on my personal laptop and tried the original script, but it still doesn't work...
I'm using Windows, and the Git env variable is set and accessible. But nevertheless it seems like it's not recognizing the GIT_WORK_TREE command. If I run it from the Git Bash it works just fine, but if I use the Windows Shell I get the same error message.
I suppose that when in my py script use the call() function, it runs the cmd using the Windows Shell. That's my guess, but I don't really know how to solve it. Google didn't help, as if no one ever had this problem before.
Maybe I'm just not seeing something obvious here, but I spent the whole day on this and I cannot get out of this bog!
Does anyone know how to solve it, or at least have an idea for a workaround?
Hope someone can help...
Thanks a lot!
The Ruby script you are talking about generates "bash" command:
GIT_WORK_TREE=/deploy/path git checkout -f ...
It means: define environment variable "GIT_WORK_TREE" with value "/deploy/path" and execute "git checkout -f ...".
As I understand it doesn't work for Windows command line.
Try to use something like:
set GIT_WORK_TREE=c:\temp\deploy && git checkout -f ...
I've had this problem as well - the best solution I've found is to pass the working tree across as one of the parameters
git --work-tree="/deploy/path" checkout -f ...
I am trying to build a set of git utilities with python. I am using subprocess.Popen to run the git binary. Right now I am just trying to find the best way to determine that there was an error in running a git command.
My question is whether or not git will always return a returncode of 0 on a successful git command and always return a returncode of non-zero on a unsuccessful call? I just want to make sure that checking the returncode is a safe way to detect an error so that I can exit the script if a git command was unsuccessful.
Yes, git (and any well-behaved *nix program) will always return 0 for success and non-zero for failure. This is the paradigm on GNU/Linux systems, and since Git was made by the same person who made Linux, you can bet it follows the convention.
I'm writing a Mercurial extension in Python and need to call the "Pull" command using the Mercurial API, but I want to suppress its output using the --quiet flag.
In Hg terms, I want to execute the following code, but from within my extension:
hg pull --quiet
Given the Mercurial API documentation, I thought it would be as simple as:
commands.pull(ui, repo, quiet=True)
Unfortunately, although this doesn't generate errors and will successfully execute the "Pull" command, the --quiet flag doesn't seem to be getting through as I still see the standard output.
All the examples only show passing non-global flags, so I'm a bit worried that this isn't possible.
What am I doing wrong? How can I pass the --quiet flag?
Global options are affected through the ui object. It allows you to control many of the things you would normally set in your (or the repository's) hgrc. In this case, you want to set the quiet option in the ui section to True.
ui.setconfig('ui', 'quiet', True)
commands.pull(ui, repo)