File: SvnCommitDialog.cpp

package info (click to toggle)
codelite 17.0.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 136,204 kB
  • sloc: cpp: 491,547; ansic: 280,393; php: 10,259; sh: 8,930; lisp: 7,664; vhdl: 6,518; python: 6,020; lex: 4,920; yacc: 3,123; perl: 2,385; javascript: 1,715; cs: 1,193; xml: 1,110; makefile: 804; cobol: 741; sql: 709; ruby: 620; f90: 566; ada: 534; asm: 464; fortran: 350; objc: 289; tcl: 258; java: 157; erlang: 61; pascal: 51; ml: 49; awk: 44; haskell: 36
file content (338 lines) | stat: -rw-r--r-- 11,783 bytes parent folder | download | duplicates (3)
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright            : (C) 2014 Eran Ifrah
// file name            : SvnCommitDialog.cpp
//
// -------------------------------------------------------------------------
// A
//              _____           _      _     _ _
//             /  __ \         | |    | |   (_) |
//             | /  \/ ___   __| | ___| |    _| |_ ___
//             | |    / _ \ / _  |/ _ \ |   | | __/ _ )
//             | \__/\ (_) | (_| |  __/ |___| | ||  __/
//              \____/\___/ \__,_|\___\_____/_|\__\___|
//
//                                                  F i l e
//
//    This program 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.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

#include "SvnCommitDialog.h"
#include "asyncprocess.h"
#include "bitmap_loader.h"
#include "clSingleChoiceDialog.h"
#include "editor_config.h"
#include "globals.h"
#include "imanager.h"
#include "lexer_configuration.h"
#include "processreaderthread.h"
#include "subversion2.h"
#include "svn_local_properties.h"
#include "svnsettingsdata.h"
#include "windowattrmanager.h"
#include <wx/msgdlg.h>
#include <wx/tokenzr.h>

class CommitMessageStringData : public wxClientData
{
    wxString m_data;

public:
    CommitMessageStringData(const wxString& data)
        : m_data(data.c_str())
    {
    }
    virtual ~CommitMessageStringData() {}

    const wxString& GetData() const { return m_data; }
};

SvnCommitDialog::SvnCommitDialog(wxWindow* parent, Subversion2* plugin)
    : SvnCommitDialogBaseClass(parent)
    , m_plugin(plugin)
    , m_process(NULL)
{
    // Hide the bug tracker ID
    m_textCtrlBugID->Clear();
    m_textCtrlBugID->Hide();
    m_staticTextBugID->Hide();

    m_textCtrlFrID->Clear();
    m_staticText32->Hide();
    m_textCtrlFrID->Hide();

    m_checkListFiles->Disable();
    m_panel1->Disable();
    DoCommonInit();
}

SvnCommitDialog::SvnCommitDialog(wxWindow* parent, const wxArrayString& paths, const wxString& url, Subversion2* plugin,
                                 const wxString& repoPath)
    : SvnCommitDialogBaseClass(parent)
    , m_plugin(plugin)
    , m_url(url)
    , m_repoPath(repoPath)
    , m_process(NULL)
{
    wxString title = GetTitle();
    if(!url.empty()) {
        title << wxT(" - ") << url;
    }
    SetTitle(title);
    DoCommonInit();

    if(!paths.empty()) {
        for(size_t i = 0; i < paths.GetCount(); ++i) {
            int index = m_checkListFiles->Append(paths.Item(i));
            m_checkListFiles->Check((unsigned int)index);
        }

        m_checkListFiles->Select(0);
        DoShowDiff(0);
    }
}

void SvnCommitDialog::DoCommonInit()
{
    Bind(wxEVT_ASYNC_PROCESS_OUTPUT, &SvnCommitDialog::OnProcessOutput, this);
    Bind(wxEVT_ASYNC_PROCESS_TERMINATED, &SvnCommitDialog::OnProcessTerminatd, this);
    m_stcDiff->SetReadOnly(true);
    m_checkListFiles->Clear();

    // These two classes will allow copy / paste etc using the keyboard on the STC classes
    m_stcMessageHelper.Reset(new clEditEventsHandler(m_stcMessage));
    m_stcDiffHelper.Reset(new clEditEventsHandler(m_stcDiff));
    DoCreateToolbar();
    int sashPos = m_plugin->GetSettings().GetCommitDlgSashPos();
    if(sashPos != wxNOT_FOUND) {
        m_splitterH->SetSashPosition(sashPos);
    }

    int sashHPos = m_plugin->GetSettings().GetCommitDlgHSashPos();
    if(sashHPos != wxNOT_FOUND) {
        m_splitterV->SetSashPosition(sashHPos);
    }

    LexerConf::Ptr_t diffLexer = EditorConfigST::Get()->GetLexer("Diff");
    if(diffLexer) {
        m_stcDiff->SetLexer(wxSTC_LEX_DIFF);
        diffLexer->Apply(m_stcDiff);
    }

    LexerConf::Ptr_t textLexer = EditorConfigST::Get()->GetLexer("text");
    if(textLexer) {
        textLexer->Apply(m_stcMessage);
    }
    ::clSetTLWindowBestSizeAndPosition(this);
}

