1

I'm trying to use the following Java syntax to execute a Git blame command:

Process process = Runtime.getRuntime().exec("/usr/bin/git blame https://github.com/git/git/alloc.c > TestGit.txt");

But, when I run the code in Java, it doesn't give any result. And, when I tried to execute it in the Terminal, it just creates an empty "TestGit.txt" file, and I got the error: fatal: Not a git repository (or any of the parent directories): .git.

4 Answers 4

3

Be sure to change directories into the git repository prior to launching the command

Process process = Runtime.getRuntime().exec("cd /path/to/repository; /usr/bin/git blame https://github.com/git/git/alloc.c > TestGit.txt");
Sign up to request clarification or add additional context in comments.

1 Comment

@caarlos0 Either you are not on a Linux / Unix system, or your shell has the command cd built-in. If the latter, try launching /bin/bash -c with the rest of the command as the "input string".
0

I think for git blame to work, you'll need to be inside the repository. You'd need to be inside the repository directory (which means you'll need to check it out first), and run:

git blame alloc.c

3 Comments

ya when I use the "cd" command to go inside "git" folder. Then, I use "git blame alloc.c" in the terminal, it works. However, I need to execute the blame command on the on-line file. But, it didn't work
You can't use git blame against an online file in the way you're doing; you have to check out the repository first.
OK, I cloned it to my computer. But now, when I use the "cd" command to be inside the directory (git), and then apply "blame" command, it works. However, when I use the path of the file "git blame /home/gh/git", I get the same error???
0

The following is the right syntax to execute blame command in Java for linux:

git --git-dir=/home/gh/git/.git --work-tree=/home/gh/git blame builtin/tag.c

Comments

0

The javier way:

Process process = Runtime.getRuntime().exec("/usr/bin/git blame https://github.com/git/git/alloc.c > TestGit.txt", null, new File('/path/to/repository'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.