/
Merge.fs
73 lines (64 loc) · 2.3 KB
/
Merge.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
namespace Fake.Tools.Git
open System.IO
open Fake.Tools.Git
/// <summary>
/// Contains helper functions which allow to deal with git merge.
/// </summary>
[<RequireQualifiedAccess>]
module Merge =
/// <summary>
/// Gets the current merge message.
/// </summary>
///
/// <param name="repositoryDir">The git repository.</param>
let getMergeMessage repositoryDir =
let file = (CommandHelper.findGitDir repositoryDir).FullName + "\\MERGE_MSG"
if File.Exists file then File.ReadAllText file else ""
/// <summary>
/// Allows git to use fast-forward merges
/// </summary>
let FastForwardFlag = "--ff"
/// <summary>
/// Forbids git to use fast-forward merges
/// </summary>
let NoFastForwardFlag = "--no-ff"
/// <summary>
/// Git merge option.
/// </summary>
type MergeType =
| SameCommit
| FirstNeedsFastForward
| SecondNeedsFastForward
| NeedsRealMerge
/// <summary>
/// Tests whether branches and their "origin" counterparts have diverged and need merging first.
/// </summary>
///
/// <param name="repositoryDir">The git repository.</param>
/// <param name="local">The local branch name.</param>
/// <param name="remote">The remote branch name.</param>
let compareBranches repositoryDir local remote =
let commit1 = Branches.getSHA1 repositoryDir local
let commit2 = Branches.getSHA1 repositoryDir remote
if commit1 = commit2 then
SameCommit
else
match Branches.findMergeBase repositoryDir commit1 commit2 with
| x when x = commit1 -> FirstNeedsFastForward
| x when x = commit2 -> SecondNeedsFastForward
| _ -> NeedsRealMerge
/// <summary>
/// Performs a merge of the given branch with the current branch
/// </summary>
///
/// <param name="repositoryDir">The git repository.</param>
/// <param name="flags">Additional flags.</param>
/// <param name="branch">The branch we want to merge in.</param>
///
/// <example>
/// <code lang="fsharp">
/// merge @"C:\code\Fake" NoFastForwardFlag "master"
/// </code>
/// </example>
let merge repositoryDir flags branch =
sprintf "merge %s %s" flags branch |> CommandHelper.gitCommand repositoryDir