SvnCommitDialog::~SvnCommitDialog()
{
    wxDELETE(m_process);

    wxString message = m_stcMessage->GetText();
    m_plugin->GetCommitMessagesCache().AddMessage(message);

    int sashPos = m_splitterH->GetSashPosition();
    int sashPosH = m_splitterV->GetSashPosition();
    SvnSettingsData ssd = m_plugin->GetSettings();
    ssd.SetCommitDlgSashPos(sashPos);
    ssd.SetCommitDlgHSashPos(sashPosH);
    m_plugin->SetSettings(ssd);
}

wxString SvnCommitDialog::GetMesasge()
{
    SubversionLocalProperties props(m_url);
    wxString msg = NormalizeMessage(m_stcMessage->GetText());
    msg << wxT("\n");

    // Append any bug URLs to the commit message
    if(m_textCtrlBugID->IsShown()) {
        wxString bugTrackerMsg = props.ReadProperty(SubversionLocalProperties::BUG_TRACKER_MESSAGE);
        wxString bugTrackerUrl = props.ReadProperty(SubversionLocalProperties::BUG_TRACKER_URL);
        wxString bugId = m_textCtrlBugID->GetValue();

        bugId.Trim().Trim(false);
        if(bugId.IsEmpty() == false) {
            // Loop over the bug IDs and append message for each bug
            wxArrayString bugs = wxStringTokenize(bugId, wxT(","), wxTOKEN_STRTOK);
            for(size_t i = 0; i < bugs.size(); i++) {

                bugs[i].Trim().Trim(false);
                if(bugs[i].IsEmpty())
                    continue;

                wxString tmpMsg = bugTrackerMsg;
                wxString tmpUrl = bugTrackerUrl;

                tmpUrl.Replace(wxT("$(BUGID)"), bugs[i]);
                tmpMsg.Replace(wxT("$(BUG_URL)"), tmpUrl);
                tmpMsg.Replace(wxT("$(BUGID)"), bugs[i]);
                msg << tmpMsg << wxT("\n");
            }
        }
    }

    // Append any FR URLs to the commit message
    if(m_textCtrlFrID->IsShown()) {
        wxString frTrackerMsg = props.ReadProperty(SubversionLocalProperties::FR_TRACKER_MESSAGE);
        wxString frTrackerUrl = props.ReadProperty(SubversionLocalProperties::FR_TRACKER_URL);
        wxString frId = m_textCtrlFrID->GetValue();

        frId.Trim().Trim(false);
        if(frId.IsEmpty() == false) {
            // Loop over the bug IDs and append message for each bug
            wxArrayString frs = wxStringTokenize(frId, wxT(","), wxTOKEN_STRTOK);
            for(size_t i = 0; i < frs.size(); i++) {

                frs[i].Trim().Trim(false);
                if(frs[i].IsEmpty())
                    continue;

                wxString tmpMsg = frTrackerMsg;
                wxString tmpUrl = frTrackerUrl;

                tmpUrl.Replace(wxT("$(FRID)"), frs[i]);
                tmpMsg.Replace(wxT("$(FR_URL)"), tmpUrl);
                tmpMsg.Replace(wxT("$(FRID)"), frs[i]);
                msg << tmpMsg << wxT("\n");
            }
        }
    }

    msg.Trim().Trim(false);
    return msg;
}

wxString SvnCommitDialog::NormalizeMessage(const wxString& message)
{
    wxString normalizedStr = message;
    normalizedStr.Trim().Trim(false);

    // SVN does not like any quotation marks in the comment -> escape them
    normalizedStr.Replace(wxT("\""), wxT("\\\""));
    return normalizedStr;
}

wxArrayString SvnCommitDialog::GetPaths()
{
    wxArrayString paths;
    for(size_t i = 0; i < m_checkListFiles->GetCount(); i++) {
        if(m_checkListFiles->IsChecked(i)) {
            paths.Add(m_checkListFiles->GetString(i));
        }
    }
    return paths;
}

void SvnCommitDialog::OnFileSelected(wxCommandEvent& event) { DoShowDiff(event.GetSelection()); }

void SvnCommitDialog::OnProcessOutput(clProcessEvent& e) { m_output << e.GetOutput(); }

