Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
git2
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
lvzhengyang
git2
Commits
af95615f
Unverified
Commit
af95615f
authored
Apr 26, 2019
by
Patrick Steinhardt
Committed by
GitHub
Apr 26, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #5051 from pks-t/pks/examples-ssh-auth
examples: implement SSH authentication
parents
b3923cf7
172786ec
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
47 additions
and
25 deletions
+47
-25
examples/blame.c
+0
-5
examples/common.c
+42
-16
examples/common.h
+5
-0
examples/merge.c
+0
-4
No files found.
examples/blame.c
View file @
af95615f
...
...
@@ -14,11 +14,6 @@
#include "common.h"
#ifdef _MSC_VER
#define snprintf sprintf_s
#define strcasecmp strcmpi
#endif
/**
* This example demonstrates how to invoke the libgit2 blame API to roughly
* simulate the output of `git blame` and a few of its command line arguments.
...
...
examples/common.c
View file @
af95615f
...
...
@@ -330,38 +330,64 @@ error:
return
error
;
}
static
int
ask
(
char
**
out
,
const
char
*
prompt
,
char
optional
)
{
printf
(
"%s "
,
prompt
);
fflush
(
stdout
);
if
(
!
readline
(
out
)
&&
!
optional
)
{
fprintf
(
stderr
,
"Could not read response: %s"
,
strerror
(
errno
));
return
-
1
;
}
return
0
;
}
int
cred_acquire_cb
(
git_cred
**
out
,
const
char
*
url
,
const
char
*
username_from_url
,
unsigned
int
allowed_types
,
void
*
payload
)
{
char
*
username
=
NULL
,
*
password
=
NULL
;
int
error
;
char
*
username
=
NULL
,
*
password
=
NULL
,
*
privkey
=
NULL
,
*
pubkey
=
NULL
;
int
error
=
1
;
UNUSED
(
url
);
UNUSED
(
username_from_url
);
UNUSED
(
allowed_types
);
UNUSED
(
payload
);
printf
(
"Username: "
);
if
(
readline
(
&
username
)
<
0
)
{
fprintf
(
stderr
,
"Unable to read username: %s"
,
strerror
(
errno
));
return
-
1
;
if
(
username_from_url
)
{
if
((
username
=
strdup
(
username_from_url
))
==
NULL
)
goto
out
;
}
else
if
((
error
=
ask
(
&
username
,
"Username:"
,
0
))
<
0
)
{
goto
out
;
}
/* Yup. Right there on your terminal. Careful where you copy/paste output. */
printf
(
"Password: "
);
if
(
readline
(
&
password
)
<
0
)
{
fprintf
(
stderr
,
"Unable to read password: %s"
,
strerror
(
errno
));
free
(
username
);
return
-
1
;
}
if
(
allowed_types
&
GIT_CREDTYPE_SSH_KEY
)
{
int
n
;
if
((
error
=
ask
(
&
privkey
,
"SSH Key:"
,
0
))
<
0
||
(
error
=
ask
(
&
password
,
"Password:"
,
1
))
<
0
)
goto
out
;
if
((
n
=
snprintf
(
NULL
,
0
,
"%s.pub"
,
privkey
))
<
0
||
(
pubkey
=
malloc
(
n
+
1
))
==
NULL
||
(
n
=
snprintf
(
pubkey
,
n
+
1
,
"%s.pub"
,
privkey
))
<
0
)
goto
out
;
error
=
git_cred_ssh_key_new
(
out
,
username
,
pubkey
,
privkey
,
password
);
}
else
if
(
allowed_types
&
GIT_CREDTYPE_USERPASS_PLAINTEXT
)
{
if
((
error
=
ask
(
&
password
,
"Password:"
,
1
))
<
0
)
goto
out
;
error
=
git_cred_userpass_plaintext_new
(
out
,
username
,
password
);
}
else
if
(
allowed_types
&
GIT_CREDTYPE_USERNAME
)
{
error
=
git_cred_username_new
(
out
,
username
);
}
out:
free
(
username
);
free
(
password
);
free
(
privkey
);
free
(
pubkey
);
return
error
;
}
examples/common.h
View file @
af95615f
...
...
@@ -26,6 +26,11 @@
#endif
#endif
#ifdef _MSC_VER
#define snprintf sprintf_s
#define strcasecmp strcmpi
#endif
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(*x))
#define UNUSED(x) (void)(x)
...
...
examples/merge.c
View file @
af95615f
...
...
@@ -15,10 +15,6 @@
#include "common.h"
#include <assert.h>
#ifdef _MSC_VER
#define snprintf sprintf_s
#endif
/** The following example demonstrates how to do merges with libgit2.
*
* It will merge whatever commit-ish you pass in into the current branch.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment