]>
git.proxmox.com Git - libgit2.git/blob - src/fetchhead.c
2 * Copyright (C) 2009-2012 the libgit2 contributors
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.
8 #include "git2/types.h"
11 #include "fetchhead.h"
16 #include "repository.h"
19 int git_fetchhead_ref_cmp(const void *a
, const void *b
)
21 const git_fetchhead_ref
*one
= (const git_fetchhead_ref
*)a
;
22 const git_fetchhead_ref
*two
= (const git_fetchhead_ref
*)b
;
24 if (one
->is_merge
&& !two
->is_merge
)
26 if (two
->is_merge
&& !one
->is_merge
)
29 return strcmp(one
->ref_name
, two
->ref_name
);
32 int git_fetchhead_ref_create(
33 git_fetchhead_ref
**fetchhead_ref_out
,
37 const char *remote_url
)
39 git_fetchhead_ref
*fetchhead_ref
= NULL
;
41 assert(fetchhead_ref_out
&& oid
&& ref_name
&& remote_url
);
43 fetchhead_ref
= git__malloc(sizeof(git_fetchhead_ref
));
44 GITERR_CHECK_ALLOC(fetchhead_ref
);
46 memset(fetchhead_ref
, 0x0, sizeof(git_fetchhead_ref
));
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
);
53 *fetchhead_ref_out
= fetchhead_ref
;
58 static int fetchhead_ref_write(
60 git_fetchhead_ref
*fetchhead_ref
)
62 char oid
[GIT_OID_HEXSZ
+ 1];
63 const char *type
, *name
;
65 assert(file
&& fetchhead_ref
);
67 git_oid_fmt(oid
, &fetchhead_ref
->oid
);
68 oid
[GIT_OID_HEXSZ
] = '\0';
70 if (git__prefixcmp(fetchhead_ref
->ref_name
, GIT_REFS_HEADS_DIR
) == 0) {
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) {
76 name
= fetchhead_ref
->ref_name
+ strlen(GIT_REFS_TAGS_DIR
);
79 name
= fetchhead_ref
->ref_name
;
82 return git_filebuf_printf(file
, "%s\t%s\t%s'%s' of %s\n",
84 (fetchhead_ref
->is_merge
) ? "" : "not-for-merge",
87 fetchhead_ref
->remote_url
);
90 int git_fetchhead_write(git_repository
*repo
, git_vector
*fetchhead_refs
)
92 git_filebuf file
= GIT_FILEBUF_INIT
;
93 git_buf path
= GIT_BUF_INIT
;
95 git_fetchhead_ref
*fetchhead_ref
;
97 assert(repo
&& fetchhead_refs
);
99 if (git_buf_joinpath(&path
, repo
->path_repository
, GIT_FETCH_HEAD_FILE
) < 0)
102 if (git_filebuf_open(&file
, path
.ptr
, GIT_FILEBUF_FORCE
) < 0) {
109 git_vector_sort(fetchhead_refs
);
111 git_vector_foreach(fetchhead_refs
, i
, fetchhead_ref
)
112 fetchhead_ref_write(&file
, fetchhead_ref
);
114 return git_filebuf_commit(&file
, GIT_REFS_FILE_MODE
);
117 void git_fetchhead_ref_free(git_fetchhead_ref
*fetchhead_ref
)
119 if (fetchhead_ref
== NULL
)
122 git__free(fetchhead_ref
->remote_url
);
123 git__free(fetchhead_ref
->ref_name
);
124 git__free(fetchhead_ref
);