NullPointerException
NullPointerException

Reputation: 37681

How to use Git commands without human interaction?

I have a remote machine with a program which downloads some source codes from some repos using a only-read svn user for that program. Now is working with svn without human interaction doing thinks like this:

Doing checkouts:

p = Runtime.getRuntime().exec("svn checkout --non-interactive --trust-server-cert --username "+Constants.svnUser+" --password "+Constants.svnPassword+" "+repositoryURL+" ." , null , dir);

Updating code:

p = Runtime.getRuntime().exec("svn update --non-interactive --trust-server-cert --username "+Constants.svnUser+" --password "+Constants.svnPassword+" ", null , dir);

It works perfectly, but now I'm trying to download source code from Git repositories using a read only Git user, and I can't find the way to do the same non interactive behaviour with Git.

Given a Git repo, a git user and a git password, how to init Git and clone, checkout or pull source code without human interaction?

I can't find any info about how to achieve that give these three items.

Upvotes: 7

Views: 8220

Answers (2)

jawira
jawira

Reputation: 4598

I often use --no-pager flag, for example to directly output the diff in terminal:

$ git --no-pager diff

Upvotes: 3

Stefan
Stefan

Reputation: 81

From git v2.3.0, you can set the environment variable GIT_TERMINAL_PROMPT=0 to avoid your script to hang on the git command expecting manual authentication.

Example:

$ GIT_TERMINAL_PROMPT=0 git clone https://git.example.com/my/repo.git
Cloning into 'repo'...
fatal: could not read Username for 'https://git.example.com': terminal prompts disabled
$ echo $?
128

Source: man 1 git

Upvotes: 8

Related Questions