1 /*
2  *             Copyright Sönke Ludwig 2014.
3  *  Distributed under the Boost Software License, Version 1.0.
4  *     (See accompanying file LICENSE_1_0.txt or copy at
5  *           http://www.boost.org/LICENSE_1_0.txt)
6  */
7 module git.reference;
8 
9 import git.object_;
10 import git.oid;
11 import git.repository;
12 import git.types;
13 import git.util;
14 
15 import deimos.git2.refs;
16 import deimos.git2.types;
17 
18 import std.conv : to;
19 import std.exception : enforce;
20 import std..string : toStringz;
21 
22 
23 GitReference lookupReference(GitRepo repo, string name)
24 {
25 	git_reference* ret;
26 	require(git_reference_lookup(&ret, repo.cHandle, name.toStringz()) == 0);
27 	return GitReference(repo, ret);
28 }
29 
30 GitOid lookupReferenceId(GitRepo repo, string name)
31 {
32 	GitOid ret;
33 	require(git_reference_name_to_id(&ret._get_oid(), repo.cHandle, name.toStringz()) == 0);
34 	return ret;
35 }
36 
37 GitReference dwimReference(GitRepo repo, string shorthand)
38 {
39 	git_reference* ret;
40 	require(git_reference_dwim(&ret,repo.cHandle, shorthand.toStringz()) == 0);
41 	return GitReference(repo, ret);
42 }
43 
44 GitReference createSymbolicReference(GitRepo repo, string name, string target, bool force)
45 {
46 	git_reference* ret;
47 	require(git_reference_symbolic_create(&ret, repo.cHandle, name.toStringz(), target.toStringz(), force) == 0);
48 	return GitReference(repo, ret);
49 }
50 
51 GitReference createReference(GitRepo repo, string name, GitOid target, bool force)
52 {
53 	git_reference* ret;
54 	require(git_reference_create(&ret, repo.cHandle, name.toStringz(), &target._get_oid(), force) == 0);
55 	return GitReference(repo, ret);
56 }
57 
58 
59 struct GitReference {
60 	this(GitObject object)
61 	{
62 		enforce(object.type == GitType.commit, "GIT object is not a reference.");
63 		_object = object;
64 	}
65 
66 	package this(GitRepo repo, git_reference* reference)
67 	{
68 		_object = GitObject(repo, cast(git_object*)reference);
69 	}
70 
71 	@property GitOid target()
72 	{
73 		auto ret = git_reference_target(this.cHandle);
74 		enforce(ret !is null, "Reference has no target");
75 		return GitOid(*ret);
76 	}
77 	@property GitOid peeledTarget()
78 	{
79 		auto ret = git_reference_target_peel(this.cHandle);
80 		enforce(ret !is null, "Reference has no target");
81 		return GitOid(*ret);
82 	}
83 	@property string symbolicTarget() { return git_reference_symbolic_target(this.cHandle).to!string; }
84 	@property git_ref_t type() { return git_reference_type(this.cHandle); }
85 	@property string name() { return git_reference_name(this.cHandle).to!string; }
86 	@property GitRepo owner() { return _object.owner; }
87 
88 	GitReference resolve()
89 	{
90 		git_reference* ret;
91 		require(git_reference_resolve(&ret, this.cHandle) == 0);
92 		return GitReference(this.owner, ret);
93 	}
94 
95 	GitReference setSymbolicTarget(string target)
96 	{
97 		git_reference* ret;
98 		require(git_reference_symbolic_set_target(&ret, this.cHandle, target.toStringz) == 0);
99 		return GitReference(this.owner, ret);
100 	}
101 
102 	GitReference setTarget(GitOid oid)
103 	{
104 		git_reference* ret;
105 		require(git_reference_set_target(&ret, this.cHandle, &oid._get_oid()) == 0);
106 		return GitReference(this.owner, ret);
107 	}
108 
109 	GitReference rename(string new_name, bool force)
110 	{
111 		git_reference* ret;
112 		require(git_reference_rename(&ret, this.cHandle, new_name.toStringz, force) == 0);
113 		return GitReference(this.owner, ret);
114 	}
115 
116 	void delete_() { require(git_reference_delete(this.cHandle) == 0); }
117 
118 
119 /*int git_reference_list(git_strarray *array, git_repository *repo);
120 
121 alias git_reference_foreach_cb = int function(git_reference *reference, void *payload);
122 alias git_reference_foreach_name_cb = int function(const(char)* name, void *payload);
123 
124 int git_reference_foreach(
125 	git_repository *repo,
126 	git_reference_foreach_cb callback,
127 	void *payload);
128 int git_reference_foreach_name(
129 	git_repository *repo,
130 	git_reference_foreach_name_cb callback,
131 	void *payload);
132 void git_reference_free(git_reference *ref_);
133 int git_reference_cmp(git_reference *ref1, git_reference *ref2);
134 int git_reference_iterator_new(
135 	git_reference_iterator **out_,
136 	git_repository *repo);
137 int git_reference_iterator_glob_new(
138 	git_reference_iterator **out_,
139 	git_repository *repo,
140 	const(char)* glob);
141 int git_reference_next(git_reference **out_, git_reference_iterator *iter);
142 int git_reference_next_name(const(char)** out_, git_reference_iterator *iter);
143 void git_reference_iterator_free(git_reference_iterator *iter);
144 int git_reference_foreach_glob(
145 	git_repository *repo,
146 	const(char)* glob,
147 	git_reference_foreach_name_cb callback,
148 	void *payload);
149 int git_reference_has_log(git_reference *ref_);
150 int git_reference_is_branch(git_reference *ref_);
151 int git_reference_is_remote(git_reference *ref_);
152 int git_reference_is_tag(git_reference *ref_);
153 
154 enum git_reference_normalize_t {
155 	GIT_REF_FORMAT_NORMAL = 0u,
156 	GIT_REF_FORMAT_ALLOW_ONELEVEL = (1u << 0),
157 	GIT_REF_FORMAT_REFSPEC_PATTERN = (1u << 1),
158 	GIT_REF_FORMAT_REFSPEC_SHORTHAND = (1u << 2),
159 }
160 mixin _ExportEnumMembers!git_reference_normalize_t;
161 
162 int git_reference_normalize_name(
163 	char *buffer_out,
164 	size_t buffer_size,
165 	const(char)* name,
166 	uint flags);
167 int git_reference_peel(
168 	git_object **out_,
169 	git_reference *ref_,
170 	git_otype type);
171 int git_reference_is_valid_name(const(char)* refname);
172 const(char)*  git_reference_shorthand(git_reference *ref_);*/
173 
174 	package @property inout(git_reference)* cHandle() inout { return cast(inout(git_reference)*)_object.cHandle; } 
175 
176 	private GitObject _object;
177 }