Python - how to check whether the path is under a git repo? and extract the repo name - gitpython

I'm trying to develop a Gatekeeping script in python rollin.py with the following requirements:
Assume user would launch the rollin.py script from anywhere within his local git cloned area to push his commits to the master repo
Now, rollin.py script would
clone a repo from master
pull user commits and merges into the cloned repo
Run the compliance tests
If passes, then push those changes to master repo else discard and notify the user
Now within rolloin.py script, how would I check the the repo name and user's git clone path? (since user can launch the rolloin.py script from anywhere in his local area)
Is there any existing function or method available? otherwise I'm thinking to implement reverse recursive search from cwd to locate the .git and and then url from it's config file.

Using the GitPython library
You can achieve this with the already authored GitPython module.
Read the docs.
$ pip install GitPython
Python snippet to print the base git path and the origin remote url.
import git
# Raises InvalidGitRepositoryError when not in a repo
repo = git.Repo(".", search_parent_directories=True)
print "Location "+ repo.working_tree_dir
print "Remote: " + repo.remote("origin").url
To create a new git repo
import git
newRepo = git.Repo.init("my-git-repo", mkdir=True)

You could use os.system to run git rev-parse --git-dir, which will return the path to the .git folder of the repository in which the command is run. For more info, see How to find the path of the local git repository when I am possibly in a subdirectory

Related

git push to create new GitHub repo not working

I am trying to create a script that prompts for a name to give to a new GitHub repo, and then create the GitHub repo by pushing to the remote URL which the new GitHub repo will have. When I use the script I get these errors:
remote: Repository not found. fatal: repository 'https://github.com/JT-style/great.git/' not found
read -p"Enter the name of remote repository: " name
mkdir ~/rep/$name
cd ~/rep/$name
echo "#$name" >> README.md
git init
git add .
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/JT-style/$name.git
git push -u origin main
echo "Done"
Why doesn't this work? What can I do to achieve my goal?
p.s. This is my first script.
You can't push to a repository to github if it doesn't already exist. Usually, you'd create one through a web browser by going on their site, but they released a cli called gh, which allows creating repositories (you have to authenticate somehow before though).

fatal: remote <Repository> already exists

As part of the development of a CI & CD flow for the company I work at, I am building a command line program (Bash script on OSX) that
creates a new local Git repo
adds some default branches to this repo
Then adds a new repo to Bitbucket using the next code:
gitUserName = Joris <-- provided by the user, this is an example
projectName = TestProject <-- provided by the user, this is an example
git remote add $projectName "https://bitbucket.org/$gitUserName/$projectName.git"
After running this command, I don't see the repository on my Bitbucket account on the website. When I try to re-run this command, it says the repository already exists.
Also, when I run git push $projectName master it says fatal: repository 'https://bitbucket.org/Joris/TestProject.git/' not found
This behavior seems inconsistent, and I have followed the Atlassian guide to set this up so I don't really understand why it doesn't add the repository as expected. I do realize that I can also just go on the BitBucket website and add the repository manually, but the purpose of my program is that it generates a fully set-up repository for a user based on as little commands as possible.
The git remote add documentation says that the command adds a remote to the local repo. This terminology is, IMO, a bit off; it would better to say it adds a remote configuration to the local repository (i.e. configures the repo to access a remote). This does not actually create the remote repo; that must be done separately.
In the case of bitbucket, the "normal" thing to do is to go to the website and create the repo through their UI. Because you're trying to automate things, you don't want to do that; so in that case, you would need to use the BitBucket REST API, which is documented here: https://developer.atlassian.com/server/bitbucket/reference/rest-api/
The "Core API" section talks about repositories and permissions, so you should be able to script out requests to (if necessary) check if the repo exists and set it up if it doesn't. You'll just need a way for your script to send HTTP requests and receive the responses.
In your machine:
Create repo:
git init
Add branches:
git checkout -b branchX
git checkout -b branchY
git checkout -b branchZ
In Bitbucket website:
Create new repository named TestProject, allow write permissions to user Joris in settings and save. Finally copy the url of the repository, this must be something like bitbucket.mydomain:port/nameofproject/testproject.git (Notice this is all in lowcase)
In your machine:
git remote add origin theURL
git push origin *:*
git push origin --tags
The last is the command to push all your local repo, this will overwrite the history and tags in your remote repo, but since is a new repo it doesn't matter.