void SvnCommitDialog::OnProcessTerminatd(clProcessEvent& e)
{
    m_cache.insert(std::make_pair(m_currentFile, m_output));
    m_stcDiff->SetReadOnly(false);
    m_stcDiff->SetText(m_output);
    m_stcDiff->SetReadOnly(true);

    // m_checkListFiles->Enable(true);
    m_currentFile.Clear();
    wxDELETE(m_process);
}

void SvnCommitDialog::DoShowDiff(int selection)
{
    if(m_repoPath.IsEmpty())
        return;

    wxString filename = m_checkListFiles->GetString(selection);

    if(filename.Contains(" ")) {
        filename.Prepend("\"").Append("\"");
    }

    if(m_cache.count(filename)) {
        m_stcDiff->SetReadOnly(false);
        m_stcDiff->SetText(m_cache[filename]);
        m_stcDiff->SetReadOnly(true);
        return;
    }

    // m_checkListFiles->Enable(false); // disable user interaction with this control until the diff process will
    // terminate
    wxString cmd;
    cmd << m_plugin->GetSvnExeNameNoConfigDir() << " diff " << filename;
    clDEBUG() << "Creating diff:" << cmd << clEndl;
    m_currentFile = filename;
    m_output.Clear();
    m_process = ::CreateAsyncProcess(this, cmd, IProcessCreateDefault, m_repoPath);
}

void SvnCommitDialog::OnShowCommitHistory(wxCommandEvent& event)
{
    wxArrayString lastMessages, previews;
    m_plugin->GetCommitMessagesCache().GetMessages(lastMessages, previews);
    clSingleChoiceDialog dlg(this, lastMessages);
    dlg.SetLabel(_("Choose a commit"));
    if(dlg.ShowModal() != wxID_OK)
        return;
    m_stcMessage->SetText(dlg.GetSelection());
}

void SvnCommitDialog::OnShowCommitHistoryUI(wxUpdateUIEvent& event)
{
    wxArrayString lastMessages, previews;
    m_plugin->GetCommitMessagesCache().GetMessages(lastMessages, previews);
    event.Enable(!lastMessages.IsEmpty());
}
void SvnCommitDialog::OnClearHistory(wxCommandEvent& event)
{
    if(::wxMessageBox(_("This will clear the message history\nContinue?"), "CodeLite",
                      wxICON_WARNING | wxCENTER | wxYES_NO | wxCANCEL | wxCANCEL_DEFAULT, this) == wxYES) {
        m_plugin->GetCommitMessagesCache().Clear();
    }
}

void SvnCommitDialog::OnClearHistoryUI(wxUpdateUIEvent& event)
{
    event.Enable(!m_plugin->GetCommitMessagesCache().IsEmpty());
}

void SvnCommitDialog::DoCreateToolbar()
{
    auto images = m_toolbar->GetBitmapsCreateIfNeeded();
    m_toolbar->AddTool(XRCID("commit-history"), _("Commit History"), images->Add("history"), "", wxITEM_DROPDOWN);
    m_toolbar->AddTool(wxID_CLEAR, _("Clear History"), images->Add("clear"));
    m_toolbar->Realize();

    m_toolbar->Bind(wxEVT_TOOL, &SvnCommitDialog::OnShowCommitHistory, this, XRCID("commit-history"));
    m_toolbar->Bind(wxEVT_TOOL_DROPDOWN, &SvnCommitDialog::OnShowCommitHistoryDropDown, this, XRCID("commit-history"));
    m_toolbar->Bind(wxEVT_UPDATE_UI, &SvnCommitDialog::OnShowCommitHistoryUI, this, XRCID("commit-history"));
    m_toolbar->Bind(wxEVT_TOOL, &SvnCommitDialog::OnClearHistory, this, wxID_CLEAR);
    m_toolbar->Bind(wxEVT_UPDATE_UI, &SvnCommitDialog::OnClearHistoryUI, this, wxID_CLEAR);
}

void SvnCommitDialog::OnShowCommitHistoryDropDown(wxCommandEvent& event)
{
    wxMenu menu;
    menu.Append(XRCID("commit-history-last-message"), _("Insert Last Message"));
    menu.Bind(
        wxEVT_MENU,
        [&](wxCommandEvent& event) {
            wxArrayString lastMessages, previews;
            m_plugin->GetCommitMessagesCache().GetMessages(lastMessages, previews);
            if(!lastMessages.empty()) {
                m_stcMessage->SetText(lastMessages.Item(0));
            }
        },
        XRCID("commit-history-last-message"));
    m_toolbar->ShowMenuForButton(event.GetId(), &menu);
}