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
503b30d5
Commit
503b30d5
authored
Jan 17, 2018
by
Etienne Samson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
examples: hoist the merge analysis back into main
parent
60c6547c
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
39 additions
and
58 deletions
+39
-58
examples/merge.c
+39
-58
No files found.
examples/merge.c
View file @
503b30d5
...
...
@@ -36,7 +36,6 @@ typedef struct {
size_t
annotated_count
;
int
no_commit
:
1
;
int
did_merge
:
1
;
}
merge_options
;
static
void
print_usage
(
void
)
...
...
@@ -203,23 +202,39 @@ static int perform_fastforward(git_repository *repo, const git_oid *target_oid,
return
0
;
}
static
int
analyze_merge
(
git_repository
*
repo
,
merge_options
*
opts
)
int
main
(
int
argc
,
char
**
argv
)
{
git_merge_options
merge_opts
=
GIT_MERGE_OPTIONS_INIT
;
git_checkout_options
checkout_opts
=
GIT_CHECKOUT_OPTIONS_INIT
;
git_repository
*
repo
=
NULL
;
merge_options
opts
;
git_index
*
index
;
git_repository_state_t
state
;
git_merge_analysis_t
analysis
;
git_merge_preference_t
preference
;
const
char
*
path
=
"."
;
int
err
=
0
;
merge_opt
s
.
flags
=
0
;
merge_opts
.
file_flags
=
GIT_MERGE_FILE_STYLE_DIFF3
;
merge_opt
ions_init
(
&
opts
)
;
parse_options
(
&
path
,
&
opts
,
argc
,
argv
)
;
checkout_opts
.
checkout_strategy
=
GIT_CHECKOUT_FORCE
|
GIT_CHECKOUT_ALLOW_CONFLICTS
;
git_libgit2_init
();
check_lg2
(
git_repository_open_ext
(
&
repo
,
path
,
0
,
NULL
),
"Could not open repository"
,
NULL
);
state
=
git_repository_state
(
repo
);
if
(
state
!=
GIT_REPOSITORY_STATE_NONE
)
{
fprintf
(
stderr
,
"repository is in unexpected state %d
\n
"
,
state
);
goto
cleanup
;
}
err
=
resolve_heads
(
repo
,
&
opts
);
if
(
err
!=
0
)
goto
cleanup
;
err
=
git_merge_analysis
(
&
analysis
,
&
preference
,
repo
,
(
const
git_annotated_commit
**
)
opts
->
annotated
,
opts
->
annotated_count
);
(
const
git_annotated_commit
**
)
opts
.
annotated
,
opts
.
annotated_count
);
check_lg2
(
err
,
"merge analysis failed"
,
NULL
);
if
(
analysis
&
GIT_MERGE_ANALYSIS_UP_TO_DATE
)
{
...
...
@@ -236,67 +251,31 @@ static int analyze_merge(git_repository *repo, merge_options *opts)
}
/* Since this is a fast-forward, there can be only one merge head */
target_oid
=
git_annotated_commit_id
(
opts
->
annotated
[
0
]);
target_oid
=
git_annotated_commit_id
(
opts
.
annotated
[
0
]);
assert
(
opts
.
annotated_count
==
1
);
return
perform_fastforward
(
repo
,
target_oid
,
(
analysis
&
GIT_MERGE_ANALYSIS_UNBORN
));
}
else
if
(
analysis
&
GIT_MERGE_ANALYSIS_NORMAL
)
{
git_merge_options
merge_opts
=
GIT_MERGE_OPTIONS_INIT
;
git_checkout_options
checkout_opts
=
GIT_CHECKOUT_OPTIONS_INIT
;
merge_opts
.
flags
=
0
;
merge_opts
.
file_flags
=
GIT_MERGE_FILE_STYLE_DIFF3
;
checkout_opts
.
checkout_strategy
=
GIT_CHECKOUT_FORCE
|
GIT_CHECKOUT_ALLOW_CONFLICTS
;
if
(
preference
&
GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY
)
{
printf
(
"Fast-forward is preferred, but only a merge is possible
\n
"
);
return
-
1
;
}
err
=
git_merge
(
repo
,
(
const
git_annotated_commit
**
)
opts
->
annotated
,
opts
->
annotated_count
,
(
const
git_annotated_commit
**
)
opts
.
annotated
,
opts
.
annotated_count
,
&
merge_opts
,
&
checkout_opts
);
if
(
err
!=
0
)
return
-
1
;
/* Inform that a merge was done */
opts
->
did_merge
=
1
;
return
0
;
check_lg2
(
err
,
"merge failed"
,
NULL
);
}
return
-
1
;
}
int
main
(
int
argc
,
char
**
argv
)
{
git_repository
*
repo
=
NULL
;
merge_options
opts
;
git_index
*
index
;
git_repository_state_t
state
;
const
char
*
path
=
"."
;
int
err
=
0
;
merge_options_init
(
&
opts
);
parse_options
(
&
path
,
&
opts
,
argc
,
argv
);
git_libgit2_init
();
check_lg2
(
git_repository_open_ext
(
&
repo
,
path
,
0
,
NULL
),
"Could not open repository"
,
NULL
);
state
=
git_repository_state
(
repo
);
if
(
state
!=
GIT_REPOSITORY_STATE_NONE
)
{
fprintf
(
stderr
,
"repository is in unexpected state %d
\n
"
,
state
);
goto
cleanup
;
}
err
=
resolve_heads
(
repo
,
&
opts
);
if
(
err
!=
0
)
goto
cleanup
;
err
=
analyze_merge
(
repo
,
&
opts
);
if
(
err
!=
0
)
{
fprintf
(
stderr
,
"merge failed
\n
"
);
goto
cleanup
;
}
if
(
!
opts
.
did_merge
)
{
/* Was either up-to-date, unborn, or a fast-forward, nothing left to do */
goto
cleanup
;
}
/* If we get here, we actually performed the merge above */
check_lg2
(
git_repository_index
(
&
index
,
repo
),
"failed to get repository index"
,
NULL
);
...
...
@@ -382,6 +361,8 @@ int main(int argc, char **argv)
/* We're done merging, cleanup the repository state */
git_repository_state_cleanup
(
repo
);
printf
(
"Merge made
\n
"
);
}
cleanup:
free
(
opts
.
heads
);
...
...
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