How to do "git add ." for a bare repo (git init -- bare) in rust with git2 or other way? - rust

I am trying to create a bare git repo, git clone into another directory, and do the git add . & git commit & git push. However, I could not successfully complete git add . After I did git add ., I checked by git status , the target file hasn't been added.
The code I tried as follows:
let mut index = repo.index().unwrap();
&index.add_all(&["."], git2::IndexAddOption::DEFAULT, None)?;
let oid = index.write_tree()?;
let signature = git2::Signature::now("secure-crates","abc#gmail.com")?;
let tree = repo.find_tree(oid)?;
let msg :&str = "Inital Commit";
repo.commit(Some("HEAD"), &signature, &signature, &msg, &tree, &[])?;
let mut remote = repo.find_remote("origin")?;
remote.push::<&'static str>(&[], None)?;
Is anybody know how to do git add operation for a bare repo?

Related

Python git package get tag and commit

I would like to print a git commit and the tag in my Python code.
How can I do this using git package?
When I am going to my Bitbucket I see
tag: 73-2-g46b9856
commit checksum: 46b9856
How can I retrieve this info from git package?
I have done the following:
import git
repo = git.Repo(search_parent_directories=True)
sha = repo.head.object.hexsha
So I assume you already have the checksum you want in the sha variable.
At this point, there's a post for how to get the tags and looking for a specific tag associated with that sha in this link: Get tags of a commit
# Example code for clarity
import git
repo = git.Repo(search_parent_directories=True)
sha = repo.head.object.hexsha
tagmap = {}
for t in repo.tags:
tagmap.setdefault(repo.commit(t), []).append(t)
tags = tagmap[repo.commit(sha)] # Warning: Your latest commit might not have a tag associated with it so this will throw an error right now.
print(tags)
Here is what solved my issue:
repo = git.Repo(search_parent_directories = True)
sha = repo.head.object.hexsha
commit_chksum = repo.git.rev_parse(sha, short = 7)
tag = subprocess.check_output(["git", "describe", "--always"]).strip().decode()

Node.js - unable to save entire output of `git clone` command using child_process spawnSync

What I want to do:
I am using Node.js to execute git clone command using child_process.spawnSync() and then save the output in a variable for later usage.
Example:
For example, I want to execute git clone https://github.com/octo-org/public-repo.git and save the resulting output in a variable:
Cloning into 'public-repo'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
Failed Attempt 1: only able to save first line of output (not the entire output)
However, I am only able to save the first line of output (i.e., Cloning into 'public-repo'...) in a variable. Here is what I have so far:
const args = ["clone", "https://github.com/octo-org/public-repo.git"];
const child = require("child_process").spawnSync("git", args);
const output = `${child.stderr}`;
console.log(output); // only returns "Cloning into 'public-repo'..."
Failed Attempt 2: entire output is printed to console, but i need to save it in a variable
Also, I looked into inherit option of options.stdio. Although it prints the entire result (instead of just the first line), I need to save the result in a variable. So, options = { stdio: "inherit" } will print the full output, but I need to save the full output in a variable. Here is my code for that:
// this solution prints the full output
// but i need to save the full output in a variable
const args = ["clone", "https://github.com/octo-org/public-repo.git"];
const options = { stdio: "inherit" };
const child = require("child_process").spawnSync("git", args, options);
Failed Attempt 3: redirect output to file (>) only writes the first line
Someone suggested redirecting output to file (>), but this also results in only the first line of output (Cloning into 'public-repo'...) being written to readme.txt file
require("child_process").exec(
"git clone https://github.com/octo-org/public-repo.git 2> readme.txt",
{ stdio: "inherit", shell: true },
(error, stdout, stderror) => {}
);
Question:
How can I use Node.js Child Processes to save the entire git clone output in a variable? So far, I am only able to print/display the entire output (but not save it in a variable). Also, I am only able to save the first line of output (but not the entire output) after executing git clone command.
Okay, so I figured out how to capture the entire output from executing git clone command by passing the --progress option to git clone (i.e., git clone --progress <some_remote_url>).
According to git clone documentation, we can pass --progress option to force progress status even if the stderr stream isn't directed to a terminal.
Since the progress status was directed at a terminal and I was spawning a child process that is not attached to a terminal, I could not capture the progress output.
So here is the updated Node.js code passing --progress option to capture the entire output of git config:
// adding --progress works
const args = [
"clone",
"https://github.com/octo-org/public-repo.git",
"--progress"
];
const child = require("child_process").spawnSync("git", args);
console.log(`${child.stderr}`);

