File: gitg-ref.vala

package info (click to toggle)
gitg 41-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 18,876 kB
  • sloc: ansic: 1,636; ruby: 1,445; sh: 314; python: 261; xml: 57; makefile: 15
file content (317 lines) | stat: -rw-r--r-- 5,885 bytes parent folder | download | duplicates (5)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
 * This file is part of gitg
 *
 * Copyright (C) 2012 - Jesse van den Kieboom
 *
 * gitg is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * gitg 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 gitg. If not, see <http://www.gnu.org/licenses/>.
 */

namespace Gitg
{

public enum RefType
{
	NONE,
	BRANCH,
	REMOTE,
	TAG,
	STASH
}

public enum RefState
{
	NONE,
	SELECTED,
	PRELIGHT
}

/**
 * Parse ref name into components.
 *
 * This class parses a refname and splits it into several components.
 *
 */
public class ParsedRefName : Object
{
	private string d_shortname;
	private string d_name;
	private string d_remote_name;
	private string d_remote_branch;
	private string? d_prefix;

	/**
	 * The type of ref.
	 */
	public RefType rtype { get; private set; }

	/**
	 * The full name of the ref.
	 */
	public string name
	{
		owned get { return d_name; }
	}

	/**
	 * The short name of the ref. This represents the name of the ref
	 * without the information of the type of ref.
	 */
	public string shortname
	{
		owned get { return d_shortname; }
	}

	/**
	 * The remote name of the ref (only for remote refs)
	 */
	public string? remote_name
	{
		owned get { return d_remote_name; }
	}

	/**
	 * The remote branch name of the ref (only for remote refs)
	 */
	public string? remote_branch
	{
		owned get { return d_remote_branch; }
	}

	public ParsedRefName(string name)
	{
		parse_name(name);
	}

	public string? prefix
	{
		get { return d_prefix; }
	}

	private void parse_name(string name)
	{
		d_name = name;

		string[] prefixes = {
			"refs/heads/",
			"refs/remotes/",
			"refs/tags/",
			"refs/stash"
		};

		d_shortname = name;
		d_prefix = null;

		if (d_name == "HEAD")
		{
			rtype = RefType.BRANCH;
		}

		for (var i = 0; i < prefixes.length; ++i)
		{
			if (!d_name.has_prefix(prefixes[i]))
			{
				continue;
			}

			d_prefix = prefixes[i];

			rtype = (RefType)(i + 1);

			if (rtype == RefType.STASH)
			{
				d_prefix = "refs/";
				d_shortname = "stash";
			}
			else
			{
				d_shortname = d_name[d_prefix.length:d_name.length];
			}

			if (rtype == RefType.REMOTE)
			{
				var pos = d_shortname.index_of_char('/');

				if (pos != -1)
				{
					d_remote_name = d_shortname.substring(0, pos);
					d_remote_branch = d_shortname.substring(pos + 1);
				}
				else
				{
					d_remote_name = d_shortname;
				}
			}
		}
	}
}

public interface Ref : Ggit.Ref
{
	private static Regex? s_remote_key_regex;

	protected abstract ParsedRefName d_parsed_name { get; set; }
	protected abstract List<Ref>? d_pushes { get; owned set; }

	public abstract RefState state { get; set; }
	public abstract bool working { get; set; }

	public ParsedRefName parsed_name
	{
		owned get
		{
			if (d_parsed_name == null)
			{
				d_parsed_name = new ParsedRefName(get_name());
			}

			return d_parsed_name;
		}
	}

	public abstract new Gitg.Repository get_owner();

	private void add_push_ref(string spec)
	{
		Gitg.Ref rf;

		try
		{
			rf = get_owner().lookup_reference(spec);
		} catch { return; }

		if (d_pushes.find_custom(rf, (a, b) => {
			return a.get_name().ascii_casecmp(b.get_name());
		}) == null)
		{
			d_pushes.append(rf);
		}
	}

	private void add_branch_configured_push(Ggit.Config cfg)
	{
		string remote;
		string merge;

		try
		{
			remote = cfg.get_string(@"branch.$(parsed_name.shortname).remote");
			merge = cfg.get_string(@"branch.$(parsed_name.shortname).merge");
		} catch { return; }

		var nm = new ParsedRefName(merge);

		add_push_ref(@"refs/remotes/$remote/$(nm.shortname)");
	}

	private void add_remote_configured_push(Ggit.Config cfg)
	{
		Regex valregex;

		try
		{
			valregex = new Regex("^%s:(.*)".printf(Regex.escape_string(get_name())));

			if (s_remote_key_regex == null)
			{
				s_remote_key_regex = new Regex("remote\\.(.*)\\.push");
			}

			cfg.match_foreach(s_remote_key_regex, (info, val) => {
				MatchInfo vinfo;

				if (!valregex.match(val, 0, out vinfo))
				{
					return 0;
				}

				var rname = info.fetch(1);
				var pref = vinfo.fetch(1);

				add_push_ref(@"refs/remotes/$rname/$pref");
				return 0;
			});

		} catch { return; }
	}

	private void add_branch_same_name_push(Ggit.Config cfg)
	{
		string remote;

		try
		{
			remote = cfg.get_string(@"branch.$(parsed_name.shortname).remote");
		} catch { return; }

		add_push_ref(@"refs/remotes/$remote/$(parsed_name.shortname)");
	}

	private void compose_pushes()
	{
		d_pushes = new List<Ref>();

		Ggit.Config cfg;

		try
		{
			cfg = get_owner().get_config();
		} catch { return; }

		/* The possible refspecs of a local $ref (branch) are resolved in the
		 * following order (duplicates are removed automatically):
		 *
		 * 1) Branch configured remote and merge (git push):
		 *
		 *    Remote: branch.<name>.remote
		 *    Spec:   branch.<name>.merge
		 *
		 * 2) Remote configured matching push refspec:
		 *    For each remote.<name>.push matching ${ref.name}:<spec>
		 *
		 *    Remote: <name>
		 *    Spec:   <spec>
		 *
		 * 3) Remote branch with the same name
		 *
		 *    Remote: branch.<name>.remote
		 *    Spec:   ${ref.name}
		 */

		// Branch configured remote and merge
		add_branch_configured_push(cfg);

		// Remote configured push spec
		add_remote_configured_push(cfg);

		// Same name push
		add_branch_same_name_push(cfg);
	}

	public List<Ref> pushes
	{
		get
		{
			if (d_pushes == null)
			{
				compose_pushes();
			}

			return d_pushes;
		}
	}
}

}

// ex:set ts=4 noet