Execute a Git command without being in the repository and outputting the current directory
up vote
1
down vote
favorite
I'm writing a Python script to loop over all Git repositories in a certain folder. Now I want to include the current folder name in the git-log result, but I can't find how to do this in the git-log documentation.
Is there a way to print out a current directory if you execute Git commands against a repository without being in that repository?
My current git-log command looks like this:
git -C ./%s log --pretty=format:-C,'"%%H","%%s"' | grep -E %s >> output.csv
I know I can use both git --git-dir=repo/.git log
and git -C /repo log
to execute commands in subfolders.
I also tried to use $(basename "$PWD")
but it shows the current folder, but not subfolders.
Any idea on how to do this?
python git github
add a comment |
up vote
1
down vote
favorite
I'm writing a Python script to loop over all Git repositories in a certain folder. Now I want to include the current folder name in the git-log result, but I can't find how to do this in the git-log documentation.
Is there a way to print out a current directory if you execute Git commands against a repository without being in that repository?
My current git-log command looks like this:
git -C ./%s log --pretty=format:-C,'"%%H","%%s"' | grep -E %s >> output.csv
I know I can use both git --git-dir=repo/.git log
and git -C /repo log
to execute commands in subfolders.
I also tried to use $(basename "$PWD")
but it shows the current folder, but not subfolders.
Any idea on how to do this?
python git github
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm writing a Python script to loop over all Git repositories in a certain folder. Now I want to include the current folder name in the git-log result, but I can't find how to do this in the git-log documentation.
Is there a way to print out a current directory if you execute Git commands against a repository without being in that repository?
My current git-log command looks like this:
git -C ./%s log --pretty=format:-C,'"%%H","%%s"' | grep -E %s >> output.csv
I know I can use both git --git-dir=repo/.git log
and git -C /repo log
to execute commands in subfolders.
I also tried to use $(basename "$PWD")
but it shows the current folder, but not subfolders.
Any idea on how to do this?
python git github
I'm writing a Python script to loop over all Git repositories in a certain folder. Now I want to include the current folder name in the git-log result, but I can't find how to do this in the git-log documentation.
Is there a way to print out a current directory if you execute Git commands against a repository without being in that repository?
My current git-log command looks like this:
git -C ./%s log --pretty=format:-C,'"%%H","%%s"' | grep -E %s >> output.csv
I know I can use both git --git-dir=repo/.git log
and git -C /repo log
to execute commands in subfolders.
I also tried to use $(basename "$PWD")
but it shows the current folder, but not subfolders.
Any idea on how to do this?
python git github
python git github
asked Nov 19 at 11:19
Lowly0palace
183
183
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
From what I understand from your question, you want to add the current git repo name with each line from git log
.
Since you tagged Python, this might be a long shot, but you can use GitPython to determine if the subfolders inside a folder are git repositories. Then you can open a git log
command with subprocess.Popen()
, and print out each line with the repos name from stdout.
Make sure to pip install GitPython
before running this code.
Here is an example:
from os import listdir
from os import chdir
from os import getcwd
from os.path import abspath
from git import Repo
from git import InvalidGitRepositoryError
from subprocess import Popen
from subprocess import PIPE
# Current working directory with all git repositories
# You can change this path to your liking
ROOT_PATH = getcwd()
# Go over each file in current working directory
for file in listdir(ROOT_PATH):
full_path = abspath(file)
# Check if file is a git repository
try:
Repo(full_path)
# Change to directory
chdir(full_path)
# Run git log command
with Popen(
args=['git', 'log', '--pretty=format:"%h - %an, %ar : %s"'],
shell=False,
stdout=PIPE,
bufsize=1,
universal_newlines=True,
) as process:
# Print out each line from stdout with repo name
for line in process.stdout:
print('%s %s' % (file, line.strip()))
# Change back to path
chdir(ROOT_PATH)
# If we hit here, file is not a git repository
except InvalidGitRepositoryError:
continue
This works for me when I run the script inside a folder with all my git repositories.
Note: There is probably a nicer way to do this with the git
command itself or with bash.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
From what I understand from your question, you want to add the current git repo name with each line from git log
.
Since you tagged Python, this might be a long shot, but you can use GitPython to determine if the subfolders inside a folder are git repositories. Then you can open a git log
command with subprocess.Popen()
, and print out each line with the repos name from stdout.
Make sure to pip install GitPython
before running this code.
Here is an example:
from os import listdir
from os import chdir
from os import getcwd
from os.path import abspath
from git import Repo
from git import InvalidGitRepositoryError
from subprocess import Popen
from subprocess import PIPE
# Current working directory with all git repositories
# You can change this path to your liking
ROOT_PATH = getcwd()
# Go over each file in current working directory
for file in listdir(ROOT_PATH):
full_path = abspath(file)
# Check if file is a git repository
try:
Repo(full_path)
# Change to directory
chdir(full_path)
# Run git log command
with Popen(
args=['git', 'log', '--pretty=format:"%h - %an, %ar : %s"'],
shell=False,
stdout=PIPE,
bufsize=1,
universal_newlines=True,
) as process:
# Print out each line from stdout with repo name
for line in process.stdout:
print('%s %s' % (file, line.strip()))
# Change back to path
chdir(ROOT_PATH)
# If we hit here, file is not a git repository
except InvalidGitRepositoryError:
continue
This works for me when I run the script inside a folder with all my git repositories.
Note: There is probably a nicer way to do this with the git
command itself or with bash.
add a comment |
up vote
3
down vote
accepted
From what I understand from your question, you want to add the current git repo name with each line from git log
.
Since you tagged Python, this might be a long shot, but you can use GitPython to determine if the subfolders inside a folder are git repositories. Then you can open a git log
command with subprocess.Popen()
, and print out each line with the repos name from stdout.
Make sure to pip install GitPython
before running this code.
Here is an example:
from os import listdir
from os import chdir
from os import getcwd
from os.path import abspath
from git import Repo
from git import InvalidGitRepositoryError
from subprocess import Popen
from subprocess import PIPE
# Current working directory with all git repositories
# You can change this path to your liking
ROOT_PATH = getcwd()
# Go over each file in current working directory
for file in listdir(ROOT_PATH):
full_path = abspath(file)
# Check if file is a git repository
try:
Repo(full_path)
# Change to directory
chdir(full_path)
# Run git log command
with Popen(
args=['git', 'log', '--pretty=format:"%h - %an, %ar : %s"'],
shell=False,
stdout=PIPE,
bufsize=1,
universal_newlines=True,
) as process:
# Print out each line from stdout with repo name
for line in process.stdout:
print('%s %s' % (file, line.strip()))
# Change back to path
chdir(ROOT_PATH)
# If we hit here, file is not a git repository
except InvalidGitRepositoryError:
continue
This works for me when I run the script inside a folder with all my git repositories.
Note: There is probably a nicer way to do this with the git
command itself or with bash.
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
From what I understand from your question, you want to add the current git repo name with each line from git log
.
Since you tagged Python, this might be a long shot, but you can use GitPython to determine if the subfolders inside a folder are git repositories. Then you can open a git log
command with subprocess.Popen()
, and print out each line with the repos name from stdout.
Make sure to pip install GitPython
before running this code.
Here is an example:
from os import listdir
from os import chdir
from os import getcwd
from os.path import abspath
from git import Repo
from git import InvalidGitRepositoryError
from subprocess import Popen
from subprocess import PIPE
# Current working directory with all git repositories
# You can change this path to your liking
ROOT_PATH = getcwd()
# Go over each file in current working directory
for file in listdir(ROOT_PATH):
full_path = abspath(file)
# Check if file is a git repository
try:
Repo(full_path)
# Change to directory
chdir(full_path)
# Run git log command
with Popen(
args=['git', 'log', '--pretty=format:"%h - %an, %ar : %s"'],
shell=False,
stdout=PIPE,
bufsize=1,
universal_newlines=True,
) as process:
# Print out each line from stdout with repo name
for line in process.stdout:
print('%s %s' % (file, line.strip()))
# Change back to path
chdir(ROOT_PATH)
# If we hit here, file is not a git repository
except InvalidGitRepositoryError:
continue
This works for me when I run the script inside a folder with all my git repositories.
Note: There is probably a nicer way to do this with the git
command itself or with bash.
From what I understand from your question, you want to add the current git repo name with each line from git log
.
Since you tagged Python, this might be a long shot, but you can use GitPython to determine if the subfolders inside a folder are git repositories. Then you can open a git log
command with subprocess.Popen()
, and print out each line with the repos name from stdout.
Make sure to pip install GitPython
before running this code.
Here is an example:
from os import listdir
from os import chdir
from os import getcwd
from os.path import abspath
from git import Repo
from git import InvalidGitRepositoryError
from subprocess import Popen
from subprocess import PIPE
# Current working directory with all git repositories
# You can change this path to your liking
ROOT_PATH = getcwd()
# Go over each file in current working directory
for file in listdir(ROOT_PATH):
full_path = abspath(file)
# Check if file is a git repository
try:
Repo(full_path)
# Change to directory
chdir(full_path)
# Run git log command
with Popen(
args=['git', 'log', '--pretty=format:"%h - %an, %ar : %s"'],
shell=False,
stdout=PIPE,
bufsize=1,
universal_newlines=True,
) as process:
# Print out each line from stdout with repo name
for line in process.stdout:
print('%s %s' % (file, line.strip()))
# Change back to path
chdir(ROOT_PATH)
# If we hit here, file is not a git repository
except InvalidGitRepositoryError:
continue
This works for me when I run the script inside a folder with all my git repositories.
Note: There is probably a nicer way to do this with the git
command itself or with bash.
edited Nov 19 at 14:23
answered Nov 19 at 12:12
RoadRunner
9,14331138
9,14331138
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373523%2fexecute-a-git-command-without-being-in-the-repository-and-outputting-the-current%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown