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
503dd0f3
Commit
503dd0f3
authored
May 09, 2013
by
Vicent Martí
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1560 from carlosmn/ref-dwim
Expose git_reference_dwim
parents
ddc5c054
98d633cc
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
78 additions
and
65 deletions
+78
-65
include/git2/refs.h
+13
-0
src/refs.c
+61
-0
src/revparse.c
+4
-65
No files found.
include/git2/refs.h
View file @
503dd0f3
...
@@ -55,6 +55,19 @@ GIT_EXTERN(int) git_reference_name_to_id(
...
@@ -55,6 +55,19 @@ GIT_EXTERN(int) git_reference_name_to_id(
git_oid
*
out
,
git_repository
*
repo
,
const
char
*
name
);
git_oid
*
out
,
git_repository
*
repo
,
const
char
*
name
);
/**
/**
* Lookup a reference by DWIMing its short name
*
* Apply the git precendence rules to the given shorthand to determine
* which reference the user is refering to.
*
* @param out pointer in which to store the reference
* @param repo the repository in which to look
* @param shrothand the short name for the reference
* @return 0 or an error code
*/
GIT_EXTERN
(
int
)
git_reference_dwim
(
git_reference
**
out
,
git_repository
*
repo
,
const
char
*
shorthand
);
/**
* Create a new symbolic reference.
* Create a new symbolic reference.
*
*
* A symbolic reference is a reference name that refers to another
* A symbolic reference is a reference name that refers to another
...
...
src/refs.c
View file @
503dd0f3
...
@@ -290,6 +290,67 @@ int git_reference_lookup_resolved(
...
@@ -290,6 +290,67 @@ int git_reference_lookup_resolved(
return
0
;
return
0
;
}
}
int
git_reference_dwim
(
git_reference
**
out
,
git_repository
*
repo
,
const
char
*
refname
)
{
int
error
=
0
,
i
;
bool
fallbackmode
=
true
,
foundvalid
=
false
;
git_reference
*
ref
;
git_buf
refnamebuf
=
GIT_BUF_INIT
,
name
=
GIT_BUF_INIT
;
static
const
char
*
formatters
[]
=
{
"%s"
,
GIT_REFS_DIR
"%s"
,
GIT_REFS_TAGS_DIR
"%s"
,
GIT_REFS_HEADS_DIR
"%s"
,
GIT_REFS_REMOTES_DIR
"%s"
,
GIT_REFS_REMOTES_DIR
"%s/"
GIT_HEAD_FILE
,
NULL
};
if
(
*
refname
)
git_buf_puts
(
&
name
,
refname
);
else
{
git_buf_puts
(
&
name
,
GIT_HEAD_FILE
);
fallbackmode
=
false
;
}
for
(
i
=
0
;
formatters
[
i
]
&&
(
fallbackmode
||
i
==
0
);
i
++
)
{
git_buf_clear
(
&
refnamebuf
);
if
((
error
=
git_buf_printf
(
&
refnamebuf
,
formatters
[
i
],
git_buf_cstr
(
&
name
)))
<
0
)
goto
cleanup
;
if
(
!
git_reference_is_valid_name
(
git_buf_cstr
(
&
refnamebuf
)))
{
error
=
GIT_EINVALIDSPEC
;
continue
;
}
foundvalid
=
true
;
error
=
git_reference_lookup_resolved
(
&
ref
,
repo
,
git_buf_cstr
(
&
refnamebuf
),
-
1
);
if
(
!
error
)
{
*
out
=
ref
;
error
=
0
;
goto
cleanup
;
}
if
(
error
!=
GIT_ENOTFOUND
)
goto
cleanup
;
}
cleanup:
if
(
error
&&
!
foundvalid
)
{
/* never found a valid reference name */
giterr_set
(
GITERR_REFERENCE
,
"Could not use '%s' as valid reference name"
,
git_buf_cstr
(
&
name
));
}
git_buf_free
(
&
name
);
git_buf_free
(
&
refnamebuf
);
return
error
;
}
/**
/**
* Getters
* Getters
*/
*/
...
...
src/revparse.c
View file @
503dd0f3
...
@@ -14,67 +14,6 @@
...
@@ -14,67 +14,6 @@
#include "git2.h"
#include "git2.h"
static
int
disambiguate_refname
(
git_reference
**
out
,
git_repository
*
repo
,
const
char
*
refname
)
{
int
error
=
0
,
i
;
bool
fallbackmode
=
true
,
foundvalid
=
false
;
git_reference
*
ref
;
git_buf
refnamebuf
=
GIT_BUF_INIT
,
name
=
GIT_BUF_INIT
;
static
const
char
*
formatters
[]
=
{
"%s"
,
GIT_REFS_DIR
"%s"
,
GIT_REFS_TAGS_DIR
"%s"
,
GIT_REFS_HEADS_DIR
"%s"
,
GIT_REFS_REMOTES_DIR
"%s"
,
GIT_REFS_REMOTES_DIR
"%s/"
GIT_HEAD_FILE
,
NULL
};
if
(
*
refname
)
git_buf_puts
(
&
name
,
refname
);
else
{
git_buf_puts
(
&
name
,
GIT_HEAD_FILE
);
fallbackmode
=
false
;
}
for
(
i
=
0
;
formatters
[
i
]
&&
(
fallbackmode
||
i
==
0
);
i
++
)
{
git_buf_clear
(
&
refnamebuf
);
if
((
error
=
git_buf_printf
(
&
refnamebuf
,
formatters
[
i
],
git_buf_cstr
(
&
name
)))
<
0
)
goto
cleanup
;
if
(
!
git_reference_is_valid_name
(
git_buf_cstr
(
&
refnamebuf
)))
{
error
=
GIT_EINVALIDSPEC
;
continue
;
}
foundvalid
=
true
;
error
=
git_reference_lookup_resolved
(
&
ref
,
repo
,
git_buf_cstr
(
&
refnamebuf
),
-
1
);
if
(
!
error
)
{
*
out
=
ref
;
error
=
0
;
goto
cleanup
;
}
if
(
error
!=
GIT_ENOTFOUND
)
goto
cleanup
;
}
cleanup:
if
(
error
&&
!
foundvalid
)
{
/* never found a valid reference name */
giterr_set
(
GITERR_REFERENCE
,
"Could not use '%s' as valid reference name"
,
git_buf_cstr
(
&
name
));
}
git_buf_free
(
&
name
);
git_buf_free
(
&
refnamebuf
);
return
error
;
}
static
int
maybe_sha_or_abbrev
(
git_object
**
out
,
git_repository
*
repo
,
const
char
*
spec
,
size_t
speclen
)
static
int
maybe_sha_or_abbrev
(
git_object
**
out
,
git_repository
*
repo
,
const
char
*
spec
,
size_t
speclen
)
{
{
git_oid
oid
;
git_oid
oid
;
...
@@ -157,7 +96,7 @@ static int revparse_lookup_object(git_object **out, git_repository *repo, const
...
@@ -157,7 +96,7 @@ static int revparse_lookup_object(git_object **out, git_repository *repo, const
if
(
error
<
0
&&
error
!=
GIT_ENOTFOUND
)
if
(
error
<
0
&&
error
!=
GIT_ENOTFOUND
)
return
error
;
return
error
;
error
=
disambiguate_refname
(
&
ref
,
repo
,
spec
);
error
=
git_reference_dwim
(
&
ref
,
repo
,
spec
);
if
(
!
error
)
{
if
(
!
error
)
{
error
=
git_object_lookup
(
out
,
repo
,
git_reference_target
(
ref
),
GIT_OBJ_ANY
);
error
=
git_object_lookup
(
out
,
repo
,
git_reference_target
(
ref
),
GIT_OBJ_ANY
);
git_reference_free
(
ref
);
git_reference_free
(
ref
);
...
@@ -242,7 +181,7 @@ static int retrieve_previously_checked_out_branch_or_revision(git_object **out,
...
@@ -242,7 +181,7 @@ static int retrieve_previously_checked_out_branch_or_revision(git_object **out,
git_buf_put
(
&
buf
,
msg
+
regexmatches
[
1
].
rm_so
,
regexmatches
[
1
].
rm_eo
-
regexmatches
[
1
].
rm_so
);
git_buf_put
(
&
buf
,
msg
+
regexmatches
[
1
].
rm_so
,
regexmatches
[
1
].
rm_eo
-
regexmatches
[
1
].
rm_so
);
if
((
error
=
disambiguate_refname
(
base_ref
,
repo
,
git_buf_cstr
(
&
buf
)))
==
0
)
if
((
error
=
git_reference_dwim
(
base_ref
,
repo
,
git_buf_cstr
(
&
buf
)))
==
0
)
goto
cleanup
;
goto
cleanup
;
if
(
error
<
0
&&
error
!=
GIT_ENOTFOUND
)
if
(
error
<
0
&&
error
!=
GIT_ENOTFOUND
)
...
@@ -323,7 +262,7 @@ static int retrieve_revobject_from_reflog(git_object **out, git_reference **base
...
@@ -323,7 +262,7 @@ static int retrieve_revobject_from_reflog(git_object **out, git_reference **base
int
error
=
-
1
;
int
error
=
-
1
;
if
(
*
base_ref
==
NULL
)
{
if
(
*
base_ref
==
NULL
)
{
if
((
error
=
disambiguate_refname
(
&
ref
,
repo
,
identifier
))
<
0
)
if
((
error
=
git_reference_dwim
(
&
ref
,
repo
,
identifier
))
<
0
)
return
error
;
return
error
;
}
else
{
}
else
{
ref
=
*
base_ref
;
ref
=
*
base_ref
;
...
@@ -351,7 +290,7 @@ static int retrieve_remote_tracking_reference(git_reference **base_ref, const ch
...
@@ -351,7 +290,7 @@ static int retrieve_remote_tracking_reference(git_reference **base_ref, const ch
int
error
=
-
1
;
int
error
=
-
1
;
if
(
*
base_ref
==
NULL
)
{
if
(
*
base_ref
==
NULL
)
{
if
((
error
=
disambiguate_refname
(
&
ref
,
repo
,
identifier
))
<
0
)
if
((
error
=
git_reference_dwim
(
&
ref
,
repo
,
identifier
))
<
0
)
return
error
;
return
error
;
}
else
{
}
else
{
ref
=
*
base_ref
;
ref
=
*
base_ref
;
...
...
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