Pygit2: Need help on how to walk on all commits in all repo's branches

I'd need to walk on entire repo's branches commits. I have tried this but with no success. :
for branch_name in list(repo.branches.remote):
try:
branch = repo.lookup_branch(branch_name)
ref = repo.lookup_reference(branch.name)
repo.checkout(ref)
for commit in repo.walk(branch.target, pygit2.GIT_SORT_TIME):
print(commit.id.hex)
Any help would be appreciated, thanks.
This is what I have:
def iterate_repository(dir: str) -> None:
repo = pygit2.Repository(dir)
for branch_name in list(repo.branches.remote):
branch = repo.branches.get(branch_name)
latest_commit_id = branch.target
latest_commit = repo.revparse_single(latest_commit_id.hex)
for commit in repo.walk(latest_commit.id, pygit2.GIT_SORT_TIME):
print(commit.id.hex)
Expanding from that should be relatively easy. What I do is gather statistics from files included in a commit.

Enable #devs to create wild repos, read/write their own master but not push to other's dev master

What I am trying to achieve is, that #dev group can make their own repos, do whatever on their dev/ branches, but can only push to their own repo master branch, and read only others developers master branches.
I have setup my rules like this in gitolite.conf:
#projects = projects/..*
repo #projects
C = #devs
- master$ = #devs
RW+ master$ = CREATOR
RW+ = #admin TRUSTED
RW+ dev/ = #devs
RW = #devs
When I check for access for a particular dev, that I would like him to be able to READ master, result is DENIED?
gitolite access -s projects/a1 [dev-member] R master
legend:
d => skipped deny rule due to ref unknown or 'any',
r => skipped due to refex not matching,
p => skipped due to perm (W, +, etc) not matching,
D => explicitly denied,
A => explicitly allowed,
F => denied due to fallthru (no rules matched)
p gitolite.conf:37 C = #devs
D gitolite.conf:39 - master$ = #devs
R refs/heads/master projects/a1 [dev-member] DENIED by refs/heads/master$
Same happens for me as the CREATOR of this repo, so I am not able even to READ my own master branch. How would I resolve this?
Even more confusing is the following case:
Given the fact that branquito is in #admin group.
gitolite access -s projects/a1 branquito W master
legend:
...
p gitolite.conf:37 C = #devs
D gitolite.conf:38 - master$ = #devs
W refs/heads/master projects/a1 branquito DENIED by refs/heads/master$
but:
gitolite access -s projects/a1 #admin W master
legend:
...
A gitolite.conf:40 RW+ = #admin TRUSTED
refs/.*

Git Aliases not working

I have specified certain aliases in the .gitconfig file located in /home/myUser/.gitconfig as below (I am pasting the entire file settings):
[user]
name = myName
email = myEmail#email.com
[core]
autocrlf = true
safecrlf = true
[push]
default = simple
[alias]
co = checkout
ci = commit
st = status
br = branch
hist = log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short
type = cat-file -t
dump = cat-file -p
I am having a problem specifically with that hist alias. Sometimes I get the desired output when y execute de git hist (I mean the format specified in the .gitconfig for the hist alias); and some other times it is not recognized. I tried restarting the terminal an the same happens: sometimes it works and some other times don't.
Would appreciate any help, thanks a lot!

Resources