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() { return GitOid(*git_reference_target(this.cHandle)); }
72 	@property GitOid peeledTarget() { return GitOid(*git_reference_target_peel(this.cHandle)); }
73 	@property string symbolicTarget() { return git_reference_symbolic_target(this.cHandle).to!string; }
74 	@property git_ref_t type() { return git_reference_type(this.cHandle); }
75 	@property string name() { return git_reference_name(this.cHandle).to!string; }
76 	@property GitRepo owner() { return _object.owner; }
77 
78 	GitReference resolve()
79 	{
80 		git_reference* ret;
81 		require(git_reference_resolve(&ret, this.cHandle) == 0);
82 		return GitReference(this.owner, ret);
83 	}
84 
85 	GitReference setSymbolicTarget(string target)
86 	{
87 		git_reference* ret;
88 		require(git_reference_symbolic_set_target(&ret, this.cHandle, target.toStringz) == 0);
89 		return GitReference(this.owner, ret);
90 	}
91 
92 	GitReference setTarget(GitOid oid)
93 	{
94 		git_reference* ret;
95 		require(git_reference_set_target(&ret, this.cHandle, &oid._get_oid()) == 0);
96 		return GitReference(this.owner, ret);
97 	}
98 
99 	GitReference rename(string new_name, bool force)
100 	{
101 		git_reference* ret;
102 		require(git_reference_rename(&ret, this.cHandle, new_name.toStringz, force) == 0);
103 		return GitReference(this.owner, ret);
104 	}
105 
106 	void delete_() { require(git_reference_delete(this.cHandle) == 0); }
107 
108 
109 /*int git_reference_list(git_strarray *array, git_repository *repo);
110 
111 alias git_reference_foreach_cb = int function(git_reference *reference, void *payload);
112 alias git_reference_foreach_name_cb = int function(const(char)* name, void *payload);
113 
114 int git_reference_foreach(
115 	git_repository *repo,
116 	git_reference_foreach_cb callback,
117 	void *payload);
118 int git_reference_foreach_name(
119 	git_repository *repo,
120 	git_reference_foreach_name_cb callback,
121 	void *payload);
122 void git_reference_free(git_reference *ref_);
123 int git_reference_cmp(git_reference *ref1, git_reference *ref2);
124 int git_reference_iterator_new(
125 	git_reference_iterator **out_,
126 	git_repository *repo);
127 int git_reference_iterator_glob_new(
128 	git_reference_iterator **out_,
129 	git_repository *repo,
130 	const(char)* glob);
131 int git_reference_next(git_reference **out_, git_reference_iterator *iter);
132 int git_reference_next_name(const(char)** out_, git_reference_iterator *iter);
133 void git_reference_iterator_free(git_reference_iterator *iter);
134 int git_reference_foreach_glob(
135 	git_repository *repo,
136 	const(char)* glob,
137 	git_reference_foreach_name_cb callback,
138 	void *payload);
139 int git_reference_has_log(git_reference *ref_);
140 int git_reference_is_branch(git_reference *ref_);
141 int git_reference_is_remote(git_reference *ref_);
142 int git_reference_is_tag(git_reference *ref_);
143 
144 enum git_reference_normalize_t {
145 	GIT_REF_FORMAT_NORMAL = 0u,
146 	GIT_REF_FORMAT_ALLOW_ONELEVEL = (1u << 0),
147 	GIT_REF_FORMAT_REFSPEC_PATTERN = (1u << 1),
148 	GIT_REF_FORMAT_REFSPEC_SHORTHAND = (1u << 2),
149 }
150 mixin _ExportEnumMembers!git_reference_normalize_t;
151 
152 int git_reference_normalize_name(
153 	char *buffer_out,
154 	size_t buffer_size,
155 	const(char)* name,
156 	uint flags);
157 int git_reference_peel(
158 	git_object **out_,
159 	git_reference *ref_,
160 	git_otype type);
161 int git_reference_is_valid_name(const(char)* refname);
162 const(char)*  git_reference_shorthand(git_reference *ref_);*/
163 
164 	package @property inout(git_reference)* cHandle() inout { return cast(inout(git_reference)*)_object.cHandle; } 
165 
166 	private GitObject _object;
167 }