File: pull.R

package info (click to toggle)
r-cran-git2r 0.26.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 6,884 kB
  • sloc: ansic: 101,730; sh: 3,364; makefile: 2
file content (116 lines) | stat: -rw-r--r-- 4,351 bytes parent folder | download
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
## git2r, R bindings to the libgit2 library.
## Copyright (C) 2013-2018 The git2r contributors
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License, version 2,
## as published by the Free Software Foundation.
##
## git2r is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License along
## with this program; if not, write to the Free Software Foundation, Inc.,
## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

##' Pull
##'
##' @template repo-param
##' @param credentials The credentials for remote repository
##'     access. Default is NULL. To use and query an ssh-agent for the
##'     ssh key credentials, let this parameter be NULL (the default).
##' @param merger Who made the merge, if the merge is non-fast forward
##'     merge that creates a merge commit. The
##'     \code{default_signature} for \code{repo} is used if this
##'     parameter is \code{NULL}.
##' @template return-git_merge_result
##' @export
##' @examples
##' \dontrun{
##' ## Initialize repositories
##' path_bare <- tempfile(pattern="git2r-")
##' path_repo_1 <- tempfile(pattern="git2r-")
##' path_repo_2 <- tempfile(pattern="git2r-")
##' dir.create(path_bare)
##' dir.create(path_repo_1)
##' dir.create(path_repo_2)
##' repo_bare <- init(path_bare, bare = TRUE)
##' repo_1 <- clone(path_bare, path_repo_1)
##'
##' ## Config first user and commit a file
##' config(repo_1, user.name="Alice", user.email="alice@@example.org")
##'
##' ## Write to a file and commit
##' writeLines("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do",
##'            file.path(path_repo_1, "example.txt"))
##' add(repo_1, "example.txt")
##' commit(repo_1, "First commit message")
##'
##' ## Push commits from first repository to bare repository
##' ## Adds an upstream tracking branch to branch 'master'
##' push(repo_1, "origin", "refs/heads/master")
##'
##' ## Clone to second repository
##' repo_2 <- clone(path_bare, path_repo_2)
##' config(repo_2, user.name="Bob", user.email="bob@@example.org")
##'
##' ## Change file and commit
##' writeLines(c("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do",
##'              "eiusmod tempor incididunt ut labore et dolore magna aliqua."),
##'            file.path(path_repo_1, "example.txt"))
##' add(repo_1, "example.txt")
##' commit(repo_1, "Second commit message")
##'
##' ## Push commits from first repository to bare repository
##' push(repo_1)
##'
##' ## Pull changes to repo_2
##' pull(repo_2)
##'
##' ## Change file again and commit. This time in repository 2
##' writeLines(c("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do",
##'              "eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad",
##'              "minim veniam, quis nostrud exercitation ullamco laboris nisi ut"),
##'            file.path(path_repo_2, "example.txt"))
##' add(repo_2, "example.txt")
##' commit(repo_2, "Third commit message")
##'
##' ## Push commits from second repository to bare repository
##' push(repo_2)
##'
##' ## Pull changes to repo_1
##' pull(repo_1)
##'
##' ## List commits in repositories
##' commits(repo_1)
##' commits(repo_2)
##' commits(repo_bare)
##' }
pull <- function(repo = ".", credentials = NULL, merger = NULL) {
    repo <- lookup_repository(repo)
    if (is.null(merger))
        merger <- default_signature(repo)
    current_branch <- repository_head(repo)

    if (is.null(current_branch))
        stop("'branch' is NULL")
    if (!is_local(current_branch))
        stop("'branch' is not local")
    upstream_branch <- branch_get_upstream(current_branch)
    if (is.null(upstream_branch))
        stop("'branch' is not tracking a remote branch")

    fetch(repo        = repo,
          name        = branch_remote_name(upstream_branch),
          credentials = credentials)

    ## fetch heads marked for merge
    fh <- fetch_heads(repo)
    fh <- fh[vapply(fh, "[[", logical(1), "is_merge")]

    if (identical(length(fh), 0L))
        stop("Remote ref was not fetched")

    .Call(git2r_merge_fetch_heads, fh, merger)
}