GIT SSH Connection to server and create repository there and add files

I need help with git.
I have a server, username and parol. I connect with ssh succesfully but i cannot add files. When i want add files (from my local pc) with bash using git add it returing cannot find file path or path doesnot exist. I think it's searching files or directory in the server.
Thanks.
With Git you don't create a repository directly on a server. Instead, you
create a repository locally (git init),
add files to it, which generally comprises two steps:
stage files (git add)
commit the staged files into the local repository (git commit)
assign a remote server's repository to it (git remote add)
Note 1: I assume you have created a remote repository somehow - please refer to your server instructions.
Note 2: You create an empty repository on the server.
upload your local repository to the remote one (git push)
Sample script:
$ cd your_local_project_dir/ # Get into your your project directory
$ git init # Initialize a local repository
$ git add --all # Stage all files from your local project
# directory for commit (note this is not
# adding to the repository yet, it's just
# "prepare them to add into the repo")
$ git commit -m "Initial commit" # Add the staged files to the local repository
$ git remote add origin https://server.com/user/project_name.git
# Link the local repository to a remote one
$ git push -u origin master # Upload you local repository data to the
# remote one

Configure git to be accessed in my network from portable HDD under Windows

I need to have a portable git setup in order to access a git repository in my LAN from a portable HDD, including the computer where the HDD is connected, under Windows. The HDD might be moved from time to time between the computers in my network. I wish to avoid SSH for the moment.
I have installed the portable version of the git, I have made a batch to set the PATH to the requested directories specified in the documentation before running git-bash or git-cmd.
I see it runs, I have made a bare repository, let's say in a path like m:/repo.git. Then, I got stuck as I don't know how to configure the remote in order to do the first push as `git push repo master' from my project path.
I think I should do a 'git remote add repo ' but I fail to set the correct URL or something. I am aware I should change the URL each time the HDD is moved or change the remote.
What are the correct setup steps?
Then, I got stuck as I don't know how to configure the remote in order to do the first push as `git push repo master' from my project path
Let git create that setting for you:
git clone m:/repo.git
cd repo
git --work-tree=..\myproject add .
git commit -m "first commit"
git push
That will import the files of your project in a local repo, which will be able to push back to your bare repo on M:\.
UNC paths are supported too
git.exe clone "d:/dev/SDK" "//comp1/Proj/git/SDK/"

Creating a github repository from command line

I am creating a new app and I want to be able to create a repository from the command line to be able to add commits without having to go to github and create a repo. I am totally noobian. is it possible? and if so how? Thank you!
for github you could try hub - is a command line tool that wraps git in order to extend it with extra features and commands that make working with GitHub easier.
Examples from doc:
$ git create
[ repo created on GitHub ]
> git remote add origin git#github.com:YOUR_USER/CURRENT_REPO.git
# with description:
$ git create -d 'It shall be mine, all mine!'
$ git create recipes
[ repo created on GitHub ]
> git remote add origin git#github.com:YOUR_USER/recipes.git
$ git create sinatra/recipes
[ repo created in GitHub organization ]
> git remote add origin git#github.com:sinatra/recipes.git
If you want to use git, then it is fairly straightforward, but you won't go far without going through some tutorial. I highly recommend Git Book by Scott Chacon. At least go through chapter 2 and perhaps chapter 3. To answer your questions though: Git is a distributed versioning system so you definitely do not need a repo in GitHub - you can create a repo on your own harddrive and then push it to any other repos (i.e. you could create a GitHub repository later and publish your repository there - it will be an exact clone!).
To create a repo you invoke a command: git init or git init repoName - the former creates the repo in current folder, the latter creates a new one named "repoName".
When you are ready to create a first commit invoke git add . to add all files to index (think of it as an area where you prepare what will go into the next commit) and the git commit -m "Commit message". nstead of adding all files you could also choose files to commit individualy by git add path/to/file
You will need at least some basics with git covered though before you can start using it comfortably.

Resources