2 * This file is part of the vng project
3 * Copyright (C) 2008 Thomas Zander <tzander@trolltech.com>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "CommandLineParser.h"
22 #include "GitRunner.h"
29 static const CommandLineOption options
[] = {
31 // {"--matches=PATTERN", "select patches matching PATTERN"},
32 // {"-p REGEXP, --patches=REGEXP", "select patches matching REGEXP"},
33 // {"-t REGEXP, --tags=REGEXP", "select tags matching REGEXP"},
34 // {"-a, --all", "answer yes to all patches"},
35 // {"-i, --interactive", "prompt user interactively"},
36 // {"-s, --summary", "summarize changes"},
37 // {"--no-summary", "don't summarize changes"},
38 {"--set-default", "set default repository [DEFAULT]"},
39 {"--no-set-default", "don't set default repository"},
44 : AbstractCommand("Push")
46 CommandLineParser::addOptionDefinitions(options
);
49 AbstractCommand::ReturnCodes
Push::run()
51 if (! checkInRepository())
53 CommandLineParser
*args
= CommandLineParser::instance();
56 QStringList arguments
;
57 arguments
<< "config" << "-l";
58 GitRunner
runner(git
, arguments
);
59 AbstractCommand::ReturnCodes rc
= runner
.start(GitRunner::WaitForStdOut
);
61 Logger::error() << "Failed to read configuration\n";
65 QRegExp
re("remote\\.(.*)url=(.*)\n");
66 QHash
<QString
, QString
> repoNames
;
68 while (true) { //read relevant lines into repoNames hash
69 qint64 length
= Vng::readLine(&git
, buf
, sizeof(buf
));
73 if (re
.exactMatch(line
)) {
74 QString context
= re
.cap(1);
75 if (context
.endsWith("."))
76 context
= context
.left(context
.length()-1);
77 repoNames
.insert(context
, re
.cap(2));
82 if (args
->arguments().count() > 1)
83 repo
= args
->arguments()[1];
87 repo
= "default"; // the name we store the default as.
88 if (repoNames
.contains(repo
))
89 url
= repoNames
[repo
];
90 if (url
.isEmpty()) { // print help
91 Logger::standardOut() << "failed: Missing argument [REPOSITORY]\n";
92 Logger::standardOut().flush();
93 args
->usage(name(), argumentDescription());
94 Logger::standardOut() << endl
<< commandDescription();
98 Logger::info() << "Pushing to `" << url
<< "'\n";
99 Logger::info().flush(); // make sure its printed before git asks for an ssh pwd.
103 // TODO when not using --all ask the remote for all the refs it has and detect which ones we still have to push
104 // TODO use interview to ask which refs to push instead of all below
106 arguments
<< "push" << "--all" << repo
;
107 runner
.setArguments(arguments
);
108 rc
= runner
.start(GitRunner::WaitForStdOut
);
110 Logger::error() << "Git push failed\n";
114 /* TODO for some reason qprocess doesn't give me any output if the child process already finished :(
116 qint64 length = Vng::readLine(&git, buf, sizeof(buf));
119 Logger::info() << QString(buf);
122 git
.waitForFinished(-1);
124 if ( !(args
->contains("no-set-default")
125 || m_config
.contains("no-set-default") && ! args
->contains("set-default"))
126 && repo
== url
) { // lets set as default
128 arguments
<< "config" << "--add" << "remote.default.url" << repo
;
129 runner
.setArguments(arguments
);
130 rc
= runner
.start(GitRunner::WaitUntilFinished
);
132 Logger::error() << "Warning; Failed to store the default value\n";
140 QString
Push::argumentDescription() const
142 return "[REPOSITORY]";
145 QString
Push::commandDescription() const
147 return "Push is the opposite of pull. Push allows you to copy changes from the\n"
148 "current repository into another repository.\n";