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
63e8c3fd
Commit
63e8c3fd
authored
May 28, 2014
by
Vicent Marti
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2337 from ethomson/merge_ff
Introduce GIT_MERGE_CONFIG_* for merge.ff settings
parents
433ba614
eff531e1
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
125 additions
and
34 deletions
+125
-34
include/git2/merge.h
+21
-0
src/merge.c
+49
-12
tests/merge/workdir/analysis.c
+55
-22
No files found.
include/git2/merge.h
View file @
63e8c3fd
...
...
@@ -268,6 +268,26 @@ typedef enum {
GIT_MERGE_ANALYSIS_UNBORN
=
(
1
<<
3
),
}
git_merge_analysis_t
;
typedef
enum
{
/*
* No configuration was found that suggests a preferred behavior for
* merge.
*/
GIT_MERGE_PREFERENCE_NONE
=
0
,
/**
* There is a `merge.ff=false` configuration setting, suggesting that
* the user does not want to allow a fast-forward merge.
*/
GIT_MERGE_PREFERENCE_NO_FASTFORWARD
=
(
1
<<
0
),
/**
* There is a `merge.ff=only` configuration setting, suggesting that
* the user only wants fast-forward merges.
*/
GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY
=
(
1
<<
1
),
}
git_merge_preference_t
;
/**
* Analyzes the given branch(es) and determines the opportunities for
* merging them into the HEAD of the repository.
...
...
@@ -280,6 +300,7 @@ typedef enum {
*/
GIT_EXTERN
(
int
)
git_merge_analysis
(
git_merge_analysis_t
*
analysis_out
,
git_merge_preference_t
*
preference_out
,
git_repository
*
repo
,
const
git_merge_head
**
their_heads
,
size_t
their_heads_len
);
...
...
src/merge.c
View file @
63e8c3fd
...
...
@@ -2564,8 +2564,42 @@ done:
return
error
;
}
static
int
merge_preference
(
git_merge_preference_t
*
out
,
git_repository
*
repo
)
{
git_config
*
config
;
const
char
*
value
;
int
bool_value
,
error
=
0
;
*
out
=
GIT_MERGE_PREFERENCE_NONE
;
if
((
error
=
git_repository_config_snapshot
(
&
config
,
repo
))
<
0
)
goto
done
;
if
((
error
=
git_config_get_string
(
&
value
,
config
,
"merge.ff"
))
<
0
)
{
if
(
error
==
GIT_ENOTFOUND
)
{
giterr_clear
();
error
=
0
;
}
goto
done
;
}
if
(
git_config_parse_bool
(
&
bool_value
,
value
)
==
0
)
{
if
(
!
bool_value
)
*
out
|=
GIT_MERGE_PREFERENCE_NO_FASTFORWARD
;
}
else
{
if
(
strcasecmp
(
value
,
"only"
)
==
0
)
*
out
|=
GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY
;
}
done:
git_config_free
(
config
);
return
error
;
}
int
git_merge_analysis
(
git_merge_analysis_t
*
out
,
git_merge_analysis_t
*
analysis_out
,
git_merge_preference_t
*
preference_out
,
git_repository
*
repo
,
const
git_merge_head
**
their_heads
,
size_t
their_heads_len
)
...
...
@@ -2573,14 +2607,7 @@ int git_merge_analysis(
git_merge_head
*
ancestor_head
=
NULL
,
*
our_head
=
NULL
;
int
error
=
0
;
assert
(
out
&&
repo
&&
their_heads
);
*
out
=
GIT_MERGE_ANALYSIS_NONE
;
if
(
git_repository_head_unborn
(
repo
))
{
*
out
=
GIT_MERGE_ANALYSIS_FASTFORWARD
|
GIT_MERGE_ANALYSIS_UNBORN
;
goto
done
;
}
assert
(
analysis_out
&&
preference_out
&&
repo
&&
their_heads
);
if
(
their_heads_len
!=
1
)
{
giterr_set
(
GITERR_MERGE
,
"Can only merge a single branch"
);
...
...
@@ -2588,20 +2615,30 @@ int git_merge_analysis(
goto
done
;
}
*
analysis_out
=
GIT_MERGE_ANALYSIS_NONE
;
if
((
error
=
merge_preference
(
preference_out
,
repo
))
<
0
)
goto
done
;
if
(
git_repository_head_unborn
(
repo
))
{
*
analysis_out
|=
GIT_MERGE_ANALYSIS_FASTFORWARD
|
GIT_MERGE_ANALYSIS_UNBORN
;
goto
done
;
}
if
((
error
=
merge_heads
(
&
ancestor_head
,
&
our_head
,
repo
,
their_heads
,
their_heads_len
))
<
0
)
goto
done
;
/* We're up-to-date if we're trying to merge our own common ancestor. */
if
(
ancestor_head
&&
git_oid_equal
(
&
ancestor_head
->
oid
,
&
their_heads
[
0
]
->
oid
))
*
out
=
GIT_MERGE_ANALYSIS_UP_TO_DATE
;
*
analysis_out
|
=
GIT_MERGE_ANALYSIS_UP_TO_DATE
;
/* We're fastforwardable if we're our own common ancestor. */
else
if
(
ancestor_head
&&
git_oid_equal
(
&
ancestor_head
->
oid
,
&
our_head
->
oid
))
*
out
=
GIT_MERGE_ANALYSIS_FASTFORWARD
|
GIT_MERGE_ANALYSIS_NORMAL
;
*
analysis_out
|
=
GIT_MERGE_ANALYSIS_FASTFORWARD
|
GIT_MERGE_ANALYSIS_NORMAL
;
/* Otherwise, just a normal merge is possible. */
else
*
out
=
GIT_MERGE_ANALYSIS_NORMAL
;
*
analysis_out
|
=
GIT_MERGE_ANALYSIS_NORMAL
;
done:
git_merge_head_free
(
ancestor_head
);
...
...
tests/merge/workdir/analysis.c
View file @
63e8c3fd
...
...
@@ -36,72 +36,105 @@ void test_merge_workdir_analysis__cleanup(void)
cl_git_sandbox_cleanup
();
}
static
git_merge_analysis_t
analysis_from_branch
(
const
char
*
branchname
)
static
void
analysis_from_branch
(
git_merge_analysis_t
*
merge_analysis
,
git_merge_preference_t
*
merge_pref
,
const
char
*
branchname
)
{
git_buf
refname
=
GIT_BUF_INIT
;
git_reference
*
their_ref
;
git_merge_head
*
their_head
;
git_merge_analysis_t
analysis
;
git_buf_printf
(
&
refname
,
"%s%s"
,
GIT_REFS_HEADS_DIR
,
branchname
);
cl_git_pass
(
git_reference_lookup
(
&
their_ref
,
repo
,
git_buf_cstr
(
&
refname
)));
cl_git_pass
(
git_merge_head_from_ref
(
&
their_head
,
repo
,
their_ref
));
cl_git_pass
(
git_merge_analysis
(
&
analysis
,
repo
,
(
const
git_merge_head
**
)
&
their_head
,
1
));
cl_git_pass
(
git_merge_analysis
(
merge_analysis
,
merge_pref
,
repo
,
(
const
git_merge_head
**
)
&
their_head
,
1
));
git_buf_free
(
&
refname
);
git_merge_head_free
(
their_head
);
git_reference_free
(
their_ref
);
return
analysis
;
}
void
test_merge_workdir_analysis__fastforward
(
void
)
{
git_merge_analysis_t
analysis
;
git_merge_analysis_t
merge_analysis
;
git_merge_preference_t
merge_pref
;
analysis
=
analysis_from_branch
(
FASTFORWARD_BRANCH
);
cl_assert_equal_i
(
GIT_MERGE_ANALYSIS_FASTFORWARD
,
(
analysis
&
GIT_MERGE_ANALYSIS_FASTFORWARD
));
cl_assert_equal_i
(
GIT_MERGE_ANALYSIS_NORMAL
,
(
analysis
&
GIT_MERGE_ANALYSIS_NORMAL
));
analysis
_from_branch
(
&
merge_analysis
,
&
merge_pref
,
FASTFORWARD_BRANCH
);
cl_assert_equal_i
(
GIT_MERGE_ANALYSIS_FASTFORWARD
,
(
merge_
analysis
&
GIT_MERGE_ANALYSIS_FASTFORWARD
));
cl_assert_equal_i
(
GIT_MERGE_ANALYSIS_NORMAL
,
(
merge_
analysis
&
GIT_MERGE_ANALYSIS_NORMAL
));
}
void
test_merge_workdir_analysis__no_fastforward
(
void
)
{
git_merge_analysis_t
analysis
;
git_merge_analysis_t
merge_analysis
;
git_merge_preference_t
merge_pref
;
analysis
=
analysis_from_branch
(
NOFASTFORWARD_BRANCH
);
cl_assert_equal_i
(
GIT_MERGE_ANALYSIS_NORMAL
,
analysis
);
analysis
_from_branch
(
&
merge_analysis
,
&
merge_pref
,
NOFASTFORWARD_BRANCH
);
cl_assert_equal_i
(
GIT_MERGE_ANALYSIS_NORMAL
,
merge_
analysis
);
}
void
test_merge_workdir_analysis__uptodate
(
void
)
{
git_merge_analysis_t
analysis
;
git_merge_analysis_t
merge_analysis
;
git_merge_preference_t
merge_pref
;
analysis
=
analysis_from_branch
(
UPTODATE_BRANCH
);
cl_assert_equal_i
(
GIT_MERGE_ANALYSIS_UP_TO_DATE
,
analysis
);
analysis
_from_branch
(
&
merge_analysis
,
&
merge_pref
,
UPTODATE_BRANCH
);
cl_assert_equal_i
(
GIT_MERGE_ANALYSIS_UP_TO_DATE
,
merge_
analysis
);
}
void
test_merge_workdir_analysis__uptodate_merging_prev_commit
(
void
)
{
git_merge_analysis_t
analysis
;
git_merge_analysis_t
merge_analysis
;
git_merge_preference_t
merge_pref
;
analysis
=
analysis_from_branch
(
PREVIOUS_BRANCH
);
cl_assert_equal_i
(
GIT_MERGE_ANALYSIS_UP_TO_DATE
,
analysis
);
analysis
_from_branch
(
&
merge_analysis
,
&
merge_pref
,
PREVIOUS_BRANCH
);
cl_assert_equal_i
(
GIT_MERGE_ANALYSIS_UP_TO_DATE
,
merge_
analysis
);
}
void
test_merge_workdir_analysis__unborn
(
void
)
{
git_merge_analysis_t
analysis
;
git_merge_analysis_t
merge_analysis
;
git_merge_preference_t
merge_pref
;
git_buf
master
=
GIT_BUF_INIT
;
git_buf_joinpath
(
&
master
,
git_repository_path
(
repo
),
"refs/heads/master"
);
p_unlink
(
git_buf_cstr
(
&
master
));
analysis
=
analysis_from_branch
(
NOFASTFORWARD_BRANCH
);
cl_assert_equal_i
(
GIT_MERGE_ANALYSIS_FASTFORWARD
,
(
analysis
&
GIT_MERGE_ANALYSIS_FASTFORWARD
));
cl_assert_equal_i
(
GIT_MERGE_ANALYSIS_UNBORN
,
(
analysis
&
GIT_MERGE_ANALYSIS_UNBORN
));
analysis
_from_branch
(
&
merge_analysis
,
&
merge_pref
,
NOFASTFORWARD_BRANCH
);
cl_assert_equal_i
(
GIT_MERGE_ANALYSIS_FASTFORWARD
,
(
merge_
analysis
&
GIT_MERGE_ANALYSIS_FASTFORWARD
));
cl_assert_equal_i
(
GIT_MERGE_ANALYSIS_UNBORN
,
(
merge_
analysis
&
GIT_MERGE_ANALYSIS_UNBORN
));
git_buf_free
(
&
master
);
}
void
test_merge_workdir_analysis__fastforward_with_config_noff
(
void
)
{
git_config
*
config
;
git_merge_analysis_t
merge_analysis
;
git_merge_preference_t
merge_pref
;
git_repository_config
(
&
config
,
repo
);
git_config_set_string
(
config
,
"merge.ff"
,
"false"
);
analysis_from_branch
(
&
merge_analysis
,
&
merge_pref
,
FASTFORWARD_BRANCH
);
cl_assert_equal_i
(
GIT_MERGE_ANALYSIS_FASTFORWARD
,
(
merge_analysis
&
GIT_MERGE_ANALYSIS_FASTFORWARD
));
cl_assert_equal_i
(
GIT_MERGE_ANALYSIS_NORMAL
,
(
merge_analysis
&
GIT_MERGE_ANALYSIS_NORMAL
));
cl_assert_equal_i
(
GIT_MERGE_PREFERENCE_NO_FASTFORWARD
,
(
merge_pref
&
GIT_MERGE_PREFERENCE_NO_FASTFORWARD
));
}
void
test_merge_workdir_analysis__no_fastforward_with_config_ffonly
(
void
)
{
git_config
*
config
;
git_merge_analysis_t
merge_analysis
;
git_merge_preference_t
merge_pref
;
git_repository_config
(
&
config
,
repo
);
git_config_set_string
(
config
,
"merge.ff"
,
"only"
);
analysis_from_branch
(
&
merge_analysis
,
&
merge_pref
,
NOFASTFORWARD_BRANCH
);
cl_assert_equal_i
(
GIT_MERGE_ANALYSIS_NORMAL
,
(
merge_analysis
&
GIT_MERGE_ANALYSIS_NORMAL
));
cl_assert_equal_i
(
GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY
,
(
merge_pref
&
GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY
));
}
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