Implement bisecting
[anjuta-git-plugin.git] / plugins / git / git-log-message-command.c
blobb0e86119d0aa7c43151b55a0dbe921b3b6f306da
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * git-command-test
4 * Copyright (C) James Liggett 2008 <jrliggett@cox.net>
5 *
6 * git-command-test 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 * git-command-test 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 git-command-test. If not, write to:
20 * The Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02110-1301, USA.
25 #define COMMITTER_REGEX "^committer"
26 #define COMMIT_REGEX "^commit"
28 #include "git-log-message-command.h"
30 struct _GitLogMessageCommandPriv
32 gchar *sha;
33 GRegex *committer_regex;
34 GRegex *commit_regex;
35 GString *log_message;
36 gboolean found_committer_line;
37 gboolean found_message;
40 G_DEFINE_TYPE (GitLogMessageCommand, git_log_message_command, GIT_TYPE_COMMAND);
42 static void
43 git_log_message_command_init (GitLogMessageCommand *self)
45 self->priv = g_new0 (GitLogMessageCommandPriv, 1);
47 self->priv->committer_regex = g_regex_new (COMMITTER_REGEX, 0, 0, NULL);
48 self->priv->commit_regex = g_regex_new (COMMIT_REGEX, 0, 0, NULL);
49 self->priv->log_message = g_string_new ("");
52 static void
53 git_log_message_command_finalize (GObject *object)
55 GitLogMessageCommand *self;
57 self = GIT_LOG_MESSAGE_COMMAND (object);
59 g_regex_unref (self->priv->committer_regex);
60 g_regex_unref (self->priv->commit_regex);
61 g_string_free (self->priv->log_message, TRUE);
63 G_OBJECT_CLASS (git_log_message_command_parent_class)->finalize (object);
66 static guint
67 git_log_message_command_run (AnjutaCommand *command)
69 GitLogMessageCommand *self;
70 gchar *revision;
72 self = GIT_LOG_MESSAGE_COMMAND (command);
74 revision = g_strdup_printf ("%s^..%s", self->priv->sha, self->priv->sha);
76 git_command_add_arg (GIT_COMMAND (command), "rev-list");
77 git_command_add_arg (GIT_COMMAND (command), "--pretty=raw");
78 git_command_add_arg (GIT_COMMAND (command), revision);
80 g_free (revision);
82 return 0;
85 static void
86 git_log_message_command_handle_output (GitCommand *git_command,
87 const gchar *output)
89 GitLogMessageCommand *self;
91 self = GIT_LOG_MESSAGE_COMMAND (git_command);
93 /* It is possible that we could encounter multiple objects, usually with
94 * merges. */
95 if (g_regex_match (self->priv->commit_regex, output, 0, NULL))
97 self->priv->found_message = FALSE;
98 self->priv->found_committer_line = FALSE;
101 if (self->priv->found_message)
102 g_string_append (self->priv->log_message, output);
104 if (self->priv->found_committer_line)
105 self->priv->found_message = TRUE;
106 else if (g_regex_match (self->priv->committer_regex, output, 0, NULL))
107 self->priv->found_committer_line = TRUE; /* Skip next line */
110 static void
111 git_log_message_command_class_init (GitLogMessageCommandClass *klass)
113 GObjectClass* object_class = G_OBJECT_CLASS (klass);
114 GitCommandClass* parent_class = GIT_COMMAND_CLASS (klass);
115 AnjutaCommandClass *command_class = ANJUTA_COMMAND_CLASS (klass);
117 object_class->finalize = git_log_message_command_finalize;
118 parent_class->output_handler = git_log_message_command_handle_output;
119 command_class->run = git_log_message_command_run;
123 GitLogMessageCommand *
124 git_log_message_command_new (const gchar *working_directory, const gchar *sha)
126 GitLogMessageCommand *self;
128 self = g_object_new (GIT_TYPE_LOG_MESSAGE_COMMAND,
129 "working-directory", working_directory,
130 "single-line-output", TRUE,
131 NULL);
133 self->priv->sha = g_strdup (sha);
135 return self;
138 gchar *
139 git_log_message_command_get_message (GitLogMessageCommand *self)
141 return g_strdup (g_strchomp (self->priv->log_message->str));