fix timezones in darcs-fast-export, take 2
[girocco-darcs-fast-export.git] / git-darcs
blobeb7033880877284acd5c8130c8ffabd43ead8210
1 #!/bin/bash
3 # git-darcs - bidirectional operation between a darcs repo and git
5 # Copyright (c) 2008 by Miklos Vajna <vmiklos@frugalware.org>
7 # Based on git-bzr, which is
9 # Copyright (c) 2008 Pieter de Bie <pdebie@ai.rug.nl>
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
24 # USA.
27 add()
29 name="$1"
30 shift
31 location="$1"
32 shift
33 if ! [ -n "$name" -a -n "$location" ]; then
34 echo "Usage: git darcs add name location [darcs-fast-export options]"
35 exit
37 if git remote show |grep -q $name; then
38 echo "There is already a remote with that name"
39 exit
41 if [ -n "$(git config git-darcs.$name.location)" ]; then
42 echo "There is already a darcs repo with that name"
43 exit
45 if [ ! -d $location/_darcs ]; then
46 echo "Remote is not a darcs repository"
47 exit
49 git config git-darcs.$name.location $location
50 git config git-darcs.$name.darcs-fast-export-options "$*"
51 echo "Darcs repo $name added. You can fetch it with 'git darcs fetch $name'"
52 if ! [ -z "$*" ]; then
53 echo "darcs-fast-export will get options: $*"
57 get_location()
59 l=$(git config git-darcs.$remote.location)
60 if [ -z "$l" ]; then
61 echo "Cannot find darcs remote with name '$remote'." >&2
62 exit
64 echo $l
67 fetch()
69 remote="$1"
70 shift
71 if ! [ -n "$remote" -a -z "$*" ]; then
72 echo "Usage: git darcs fetch reponame"
73 exit
75 location=$(get_location $remote)
76 git_map=$git_dir/darcs-git/$remote-git-map
77 darcs_map=$git_dir/darcs-git/$remote-darcs-map
78 common_opts="--working $git_dir/darcs-git/repo --logfile $git_dir/darcs-git/fetch.log --git-branch=darcs/$remote"
79 dfe_opts=$(git config git-darcs.$remote.darcs-fast-export-options)
80 if [ ! -f $git_map -a ! -f $darcs_map ]; then
81 echo "There doesn't seem to be an existing refmap."
82 echo "Doing an initial import"
83 mkdir -p $git_dir/darcs-git
84 darcs-fast-export --export-marks=$darcs_map $common_opts $dfe_opts $location | \
85 git fast-import --export-marks=$git_map
86 elif [ -f $git_map -a -f $darcs_map ]; then
87 echo "Updating remote $remote"
88 old_rev=$(git rev-parse darcs/$remote)
89 darcs-fast-export --import-marks=$darcs_map --export-marks=$darcs_map $common_opts $dfe_opts $location | \
90 git fast-import --quiet --import-marks=$git_map --export-marks=$git_map
91 new_rev=$(git rev-parse darcs/$remote)
92 if [ "$old_rev" != "$new_rev" ]; then
93 echo "Fetched the following updates:"
94 git shortlog $old_rev..$new_rev
95 else
96 echo "Nothing fetched."
97 exit
99 else
100 echo "One of the mapfiles is missing! Something went wrong!"
101 exit
105 pull()
107 remote="$1"
108 shift
109 if ! [ -n "$remote" -a -z "$*" ]; then
110 echo "Usage: git darcs pull reponame"
111 exit
113 fetch $remote
114 # see if we need to merge or rebase
115 branch=$(git symbolic-ref HEAD|sed 's|.*/||')
116 if [ "$(git config branch.$branch.rebase)" = "true" ]; then
117 git rebase darcs/$remote
118 else
119 git merge darcs/$remote
123 push()
125 remote="$1"
126 shift
127 if ! [ -n "$remote" -a -z "$*" ]; then
128 echo "Usage: git darcs push reponame"
129 exit
131 location=$(get_location $remote)
132 if [ -n "$(git rev-list --left-right HEAD...darcs/$remote | sed -n '/^>/ p')" ]; then
133 echo "HEAD is not a strict child of $remote, cannot push. Merge first"
134 exit
136 if [ -z "$(git rev-list --left-right HEAD...darcs/$remote | sed -n '/^</ p')" ]; then
137 echo "Nothing to push. Commit something first"
138 exit
140 git_map=$git_dir/darcs-git/$remote-git-map
141 darcs_map=$git_dir/darcs-git/$remote-darcs-map
142 if [ ! -f $git_map -o ! -f $darcs_map ]; then
143 echo "We do not have refmapping yet. Then how can I push?"
144 exit
146 echo "Pushing the following updates:"
147 git shortlog darcs/$remote..
148 git fast-export --import-marks=$git_map --export-marks=$git_map HEAD | \
149 (cd $location; darcs-fast-import --import-marks=$darcs_map --export-marks=$darcs_map \
150 --logfile $git_dir/darcs-git/push.log)
151 if [ $? == 0 ]; then
152 git update-ref darcs/$remote HEAD
156 # List the darcs remotes
157 list()
159 if [ -z "$*" ]
160 then
161 git config -l | sed -n -e '/git-darcs\..*/ {s/git-darcs\.//; s/\.location=.*//p}'
162 exit
163 elif [ "$#" -eq 1 ]
164 then
165 case $1 in
166 -v|--verbose)
167 git config -l | sed -n -e '/git-darcs\..*/ {s/git-darcs\.//; s/\.location=/\t/p}'
168 exit
170 esac
172 echo "Usage: git darcs list [-v|--verbose]"
173 exit 1
176 # Find the darcs commit(s) supporting a git SHA1 prefix
177 find_darcs()
179 sha1="$1"
180 shift
181 if [ -z "$sha1" -o -n "$*" ]
182 then
183 echo "Usage: git darcs find-darcs <sha1-prefix>"
184 exit 1
186 for remote in $git_dir/darcs/*
188 remote=`basename $remote`
189 git_map=$git_dir/darcs-git/$remote-git-map
190 darcs_map=$git_dir/darcs-git/$remote-darcs-map
191 if [ ! -f $git_map -o ! -f $darcs_map ]
192 then
193 echo "Missing mappings for remote $remote"
194 exit 1
196 for row in `sed -n -e "/:.* $sha1.*/ s/[^ ]*/&/p" $git_map`
198 sed -n -e "/$row / {s/[^ ]*//; s/.*/$remote\t&/p}" $darcs_map
199 done
200 done
203 # Find the git commit(s) supporting a darcs patch prefix
204 find_git()
206 patch="$1"
207 shift
208 if [ -z "$patch" -o -n "$*" ]
209 then
210 echo "Usage: git darcs find-git <patch-prefix>"
211 exit 1
213 for remote in $git_dir/darcs/*
215 remote=`basename $remote`
216 git_map=$git_dir/darcs-git/$remote-git-map
217 darcs_map=$git_dir/darcs-git/$remote-darcs-map
218 if [ ! -f $git_map -o ! -f $darcs_map ]
219 then
220 echo "Missing mappings for remote $remote"
221 exit 1
223 for row in `sed -n -e "/:.* $patch.*/ s/[^ ]*/&/p" $darcs_map`
225 sed -n -e "/$row / {s/[^ ]* \(.*\)/$remote\t\1/p}" $git_map
226 done
227 done
230 git rev-parse 2> /dev/null
231 if [ $? != 0 ]; then
232 echo "Must be inside a git repository to work"
233 exit
236 git_dir=$(git rev-parse --git-dir)
237 # make it absolute
238 cd $git_dir
239 git_dir=$(pwd)
240 cd - >/dev/null
241 command="$1"
242 shift
244 case $command in
245 add|push|fetch|pull|list)
247 find-darcs)
248 command=find_darcs
250 find-git)
251 command=find_git
254 echo "Usage: git darcs [COMMAND] [OPTIONS]"
255 echo "Commands: add, push, fetch, pull, list, find-darcs, find-git"
256 exit
258 esac
261 up=$(git rev-parse --show-cdup)
262 [ -z "$up" ] && up="."
263 cd $up
264 $command "$@"