% Copyright (C) 2002-2004 David Roundy
%
% 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, or (at your option)
% any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; see the file COPYING. If not, write to
% the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
% Boston, MA 02110-1301, USA.
\darcsCommand{push}
\begin{code}

{-# OPTIONS_GHC -cpp #-}{-# LANGUAGE CPP, TypeOperators #-}moduleDarcs.Commands.Push(push)whereimportSystem.Exit(exitWith,ExitCode(ExitSuccess,ExitFailure))importControl.Monad(when)importData.Char(toUpper)importWorkaround(getCurrentDirectory)importDarcs.Commands(DarcsCommand(..),putVerbose,putInfo,abortRun)importDarcs.Arguments(DarcsFlag(DryRun,Sign,SignAs,NoSign,SignSSL),definePatches,workingRepoDir,summary,printDryRunMessageAndExit,applyas,matchSeveral,fixUrl,depsSel,allInteractive,dryRun,nolinks,remoteRepo,networkOptions,setDefault,sign,allowUnrelatedRepos)importDarcs.Hopefully(PatchInfoAnd,hopefully)importDarcs.Repository(Repository,withRepoReadLock,($-),identifyRepositoryFor,read_repo,amInRepository,checkUnrelatedRepos)importDarcs.Patch(RepoPatch,description)importDarcs.Witnesses.Ordered((:>)(..),(:\/:)(..),RL,FL,nullRL,nullFL,reverseRL,mapFL_FL,mapRL,lengthRL)importDarcs.Repository.Prefs(defaultrepo,setDefaultrepo,getPreflist)importDarcs.External(maybeURLCmd,signString)importDarcs.URL(is_url,is_file)importDarcs.SelectChanges(with_selected_changes)importDarcs.Utils(formatPath)importDarcs.Patch.Depends(get_common_and_uncommon)importDarcs.Patch.Bundle(make_bundle)importDarcs.Patch.Patchy(ShowPatch)importDarcs.Patch.Info(PatchInfo)importDarcs.Patch.Set(PatchSet)importPrinter(Doc,vcat,empty,text,($$))importDarcs.RemoteApply(remote_apply,apply_as)importDarcs.Email(make_email)importEnglish(englishNum,Noun(..))#include "impossible.h"#include "gadts.h"pushDescription::StringpushDescription="Copy and apply patches from this repository to another one."pushHelp::StringpushHelp="Push is the opposite of pull. Push allows you to copy changes from the\n"++"current repository into another repository.\n"push::DarcsCommandpush=DarcsCommand{commandName="push",commandHelp=pushHelp,commandDescription=pushDescription,commandExtraArgs=1,commandExtraArgHelp=["[REPOSITORY]"],commandCommand=pushCmd,commandPrereq=amInRepository,commandGetArgPossibilities=getPreflist"repos",commandArgdefaults=defaultrepo,commandAdvancedOptions=[applyas,nolinks,remoteRepo]++networkOptions,commandBasicOptions=[matchSeveral,depsSel,allInteractive,sign]++dryRun++[summary,workingRepoDir,setDefault,allowUnrelatedRepos]}pushCmd::[DarcsFlag]->[String]->IO()pushCmd_[""]=impossiblepushCmdopts[unfixedrepodir]=dorepodir<-fixUrloptsunfixedrepodir-- Test to make sure we aren't trying to push to the current repohere<-getCurrentDirectorycheckOptionsSanityoptsrepodirwhen(repodir==here)$fail"Cannot push from repository to itself."-- absolute '.' also taken into account by fix_filepath(bundle)<-withRepoReadLockopts$-prepareBundleoptsrepodirsbundle<-signStringoptsbundleletbody=ifis_filerepodirthensbundleelsemake_emailrepodir[]NothingsbundleNothingrval<-remote_applyoptsrepodirbodycaservalofExitFailureec->doputStrLn$"Apply failed!"exitWith(ExitFailureec)ExitSuccess->putInfoopts$text"Push successful."pushCmd__=impossibleprepareBundle::forallpC(rut).(RepoPatchp)=>[DarcsFlag]->String->RepositorypC(rut)->IO(Doc)prepareBundleoptsrepodirrepository=dothem<-identifyRepositoryForrepositoryrepodir>>=read_repoold_default<-getPreflist"defaultrepo"setDefaultreporepodiroptswhen(old_default==[repodir])$letpushing=ifDryRun`elem`optsthen"Would push"else"Pushing"inputInfoopts$text$pushing++" to "++formatPathrepodir++"..."us<-read_reporepositorycaseget_common_and_uncommon(us,them)of(common,us':\/:them')->doprePushChatteroptscommonusus'themthem'with_selected_changes"push"optsNothing(reverseRLus')$bundlePatchesoptscommonprePushChatter::forallpaC(xyt).(ShowPatcha)=>[DarcsFlag]->[PatchInfo]->PatchSetpC(x)->RLaC(tx)->PatchSetpC(y)->RLaC(ty)->IO()prePushChatteroptscommonusus'themthem'=docheckUnrelatedReposoptscommonusthemletnum_to_pull=lengthRLthem'letpull_reminder=ifnum_to_pull>0thentext$"The remote repository has "++shownum_to_pull++" "++englishNumnum_to_pull(Noun"patch")" to pull."elseemptyputVerboseopts$text"We have the following patches to push:"$$(vcat$mapRLdescriptionus')when(not$nullRLus')$doputInfoopts$pull_reminderwhen(nullRLus')$doputInfoopts$text"No recorded local changes to push!"exitWithExitSuccessbundlePatches::foralltpC(xyzw).RepoPatchp=>[DarcsFlag]->[PatchInfo]->(FL(PatchInfoAndp):>t)C(zw)->IO(Doc)bundlePatchesoptscommon(to_be_pushed:>_)=dodefinePatchesto_be_pushedprintDryRunMessageAndExit"push"optsto_be_pushedwhen(nullFLto_be_pushed)$doputInfoopts$text"You don't want to push any patches, and that's fine with me!"exitWithExitSuccessbundle<-make_bundle[](bug"using slurpy in make_bundle called from Push")common(mapFL_FLhopefullyto_be_pushed)return(bundle)wantSign::[DarcsFlag]->BoolwantSignopts=caseoptsof[]->FalseSign:_->True(SignAs_):_->True(SignSSL_):_->TrueNoSign:_->False_:opts'->wantSignopts'checkOptionsSanity::[DarcsFlag]->String->IO()checkOptionsSanityoptsrepodir=ifis_urlrepodirthendowhen(apply_asopts/=Nothing)$abortRunopts$text"Cannot --apply-as when pushing to URLs"maybeapply<-maybeURLCmd"APPLY"repodirwhen(maybeapply==Nothing)$letlprot=takeWhile(/=':')repodirprot=maptoUpperlprotmsg=text("Pushing to "++lprot++" URLs is not supported.\n"++"You may be able to hack this to work"++" using DARCS_APPLY_"++prot)inabortRunoptsmsgelsedowhen(wantSignopts)$abortRunopts$text"Signing doesn't make sense for local repositories or when pushing over ssh."

\end{code}
For obvious reasons, you can only push to repositories to which you have
write access. In addition, you can only push to repos that you access
either on the local file system or with ssh. In order to apply with ssh,
darcs must also be installed on the remote computer. The command invoked
to run ssh may be configured by the \verb!DARCS_SSH! environment variable
(see subsection~\ref{env:DARCS_SSH}). The command invoked via ssh is always
\verb!darcs!, i.e.\ the darcs executable must be in the default path on
the remote machine.
Push works by creating a patch bundle, and then running darcs apply in the
target repository using that patch bundle. This means that the default
options for \emph{apply} in the \emph{target} repository (such as, for
example, \verb!--test!) will affect the behavior of push. This also means
that push is somewhat less efficient than pull.
When you receive an error message such as
\begin{verbatim}
bash: darcs: command not found
\end{verbatim}
then this means that the darcs on the remote machine could
not be started. Make sure that the darcs executable is called
\verb!darcs! and is found in the default path. The default path can
be different in interactive and in non-interactive shells. Say
\begin{verbatim}
ssh login@remote.machine darcs
\end{verbatim}
to try whether the remote darcs can be found, or
\begin{verbatim}
ssh login@remote.machine 'echo $PATH'
\end{verbatim}
(note the single quotes) to check the default path.
\begin{options}
--apply-as
\end{options}
If you give the \verb!--apply-as! flag, darcs will use sudo to apply the
changes as a different user. This can be useful if you want to set up a
system where several users can modify the same repository, but you don't
want to allow them full write access. This isn't secure against skilled
malicious attackers, but at least can protect your repository from clumsy,
inept or lazy users.
\begin{options}
--matches, --patches, --tags, --no-deps
\end{options}
The \verb!--patches!, \verb!--matches!, \verb!--tags!, and \verb!--no-deps!
options can be used to select which patches to push, as described in
subsection~\ref{selecting}.
When there are conflicts, the behavior of push is determined by the default
flags to \verb!apply! in the \emph{target} repository. Most commonly, for
pushed-to repositories, you'd like to have \verb!--dont-allow-conflicts! as
a default option to apply (by default, it is already the default\ldots). If
this is the case, when there are conflicts on push, darcs will fail with an
error message. You can then resolve by pulling the conflicting patch,
recording a resolution and then pushing the resolution together with the
conflicting patch.
Darcs does not have an explicit way to tell you which patch conflicted, only the
file name. You may want to pull all the patches from the remote repository just
to be sure. If you don't want to do this in your working directory,
you can create another darcs working directory for this purpose.
If you want, you could set the target repository to use \verb!--allow-conflicts!.
In this case conflicting patches will be applied, but the conflicts will
not be marked in the working directory.
If, on the other hand, you have \verb!--mark-conflicts! specified as a
default flag for apply in the target repository, when there is a conflict,
it will be marked in the working directory of the target repository. In
this case, you should resolve the conflict in the target repository itself.