GitPython get list of commits from a repo-branch - gitpython

I have a Git repository URL and a branch name.
Using GitPython how do I get all the commits from the branch?

From https://gitpython.readthedocs.io/en/stable/tutorial.html
Meet the Repo type
The first step is to create a git.Repo object to represent your repository.
from git import Repo
# rorepo is a Repo instance pointing to the git-python repository.
# For all you know, the first argument to Repo is a path to the repository
# you want to work with
repo = Repo(self.rorepo.working_tree_dir)
assert not repo.bare
In the above example, the directory self.rorepo.working_tree_dir equals /Users/mtrier/Development/git-python and is my working repository which contains the .git directory. You can also initialize GitPython with a bare repository.
...
...
The Commit object
Commit objects contain information about a specific commit. Obtain commits using references as done in Examining References or as follows.
Obtain commits at the specified revision
repo.commit('master')
repo.commit('v0.8.1')
repo.commit('HEAD~10')
...
I would suggest reading the tutorial I quoted and at least the entire The Commit Object section of it. (https://gitpython.readthedocs.io/en/stable/tutorial.html#the-commit-object)
(Sorry for bad formatting, for some reason stackoverflow didn't let me make it a quote block)

Related

How to import a specific branch of forked repo into Go code

I faced with the following problem:
I forked a repo, and made some modifications to it under new branch
I create PR to upstream repo and it is not yet merged
I want to adjust my Go codebase to import the specific branch of my forked repo
The problem:
if the original repo is github.com/user/pkg/v3 then the forked repo appears as github.com/myusername/pkg
moreover, I made a new branch, e.g. mybranch where I made my fixes
in the code where I used original repo I have an entry in my go.mod file as github.com/user/pkg/v3 which I want to replace with specific branch of my forked repo
How should I correctly solve this issue?
What I see when I tried to change go.mod of my forked repo to be github.com/myusername/pkg/v3 and then call go get github.com/myusername/pkg#mybranch is the following
go: github.com/myusername/pkg/v3#vxx-xx-xx: parsing go.mod:
module declares its path as: github.com/user/pkg/v3
but was required as: github.com/myusername/pkg/v3
I found required solution. The trick was to perform the following series of steps:
fork original repo
create new branch
add modifications to the code
push branch into forked repo
tag this branch with version higher then original repo, e.g. if original repo had v3.1.1 then the tag I applied to my forked branch was v3.1.2
go to code which depends on this package
change go.mod file of my package to use replace directive and my new tag like this
replace github.com/user/pkg/v3 => github.com/myusername/pkg/v3 v3.1.2
Therefore, in order to use forked repo with new branch we must tag this branch in forked repo with version higher of the tag in upstream repo.

How to retrieve default repository branch using gitpython?

While I do know how to get the current active branch, I do want to retrieve the default repository branch (as in master or main) using gitpython and I was not able to find any docs on that (even pdb was of not much help).
import git
repo = git.Repo(".", search_parent_directories=True)
repo.active_branch # <-- that is current branch

Spring and GitHub: hide sensitive data

I have a repository on GitHub that I would like to make public so recruiters can view it.
This repository though holds my SMTP and a MongoDB URI that shouldn't be shared with others. This information is in my application.properties file.
What's the simplest way to hide this sensitive data and also make sure no one can go look at old commits and see how it was before hiding it?
I have seen some ways on the web but they all look quite complicated...
Thank you for your experience and time
Use environment variables to hide your sensitive data. Like
spring.data.mongodb.host=${MONGO_DB_HOST}
spring.mail.host=${MAIL_HOST}
Set the values at your dev environment.
I don't have any idea about how to hide your old commits.
Make a .gitignore file at the root of your project and inside list whatever files you don't want git to have access to it when you push into GitHUb, for example:
/public/packs
/node_modules/
.pnp.js
/ (forward slash) is used for folders and
. (dot) is used for files
Here follows a picture of the location of the .gitignore file.
If the goal is just for recruitment, would it be acceptable to have a second copy for recruitment, while leaving the original copy alone?
While there's certainly more idiomatic ways of achieving this through git, a simple solution with minimal git knowledge or advanced techniques would be:
Create a new empty git project on GitHub
Clone the new project locally
Copy the (non-.git) files from the existing project into the new project (using either the console or your OS's windowed UI)
Delete or redact the offending entries from the new project
Commit the changes as a single commit
Push the new project back to GitHub
I have not used it myself, but the open source BFG Repo-Cleaner looks like it might satisfy your requirements of simplicity while retaining the activity chart for reviewers to view. This can be done on a publicly-facing copy of the repo if you wish to keep your private working copy, while still keeping the activity history viewable.
Following the tool's usage instructions, you should be able do the following (assuming you want these changes in a fresh copy of the repo):
The first step is to duplicate the repository on GitHub, following the instructions in the GitHub docs.
To do this, first create a new repository.
Next, mirror the repository, following the GitHub instructions:
Open Terminal.
Create a bare clone of the repository.
$ git clone --bare https://github.com/exampleuser/old-repository.git
Mirror-push to the new repository.
$ cd old-repository.git
$ git push --mirror https://github.com/exampleuser/new-repository.git
Remove the temporary local repository you created earlier.
$ cd ..
$ rm -rf old-repository.git
Now that you have the duplicate repository, you can run the BFG Repo-Cleaner to replace all instances of text you want hidden with ***REMOVED***.
$ java -jar bfg.jar --replace-text replacements.txt my-repo.git
The replacements.txt file would contain the SMTP, MongoDB URI, and any other text you want hidden.
mongodb://my-username:my-password#host1.example.com:27017,host2.example.com:27017/my-database
marco-f#example.com
Note that this does not update the latest commit on the master/HEAD branch, so this will need to be manually changed, and then committed. This can either achieved using a final commit using the --amend option, or by making a new commit prior to running the BFG Repo-Cleaner with the files manually changed.
$ git commit --amend
Now that the changes have been made, they can be pushed to GitHub.
$ git push

Converting a submodule to a folder within a GitHub repo

I'm new to using GitHub and I read up a few of the solutions on stack overflow but I couldn't understand them.
I pushed a folder in one of my repository and it shows up as a gray submodule. I need it to be a regular directory inside my repo. Can someone simplify what I need to do or show steps because I don't know how to deal with submodules or .gitmodules or where to find them
Thanks in advance!
If you don't see a .gitmodule at the root of your main repository, that means your subfolder is a nested git repo, recorded as a gitlink (a SHA1 reference), which is what you see as a grayed folder.
All you need to do is:
delete the .git subfolder you should find in that folder
go back to the main repo (one folder up)
add, commit and push: that should push the folder content instead of a tree SHA1 reference.

Working with multiple Git

I have following dir stucture
root
root/framework (Yii)
root/protected/messages
All of this folder must be separate git repos
What I want to do is
root and root/framework must be separate repos. But
root/framework must be pull only because I have no push access to this repository. I mean I want to pull yii when I pull parent repo, but don't want to push when I push parent repo.
Another problem is, remote dir structure of Yii (root/framework) looks like http://screencast.com/t/mU1TgXuZDv
I need only framework folder's contents. How can I pull only this folder's contents into root/framework ?
To make root/protected/messages separate git repo so that, when I push & pull root git repo, to do it for this one too. In other words, to push & pull with parent one to 2 separate remotes.
To solve second problem, I initialized new repo inside root/protected/messages but now they push & pull separatelly. I mean, I want them to push & pull changes to/from 2 remotes at once. Can't figure out how to do it.
Also I have no idea about first problem.
Any suggestions?
In order to create a separate and independent git repos within a parent git repo, you want to look into Git Submodules (http://git-scm.com/book/en/Git-Tools-Submodules). These basically allow you to create a completely independent git repos inside a directory which by itself is a git repository.
To create the submodule the command is git submodule add git://path/to/gitname.git folder-containing-the-inner-git. Of course you will need to cd into the parent folder before firing this command, which in your case will be root. The git://path/to/gitname.git will be the git url for Yii and folder-containing-the-inner-git will be root/framework.
In order to pull a specific folder of Yii of the entire git repo you might want to try out git checkout as suggested by this question on stackoverflow How to pull specific directory with git. I have never tried this myself.
Also, as of Git 1.7 you can also do a sparse checkout (https://www.kernel.org/pub/software/scm/git/docs/v1.7.0/git-read-tree.html#_sparse_checkout). Although you will still have to fetch the entire repo.
Once you create a separate git repo using git submodules inside root, you will have to push and pull the git inside root/protected/messages seperately. You can however automate this process by creating a git hook (http://git-scm.com/book/en/Customizing-Git-Git-Hooks) for the repo inside root. A hook is a script that can be executed upon specific git events/operations like committing, merging, etc. For a full list of these events you can refer to this page ... http://www.manpagez.com/man/5/githooks/
It seems that there is no event for a git push or pull. However there is an event for git merge ... post-merge :
This hook is invoked by git merge, which happens when a git pull is
done on a local repository. The hook takes a single parameter, a status
flag specifying whether or not the merge being done was a squash merge.
This hook cannot affect the outcome of git merge and is not executed,
if the merge failed due to conflicts.
This hook can be used in conjunction with a corresponding pre-commit
hook to save and restore any form of metadata associated with the
working tree (eg: permissions/ownership, ACLS, etc). See
contrib/hooks/setgitperms.perl for an example of how to do this.
So you can write a simple bash script like :
cd root/protected/messages
git pull origin master
So everytime you pull from the outer repo in root this script will get fired and you will be able to pull the contents of your inner repo as well. However, this will happen on every merge, not just the merges that happen on a pull so you might want to be careful.
Hope this helps.
You may try more straightforward way:
Init your git repo in root;
Add your root/framework to .gitignore in it;
Go to root/framework and init new git repository there;
You will have matroshka styled repos. But, to be frankly, they will be harder to support than git-submodules solution, since root repo does not aware about other repos at all, and all pushesh, pulls need to be done separately inn each repo.

Resources