]> git.proxmox.com Git - libgit2.git/blob - src/fetchhead.c
Merge pull request #1016 from arrbee/fix-checkout-dir-removal
[libgit2.git] / src / fetchhead.c
1 /*
2 * Copyright (C) 2009-2012 the libgit2 contributors
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7
8 #include "git2/types.h"
9 #include "git2/oid.h"
10
11 #include "fetchhead.h"
12 #include "common.h"
13 #include "fileops.h"
14 #include "filebuf.h"
15 #include "refs.h"
16 #include "repository.h"
17
18
19 int git_fetchhead_ref_cmp(const void *a, const void *b)
20 {
21 const git_fetchhead_ref *one = (const git_fetchhead_ref *)a;
22 const git_fetchhead_ref *two = (const git_fetchhead_ref *)b;
23
24 if (one->is_merge && !two->is_merge)
25 return -1;
26 if (two->is_merge && !one->is_merge)
27 return 1;
28
29 return strcmp(one->ref_name, two->ref_name);
30 }
31
32 int git_fetchhead_ref_create(
33 git_fetchhead_ref **fetchhead_ref_out,
34 git_oid *oid,
35 int is_merge,
36 const char *ref_name,
37 const char *remote_url)
38 {
39 git_fetchhead_ref *fetchhead_ref = NULL;
40
41 assert(fetchhead_ref_out && oid && ref_name && remote_url);
42
43 fetchhead_ref = git__malloc(sizeof(git_fetchhead_ref));
44 GITERR_CHECK_ALLOC(fetchhead_ref);
45
46 memset(fetchhead_ref, 0x0, sizeof(git_fetchhead_ref));
47
48 git_oid_cpy(&fetchhead_ref->oid, oid);
49 fetchhead_ref->is_merge = is_merge;
50 fetchhead_ref->ref_name = git__strdup(ref_name);
51 fetchhead_ref->remote_url = git__strdup(remote_url);
52
53 *fetchhead_ref_out = fetchhead_ref;
54
55 return 0;
56 }
57
58 static int fetchhead_ref_write(
59 git_filebuf *file,
60 git_fetchhead_ref *fetchhead_ref)
61 {
62 char oid[GIT_OID_HEXSZ + 1];
63 const char *type, *name;
64
65 assert(file && fetchhead_ref);
66
67 git_oid_fmt(oid, &fetchhead_ref->oid);
68 oid[GIT_OID_HEXSZ] = '\0';
69
70 if (git__prefixcmp(fetchhead_ref->ref_name, GIT_REFS_HEADS_DIR) == 0) {
71 type = "branch ";
72 name = fetchhead_ref->ref_name + strlen(GIT_REFS_HEADS_DIR);
73 } else if(git__prefixcmp(fetchhead_ref->ref_name,
74 GIT_REFS_TAGS_DIR) == 0) {
75 type = "tag ";
76 name = fetchhead_ref->ref_name + strlen(GIT_REFS_TAGS_DIR);
77 } else {
78 type = "";
79 name = fetchhead_ref->ref_name;
80 }
81
82 return git_filebuf_printf(file, "%s\t%s\t%s'%s' of %s\n",
83 oid,
84 (fetchhead_ref->is_merge) ? "" : "not-for-merge",
85 type,
86 name,
87 fetchhead_ref->remote_url);
88 }
89
90 int git_fetchhead_write(git_repository *repo, git_vector *fetchhead_refs)
91 {
92 git_filebuf file = GIT_FILEBUF_INIT;
93 git_buf path = GIT_BUF_INIT;
94 unsigned int i;
95 git_fetchhead_ref *fetchhead_ref;
96
97 assert(repo && fetchhead_refs);
98
99 if (git_buf_joinpath(&path, repo->path_repository, GIT_FETCH_HEAD_FILE) < 0)
100 return -1;
101
102 if (git_filebuf_open(&file, path.ptr, GIT_FILEBUF_FORCE) < 0) {
103 git_buf_free(&path);
104 return -1;
105 }
106
107 git_buf_free(&path);
108
109 git_vector_sort(fetchhead_refs);
110
111 git_vector_foreach(fetchhead_refs, i, fetchhead_ref)
112 fetchhead_ref_write(&file, fetchhead_ref);
113
114 return git_filebuf_commit(&file, GIT_REFS_FILE_MODE);
115 }
116
117 void git_fetchhead_ref_free(git_fetchhead_ref *fetchhead_ref)
118 {
119 if (fetchhead_ref == NULL)
120 return;
121
122 git__free(fetchhead_ref->remote_url);
123 git__free(fetchhead_ref->ref_name);
124 git__free(fetchhead_ref);
125 }
126