1 /*
2  * ggit-revert-options.c
3  * This file is part of libgit2-glib
4  *
5  * Copyright (C) 2013 - Ignacio Casal Quinteiro
6  *
7  * libgit2-glib is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * libgit2-glib is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with libgit2-glib. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "ggit-revert-options.h"
22 #include "ggit-merge-options.h"
23 #include "ggit-checkout-options.h"
24 
25 struct _GgitRevertOptions
26 {
27 	git_revert_options revert_options;
28 
29 	GgitMergeOptions *merge_options;
30 	GgitCheckoutOptions *checkout_options;
31 };
32 
G_DEFINE_BOXED_TYPE(GgitRevertOptions,ggit_revert_options,ggit_revert_options_copy,ggit_revert_options_free)33 G_DEFINE_BOXED_TYPE (GgitRevertOptions, ggit_revert_options,
34                      ggit_revert_options_copy, ggit_revert_options_free)
35 
36 const git_revert_options *
37 _ggit_revert_options_get_revert_options (GgitRevertOptions *revert_options)
38 {
39 	/* NULL is common for revert_options as it specifies to use the default
40 	 * so handle a NULL revert_options here instead of in every caller.
41 	 */
42 	if (revert_options == NULL)
43 	{
44 		return NULL;
45 	}
46 
47 	return (const git_revert_options *)&revert_options->revert_options;
48 }
49 
50 /**
51  * ggit_revert_options_copy:
52  * @revert_options: a #GgitRevertOptions.
53  *
54  * Copies @revert_options into a newly allocated #GgitRevertOptions.
55  *
56  * Returns: (transfer full) (nullable): a newly allocated #GgitRevertOptions or %NULL.
57  */
58 GgitRevertOptions *
ggit_revert_options_copy(GgitRevertOptions * revert_options)59 ggit_revert_options_copy (GgitRevertOptions *revert_options)
60 {
61 	g_return_val_if_fail (revert_options != NULL, NULL);
62 
63 	return ggit_revert_options_new (revert_options->revert_options.mainline,
64 	                                revert_options->merge_options,
65 	                                revert_options->checkout_options);
66 }
67 
68 /**
69  * ggit_revert_options_free:
70  * @revert_options: a #GgitRevertOptions.
71  *
72  * Frees @revert_options.
73  */
74 void
ggit_revert_options_free(GgitRevertOptions * revert_options)75 ggit_revert_options_free (GgitRevertOptions *revert_options)
76 {
77 	g_return_if_fail (revert_options != NULL);
78 
79 	if (revert_options->merge_options)
80 	{
81 		ggit_merge_options_free (revert_options->merge_options);
82 	}
83 
84 	g_clear_object (&revert_options->checkout_options);
85 
86 	g_slice_free (GgitRevertOptions, revert_options);
87 }
88 
89 /**
90  * ggit_revert_options_new:
91  * @mainline: the mainline.
92  * @merge_options: (allow-none): a #GgitMergeOptions.
93  * @checkout_options: (allow-none): a #GgitCheckoutOptions.
94  *
95  * Create a new #GgitRevertOptions. Note that the passed in @merge_options and
96  * @checkout_options are copied by this function, and alterations in either
97  * after this call are therefore not reflected in the revert options.
98  *
99  * The @mainline indicates which parent to use for the revert when reverting
100  * a merge commit.
101  *
102  * Returns: (transfer full) (nullable): a #GgitRevertOptions or %NULL.
103  *
104  **/
105 GgitRevertOptions *
ggit_revert_options_new(guint mainline,GgitMergeOptions * merge_options,GgitCheckoutOptions * checkout_options)106 ggit_revert_options_new (guint                mainline,
107                          GgitMergeOptions    *merge_options,
108                          GgitCheckoutOptions *checkout_options)
109 {
110 	GgitRevertOptions *ret;
111 
112 	g_return_val_if_fail (checkout_options == NULL || GGIT_IS_CHECKOUT_OPTIONS (checkout_options), NULL);
113 
114 	ret = g_slice_new0 (GgitRevertOptions);
115 
116 	git_revert_init_options (&ret->revert_options, GIT_REVERT_OPTIONS_VERSION);
117 
118 	if (merge_options)
119 	{
120 		ret->merge_options = ggit_merge_options_copy (merge_options);
121 		ret->revert_options.merge_opts =
122 			*_ggit_merge_options_get_merge_options (ret->merge_options);
123 
124 	}
125 
126 	if (checkout_options)
127 	{
128 		ret->checkout_options = g_object_ref (checkout_options);
129 		ret->revert_options.checkout_opts =
130 			*_ggit_checkout_options_get_checkout_options (ret->checkout_options);
131 	}
132 
133 	ret->revert_options.mainline = mainline;
134 	return ret;
135 }
136 
137 /* ex:set ts=8 noet: */
138