debug-manager: use g_spawn_sync() instead of fork() and waitpid()
[anjuta.git] / plugins / git / git-revision.c
blob7eece16cda3ff605bf2c7c9ee7fded165f30ed0c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * anjuta
4 * Copyright (C) James Liggett 2008 <jrliggett@cox.net>
5 *
6 * anjuta is free software.
7 *
8 * You may redistribute it and/or modify it under the terms of the
9 * GNU General Public License, as published by the Free Software
10 * Foundation; either version 2 of the License, or (at your option)
11 * any later version.
13 * anjuta is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 * See the GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with anjuta. If not, write to:
20 * The Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02110-1301, USA.
25 #include "git-revision.h"
27 struct _GitRevisionPriv
29 gchar *sha;
30 gchar *author;
31 gchar *date;
32 gchar *short_log;
33 GList *children;
34 gboolean has_parents;
37 G_DEFINE_TYPE (GitRevision, git_revision, G_TYPE_OBJECT);
39 static void
40 git_revision_init (GitRevision *self)
42 self->priv = g_new0 (GitRevisionPriv, 1);
45 static void
46 git_revision_finalize (GObject *object)
48 GitRevision *self;
50 self = GIT_REVISION (object);
52 g_free (self->priv->sha);
53 g_free (self->priv->author);
54 g_free (self->priv->date);
55 g_free (self->priv->short_log);
57 g_list_free (self->priv->children);
58 g_free (self->priv);
60 G_OBJECT_CLASS (git_revision_parent_class)->finalize (object);
63 static void
64 git_revision_class_init (GitRevisionClass *klass)
66 GObjectClass* object_class = G_OBJECT_CLASS (klass);
68 object_class->finalize = git_revision_finalize;
71 GitRevision *
72 git_revision_new (void)
74 return g_object_new (GIT_TYPE_REVISION, NULL);
77 void
78 git_revision_set_sha (GitRevision *self, const gchar *sha)
80 g_free (self->priv->sha);
81 self->priv->sha = g_strdup (sha);
84 gchar *
85 git_revision_get_sha (GitRevision *self)
87 return g_strdup (self->priv->sha);
90 gchar *
91 git_revision_get_short_sha (GitRevision *self)
93 return g_strndup (self->priv->sha, 7);
96 void
97 git_revision_set_author (GitRevision *self, const gchar *author)
99 g_free (self->priv->author);
100 self->priv->author = g_strdup (author);
103 gchar *
104 git_revision_get_author (GitRevision *self)
106 return g_strdup (self->priv->author);
109 void
110 git_revision_set_short_log (GitRevision *self, const gchar *short_log)
112 g_free (self->priv->short_log);
113 self->priv->short_log = g_strdup (short_log);
115 g_strchug (self->priv->short_log);
118 gchar *
119 git_revision_get_short_log (GitRevision *self)
121 return g_strdup (self->priv->short_log);
124 static const gchar *
125 git_revision_get_time_format (struct tm *revision_time)
127 struct tm *tm;
128 time_t t1, t2;
130 t1 = mktime ((struct tm *) revision_time);
132 /* check whether it's ahead in time */
133 time (&t2);
134 if (t1 > t2)
136 return "%c";
139 /* check whether it's as fresh as today's bread */
140 t2 = time (NULL);
141 tm = localtime (&t2);
142 tm->tm_sec = tm->tm_min = tm->tm_hour = 0;
143 t2 = mktime (tm);
145 if (t1 > t2)
147 /* TRANSLATORS: it's a strftime format string */
148 return "%I:%M %p";
151 /* check whether it's older than a week */
152 t2 = time (NULL);
153 tm = localtime (&t2);
154 tm->tm_sec = tm->tm_min = tm->tm_hour = 0;
155 t2 = mktime (tm);
157 t2 -= 60 * 60 * 24 * 6; /* substract 1 week */
159 if (t1 > t2)
161 /* TRANSLATORS: it's a strftime format string */
162 return "%a %I:%M %p";
165 /* check whether it's more recent than the new year hangover */
166 t2 = time (NULL);
167 tm = localtime (&t2);
168 tm->tm_sec = tm->tm_min = tm->tm_hour = tm->tm_mon = 0;
169 tm->tm_mday = 1;
170 t2 = mktime (tm);
172 if (t1 > t2)
174 /* TRANSLATORS: it's a strftime format string */
175 return "%b %d %I:%M %p";
178 /* it's older */
179 /* TRANSLATORS: it's a strftime format string */
180 return "%b %d %Y";
183 void
184 git_revision_set_date (GitRevision *self, time_t unix_time)
186 struct tm time;
187 const gchar *format;
188 gchar buffer[256];
190 localtime_r (&unix_time, &time);
191 format = git_revision_get_time_format (&time);
192 strftime (buffer, sizeof (buffer), format, &time);
194 g_free (self->priv->date);
195 self->priv->date = g_strdup (buffer);
198 gchar *
199 git_revision_get_formatted_date (GitRevision *self)
201 return g_strdup (self->priv->date);
204 void
205 git_revision_add_child (GitRevision *self, GitRevision *child)
207 self->priv->children = g_list_prepend (self->priv->children,
208 child);
209 git_revision_set_has_parents (child, TRUE);
212 GList *
213 git_revision_get_children (GitRevision *self)
215 return self->priv->children;
218 void
219 git_revision_set_has_parents (GitRevision *self, gboolean has_parents)
221 self->priv->has_parents = has_parents;
224 gboolean
225 git_revision_has_parents (GitRevision *self)
227 return self->priv->has_parents;