]> git.pld-linux.org Git - packages/rpm-build-tools.git/blobdiff - builder.sh
on -j define __jobs instead of _smp_mflags
[packages/rpm-build-tools.git] / builder.sh
old mode 100644 (file)
new mode 100755 (executable)
index 2e004f8..53ef564
@@ -17,6 +17,7 @@
 #        9 - Requested tag already exist
 #       10 - Refused to build fractional release
 #      100 - Unknown error (should not happen)
+#   110 - Functions not yet implemented
 
 # Notes (todo/bugs):
 # - when Icon: field is present, -5 and -a5 doesn't work
 
 PROGRAM=${0##*/}
 APPDIR=$(d=$0; [ -L "$d" ] && d=$(readlink -f "$d"); dirname "$d")
-RCSID='$Id$' r=${RCSID#* * } rev=${r%% *}
-VERSION="v0.35/$rev"
+VERSION="v0.35"
 VERSIONSTRING="\
 Build package utility from PLD Linux Packages repository
-$VERSION (C) 1999-2011 Free Penguins".
+$VERSION (C) 1999-2020 Free Penguins".
 
-PATH="/bin:/usr/bin:/usr/sbin:/sbin:/usr/X11R6/bin"
+# Clean PATH without /usr/local or user paths
+CLEAN_PATH="/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin"
 
 # required rpm-build-macros
 RPM_MACROS_VER=1.534
@@ -54,7 +55,6 @@ QUIET=""
 CLEAN=""
 DEBUG=""
 NOURLS=""
-NOCVS=""
 NOCVSSPEC=""
 NODIST=""
 NOINIT=""
@@ -63,8 +63,6 @@ UPDATE=""
 ADD5=""
 NO5=""
 ALWAYS_CVSUP=${ALWAYS_CVSUP:-"yes"}
-CVSROOT=""
-GREEDSRC=""
 
 # use rpm 4.4.6+ digest format instead of comments if non-zero
 USEDIGEST=
@@ -76,13 +74,21 @@ USER_AGENT="PLD/Builder($VERSION)"
 # See LOGFILE example.
 DATE=`date +%Y-%m-%d_%H-%M-%S`
 
+# target arch, can also be used for log file naming
+TARGET=$(rpm -E %{_target})
+
+# Note the *single* quotes, this allows using shell variables expanded at build time
 # Example: LOGFILE='../log.$PACKAGE_NAME'
 # Example: LOGFILE='../LOGS/log.$PACKAGE_NAME.$DATE'
 # Example: LOGFILE='$PACKAGE_NAME/$PACKAGE_NAME.$DATE.log'
 # Example: LOGFILE='$PACKAGE_NAME.$DATE.log'
-# Yes, you can use variable name! Note _single_ quotes!
+# Example: LOGFILE='.log.$PACKAGE_NAME-$PACKAGE_VERSION-$PACKAGE_RELEASE.$TARGET.$DATE'
 LOGFILE=''
 
+# use teeboth Perl wrapper
+# temporary option to disable if broken
+USE_TEEBOTH=yes
+
 LOGDIR=""
 LOGDIROK=""
 LOGDIRFAIL=""
@@ -106,14 +112,25 @@ PACKAGE_VERSION=""
 PACKAGE_NAME=""
 ASSUMED_NAME=""
 PROTOCOL="http"
+IPOPT=""
+
+# use lftp by default when available
+test -z "${USE_LFTP+x}" && lftp --version > /dev/null 2>&1 && USE_LFTP=yes
+PARALLEL_DOWNLOADS=10
+
 WGET_RETRIES=${MAX_WGET_RETRIES:-0}
 
-CVS_COMMAND=${CVS_COMMAND:-cvs}
 CVS_FORCE=""
 CVSIGNORE_DF="yes"
-CVS_RETRIES=${MAX_CVS_RETRIES:-1000}
-CVS_SERVER="cvs.pld-linux.org"
 CVSTAG=""
+GIT_SERVER="git://git.pld-linux.org"
+GIT_PUSH="git@git.pld-linux.org"
+PACKAGES_DIR="packages"
+HEAD_DETACHED=""
+DEPTH=""
+ALL_BRANCHES=""
+REMOTE_PLD="origin"
+NEW_REPO=""
 
 RES_FILE=""
 
@@ -144,6 +161,7 @@ else
        fi
 fi
 
+UPDATE_POLDEK_INDEXES_OPTS=""
 
 # Here we load saved user environment used to
 # predefine options set above, or passed to builder
@@ -157,13 +175,15 @@ fi
 # Example of ~/.builderrc:
 #
 #UPDATE_POLDEK_INDEXES="yes"
+#UPDATE_POLDEK_INDEXES_OPTS="--mo=nodiff"
 #FETCH_BUILD_REQUIRES="yes"
 #REMOVE_BUILD_REQUIRES="force"
 #GROUP_BCONDS="yes"
 #LOGFILE='../LOGS/log.$PACKAGE_NAME.$DATE'
 #TITLECHANGE=no
-#
-SU_SUDO=""
+
+SU_SUDO="sudo"
+
 if [ -n "$HOME_ETC" ]; then
        USER_CFG="$HOME_ETC/.builderrc"
        BUILDER_MACROS="$HOME_ETC/.builder-rpmmacros"
@@ -183,33 +203,19 @@ if [ "$SCHEDTOOL" = "auto" ]; then
 fi
 
 if [ -n "$USE_PROZILLA" ]; then
-       GETURI="proz --no-getch -r -P ./ -t$WGET_RETRIES $PROZILLA_OPTS"
-       GETURI2="$GETURI"
-       OUTFILEOPT="-O"
+       GETURI=download_proz
 elif [ -n "$USE_AXEL" ]; then
-       GETURI="axel -a $AXEL_OPTS"
-       GETURI2="$GETURI"
-       OUTFILEOPT="-o"
+       GETURI=download_axel
+elif [ -n "$USE_LFTP" ]; then
+       GETURI=download_lftp
 else
-       wget --help 2>&1 | grep -q -- ' --no-check-certificate ' && WGET_OPTS="$WGET_OPTS --no-check-certificate"
-       wget --help 2>&1 | grep -q -- ' --inet ' && WGET_OPTS="$WGET_OPTS --inet"
-       wget --help 2>&1 | grep -q -- ' --retry-connrefused ' && WGET_OPTS="$WGET_OPTS --retry-connrefused"
-       WGET_OPTS="$WGET_OPTS --user-agent=$USER_AGENT"
-
-       GETURI="wget --passive-ftp -c -nd -t$WGET_RETRIES $WGET_OPTS"
-       GETURI2="wget -c -nd -t$WGET_RETRIES $WGET_OPTS"
-       OUTFILEOPT="-O"
+       GETURI=download_wget
 fi
 
-GETLOCAL="cp -a"
+GETLOCAL=${GETLOCAL:-cp -a}
 
-if (rpm --version 2>&1 | grep -q '4.0.[0-2]'); then
-       RPM="rpm"
-       RPMBUILD="rpm"
-else
-       RPM="rpm"
-       RPMBUILD="rpmbuild"
-fi
+RPM="rpm"
+RPMBUILD="rpmbuild"
 
 #
 # sanity checks
@@ -220,27 +226,23 @@ if [ -d $HOME/rpm/SOURCES ]; then
        exit 1
 fi
 
-#
-# are we using cvs-nserver ?
-#
-CVS_NSERVER=0
-$CVS_COMMAND --version 2>&1 | grep -q 'CVS-nserver'
-[ $? -eq 0 ] && CVS_NSERVER=1
-
 POLDEK_INDEX_DIR="$($RPM --eval %_rpmdir)/"
-POLDEK_CMD="$SU_SUDO /usr/bin/poldek --noask"
+POLDEK_CMD="$SU_SUDO /usr/bin/poldek"
 
+# TODO: add teeboth
+# TODO: what this function does?
 run_poldek() {
-       RES_FILE=$(mktemp -t builder.XXXXXX || ${TMPDIR:-/tmp}/builder.$RANDOM)
+       RES_FILE=$(tempfile)
        if [ -n "$LOGFILE" ]; then
                LOG=`eval echo $LOGFILE`
                if [ -n "$LASTLOG_FILE" ]; then
                        echo "LASTLOG=$LOG" > $LASTLOG_FILE
                fi
-               (${NICE_COMMAND} ${POLDEK_CMD} `while test $# -gt 0; do echo "$1 ";shift;done` ; echo $? > ${RES_FILE})|tee -a $LOG
+               (${NICE_COMMAND} ${POLDEK_CMD} --noask `while test $# -gt 0; do echo "$1 ";shift;done` ; echo $? > ${RES_FILE})|tee -a $LOG
+               # FIXME $exit_pldk undefined
                return $exit_pldk
        else
-               (${NICE_COMMAND} ${POLDEK_CMD} `while test $# -gt 0; do echo "$1 ";shift;done` ; echo $? > ${RES_FILE}) 1>&2 >/dev/null
+               (${NICE_COMMAND} ${POLDEK_CMD} --noask `while test $# -gt 0; do echo "$1 ";shift;done` ; echo $? > ${RES_FILE}) 1>&2 >/dev/null
                return `cat ${RES_FILE}`
                rm -rf ${RES_FILE}
        fi
@@ -249,80 +251,154 @@ run_poldek() {
 #---------------------------------------------
 # functions
 
+download_prozilla() {
+       local outfile=$1 url=$2 retval
+
+       proz --no-getch -r -P ./ -t$WGET_RETRIES $PROZILLA_OPTS -O "$outfile" "$url"
+       retval=$?
+
+       return $retval
+}
+
+download_axel() {
+       local outfile=$1 url=$2 retval
+
+       axel -a $AXEL_OPTS -o "$outfile" "$url"
+       retval=$?
+
+       return $retval
+}
+
+download_wget() {
+       local outfile=$1 url=$2 retval wget_help
+       if [ -z "${WGET_OPTS_SET+x}" ]; then
+               wget_help="$(wget --help 2>&1)"
+               echo "$wget_help" | grep -q -- ' --inet ' && WGET_OPTS="$WGET_OPTS --inet"
+               echo "$wget_help" | grep -q -- ' --retry-connrefused ' && WGET_OPTS="$WGET_OPTS --retry-connrefused"
+               echo "$wget_help" | grep -q -- ' --no-iri ' && WGET_OPTS="$WGET_OPTS --no-iri"
+               WGET_OPTS="-c -nd -t$WGET_RETRIES $WGET_OPTS --user-agent=$USER_AGENT $IPOPT --passive-ftp"
+               WGET_OPTS_SET=1
+       fi
+
+       wget $WGET_OPTS -O "$outfile" "$url"
+       retval=$?
+       if [ $retval -ne 0 ]; then
+               if [ "`echo $url | grep -E 'ftp://'`" ]; then
+                       ${GETURI} -O "$outfile" "$url"
+                       retval=$?
+               fi
+       fi
+       return $retval
+}
+
+download_lftp() {
+       local outfile=$1 url=$2 retval tmpfile
+       tmpfile=$(tempfile) || exit 1
+       lftp -c "
+               $([ "$DEBUG" = "yes" ] && echo "debug 5;")
+               $([ "$IPOPT" = "-4" ] && echo "set dns:order \"inet\";")
+               $([ "$IPOPT" = "-6" ] && echo "set dns:order \"inet6\";")
+               set ssl:verify-certificate no;
+               set net:max-retries $WGET_RETRIES;
+               set http:user-agent \"$USER_AGENT\";
+               pget -n $PARALLEL_DOWNLOADS -c \"$url\" -o \"$tmpfile\"
+       "
+
+       retval=$?
+       if [ $retval -eq 0 ]; then
+               mv -f "$tmpfile" "$outfile"
+       else
+               rm -f "$tmpfile"
+       fi
+       return $retval
+}
+
 usage() {
        if [ -n "$DEBUG" ]; then set -xv; fi
+# NOTE:
+# to make this output parseable by bash-completion _parse_help()
+# if the line contains short and long option, it will take only the long option
+# but if you want both being completed, put the short option to separate line
        echo "\
-Usage: builder [-D|--debug] [-V|--version] [--short-version] [--as_anon] [-a|--add_cvs] [-b|-ba|--build]
+Usage: builder [--all-branches] [-D|--debug] [-V|--version] [--short-version]  [-a|--add_cvs] [-b|-ba|--build]
 [-bb|--build-binary] [-bs|--build-source] [-bc] [-bi] [-bl] [-u|--try-upgrade]
-[{-cf|--cvs-force}] [{-B|--branch} <branch>] [{-d|--cvsroot} <cvsroot>]
+[{-cf|--cvs-force}] [{-B|--branch} <branch>] [--depth <number>]
 [-g|--get] [-h|--help] [--ftp] [--http] [{-l|--logtofile} <logfile>] [-m|--mr-proper]
-[-q|--quiet] [--date <yyyy-mm-dd> [-r <cvstag>] [{-T|--tag <cvstag>]
+[-q|--quiet] [--date <yyyy-mm-dd> [-r <tag>] [{-T|--tag <tag>]
 [-Tvs|--tag-version-stable] [-Ts|--tag-stable] [-Tv|--tag-version]
-[{-Tp|--tag-prefix} <prefix>] [{-tt|--test-tag}] [--use-greed-sources]
+[{-Tp|--tag-prefix} <prefix>] [{-tt|--test-tag}]
 [-nu|--no-urls] [-v|--verbose] [--opts <rpm opts>] [--short-circuit]
 [--show-bconds] [--with/--without <feature>] [--define <macro> <value>]
-<package>[.spec][:cvstag]
-
--5, --update-md5    - update md5 comments in spec, implies -nd -ncs
--a5, --add-md5      - add md5 comments to URL sources, implies -nc -nd -ncs
+<package>[.spec][:tag]
+
+-4                  - force IPv4 when transferring files
+-6                  - force IPv6 when transferring files
+-5,
+--update-md5        - update md5 comments in spec, implies -nd -ncs
+-a5,
+--add-md5           - add md5 comments to URL sources, implies -nc -nd -ncs
+--all-branches      - make shallow fetch of all branches; --depth required
 -n5, --no-md5       - ignore md5 comments in spec
 -D, --debug         - enable builder script debugging mode,
 -debug              - produce rpm debug package (same as --opts -debug)
 -V, --version       - output builder version string
 --short-version     - output builder short version
---as_anon           - get files via pserver as cvs@$CVS_SERVER,
--a, --add_cvs       - try add new package to CVS.
--b, -ba, --build    - get all files from CVS repo or HTTP/FTP and build package
+-a                  - try add new package to PLD repo.
+-b,
+-ba
+                    - get all files from PLD repo or HTTP/FTP and build package
                       from <package>.spec,
--bb, --build-binary - get all files from CVS repo or HTTP/FTP and build binary
+-bb                 - get all files from PLD repo or HTTP/FTP and build binary
                       only package from <package>.spec,
--bp, --build-prep   - execute the %prep phase of <package>.spec,
+-bp                 - execute the %prep phase of <package>.spec,
 -bc                 - execute the %build phase of <package>.spec,
 -bi                 - execute the %install phase of <package>.spec
--bl                                    - execute the %files phase of <package>.spec
--bs, --build-source - get all files from CVS repo or HTTP/FTP and only pack
+-bl                 - execute the %files phase of <package>.spec
+-bs                 - get all files from PLD repo or HTTP/FTP and only pack
                       them into src.rpm,
 --short-circuit     - short-circuit build
 -B, --branch        - add branch
--c, --clean         - clean all temporarily created files (in BUILD\$RPM_BUILD_ROOT) after rpmbuild commands.
+-c,
+--clean             - clean all temporarily created files (in BUILD\$RPM_BUILD_ROOT) after rpmbuild commands.
                       may be used with building process.
 -m, --mr-proper     - clean all temporarily created files (in BUILD, SOURCES,
-                                         SPECS and \$RPM_BUILD_ROOT and CVS/Entries). Doesn't run
-                                         any rpm building.
--cf, --cvs-force       - use -F when tagging (useful when moving branches)
--d <cvsroot>, --cvsroot <cvsroot>
-                    - setup \$CVSROOT,
---define <macro> <value>
+                      SPECS and \$RPM_BUILD_ROOT). Doesn't run any rpm building.
+-cf, --cvs-force    - use -f when tagging
+--define '<macro> <value>'
                     - define a macro <macro> with value <value>,
+--depth <number>    - make shallow fetch
 --alt_kernel <kernel>
                     - same as --define 'alt_kernel <kernel>'
 --nodeps            - rpm won't check any dependences
--g, --get           - get <package>.spec and all related files from CVS repo
-                      or HTTP/FTP,
+-g
+--get               - get <package>.spec and all related files from PLD repo
 -h, --help          - this message,
--jN, -j N           - set %_smp_mflags to propagate concurrent jobs
---ftp, --http       - use ftp or http protocol to access distfiles server
--l <logfile>, --logtofile <logfile>
+-j N                - set %_smp_mflags to propagate concurrent jobs
+--ftp               - use FTP protocol to access distfiles server
+--http              - use HTTP protocol to access distfiles server
+-l <logfile>, --logtofile=<logfile>
                     - log all to file,
--nc, --no-cvs       - don't download sources from CVS, if source URL is given,
 -ncs, --no-cvs-specs
-                    - don't check specs in CVS
+                    - don't pull from PLD repo
 -nd, --no-distfiles - don't download from distfiles
 -nm, --no-mirrors   - don't download from mirror, if source URL is given,
 -nu, --no-urls      - don't try to download from FTP/HTTP location,
 -ns, --no-srcs      - don't download Sources/Patches
 -ns0, --no-source0  - don't download Source0
 -nn, --no-net       - don't download anything from the net
--pm, --prefer-mirrors - prefer mirrors (if any) over distfiles for SOURCES
+-p N                - set PARALLEL_DOWNLOADS to N (default $PARALLEL_DOWNLOADS)
+-pm, --prefer-mirrors
+                    - prefer mirrors (if any) over distfiles for SOURCES
 --no-init           - don't initialize builder paths (SPECS and SOURCES)
 -ske,
---skip-existing-files - skip existing files in get_files
+--skip-existing-files
+                    - skip existing files in get_files
 --opts <rpm opts>   - additional options for rpm
 -q, --quiet         - be quiet,
---date yyyy-mm-dd   - build package using resources from specified CVS date,
--r <cvstag>, --cvstag <cvstag>
-                    - build package using resources from specified CVS tag,
--A                  - build package using CVS resources as any sticky tags/date/kopts being reset.
+--date yyyy-mm-dd   - build package using resources from specified date,
+-r <tag>, --cvstag <ref>
+                    - build package using resources from specified branch/tag,
+-A                  - build package using master branch as any sticky tags/branch/date being reset.
 -R, --fetch-build-requires
                     - fetch what is BuildRequired,
 -RB, --remove-build-requires
@@ -331,30 +407,33 @@ Usage: builder [-D|--debug] [-V|--version] [--short-version] [--as_anon] [-a|--a
 -FRB, --force-remove-build-requires
                     - remove all you fetched with -R or --fetch-build-requires
                       remember, this option works without confirmation,
--sd, --source-distfiles - list sources available from distfiles (intended for offline
+-sd, --source-distfiles
+                    - list sources available from distfiles (intended for offline
                       operations; does not work when Icon field is present
                       but icon file is absent),
--sc, --source-cvs - list sources available from CVS
--sdp, --source-distfiles-paths - list sources available from distfiles -
+-sc, --source-cvs   - list sources available from PLD repo
+-sdp, --source-distfiles-paths
+                    - list sources available from distfiles -
                       paths relative to distfiles directory (intended for offline
                       operations; does not work when Icon field is present
                       but icon file is absent),
 -sf, --source-files - list sources - bare filenames (intended for offline
                       operations; does not work when Icon field is present
                       but icon file is absent),
--lsp, --source-paths - list sources - filenames with full local paths (intended for
+-lsp, --source-paths
+                    - list sources - filenames with full local paths (intended for
                       offline operations; does not work when Icon field is present
                       but icon file is absent),
 -su, --source-urls  - list urls - urls to sources and patches
                       intended for copying urls with spec with lots of macros in urls
--T <cvstag> , --tag <cvstag>
-                    - add cvs tag <cvstag> for files,
+-T <tag> , --tag <tag>
+                    - add git tag <tag> for files,
 -Tvs, --tag-version-stable
-                    - add cvs tags STABLE and NAME-VERSION-RELEASE for files,
+                    - add git tags STABLE and NAME-VERSION-RELEASE for files,
 -Ts, --tag-stable
-                    - add cvs tag STABLE for files,
--Tv, --tag-version
-                    - add cvs tag NAME-VERSION-RELEASE for files,
+                    - add git tag STABLE for files,
+-Tv,
+--tag-version       - add git tag NAME-VERSION-RELEASE for files,
 -Tp, --tag-prefix <prefix>
                     - add <prefix> to NAME-VERSION-RELEASE tags,
 -tt, --test-tag <prefix>
@@ -365,47 +444,161 @@ Usage: builder [-D|--debug] [-V|--version] [--short-version] [--as_anon] [-a|--a
 -u, --try-upgrade   - check version, and try to upgrade package
 -un, --try-upgrade-with-float-version
                     - as above, but allow float version
-                                       php-pear-Services_Digg/
+                      php-pear-Services_Digg/
 --upgrade-version   - upgrade to specified version in try-upgrade
---use-greed-sources
-                    - try download source from tag head if don't find it in
-                      current tag
 -U, --update        - refetch sources, don't use distfiles, and update md5 comments
 -Upi, --update-poldek-indexes
                     - refresh or make poldek package index files.
--sp, --skip-patch <patchnumber>
+-sp <patchnumber>,
+--skip-patch <patchnumber>
                     - don't apply <patchnumber>. may be repeated.
--np, --nopatch <patchnumber>
+-np <patchnumber>,
+--nopatch <patchnumber>
                     - abort instead of applying patch <patchnumber>
+--noinit
+                    - do not initialize SPECS_DIR and SOURCES_DIR (set them to .)
 --show-bconds       - show available conditional builds, which can be used
                     - with --with and/or --without switches.
---show-bcond-args   - show active bconds, from ~/.bcondrc. this is used by
-                      ./repackage.sh script. in other words, the output is
-                      parseable by scripts.
+--show-bcond-args   - show active bconds, from ~/.bcondrc. this is used by ./repackage.sh script.
+                      In other words, the output is parseable by scripts.
 --show-avail-bconds - show available bconds
---with/--without <feature>
+--with <feature>,
+--without <feature>
                     - conditional build package depending on %_with_<feature>/
                       %_without_<feature> macro switch.  You may now use
                       --with feat1 feat2 feat3 --without feat4 feat5 --with feat6
                       constructions. Set GROUP_BCONDS to yes to make use of it.
 --target <platform>, --target=<platform>
-                     - build for platform <platform>.
---init-rpm-dir       - initialize ~/rpm directory structure
+                    - build for platform <platform>.
+--init-rpm-dir, --init
+                    - initialize ~/rpm directory structure
 "
 }
 
+is_rpmorg() {
+       local v
+
+       v=$(LC_ALL=C LANG=C rpm --version 2>&1)
+       v=${v#RPM version } # rpm 4
+       v=${v#rpm \(RPM\) } # rpm 5
+
+       case "$v" in
+               4.5|5.*)
+                       return 1
+                       ;;
+               4.*)
+                       return 0;
+                       ;;
+               *)
+                       echo "ERROR: unsupported RPM version $v" >&2
+                       exit 1
+       esac
+}
+
+# create tempfile. as secure as possible
+tempfile() {
+       local prefix=builder.$PACKAGE_NAME${1:+.$1}
+       mktemp --tmpdir -t $prefix.XXXXXX || echo ${TMPDIR:-/tmp}/$prefix.$RANDOM.$$
+}
+
+tempdir() {
+       local prefix=builder.$PACKAGE_NAME${1:+.$1}
+       mktemp --tmpdir -d $prefix.XXXXXX
+}
+
+# inserts git log instead of %changelog
+# @output directory containing modified specfile
+insert_gitlog() {
+       local SPECFILE=$1 specdir=$(tempdir) gitlog=$(tempfile) speclog=$(tempfile)
+
+       # allow this being customized
+       local log_entries=$(rpm -E '%{?_buildchangelogtruncate}')
+
+       # rpm5.org/rpm.org do not parse any other date format than 'Wed Jan 1 1997'
+       # otherwise i'd use --date=iso here
+       # http://rpm5.org/cvs/fileview?f=rpm/build/parseChangelog.c&v=2.44.2.1
+       # http://rpm.org/gitweb?p=rpm.git;a=blob;f=build/parseChangelog.c;h=56ba69daa41d65ec9fd18c9f371b8ff14118cdca;hb=a113baa510a004476edc44b5ebaaf559238a18b6#l33
+       # NOTE: changelog date is always in UTC for rpmbuild
+       # * 1265749244 +0000 Random Hacker <nikt@pld-linux.org> 9370900
+       git rev-list --date-order -${log_entries:-20} HEAD 2>/dev/null | while read sha1; do
+               local logfmt='%B%n'
+               git notes list $sha1 > /dev/null 2>&1 && logfmt='%N'
+               git log -n 1 $sha1 --format=format:"* %ad %an <%ae> %h%n- ${logfmt}%n" --date=raw | sed -re 's/^- +- */- /'| sed '/^$/q'
+       done > $gitlog
+
+       # add link to full git logs
+       local giturl="http://git.pld-linux.org/?p=packages/${SPECFILE%.spec}.git;a=log"
+       if [ -n "$CVSTAG" ]; then
+               giturl="$giturl;h=$CVSTAG"
+       fi
+       local gitauthor="PLD Linux Team <feedback@pld-linux.org>"
+       LC_ALL=C gawk -vgiturl="$giturl" -vgitauthor="$gitauthor" -vpackage=$PACKAGE_NAME 'BEGIN{
+               printf("* %s %s\n- For complete changelog see: %s\n", strftime("%a %b %d %Y"), gitauthor, giturl);
+               print;
+               exit
+       }' > $speclog
+
+       LC_ALL=C gawk '/^\* /{printf("* %s %s\n", strftime("%a %b %d %Y", $2), substr($0, length($1)+length($2)+length($3)+4)); next}{print}' $gitlog >> $speclog
+       sed '/^%changelog/,$d' $SPECFILE | sed -e "\${
+                       a%changelog
+                       r $speclog
+               }
+       " > $specdir/$SPECFILE
+       rm -f $gitlog $speclog
+       echo $specdir
+}
+
+# @param string logfile
+# @param varargs... commands to execute
+teeboth() {
+       local rc
+       # use teeboth from toys/cleanbuild, if available and enabled
+       if [ "$USE_TEEBOTH" = "yes" ] && [ -x $APPDIR/teeboth ]; then
+               $APPDIR/teeboth "$@"
+               rc=$?
+       else
+               local efile rc logfile=$1; shift
+               if [ "$logfile" ]; then
+                       efile=$(tempfile)
+                       { "$@" 2>&1; echo $? > $efile; } | tee -a $logfile
+                       rc=$(< $efile)
+                       rm -f $efile
+               else
+                       "$@"
+                       rc=$?
+               fi
+       fi
+       return $rc
+}
+
 # change dependency to specname
 # common changes:
 # - perl(Package::Name) -> perl-Package-Name
 depspecname() {
-       local package="$1"
+       local DEPS
+
+       if [ $# -gt 0 ]; then
+               DEPS="$@"
+       else
+               DEPS=$(cat)
+       fi
 
-       package=$(echo "$package" | sed -e '/perl(.*)/{s,perl(\(.*\)),perl-\1,;s,::,-,g};' -e 's/-\(devel\|static\)$//' )
-       echo "$package"
+       echo "$DEPS" | tr ' ' '\n' | sed -re '
+               # perl virtual deps
+               /perl\(.*\)/{
+                       s/perl\((.*)\)/perl-\1/
+                       s/::/-/g
+               }
+
+               s/apache\(EAPI\)-devel/apache-devel/
+
+               s/db-devel/db5.3-devel/
+               s/libjpeg-devel/libjpeg-turbo-devel/
+       '
 }
 
 update_shell_title() {
-       [ -t 1 ] || return
+       [ -t 2 ] || return
        local len=${COLUMNS:-80}
        local msg="$(echo "$*" | cut -c-$len)"
 
@@ -437,9 +630,9 @@ update_shell_title() {
 # set TARGET from BuildArch: from SPECFILE
 set_spec_target() {
        if [ -n "$SPECFILE" ] && [ -z "$TARGET" ]; then
-               tmp=$(awk '/^BuildArch:/ { print $NF; exit }' $ASSUMED_NAME/$SPECFILE)
+               local tmp=$(awk '/^BuildArch:/ { print $NF; exit }' $ASSUMED_NAME/$SPECFILE)
                if [ "$tmp" ]; then
-                               target_platform=$(rpm -E '%{_target_vendor}-%{_target_os}%{?_gnu}')
+                               local target_platform=$(rpm -E '%{_target_vendor}-%{_target_os}%{?_gnu}')
                                TARGET="$tmp"
                                case "$RPMBUILD" in
                                "rpmbuild")
@@ -453,11 +646,6 @@ set_spec_target() {
 
 # runs rpm with minimal macroset
 minirpm() {
-       # we reset macros not to contain macros.build as all the %() macros are
-       # executed here, while none of them are actually needed.
-       # at the time of this writing macros.build + macros contained 70 "%(...)" macros.
-       safe_macrofiles=$(rpm $TARGET_SWITCH --showrc | awk -F: '/^macrofiles/ { gsub(/^macrofiles[ \t]+:/, "", $0); gsub(/:.*macros.build:/, ":", $0); print $0 } ')
-
        # TODO: move these to /usr/lib/rpm/macros
        cat > $BUILDER_MACROS <<'EOF'
 %x8664 x86_64 amd64 ia32e
@@ -477,6 +665,7 @@ minirpm() {
 %releq_kernel_up(n:) ERROR
 %releq_kernel_smp(n:) ERROR
 %releq_kernel(n:) ERROR
+%py_postclean(x:) ERROR
 %kgcc_package ERROR
 %_fontsdir ERROR
 %ruby_version ERROR
@@ -509,7 +698,7 @@ minirpm() {
 %remove_etc_shells(p) %{p:<lua>}
 %lua_add_etc_shells()  %{nil}
 %lua_remove_etc_shells() %{nil}
-%required_jdk %{nil}
+%required_jdk jdk
 %buildrequires_jdk %{nil}
 %pear_package_print_optionalpackages %{nil}
 EOF
@@ -519,7 +708,13 @@ EOF
 %_sourcedir ./
 EOF
        fi
-       eval $RPMBUILD $TARGET_SWITCH --macros "$safe_macrofiles:$BUILDER_MACROS" $QUIET $RPMOPTS $RPMBUILDOPTS $BCOND $* 2>&1
+       if ! is_rpmorg; then
+               local safe_macrofiles
+               safe_macrofiles=$(rpm $TARGET_SWITCH --showrc | awk -F: '/^macrofiles/ { gsub(/^macrofiles[ \t]+:/, "", $0); print $0 } ')
+               eval PATH=$CLEAN_PATH $RPMBUILD $TARGET_SWITCH --macros "$safe_macrofiles:$BUILDER_MACROS" $QUIET $RPMOPTS $RPMBUILDOPTS $BCOND $* 2>&1
+       else
+               eval PATH=$CLEAN_PATH $RPMBUILD $TARGET_SWITCH --load "$BUILDER_MACROS" $QUIET $RPMOPTS $RPMBUILDOPTS $BCOND $* 2>&1
+       fi
 }
 
 cache_rpm_dump() {
@@ -530,7 +725,7 @@ cache_rpm_dump() {
 
        if [ -x /usr/bin/rpm-specdump ]; then
                update_shell_title "cache_rpm_dump using rpm-specdump command"
-               rpm_dump_cache=$(rpm-specdump $TARGET_SWITCH $BCOND $SPECFILE)
+               rpm_dump_cache=$(rpm-specdump $TARGET_SWITCH $BCOND --define "_specdir $PACKAGE_DIR" --define "_sourcedir $PACKAGE_DIR" $PACKAGE_DIR/$SPECFILE)
        else
                update_shell_title "cache_rpm_dump using rpmbuild command"
                local rpm_dump
@@ -595,7 +790,7 @@ parse_spec() {
        cd $PACKAGE_DIR
        cache_rpm_dump
 
-       if (rpm_dump | grep -qEi ":.*nosource.*1"); then
+       if rpm_dump | grep -qEi ":.*nosource.*1"; then
                FAIL_IF_NO_SOURCES="no"
        fi
 
@@ -633,6 +828,13 @@ parse_spec() {
        update_shell_title "parse_spec: OK!"
 }
 
+# aborts program abnormally
+die() {
+       local rc=${2:-1}
+       echo >&2 "$PROGRAM: ERROR: $*"
+       exit $rc
+}
+
 Exit_error() {
        if [ -n "$DEBUG" ]; then
                set -x
@@ -651,14 +853,18 @@ Exit_error() {
                        exit 2 ;;
                "err_no_spec_in_repo" )
                        remove_build_requires
-                       echo >&2 "Error: spec file not stored in CVS repo."
+                       echo >&2 "Error: spec file not stored in repository."
+                       if [ -n "$2" ]; then
+                               echo >&2 "Tried: $2"
+                       fi
+
                        exit 3 ;;
                "err_no_source_in_repo" )
                        remove_build_requires
-                       echo >&2 "Error: some source, patch or icon files not stored in CVS repo. ($2)"
+                       echo >&2 "Error: some source, patch or icon files not stored in PLD repo. ($2)"
                        exit 4 ;;
                "err_cvs_add_failed" )
-                       echo >&2 "Error: failed to add package to CVS repo."
+                       echo >&2 "Error: failed to add package to PLD repo."
                        exit 4 ;;
                "err_build_fail" )
                        remove_build_requires
@@ -670,7 +876,7 @@ Exit_error() {
                        exit 6 ;;
                "err_tag_exists" )
                        remove_build_requires
-                       echo >&2 "Tag ${2} already exists (spec release: ${3})."
+                       echo >&2 "Tag ${2} already exists"
                        exit 9 ;;
                "err_fract_rel" )
                        remove_build_requires
@@ -684,6 +890,17 @@ Exit_error() {
                        remove_build_requires
                        echo >&2 "Error: conditions reject building this spec (${2})."
                        exit 12 ;;
+               "err_remote_problem" )
+                       remove_build_requires
+                       echo >&2 "Error: problem with remote (${2})"
+                       exit 13 ;;
+               "err_no_checkut" )
+                       echo >&2 "Error: cannot checkout $2"
+                       exit 14 ;;
+               "err_not_implemented" )
+                       remove_build_requires
+                       echo >&2 "Error: functionality not yet imlemented"
+                       exit 110 ;;
        esac
        echo >&2 "Unknown error."
        exit 100
@@ -711,16 +928,34 @@ init_builder() {
                        PACKAGE_DIR=$REPO_DIR/$ASSUMED_NAME
                fi
        else
-               REPO_DIR="."
-               PACKAGE_DIR="."
+               TOP_DIR=$(pwd)
+               PACKAGE_DIR=$TOP_DIR
+               REPO_DIR=$PACKAGE_DIR
+               RPMBUILDOPTS="$RPMBUILDOPTS --define '_topdir $TOP_DIR' --define '_builddir %_topdir' --define '_rpmdir %_topdir' --define '_srcrpmdir %_topdir'"
+       fi
+       export GIT_WORK_TREE=$PACKAGE_DIR
+       export GIT_DIR=$PACKAGE_DIR/.git
+
+       if [ -d "$GIT_DIR" ] &&  [ -z "$CVSTAG" ] && git rev-parse --verify -q HEAD > /dev/null; then
+               if CVSTAG=$(GIT_DIR=$GIT_DIR git symbolic-ref HEAD) 2>/dev/null; then
+                       CVSTAG=${CVSTAG#refs/heads/}
+                       if [ "$CVSTAG" != "master" ]; then
+                               echo >&2 "builder: Active branch $CVSTAG. Use -r BRANCHNAME to override"
+                       fi
+               else
+                       echo >&2 "On detached HEAD. Use -r BRANCHNAME to override"
+                       HEAD_DETACHED="yes"
+               fi
+       elif [ "$CVSTAG" = "HEAD" ]; then
+               # assume -r HEAD is same as -A
+               CVSTAG="master"
        fi
 
        __PWD=$(pwd)
 }
 
-get_spec() {
-
-       update_shell_title "get_spec"
+create_git_repo() {
+       update_shell_title "add_package"
 
        if [ -n "$DEBUG" ]; then
                set -x
@@ -728,84 +963,161 @@ get_spec() {
        fi
 
        cd "$REPO_DIR"
+       SPECFILE=$(basename $SPECFILE)
        if [ ! -f "$ASSUMED_NAME/$SPECFILE" ]; then
-               # XXX: still needed?
-               SPECFILE=$(basename $SPECFILE)
+               echo "ERROR: No package to add ($ASSUMED_NAME/$SPECFILE)" >&2
+               exit 101
+       fi
+       [ -d "$ASSUMED_NAME/.git" ] || NEW_REPO=yes
+       ssh $GIT_PUSH create ${ASSUMED_NAME} || Exit_error err_cvs_add_failed
+       (
+       set -e
+       git init
+       git remote add $REMOTE_PLD ${GIT_SERVER}/${PACKAGES_DIR}/${ASSUMED_NAME}.git
+       git remote set-url --push $REMOTE_PLD ssh://${GIT_PUSH}/${PACKAGES_DIR}/${ASSUMED_NAME}
+
+       git config --local push.default current
+       git config --local branch.master.remote $REMOTE_PLD
+       git config --local branch.master.merge refs/heads/master
+       )
+       test $? = 0 || Exit_error err_remote_problem $REMOTE_PLD
+}
+
+get_spec() {
+
+       update_shell_title "get_spec"
+
+       if [ -n "$DEBUG" ]; then
+               set -x
+               set -v
        fi
 
+       cd "$REPO_DIR"
+       SPECFILE=$(basename $SPECFILE)
        if [ "$NOCVSSPEC" != "yes" ]; then
-               if [ ! -s CVS/Root -a "$NOCVSSPEC" != "yes" ]; then
-                       echo "Warning: No CVS access defined - using local .spec file"
-                       NOCVSSPEC="yes"
-               fi
-
-               if [ -d "$ASSUMED_NAME" -a -s "$ASSUMED_NAME/CVS/Root" ]; then
-                       cvsup "$ASSUMED_NAME/$SPECFILE" || Exit_error err_no_spec_in_repo
-               elif [ "$ADD_PACKAGE_CVS" = "yes" ]; then
-                       if [ ! -r "$ASSUMED_NAME/$SPECFILE" ]; then
-                               echo "ERROR: No package to add ($ASSUMED_NAME/$SPECFILE)" >&2
-                               exit 101
-                       fi
-                       if [ ! -s "$ASSUMED_NAME/CVS/Root" ]; then
-                               cvsup -a $ASSUMED_NAME || Exit_error err_cvs_add_failed
+               if [ -z "$DEPTH" ]; then
+                       if [ -d "$PACKAGE_DIR/.git" ]; then
+                               git fetch $IPOPT $REMOTE_PLD || Exit_error err_no_spec_in_repo
+                       elif [ "$ADD_PACKAGE_CVS" = "yes" ]; then
+                               if [ ! -r "$PACKAGE_DIR/$SPECFILE" ]; then
+                                       echo "ERROR: No package to add ($PACKAGE_DIR/$SPECFILE)" >&2
+                                       exit 101
+                               fi
+                               Exit_error err_not_implemented
+                       else
+                               (
+                                       unset GIT_WORK_TREE
+                                       git clone $IPOPT -o $REMOTE_PLD ${GIT_SERVER}/${PACKAGES_DIR}/${ASSUMED_NAME}.git || {
+                                               # softfail if new package, i.e not yet added to PLD rep
+                                               [ ! -f "$PACKAGE_DIR/$SPECFILE" ] && Exit_error err_no_spec_in_repo
+                                               echo "Warning: package not in Git - assuming new package"
+                                               NOCVSSPEC="yes"
+                                       }
+                                       git config --local --add "remote.$REMOTE_PLD.fetch" 'refs/notes/*:refs/notes/*'
+                                       git config --local --add "remote.$REMOTE_PLD.push" 'refs/notes/*:refs/notes/*'
+                                       git config --local --add "remote.$REMOTE_PLD.push" HEAD
+                                       git config --local push.default current
+                                       git remote set-url --push  $REMOTE_PLD ssh://${GIT_PUSH}/${PACKAGES_DIR}/${ASSUMED_NAME}
+                               )
                        fi
-                       cvsup -a "$ASSUMED_NAME/$SPECFILE" || Exit_error err_cvs_add_failed
                else
-                       cvsup -c -d $ASSUMED_NAME "packages/$ASSUMED_NAME/$SPECFILE" || {
-                               # softfail if new package, i.e not yet added to cvs
-                               [ ! -f "$ASSUMED_NAME/$SPECFILE" ] && Exit_error err_no_spec_in_repo
-                               echo "Warning: package not in CVS - assuming new package"
-                               NOCVSSPEC="yes"
-                               NOCVS="yes"
+                       if [ ! -d "$PACKAGE_DIR/.git" ]; then
+                               if [ ! -d "$PACKAGE_DIR" ]; then
+                                       install -d $PACKAGE_DIR
+                               fi
+                               git init
+                               git remote add $REMOTE_PLD ${GIT_SERVER}/${PACKAGES_DIR}/${ASSUMED_NAME}.git
+                               git config --local --add "remote.$REMOTE_PLD.fetch" 'refs/notes/*:refs/notes/*'
+                               git config --local --add "remote.$REMOTE_PLD.push" 'refs/heads/*:refs/remotes/origin/*'
+                               git config --local --add "remote.$REMOTE_PLD.push" HEAD
+                               git config --local push.default current
+                               git remote set-url --push  $REMOTE_PLD ssh://${GIT_PUSH}/${PACKAGES_DIR}/${ASSUMED_NAME}
+                               CVSTAG=${CVSTAG:-"master"}
+                       fi
+                       local refs=''
+                       if [ -z "$ALL_BRANCHES" ]; then
+                               refs="${CVSTAG}:remotes/${REMOTE_PLD}/${CVSTAG}"
+                       fi
+                       git fetch $IPOPT $DEPTH $REMOTE_PLD $refs || {
+                               echo >&2 "Error: branch $CVSTAG does not exist"
+                               exit 3
                        }
-
-                       # remove Entries.Static -- so 'cvs up' would update all files in a repo
-                       rm -f "$ASSUMED_NAME/CVS/Entries.Static"
                fi
+               git fetch $IPOPT $REMOTE_PLD 'refs/notes/*:refs/notes/*'
 
-               cvsignore_df .cvsignore
+               cvsignore_df .gitignore
 
-               # add default log format to .cvsignore if it is relative to package dir
+               # add default log format to .gitignore if it is relative to package dir
                if [ -n "$LOGFILE" -a "$LOGFILE" = "${LOGFILE##*/}" ]; then
                        # substitute known "macros" to glob
-                       local logfile=$(echo "$LOGFILE" | sed -e 's,\$\(PACKAGE_NAME\|DATE\),*,g')
+                       local logfile=$(echo "$LOGFILE" | sed -r -e 's,\$(PACKAGE_(NAME|VERSION|RELEASE)|DATE|TARGET),*,g')
                        if [ "$logfile" ]; then
                                cvsignore_df "$logfile"
                        fi
                fi
 
                # create symlinks for tools
-               if [ "$SYMLINK_TOOLS" != "no" ]; then
-                       for a in dropin md5 adapter builder {relup,compile,repackage,rsync,pearize}.sh pldnotify.awk; do
+               if [ "$SYMLINK_TOOLS" != "no" -a -d "$PACKAGE_DIR" ]; then
+                       for a in dropin md5 builder {relup,compile,repackage,rsync,pearize}.sh; do
+                               # skip tools that don't exist in top dir
                                [ -f $a ] || continue
-                               ln -sf ../$a $ASSUMED_NAME
+                               # skip tools that already exist
+                               [ -f $PACKAGE_DIR/$a ] && continue
+                               ln -s ../$a $PACKAGE_DIR
                                cvsignore_df $a
                        done
                fi
        fi
 
-       if [ ! -f "$ASSUMED_NAME/$SPECFILE" ]; then
-               Exit_error err_no_spec_in_repo
+       if [ -n "$CVSTAG" ]; then
+               if git rev-parse --verify -q "$CVSTAG" >/dev/null; then
+                       # checkout only if differs, so this will not trash git reflog
+                       if [ $(git rev-parse "$CVSTAG") != $(git rev-parse HEAD) ]; then
+                               git checkout "$CVSTAG" --
+                       fi
+               elif git rev-parse --verify -q "refs/remotes/${REMOTE_PLD}/$CVSTAG"; then
+                       git checkout -t "refs/remotes/${REMOTE_PLD}/$CVSTAG" > /dev/null
+               fi
+               if [ $(git rev-parse "$CVSTAG") != $(git rev-parse HEAD) ]; then
+                       Exit_error "err_no_checkut" "$CVSTAG"
+               fi
+
+               git merge --ff-only '@{u}'
+               git symbolic-ref -q HEAD > /dev/null && [ "$NOCVSSPEC" != "yes" ] &&
+               if [ -n "$CVSDATE" ]; then
+                       git checkout $(git rev-list -n1 --before="'$CVSDATE'" $CVSTAG) || exit 1
+               fi
+       fi
+
+       if [ ! -f "$PACKAGE_DIR/$SPECFILE" ]; then
+               Exit_error err_no_spec_in_repo "$PACKAGE_DIR/$SPECFILE"
        fi
 
        if [ "$CHMOD" = "yes" -a -n "$SPECFILE" ]; then
-               chmod $CHMOD_MODE $ASSUMED_NAME/$SPECFILE
+               chmod $CHMOD_MODE $PACKAGE_DIR/$SPECFILE
        fi
        unset OPTIONS
-       [ -n "$DONT_PRINT_REVISION" ] || grep -E -m 1 "^#.*Revision:.*Date" $ASSUMED_NAME/$SPECFILE
+       [ -n "$DONT_PRINT_REVISION" ] || grep -E -m 1 "^#.*Revision:.*Date" $PACKAGE_DIR/$SPECFILE
 
        set_spec_target
 }
 
+# find mirrors in this order. first match wins:
+# - package dir (~/rpm/packages/foo)
+# - repository dir (~/rpm/packages)
+# - tools dir dir (~/rpm/packages/rpm-build-tools)
 find_mirror() {
-       cd "$REPO_DIR"
        local url="$1"
-       if [ ! -f "mirrors" -a "$NOCVSSPEC" != "yes" ] ; then
-               $CVS_COMMAND update mirrors >&2
-       fi
 
-       IFS="|"
+       update_shell_title "find_mirror[$url][$REPO_DIR]"
+
+       # NOTE: as while loop runs in subshell,
+       # we use exit 2 to indicate  that the match was found
+       # otherwise we end up outputing mirror url and origin url.
+
        local origin mirror name rest ol prefix
+       IFS="|"
+       cat "$PACKAGE_DIR/mirrors" "$REPO_DIR/mirrors" "$REPO_DIR/../rpm-build-tools/mirrors" /dev/null 2>/dev/null | \
        while read origin mirror name rest; do
                # skip comments and empty lines
                if [ -z "$origin" ] || [ "${origin#\#}" != "$origin" ]; then
@@ -816,19 +1128,21 @@ find_mirror() {
                if [ "$prefix" = "$origin" ] ; then
                        suffix=$(echo "$url" | cut -b $((ol+1))-)
                        echo -n "$mirror$suffix"
-                       return 0
+                       exit 2
                fi
-       done < mirrors
-       echo "$url"
+       done && echo "$url"
 }
 
 # Warning: unpredictable results if same URL used twice
 src_no() {
+       local file="$1"
+       # escape some regexp characters if part of file name
+       file=$(echo "$file" | sed -e 's#\([\+\*\.\&\#\?]\)#\\\1#g')
        cd $PACKAGE_DIR
        rpm_dump | \
-       grep "SOURCEURL[0-9]*[  ]*$1""[         ]*$" | \
-       sed -e 's/.*SOURCEURL\([0-9][0-9]*\).*/\1/' | \
-       head -n 1 | xargs
+       grep -E "(SOURCE|PATCH)URL[0-9]*[       ]*${file}""[    ]*$" | \
+       sed -e 's/.*\(SOURCE\|PATCH\)URL\([0-9][0-9]*\).*/\1\2/' | \
+       head -n 1 | tr OURCEATH ourceath | xargs
 }
 
 src_md5() {
@@ -838,27 +1152,23 @@ src_md5() {
        cd $PACKAGE_DIR
        local md5
 
-       if [ -f additional-md5sums ]; then
-               local spec_rev=$(grep $SPECFILE CVS/Entries 2>/dev/null | sed -e s:/$SPECFILE/:: -e s:/.*::)
-               if [ -z "$spec_rev" ]; then
-                       spec_rev=$(head -n 1 $SPECFILE | sed -e 's/.*\$Revision: \([0-9.]*\).*/\1/')
-               fi
-               local spec="$SPECFILE[0-9.,]*,$(echo $spec_rev | sed 's/\./\\./g')"
-               md5=$(grep -s -v '^#' additional-md5sums | \
-               grep -E "[      ]$(basename "$1")[      ]+${spec}([     ,]|\$)" | \
+       # use "sources" file from package dir, like vim
+       if [ -f sources ]; then
+               md5=$(grep -s -v '^#' sources | \
+               grep -E "[      *]$(basename "$1")([    ,]|\$)" | \
                sed -e 's/^\([0-9a-f]\{32\}\).*/\1/' | \
                grep -E '^[0-9a-f]{32}$')
 
                if [ "$md5" ]; then
                        if [ $(echo "$md5" | wc -l) != 1 ] ; then
-                               echo "$SPECFILE: more then one entry in additional-md5sums for $1" 1>&2
+                               echo "$SPECFILE: more then one entry in sources for $1" 1>&2
                        fi
                        echo "$md5" | tail -n 1
                        return
                fi
        fi
 
-       source_md5=`grep -i "^#[        ]*Source$no-md5[        ]*:" $SPECFILE | sed -e 's/.*://'`
+       source_md5=$(grep -iE "^#[      ]*(No)?$no-md5[         ]*:" $SPECFILE | sed -e 's/.*://')
        if [ -n "$source_md5" ]; then
                echo $source_md5
        else
@@ -868,7 +1178,7 @@ src_md5() {
                else
                        # we have empty SourceX-md5, but it is still possible
                        # that we have NoSourceX-md5 AND NoSource: X
-                       nosource_md5=`grep -i "^#[       ]*NoSource$no-md5[      ]*:" $SPECFILE | sed -e 's/.*://'`
+                       nosource_md5=`grep -i "^#[       ]*No$no-md5[    ]*:" $SPECFILE | sed -e 's/.*://'`
                        if [ -n "$nosource_md5" -a -n "`grep -i "^NoSource:[     ]*$no$" $SPECFILE`" ] ; then
                                echo $nosource_md5
                        fi
@@ -903,7 +1213,7 @@ cvsignore_df() {
        if [ "$CVSIGNORE_DF" != "yes" ]; then
                return
        fi
-       cvsignore=${PACKAGE_DIR}/.cvsignore
+       local cvsignore=${PACKAGE_DIR}/.git/info/exclude
 
        # add only if not yet there
        if ! awk -vf="$1" -vc=1 '$0 == f { c = 0 } END { exit c }' $cvsignore 2>/dev/null; then
@@ -911,67 +1221,6 @@ cvsignore_df() {
        fi
 }
 
-cvsup() {
-       update_shell_title "cvsup"
-       local OPTIONS="" ACTION="up"
-
-       # checkout
-       if [ "$1" = "-c" ]; then
-               ACTION="co"
-               shift
-       fi
-       # add
-       if [ "$1" = "-a" ]; then
-               ACTION="add"
-               shift
-       fi
-
-       OPTIONS="$ACTION "
-
-       if [ -n "$CVSROOT" ]; then
-               OPTIONS="-d $CVSROOT $OPTIONS"
-       fi
-
-       if [ "$ACTION" != "add" ]; then
-               if [ -z "$CVSDATE" -a -z "$CVSTAG" ]; then
-                       OPTIONS="$OPTIONS -A"
-               else
-                       if [ -n "$CVSDATE" ]; then
-                               OPTIONS="$OPTIONS -D $CVSDATE"
-                       fi
-                       if [ -n "$CVSTAG" ]; then
-                               # FIXME: cvs add actually works with -r ?
-                               OPTIONS="$OPTIONS -r $CVSTAG"
-                       fi
-               fi
-       fi
-
-       local result=1
-       local retries_counter=0
-       if [ $# = 1 ]; then
-               update_shell_title "cvsup: $*"
-       else
-               update_shell_title "cvsup: $# files"
-       fi
-       while [ "$result" != "0" -a "$retries_counter" -le "$CVS_RETRIES" ]; do
-               retries_counter=$(( $retries_counter + 1 ))
-               output=$(LC_ALL=C $CVS_COMMAND $OPTIONS "$@" 2>&1)
-               result=$?
-               [ -n "$output" ] && echo "$output"
-               if echo "$output" | grep -qE "(Cannot connect to|connect to .* failed|Connection reset by peer|Connection timed out|Unknown host)" \
-                       && [ "$result" -ne "0" -a "$retries_counter" -le "$CVS_RETRIES" ]; then
-                       echo "Trying again [$*]... ($retries_counter)"
-                       update_shell_title "cvsup: retry #$retries_counter"
-                       sleep 2
-                       continue
-               else
-                       break
-               fi
-       done
-       update_shell_title "cvsup: done!"
-       return $result
-}
-
 # returns true if "$1" is ftp, http or https protocol url
 is_url() {
        case "$1" in
@@ -1003,10 +1252,10 @@ update_md5() {
                local srcno=$(src_no "$i")
                if [ -n "$ADD5" ]; then
                        [ "$fp" = "$i" ] && continue # FIXME what is this check doing?
-                       grep -qiE '^#[  ]*Source'$srcno'-md5[   ]*:' $PACKAGE_DIR/$SPECFILE && continue
+                       grep -qiE '^#[  ]*'$srcno'-md5[         ]*:' $PACKAGE_DIR/$SPECFILE && continue
                        grep -qiE '^BuildRequires:[     ]*digest[(]%SOURCE'$srcno'[)][  ]*=' $PACKAGE_DIR/$SPECFILE && continue
                else
-                       grep -qiE '^#[  ]*Source'$srcno'-md5[   ]*:' $PACKAGE_DIR/$SPECFILE || grep -qiE '^BuildRequires:[      ]*digest[(]%SOURCE'$srcno'[)][  ]*=' $PACKAGE_DIR/$SPECFILE || continue
+                       grep -qiE '^#[  ]*'$srcno'-md5[         ]*:' $PACKAGE_DIR/$SPECFILE || grep -qiE '^BuildRequires:[      ]*digest[(]%SOURCE'$srcno'[)][  ]*=' $PACKAGE_DIR/$SPECFILE || continue
                fi
                if [ ! -f "$fp" ] || [ $ALWAYS_CVSUP = "yes" ]; then
                        need_files="$need_files $i"
@@ -1022,22 +1271,22 @@ update_md5() {
        for i in "$@"; do
                local fp=$(nourl "$i")
                local srcno=$(src_no "$i")
-               local md5=$(grep -iE '^#[       ]*(No)?Source'$srcno'-md5[      ]*:' $PACKAGE_DIR/$SPECFILE )
+               local md5=$(grep -iE '^#[       ]*(No)?'$srcno'-md5[    ]*:' $PACKAGE_DIR/$SPECFILE )
                if [ -z "$md5" ]; then
                        md5=$(grep -iE '^[      ]*BuildRequires:[       ]*digest[(]%SOURCE'$srcno'[)][  ]*=' $PACKAGE_DIR/$SPECFILE )
                fi
                if [ -n "$ADD5" ] && is_url $i || [ -n "$md5" ]; then
-                       local tag="# Source$srcno-md5:\t"
+                       local tag="# $srcno-md5:\t"
                        if [[ "$md5" == *NoSource* ]]; then
-                               tag="# NoSource$srcno-md5:\t"
+                               tag="# No$srcno-md5:\t"
                        elif [ -n "$USEDIGEST" ]; then
                                tag="BuildRequires:\tdigest(%SOURCE$srcno) = "
                        fi
                        md5=$(md5sum "$fp" | cut -f1 -d' ')
-                       echo "Updating Source$srcno ($md5: $fp)."
+                       echo "Updating $srcno ($md5: $fp)."
                        perl -i -ne '
-                               print unless (/^\s*#\s*(No)?Source'$srcno'-md5\s*:/i or /^\s*BuildRequires:\s*digest\(%SOURCE'$srcno'\)/i);
-                               print "'"$tag$md5"'\n" if /^Source'$srcno'\s*:\s+/;
+                               print unless (/^\s*#\s*(No)?'$srcno'-md5\s*:/i or /^\s*BuildRequires:\s*digest\(%SOURCE'$srcno'\)/i);
+                               print "'"$tag$md5"'\n" if /^'$srcno'\s*:\s+/i;
                        ' \
                        $PACKAGE_DIR/$SPECFILE
                fi
@@ -1080,11 +1329,6 @@ get_files() {
        if [ $# -gt 0 ]; then
                cd "$PACKAGE_DIR"
 
-               if [ ! -s CVS/Root -a "$NOCVS" != "yes" ]; then
-                       echo "Warning: No CVS access defined for SOURCES"
-                       NOCVS="yes"
-               fi
-
                local nc=0
                local get_files_cvs=""
                for i in "$@"; do
@@ -1132,10 +1376,6 @@ get_files() {
                                                update_shell_title "${GETLOCAL%% *}: $url"
                                                ${GETLOCAL} $url $target
                                        else
-                                               if [ -z "$NOMIRRORS" ]; then
-                                                       url=$(find_mirror "$url")
-                                               fi
-
                                                local uri=${url}
                                                # make shorter message for distfiles urls
                                                if [[ "$uri" = ${PROTOCOL}${DISTFILES_SERVER}* ]] || [[ "$uri" = ${PROTOCOL}${ATTICDISTFILES_SERVER}* ]]; then
@@ -1144,11 +1384,7 @@ get_files() {
                                                        uri="df: $uri"
                                                fi
                                                update_shell_title "${GETURI%% *}: $uri"
-                                               ${GETURI} ${OUTFILEOPT} "$target" "$url" || \
-                                               if [ "`echo $url | grep -E 'ftp://'`" ]; then
-                                                       update_shell_title "${GETURI2%% *}: $url"
-                                                       ${GETURI2} ${OUTFILEOPT} "$target" "$url"
-                                               fi
+                                               ${GETURI} "$target" "$url"
                                        fi
 
                                        # is it empty file?
@@ -1158,15 +1394,8 @@ get_files() {
                                                        update_shell_title "${GETLOCAL%% *}: $url_attic"
                                                        ${GETLOCAL} $url_attic $target
                                                else
-                                                       if [ -z "$NOMIRRORS" ]; then
-                                                               url_attic=$(find_mirror "$url_attic")
-                                                       fi
                                                        update_shell_title "${GETURI%% *}: $url_attic"
-                                                       ${GETURI} ${OUTFILEOPT} "$target" "$url_attic" || \
-                                                       if [ "`echo $url_attic | grep -E 'ftp://'`" ]; then
-                                                               update_shell_title "${GETURI2%% *}: $url_attic"
-                                                               ${GETURI2} ${OUTFILEOPT} "$target" "$url_attic"
-                                                       fi
+                                                       ${GETURI} "$target" "$url_attic"
                                                        test -s "$target" || rm -f "$target"
                                                fi
                                        fi
@@ -1177,14 +1406,6 @@ get_files() {
                                                rm -f "$target"
                                                FROM_DISTFILES=0
                                        fi
-                               elif [ "$NOCVS" != "yes" -a -z "$srcmd5" ]; then
-                                       if [ $# -gt 1 ]; then
-                                               get_files_cvs="$get_files_cvs $fp"
-                                               update_shell_title "$fp (will cvs up later)"
-                                               cvsup=1
-                                       else
-                                               cvsup $fp
-                                       fi
                                fi
 
                                if [ -z "$NOURLS" ] && [ ! -f "$fp" -o -n "$UPDATE" ] && [ "`echo $i | grep -E 'ftp://|http://|https://'`" ]; then
@@ -1194,11 +1415,7 @@ get_files() {
                                                im="$i"
                                        fi
                                        update_shell_title "${GETURI%% *}: $im"
-                                       ${GETURI} "$im" ${OUTFILEOPT} "$target" || \
-                                       if [ "`echo $im | grep -E 'ftp://'`" ]; then
-                                               update_shell_title "${GETURI2%% *}: $im"
-                                               ${GETURI2} "$im" ${OUTFILEOPT} "$target"
-                                       fi
+                                       ${GETURI} "$target" "$im"
                                        test -s "$target" || rm -f "$target"
                                fi
 
@@ -1210,11 +1427,7 @@ get_files() {
 
                        # the md5 check must be moved elsewhere as if we've called from update_md5 the md5 is wrong.
                        if [ ! -f "$fp" -a "$FAIL_IF_NO_SOURCES" != "no" ]; then
-                               if [ -n "GREEDSRC" ]; then
-                                       get_greed_sources $i
-                               else
-                                       Exit_error err_no_source_in_repo $i
-                               fi
+                               Exit_error err_no_source_in_repo $i
                        fi
 
                        # we check md5 here just only to refetch immediately
@@ -1227,28 +1440,17 @@ get_files() {
                                FROM_DISTFILES=2
                                rm -f $target
                                update_shell_title "${GETURI%% *}: $url"
-                               ${GETURI} ${OUTFILEOPT} "$target" "$url" || \
-                               if [ "`echo $url | grep -E 'ftp://'`" ]; then
-                                       update_shell_title "${GETURI2%% *}: $url"
-                                       ${GETURI2} ${OUTFILEOPT} "$target" "$url"
-                               fi
+                               ${GETURI} "$target" "$url"
                                if [ ! -s "$target" ]; then
                                        rm -f "$target"
                                        update_shell_title "${GETURI%% *}: $url_attic"
-                                       ${GETURI} ${OUTFILEOPT} "$target" "$url_attic" || \
-                                       if [ "`echo $url_attic | grep -E 'ftp://'`" ]; then
-                                               update_shell_title "${GETURI2%% *}: $url_attic"
-                                               ${GETURI2} ${OUTFILEOPT} "$target" "$url_attic"
-                                       fi
+                                       ${GETURI} "$target" "$url_attic"
                                fi
                                test -s "$target" || rm -f "$target"
                        fi
                done
                SHELL_TITLE_PREFIX=""
 
-               if [ "$get_files_cvs" ]; then
-                       cvsup $get_files_cvs
-               fi
 
                if [ "$CHMOD" = "yes" ]; then
                        CHMOD_FILES=$(nourl "$@")
@@ -1259,6 +1461,23 @@ get_files() {
        fi
 }
 
+tag_exist() {
+# If tag exists and points to other commit exit with error
+# If it existsts and points to HEAD return 1
+# If it doesn't exist return 0
+       local _tag="$1"
+       local sha1=$(git rev-parse HEAD)
+       echo "Searching for tag $_tag..."
+       if [ -n "$DEPTH" ]; then
+               local ref=$(git ls-remote $REMOTE_PLD "refs/tags/$_tag"  | cut -c -40)
+       else
+               local ref=$(git show-ref -s "refs/tags/$_tag")
+       fi
+       [ -z "$ref" ] && return 0
+       [ "$ref" = "$sha1" ] || Exit_error err_tag_exists "$_tag"
+       return 1
+}
+
 make_tagver() {
        if [ -n "$DEBUG" ]; then
                set -x
@@ -1271,42 +1490,20 @@ make_tagver() {
        fi
 
        # NOTE: CVS tags may must not contain the characters `$,.:;@'
-       TAGVER=$(echo $TAG_PREFIX$PACKAGE_NAME-$PACKAGE_VERSION-$PACKAGE_RELEASE | tr '[.@]' '[_#]')
+       TAGVER=$(echo $TAG_PREFIX$PACKAGE_NAME-$PACKAGE_VERSION-$PACKAGE_RELEASE)
 
-       # Remove #kernel.version_release from TAGVER because tagging sources
+       # Remove @kernel.version_release from TAGVER because tagging sources
        # could occur with different kernel-headers than kernel-headers used at build time.
        # besides, %{_kernel_ver_str} is not expanded.
 
-       # TAGVER=auto-ac-madwifi-ng-0-0_20070225_1#%{_kernel_ver_str}
+       # TAGVER=auto-ac-madwifi-ng-0-0_20070225_1@%{_kernel_ver_str}
        # TAGVER=auto-ac-madwifi-ng-0-0_20070225_1
 
-       TAGVER=${TAGVER%#*}
+       TAGVER=${TAGVER%@*}
        echo -n "$TAGVER"
 }
 
-# bool is_tag_a_branch(tag)
-#
-# returns 1 if a tag is a branch set on SPECFILE
-is_tag_a_branch() {
-       if [ -n "$DEBUG" ]; then
-               set -x
-               set -v
-       fi
-
-       if [ $# -ne 1 ]; then
-               return 0;
-       fi
-
-       TAG=$1
-
-       cd "$PACKAGE_DIR"
-       $CVS_COMMAND status -v $SPECFILE | grep -Eiq "${TAG}.+(branch: [0-9.]+)"
-       return $?
-}
-
 tag_files() {
-       TAG_FILES="$@"
-
        if [ -n "$DEBUG" ]; then
                set -x
                set -v
@@ -1315,117 +1512,42 @@ tag_files() {
        echo "Version: $PACKAGE_VERSION"
        echo "Release: $PACKAGE_RELEASE"
 
-       local TAGVER
+       local _tag
        if [ "$TAG_VERSION" = "yes" ]; then
-               TAGVER=`make_tagver`
-               echo "CVS tag: $TAGVER"
+               _tag=`make_tagver`
        fi
        if [ -n "$TAG" ]; then
-               echo "CVS tag: $TAG"
+               _tag="$TAG"
        fi
+       echo "tag: $_tag"
 
        local OPTIONS="tag $CVS_FORCE"
-       if [ -n "$CVSROOT" ]; then
-               OPTIONS="-d $CVSROOT $OPTIONS"
-       fi
-
-       # if a tagname we are about to set already exists
-       # and happens to be a branch (common case with AC-branch)
-       # pass -B (allows -F to disturb branch tag)
-       local _tag=$TAG
-       if [ "$TAG_VERSION" = "yes" ]; then
-               _tag=$TAGVER
-       fi;
-       is_tag_a_branch $_tag
-       if [ $? -eq 0 -a $CVS_NSERVER -eq 0 ]; then
-               OPTIONS="$OPTIONS -B"
-       fi;
 
        cd "$PACKAGE_DIR"
-       local tag_files
-       for i in $TAG_FILES; do
-               # don't tag files stored on distfiles
-               [ -n "`src_md5 $i`" ] && continue
-               local fp=`nourl "$i"`
-               if [ -f "$fp" ]; then
-                       tag_files="$tag_files $fp"
-               elif [ -n "GREEDSRC" ]; then
-                       get_greed_sources $i
-               else
-                       Exit_error err_no_source_in_repo $i
-               fi
-       done
-
-       if [ "$tag_files" ]; then
-               if [ "$TAG_VERSION" = "yes" ]; then
-                       update_shell_title "tag sources: $TAGVER"
-                       printf "Tagging %d files\n" $(echo $tag_files | wc -w)
-                       $CVS_COMMAND $OPTIONS $TAGVER $tag_files || exit
-               fi
-               if [ -n "$TAG" ]; then
-                       update_shell_title "tag sources: $TAG"
-
-                       while [ "$tag_files" ]; do
-                               local chunk=$(echo $tag_files | tr ' ' '\n' | head -n 100)
-                               printf "Tagging %d files\n" $(echo $chunk | wc -w)
-                               $CVS_COMMAND $OPTIONS $TAG $chunk || exit
-                               tag_files=$(echo $tag_files | tr ' ' '\n' | tail +101)
-                       done
-               fi
-       fi
 
-       cd "$PACKAGE_DIR"
-       if [ "$TAG_VERSION" = "yes" ]; then
-               update_shell_title "tag spec: $TAGVER"
-               $CVS_COMMAND $OPTIONS $TAGVER $SPECFILE || exit
-       fi
-       if [ -n "$TAG" ]; then
-               update_shell_title "tag spec: $TAG"
-               $CVS_COMMAND $OPTIONS $TAG $SPECFILE || exit
+       if tag_exist $_tag || [ -n "$CVS_FORCE" ]; then
+               update_shell_title "tag sources: $_tag"
+               git $OPTIONS $_tag || exit
+               git push $IPOPT $CVS_FORCE $REMOTE_PLD tag $_tag || Exit_error err_remote_problem $REMOTE_PLD
+       else
+               echo "Tag $_tag already exists and points to the same commit"
        fi
 }
 
 branch_files() {
        TAG=$1
-       echo "CVS branch tag: $TAG"
+       echo "Git branch: $TAG"
        shift
 
-       TAG_FILES="$@"
-
        if [ -n "$DEBUG" ]; then
                set -x
                set -v
        fi
 
-       local OPTIONS="tag $CVS_FORCE -b"
-
-       # branch exists?
-       is_tag_a_branch $TAG
-       if [ $? -eq 1 ]; then
-               OPTIONS="$OPTIONS -B"
-       fi
-
-       if [ -n "$CVSROOT" ]; then
-               OPTIONS="-d $CVSROOT $OPTIONS"
-       fi
-       cd "$PACKAGE_DIR"
-       local tag_files
-       for i in $TAG_FILES; do
-               local fp=`nourl "$i"`
-               if [ -f "$fp" ]; then
-                       tag_files="$tag_files $fp"
-               elif [ -n "GREEDSRC" ]; then
-                       get_greed_sources $i
-               else
-                       Exit_error err_no_source_in_repo $i
-               fi
-       done
-       if [ "$tag_files" ]; then
-               $CVS_COMMAND $OPTIONS $TAG $tag_files || exit
-       fi
+       local OPTIONS="branch $CVS_FORCE"
 
        cd "$PACKAGE_DIR"
-       $CVS_COMMAND $OPTIONS $TAG $SPECFILE || exit
+       git $OPTIONS $TAG || exit
 }
 
 
@@ -1471,50 +1593,61 @@ set_version() {
        " $specfile
 }
 
+# try to upgrade .spec to new version
+# if --upgrade-version is specified, use that as new version, otherwise invoke pldnotify to find new version
+#
+# return 1: if .spec was updated
+# return 0: no changes to .spec
+# exit 1 in case of error
 try_upgrade() {
-       if [ -n "$TRY_UPGRADE" ]; then
-               local TNOTIFY TNEWVER TOLDVER
-               update_shell_title "build_package: try_upgrade"
+       if [ -z "$TRY_UPGRADE" ]; then
+               return 0
+       fi
 
-               cd "$PACKAGE_DIR"
-               
-               if [ "$UPGRADE_VERSION" ]; then
-                       TNEWVER=$UPGRADE_VERSION
-               else
-                       if [ -n "$FLOAT_VERSION" ]; then
-                               TNOTIFY=$($APPDIR/pldnotify.awk ${BE_VERBOSE:+-vDEBUG=1} $SPECFILE -n) || exit 1
-                       else
-                               TNOTIFY=$($APPDIR/pldnotify.awk ${BE_VERBOSE:+-vDEBUG=1} $SPECFILE) || exit 1
-                       fi
+       local TNOTIFY TNEWVER TOLDVER
+       update_shell_title "build_package: try_upgrade"
 
-                       # pldnotify.awk does not set exit codes, but it has match for ERROR
-                       # in output which means so.
-                       if [[ "$TNOTIFY" = *ERROR* ]]; then
-                               echo >&2 "$TNOTIFY"
-                               exit 1
-                       fi
+       cd "$PACKAGE_DIR"
 
-                       TNEWVER=$(echo $TNOTIFY | awk '{ match($4,/\[NEW\]/); print $5 }')
+       if [ "$UPGRADE_VERSION" ]; then
+               TNEWVER=$UPGRADE_VERSION
+               echo "Updating spec file to version $TNEWVER"
+       else
+               if [ -n "$FLOAT_VERSION" ]; then
+                       TNOTIFY=$(pldnotify ${BE_VERBOSE:+-vDEBUG=1} $SPECFILE -n) || exit 1
+               else
+                       TNOTIFY=$(pldnotify ${BE_VERBOSE:+-vDEBUG=1} $SPECFILE) || exit 1
                fi
 
-               if [ -n "$TNEWVER" ]; then
-                       TOLDVER=`echo $TNOTIFY | awk '{ print $3; }'`
-                       echo "New version found, updating spec file from $TOLDVER to version $TNEWVER"
-                       if [ "$REVERT_BROKEN_UPGRADE" = "yes" ]; then
-                               cp -f $SPECFILE $SPECFILE.bak
-                       fi
-                       chmod +w $SPECFILE
-                       set_release $SPECFILE $PACKAGE_RELEASE 1
-                       set_version $SPECFILE $PACKAGE_VERSION $TNEWVER
-                       parse_spec
-                       if [ "$PACKAGE_VERSION" != "$TNEWVER" ]; then
-                               echo >&2 "Upgrading version failed, you need to update spec yourself"
-                               exit 1
-                       fi
-                       return 1
+               # pldnotify does not set exit codes, but it has match for ERROR
+               # in output which means so.
+               if [[ "$TNOTIFY" = *ERROR* ]]; then
+                       echo >&2 "$TNOTIFY"
+                       exit 1
                fi
+
+               TOLDVER=`echo $TNOTIFY | awk '{ print $3; }'`
+               echo "New version found, updating spec file from $TOLDVER to version $TNEWVER"
+
+               TNEWVER=$(echo $TNOTIFY | awk '{ match($4,/\[NEW\]/); print $5 }')
+       fi
+
+       if [ -z "$TNEWVER" ]; then
+               return 0
+       fi
+
+       if [ "$REVERT_BROKEN_UPGRADE" = "yes" ]; then
+               cp -f $SPECFILE $SPECFILE.bak
        fi
-       return 0
+       chmod +w $SPECFILE
+       set_version $SPECFILE $PACKAGE_VERSION $TNEWVER
+       set_release $SPECFILE $PACKAGE_RELEASE 1
+       parse_spec
+       if [ "$PACKAGE_VERSION" != "$TNEWVER" ]; then
+               echo >&2 "Upgrading version failed, you need to update spec yourself"
+               exit 1
+       fi
+       return 1
 }
 
 build_package() {
@@ -1545,33 +1678,46 @@ build_package() {
        esac
 
        update_shell_title "build_package: $COMMAND"
+       local logfile retval
        if [ -n "$LOGFILE" ]; then
-               LOG=`eval echo $LOGFILE`
-               if [ -d "$LOG" ]; then
-                       echo "Log file $LOG is a directory."
+               logfile=`eval echo $LOGFILE`
+               if [ -d "$logfile" ]; then
+                       echo "Log file $logfile is a directory."
                        echo "Parse error in the spec?"
                        Exit_error err_build_fail
                fi
                if [ -n "$LASTLOG_FILE" ]; then
-                       echo "LASTLOG=$LOG" > $LASTLOG_FILE
+                       echo "LASTLOG=$logfile" > $LASTLOG_FILE
                fi
-               RES_FILE=$(mktemp -t builder.XXXXXX || ${TMPDIR:-/tmp}/builder.$RANDOM)
-
-               (time eval ${NICE_COMMAND} $RPMBUILD $TARGET_SWITCH $BUILD_SWITCH -v $QUIET $CLEAN $RPMOPTS $RPMBUILDOPTS $BCOND --define \'_specdir $PACKAGE_DIR\' --define \'_sourcedir $PACKAGE_DIR\' $SPECFILE; echo $? > $RES_FILE) 2>&1 |tee $LOG
-               RETVAL=`cat $RES_FILE`
-               rm $RES_FILE
-               if [ -n "$LOGDIROK" ] && [ -n "$LOGDIRFAIL" ]; then
-                       if [ "$RETVAL" -eq "0" ]; then
-                               mv $LOG $LOGDIROK
-                       else
-                               mv $LOG $LOGDIRFAIL
-                       fi
+       fi
+
+       # unset these, should not be exposed to builder shell!
+       unset GIT_WORK_TREE GIT_DIR
+       # these are set by jenkins
+       unset GIT_PREVIOUS_COMMIT GIT_URL GIT_PREVIOUS_SUCCESSFUL_COMMIT GIT_BRANCH GIT_COMMIT
+       # this may be set by user
+       unset GIT_SSH
+       # may be set by user
+       unset GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_TESTING_PORCELAIN_COMMAND_LIST
+       # fail if something still set
+       env | grep ^GIT_ && Exit_error err_build_fail
+
+       local specdir=$(insert_gitlog $SPECFILE)
+       ulimit -c unlimited
+       # FIXME: eval here is exactly why?
+       PATH=$CLEAN_PATH eval teeboth "'$logfile'" ${TIME_COMMAND} ${NICE_COMMAND} $RPMBUILD $TARGET_SWITCH $BUILD_SWITCH -v $QUIET $CLEAN $RPMOPTS $RPMBUILDOPTS $BCOND --define \'_specdir $PACKAGE_DIR\' --define \'_sourcedir $PACKAGE_DIR\' $specdir/$SPECFILE
+       retval=$?
+       rm -r $specdir
+
+       if [ -n "$logfile" ] && [ -n "$LOGDIROK" ] && [ -n "$LOGDIRFAIL" ]; then
+               if [ "$retval" -eq "0" ]; then
+                       mv $logfile $LOGDIROK
+               else
+                       mv $logfile $LOGDIRFAIL
                fi
-       else
-               eval ${NICE_COMMAND} $RPMBUILD $TARGET_SWITCH $BUILD_SWITCH -v $QUIET $CLEAN $RPMOPTS $RPMBUILDOPTS $BCOND --define \'_specdir $PACKAGE_DIR\' --define \'_sourcedir $PACKAGE_DIR\' $SPECFILE
-               RETVAL=$?
        fi
-       if [ "$RETVAL" -ne "0" ]; then
+
+       if [ "$retval" -ne "0" ]; then
                if [ -n "$TRY_UPGRADE" ]; then
                        echo "\nUpgrade package to new version failed."
                        if [ "$REVERT_BROKEN_UPGRADE" = "yes" ]; then
@@ -1615,7 +1761,7 @@ process_bcondrc() {
        # w32codec-installer license_agreement
        # php +mysqli
        # ---
-       if ([ -f $HOME/.bcondrc ] || ([ -n $HOME_ETC ] && [ -f $HOME_ETC/.bcondrc ])); then
+       if [ -f $HOME/.bcondrc ] || ([ -n $HOME_ETC ] && [ -f $HOME_ETC/.bcondrc ]); then
                :
        else
                return
@@ -1681,7 +1827,7 @@ set_bconds_values() {
                without_*)
                        bcond=${opt#without_}
                        case "$BCOND" in
-                       *--without?${bcond}*)
+                       *--without?${bcond}\ *|*--without?${bcond})
                                AVAIL_BCONDS_WITHOUT="$AVAIL_BCONDS_WITHOUT <$bcond>"
                                ;;
                        *)
@@ -1692,7 +1838,7 @@ set_bconds_values() {
                with_*)
                        bcond=${opt#with_}
                        case "$BCOND" in
-                       *--with?${bcond}*)
+                       *--with?${bcond}\ *|*--with?${bcond})
                                AVAIL_BCONDS_WITH="$AVAIL_BCONDS_WITH <$bcond>"
                                ;;
                        *)
@@ -1712,13 +1858,13 @@ run_sub_builder() {
        package_name="${1}"
        update_shell_title "run_sub_builder $package_name"
        #
-       # No i tutaj bym chcia³ zrobiæ sztuczn± inteligencjê, która spróbuje tego
-       # pakieta zbudowaæ. Aktualnie niewiele dziala, bo generalnie nie widze do
+       # No i tutaj bym chciaÅ‚ zrobić sztucznÄ… inteligencjÄ™, która spróbuje tego
+       # pakieta zbudować. Aktualnie niewiele dziala, bo generalnie nie widze do
        # konca algorytmu... Ale damy rade. :) Na razie po prostu sie wyjebie tak samo
        # jakby nie bylo tego kawalka kodu.
        #
-       # Update: Poprawi³em parê rzeczy i zaczê³o generowaæ pakiety spoza zadanej listy.
-       #         Jednym s³owem budowanie niespoldkowanych zale¿no¶ci dzia³a w paru przypadkach.
+       # Update: PoprawiÅ‚em parÄ™ rzeczy i zaczęło generować pakiety spoza zadanej listy.
+       #         Jednym sÅ‚owem budowanie niespoldkowanych zależnoÅ›ci dziaÅ‚a w paru przypadkach.
        #
        #
        # y0shi.
@@ -1743,6 +1889,46 @@ run_sub_builder() {
        NOT_INSTALLED_PACKAGES="$NOT_INSTALLED_PACKAGES $package_name"
 }
 
+# install package with poldek
+# @return exit code from poldek
+#
+# this requires following sudo rules:
+# - poldek --noask --caplookup -ug
+poldek_install() {
+       LC_ALL=C LANG=C $POLDEK_CMD --noask --caplookup --uniq -ug "$@"
+}
+
+# install packages
+#
+# this requires following sudo rules:
+# - poldek -q --update --upa
+install_packages() {
+       # sync poldek indexes once per invocation
+       if [ -z "$package_indexes_updated" ]; then
+               update_shell_title "poldek: update indexes"
+               $POLDEK_CMD -q --update --upa --mo=nodesc
+               package_indexes_updated=true
+       fi
+
+       update_shell_title "install packages: $*"
+       poldek_install "$@" && return
+
+       # retry install, install packages one by one
+       # this is slower one
+       local rc=0 package
+       for package in $*; do
+               package=$(depspecname $package)
+               update_shell_title "install package: $package"
+               poldek_install "$package" || rc=$?
+       done
+       return $rc
+}
+
+uninstall_packages() {
+       update_shell_title "uninstall packages: $*"
+       $POLDEK_CMD --noask --nofollow -ev "$@"
+}
+
 spawn_sub_builder() {
        package_name="${1}"
        update_shell_title "spawn_sub_builder $package_name"
@@ -1801,31 +1987,29 @@ display_bconds() {
 }
 
 display_branches() {
-       if [ "$NOCVSSPEC" != "yes" ]; then
-               echo -n "Available branches: "
-               $CVS_COMMAND status -v "${SPECFILE}" | awk '!/Sticky Tag:/ && /\(branch:/ { print $1 } ' | xargs
-       fi
+       echo -n "Available branches: "
+       git branch -r 2>/dev/null | grep "^  ${REMOTE_PLD}" | grep -v ${REMOTE_PLD}/HEAD | sed "s#^ *${REMOTE_PLD}/##" | xargs
 }
 
-# checks a given list of packages/files/provides agains current rpmdb.
+# checks a given list of packages/files/provides against current rpmdb.
 # outputs all dependencies which current rpmdb doesn't satisfy.
 # input can be either STDIN or parameters
 _rpm_prov_check() {
-       local DEPS
+       local deps out
 
        if [ $# -gt 0 ]; then
-               DEPS="$@"
+               deps="$@"
        else
-               DEPS=$(cat)
+               deps=$(cat)
        fi
 
-       DEPS=$(LANG=C rpm -q --whatprovides $DEPS 2>&1 | awk '/^(error:|no package provides)/ { print }')
+       out=$(LC_ALL=C rpm -q --whatprovides $deps 2>&1)
 
        # packages
-       echo "$DEPS" | awk '/^no package provides/ { print $NF }'
+       echo "$out" | awk '/^no package provides/ { print $NF }'
 
        # other deps (files)
-       echo "$DEPS" | awk -F: '/^error:.*No such file/{o = $2; gsub("^ file ", "", o); print o}'
+       echo "$out" | sed -rne 's/file (.*): No such file or directory/\1/p'
 }
 
 # checks if given package/files/provides exists in rpmdb.
@@ -1840,49 +2024,31 @@ _rpm_cnfl_check() {
                DEPS=$(cat)
        fi
 
-       LANG=C rpm -q --whatprovides $DEPS 2>/dev/null | awk '!/no package provides/ { print }'
+       LC_ALL=C LANG=C rpm -q --whatprovides $DEPS 2>/dev/null | awk '!/no package provides/ { print }'
 }
 
 # install deps via information from 'rpm-getdeps' or 'rpm --specsrpm'
 install_build_requires_rpmdeps() {
+       local DEPS CNFL
        if [ "$FETCH_BUILD_REQUIRES_RPMGETDEPS" = "yes" ]; then
                # TODO: Conflicts list doesn't check versions
-               local CNFL=$(rpm-getdeps $BCOND $SPECFILE 2> /dev/null | awk '/^\-/ { print $3 } ' | _rpm_cnfl_check | xargs)
-               local DEPS=$(rpm-getdeps $BCOND $SPECFILE 2> /dev/null | awk '/^\+/ { print $3 } ' | _rpm_prov_check | xargs)
+               CNFL=$(eval rpm-getdeps $BCOND $RPMOPTS $SPECFILE 2> /dev/null | awk '/^\-/ { print $3 } ' | _rpm_cnfl_check | xargs)
+               DEPS=$(eval rpm-getdeps $BCOND $RPMOPTS $SPECFILE 2> /dev/null | awk '/^\+/ { print $3 } ' | _rpm_prov_check | xargs)
        fi
        if [ "$FETCH_BUILD_REQUIRES_RPMSPECSRPM" = "yes" ]; then
-               local CNFL=$(rpm -q --specsrpm --conflicts $BCOND $SPECFILE | awk '{print $1}' | _rpm_cnfl_check | xargs)
-               local DEPS=$(rpm -q --specsrpm --requires $BCOND $SPECFILE | awk '{print $1}' | _rpm_prov_check | xargs)
+               CNFL=$(eval rpm -q --specsrpm --conflicts $BCOND $RPMOPTS $SPECFILE | awk '{print $1}' | _rpm_cnfl_check | xargs)
+               DEPS=$(eval rpm -q --specsrpm --requires $BCOND $RPMOPTS $SPECFILE | awk '{print $1}' | _rpm_prov_check | xargs)
        fi
 
-       if [ -n "$CNFL" ] || [ -n "$DEPS" ]; then
-               echo "fetch BuildRequires: install [$DEPS]; remove [$CNFL]"
-               update_shell_title "poldek: install [$DEPS]; remove [$CNFL]"
-               $SU_SUDO /usr/bin/poldek -q --update || $SU_SUDO /usr/bin/poldek -q --upa
-       fi
        if [ -n "$CNFL" ]; then
-               update_shell_title "uninstall conflicting packages: $CNFL"
-               echo "Trying to uninstall conflicting packages ($CNFL):"
-               $SU_SUDO /usr/bin/poldek --noask --nofollow -ev $CNFL
-       fi
-
-       while [ "$DEPS" ]; do
-                       update_shell_title "install deps: $DEPS"
-                       echo "Trying to install dependencies ($DEPS):"
-                       local log=.${SPECFILE}_poldek.log
-                       LANG=C $SU_SUDO /usr/bin/poldek --noask --caplookup -uGqQ $DEPS | tee $log
-                       failed=$(awk '/^error:/{a=$2; sub(/^error: /, "", a); sub(/:$/, "", a); print a}' $log)
-                       rm -f $log
-                       local ok
-                       if [ -n "$failed" ]; then
-                               for package in $failed; do
-                                       spawn_sub_builder -bb $(depspecname $package) && ok="$ok $package"
-                               done
-                               DEPS="$ok"
-                       else
-                               DEPS=""
-                       fi
-       done
+               echo "Uninstall conflicting packages: $CNFL"
+               uninstall_packages $CNFL
+       fi
+
+       if [ -n "$DEPS" ]; then
+               echo "Install dependencies: $DEPS"
+               install_packages $DEPS
+       fi
 }
 
 fetch_build_requires()
@@ -1897,223 +2063,52 @@ fetch_build_requires()
                return
        fi
 
-               # XXX is this ugliest code written in human history still needed?
-               echo "All packages installed by fetch_build_requires() are written to:"
-               echo "`pwd`/.${SPECFILE}_INSTALLED_PACKAGES"
-               echo ""
-               echo "If anything fails, you may get rid of them by executing:"
-               echo "poldek -e \`cat `pwd`/.${SPECFILE}_INSTALLED_PACKAGES\`"
-               echo ""
-               echo > `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
-               for package_item in $(cat $SPECFILE | grep -B100000 ^%changelog|grep -v ^#|grep BuildRequires|grep -v ^-|sed -e "s/^.*BuildRequires://g"|awk '{print $1}'|sed -e s,perl\(,perl-,g -e s,::,-,g -e s,\(.*\),,g -e s,%{,,g -e s,},,g|grep -v OpenGL-devel|sed -e s,sh-utils,coreutils,g -e s,fileutils,coreutils,g -e s,textutils,coreutils,g -e s,kgcc_package,gcc,g -e s,\),,g)
-               do
-                       package_item=$(echo $package_item|sed -e s,rpmbuild,rpm-build,g |sed -e s,__perl,perl,g |sed -e s,gasp,binutils-gasp,g -e s,binutils-binutils,binutils,g -e s,apxs,apache,g|sed -e s,apache\(EAPI\)-devel,apache-devel,g -e s,kernel-headers\(netfilter\),kernel-headers,g -e s,awk,mawk,g -e s,mmawk,mawk,g -e s,motif,openmotif,g -e s,openopenmotif,openmotif,g)
-                       GO="yes"
-                       package=$(basename "$package_item"|sed -e "s/}$//g")
-                       COND_ARCH_TST=$(cat $SPECFILE|grep -B1 BuildRequires|grep -B1 $package|grep ifarch|sed -e "s/^.*ifarch//g")
-                       mach=$(uname -m)
-
-                       COND_TST=`cat $SPECFILE|grep BuildRequires|grep "$package"`
-                       if `echo $COND_TST|grep -q '^BuildRequires:'`; then
-                               if [ "$COND_ARCH_TST" != "" ] && [ "`echo $COND_ARCH_TST|sed -e "s/i.86/ix86/g"`" != "`echo $mach|sed -e "s/i.86/ix86/g"`" ]; then
-                                       GO="yes"
-                               fi
-                       # bcond:
-                       else
-                               COND_NAME=`echo $COND_TST|sed -e s,:BuildRequires:.*$,,g`
-                               GO=""
-                               # %{without}
-                               if `echo $COND_TST|grep -q 'without_'`; then
-                                       COND_NAME=`echo $COND_NAME|sed -e s,^.*without_,,g`
-                                       if `echo $COND_TST|grep -q !`; then
-                                               COND_STATE="with"
-                                       else
-                                               COND_STATE="wout"
-                                       fi
-                                       COND_WITH=`echo $AVAIL_BCONDS_WITH|grep "<$COND_NAME>"`
-                                       COND_WITHOUT=`echo $AVAIL_BCONDS_WITHOUT|grep "<$COND_NAME>"`
-                                       if [ -n "$COND_WITHOUT" ] || [ -z "$COND_WITH" ]; then
-                                               COND_ARGV="wout"
-                                       else
-                                               COND_ARGV="with"
-                                       fi
-                               # %{with}
-                               elif `echo $COND_TST|grep -q 'with_'`; then
-                                       COND_NAME=`echo $COND_NAME|sed -e s,^.*with_,,g`
-                                       if `echo $COND_TST|grep -q !`; then
-                                               COND_STATE="wout"
-                                       else
-                                               COND_STATE="with"
-                                       fi
-                                       COND_WITH=`echo $AVAIL_BCONDS_WITH|grep "<$COND_NAME>"`
-                                       COND_WITHOUT=`echo $AVAIL_BCONDS_WITHOUT|grep "<$COND_NAME>"`
-                                       if [ -n "$COND_WITH" ] || [ -z "$COND_WITHOUT" ]; then
-                                               COND_ARGV="with"
-                                       else
-                                               COND_ARGV="wout"
-                                       fi
-                               fi
-                               RESULT="${COND_STATE}-${COND_ARGV}"
-                               case "$RESULT" in
-                                       "with-wout" | "wout-with" )
-                                               GO=""
-                                               ;;
-                                       "wout-wout" | "with-with" )
-                                               GO="yes"
-                                               ;;
-                                       * )
-                                               echo "Action '$RESULT' was not defined for package '$package_item'"
-                                               GO="yes"
-                                               ;;
-                               esac
-                       fi
+       die "need rpm-getdeps tool"
+}
 
-                       if [ "$GO" = "yes" ]; then
-                               if [ "`rpm -q $package|sed -e "s/$package.*/$package/g"`" != "$package" ]; then
-                                       echo "Testing if $package has subrequirements..."
-                                       run_poldek -t -i $package --dumpn=".$package-req.txt"
-                                       if [ -f ".$package-req.txt" ]; then
-                                               for package_name in `cat ".$package-req.txt"|grep -v ^#`
-                                               do
-                                                       if [ "$package_name" = "$package" ]; then
-                                                               echo "Installing BuildRequired package:\t$package_name"
-                                                               update_shell_title "Installing BuildRequired package: ${package_name}"
-                                                               install_required_packages $package
-                                                       else
-                                                               echo "Installing (sub)Required package:\t$package_name"
-                                                               update_shell_title "Installing (sub)Required package: ${package_name}"
-                                                               install_required_packages $package_name
-                                                       fi
-                                                       case $? in
-                                                               0)
-                                                                       INSTALLED_PACKAGES="$package_name $INSTALLED_PACKAGES"
-                                                                       echo $package_name >> `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
-                                                                       ;;
-                                                               *)
-                                                                       echo "Attempting to run spawn sub - builder..."
-                                                                       echo "Package installation failed:\t$package_name"
-                                                                       run_sub_builder $package_name
-                                                                       if [ $? -eq 0 ]; then
-                                                                               install_required_packages $package_name
-                                                                               case $? in
-                                                                                       0)
-                                                                                               INSTALLED_PACKAGES="$package_name $INSTALLED_PACKAGES"
-                                                                                               echo $package_name >> `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
-                                                                                               ;;
-                                                                                       *)
-                                                                                               NOT_INSTALLED_PACKAGES="$package_name $NOT_INSTALLED_PACKAGES"
-                                                                                               ;;
-                                                                               esac
-                                                                       fi
-                                                                       ;;
-                                                       esac
-                                               done
-                                               rm -f ".$package-req.txt"
-                                       else
-                                               echo "Attempting to run spawn sub - builder..."
-                                               echo "Package installation failed:\t$package"
-                                               run_sub_builder $package
-                                               if [ $? -eq 0 ]; then
-                                                       install_required_packages $package
-                                                       case $? in
-                                                               0)
-                                                                       INSTALLED_PACKAGES="$package_name $INSTALLED_PACKAGES"
-                                                                       echo $package_name >> `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
-                                                                       ;;
-                                                               *)
-                                                                       NOT_INSTALLED_PACKAGES="$package_name $NOT_INSTALLED_PACKAGES"
-                                                                       ;;
-                                                       esac
-                                               fi
-                                       fi
-                               else
-                                       echo "Package $package is already installed. BuildRequirement satisfied."
-                               fi
-                       fi
-               done
-               if [ "$NOT_INSTALLED_PACKAGES" != "" ]; then
-                       echo >&2 "Unable to install following packages and their dependencies:"
-                       for pkg in "$NOT_INSTALLED_PACKAGES"
-                       do
-                               echo $pkg
-                       done
-                       remove_build_requires
-                       exit 8
-               fi
+init_repository() {
+       local remoterepo=$1
+       local localrepo=$2
+
+       if [ ! -e $localrepo ]; then
+               git clone $IPOPT -o $REMOTE_PLD ${GIT_SERVER}/$remoterepo $localrepo
+               git --git-dir=$localrepo/.git remote set-url --push  $REMOTE_PLD ssh://${GIT_PUSH}/$remoterepo
+       fi
 }
 
 init_rpm_dir() {
-       local CVSROOT=":pserver:cvs@$CVS_SERVER:/cvsroot"
        local TOP_DIR=$(eval $RPM $RPMOPTS --eval '%{_topdir}')
        local rpmdir=$(eval $RPM $RPMOPTS --eval '%{_rpmdir}')
        local buildir=$(eval $RPM $RPMOPTS --eval '%{_builddir}')
        local srpmdir=$(eval $RPM $RPMOPTS --eval '%{_srcrpmdir}')
+       local TEMPLATES=template-specs
        local tmp
 
-       echo "Initializing rpm directories to $TOP_DIR from $CVSROOT"
+       echo "Initializing rpm directories to $TOP_DIR from $GIT_SERVER"
        mkdir -p $TOP_DIR $rpmdir $buildir $srpmdir
 
-       # need to checkout to tmp dir or we can't name our topdir
-       tmp=$(TMPDIR= TEMPDIR= mktemp -p $TOP_DIR -d) || exit 1
-       cd $tmp
-       $CVS_COMMAND -d $CVSROOT co packages/{.cvsignore,rpm.groups,dropin,mirrors,md5,adapter{,.awk},fetchsrc_request,builder,{relup,compile,repackage}.sh,ci,civim}
-       cd -
-       mv $tmp/packages/* $TOP_DIR
-       rm -rf $tmp
-
-       init_builder
-
-       echo "To checkout *all* .spec files (read-only):"
-       echo "- run cvs co SPECS"
-
-       echo "To checkout *all* packages:"
-       echo "- run cvs up -dP in $TOP_DIR dir"
-
-       echo ""
-       echo "To commit with your developer account:"
-       echo "- edit $TOP_DIR/CVS/Root"
-}
-
-get_greed_sources() {
-       CVSROOT=":pserver:cvs@$CVS_SERVER:/cvsroot"
-       if [ -n "BE_VERBOSE" ]; then
-               echo "Try greed download: $1 from: $CVSROOT"
-       fi
-       $CVS_COMMAND -d $CVSROOT get SOURCES/$1
-       if [ $? != 0 ]; then
-               Exit_error err_no_source_in_repo $1
-       fi
-
-}
-
-# remove entries from CVS/Entries
-cvs_entry_remove() {
-       local cvsdir="$1"; shift
-       if [ ! -d "$cvsdir" ]; then
-               echo >&2 "cvs_entry_remove: $cvsdir is not a directory"
-               exit 1
-       fi
-
-       for file in "$@"; do
-               rm -f $cvsdir/CVS/Entries.new || return 1
-               awk -ve="${file##*/}" -F/ '$2 != e {print}' $cvsdir/CVS/Entries > $cvsdir/CVS/Entries.new || return 1
-               mv -f $cvsdir/CVS/Entries.new $cvsdir/CVS/Entries || return 1
+       cd "$TOP_DIR"
+       init_repository ${PACKAGES_DIR}/rpm-build-tools.git ../rpm-build-tools
+       init_repository projects/$TEMPLATES ../$TEMPLATES
+       for a in builder fetchsrc_request compile repackage; do
+               ln -sf ../rpm-build-tools/${a}.sh $a
+       done
+       for a in md5; do
+               ln -sf ../rpm-build-tools/${a} $a
        done
-       return 0
+       ln -sf ../rpm-build-tools/mirrors mirrors
+       init_builder
 }
 
 mr_proper() {
        init_builder
        NOCVSSPEC="yes"
        DONT_PRINT_REVISION="yes"
-       get_spec
-       parse_spec
-
-       # remove from CVS/Entries
-       cvs_entry_remove $PACKAGE_DIR $SPECFILE $SOURCES $PATCHES
 
        # remove spec and sources
-       $RPMBUILD --clean --rmsource --rmspec --nodeps --define \'_specdir $PACKAGE_DIR\' --define \'_sourcedir $PACKAGE_DIR\' $SPECFILE
+       PATH=$CLEAN_PATH $RPMBUILD --clean --rmsource --rmspec --nodeps --define "__urlgetfile() %nil" --define "_specdir $PACKAGE_DIR" --define "_sourcedir $PACKAGE_DIR" --define "_builddir $builddir" $PACKAGE_DIR/$SPECFILE
+       rm -rf $PACKAGE_DIR/{.git,.gitignore}
+       rmdir --ignore-fail-on-non-empty $PACKAGE_DIR
 }
 
 #---------------------------------------------
@@ -2124,8 +2119,17 @@ if [ $# = 0 ]; then
        exit 1
 fi
 
+# stuff global $BUILDER_OPTS from env as args
+if [ "$BUILDER_OPTS" ]; then
+       set -- "$BUILDER_OPTS" "$@"
+fi
+
 while [ $# -gt 0 ]; do
        case "${1}" in
+               -4|-6)
+                       IPOPT="${1}"
+                       shift
+                       ;;
                -5 | --update-md5)
                        COMMAND="update_md5"
                        NODIST="yes"
@@ -2134,7 +2138,6 @@ while [ $# -gt 0 ]; do
                -a5 | --add-md5 )
                        COMMAND="update_md5"
                        NODIST="yes"
-                       NOCVS="yes"
                        NOCVSSPEC="yes"
                        ADD5="yes"
                        shift ;;
@@ -2147,11 +2150,12 @@ while [ $# -gt 0 ]; do
                        COMMAND="version"; shift ;;
                --short-version )
                        COMMAND="short-version"; shift ;;
-               --as_anon )
-                       CVSROOT=":pserver:cvs@$CVS_SERVER:/cvsroot"; shift ;;
                -a | --add_cvs)
                        COMMAND="add_cvs";
                        shift ;;
+               --all-branches )
+                       ALL_BRANCHES="yes"
+                       shift ;;
                -b | -ba | --build )
                        COMMAND="build"; shift ;;
                -bb | --build-binary )
@@ -2171,9 +2175,11 @@ while [ $# -gt 0 ]; do
                -c | --clean )
                        CLEAN="--clean"; shift ;;
                -cf | --cvs-force )
-                       CVS_FORCE="-F"; shift;;
-               -d | --cvsroot )
-                       shift; CVSROOT="${1}"; shift ;;
+                       CVS_FORCE="-f"; shift;;
+               --depth )
+                       DEPTH="--depth=$2"
+                       shift 2
+                       ;;
                -g | --get )
                        COMMAND="get"; shift ;;
                -h | --help )
@@ -2183,11 +2189,19 @@ while [ $# -gt 0 ]; do
                --http )
                        PROTOCOL="http"; shift ;;
                -j)
-                       RPMOPTS="${RPMOPTS} --define \"_smp_mflags -j$2\""
+                       RPMOPTS="${RPMOPTS} --define \"__jobs $2\""
                        shift 2
                        ;;
                -j[0-9]*)
-                       RPMOPTS="${RPMOPTS} --define \"_smp_mflags $1\""
+                       RPMOPTS="${RPMOPTS} --define \"__jobs ${1#-j}\""
+                       shift
+                       ;;
+               -p)
+                       PARALLEL_DOWNLOADS=$2
+                       shift 2
+                       ;;
+               -p[0-9])
+                       PARALLEL_DOWNLOADS=${1#-p}
                        shift
                        ;;
                -l | --logtofile )
@@ -2198,8 +2212,6 @@ while [ $# -gt 0 ]; do
                        SKIP_EXISTING_FILES="yes"; shift ;;
                -m | --mr-proper )
                        COMMAND="mr-proper"; shift ;;
-               -nc | --no-cvs )
-                       NOCVS="yes"; shift ;;
                -ncs | --no-cvs-specs )
                        NOCVSSPEC="yes"; shift ;;
                -nd | --no-distfiles )
@@ -2213,7 +2225,6 @@ while [ $# -gt 0 ]; do
                -ns0 | --no-source0 )
                        NOSOURCE0="yes"; shift ;;
                -nn | --no-net )
-                       NOCVS="yes"
                        NOCVSSPEC="yes"
                        NODIST="yes"
                        NOMIRRORS="yes"
@@ -2224,7 +2235,7 @@ while [ $# -gt 0 ]; do
                -pm | --prefer-mirrors )
                        PREFMIRRORS="yes"
                        shift;;
-               --no-init )
+               --noinit | --no-init )
                        NOINIT="yes"
                        shift;;
                --opts )
@@ -2266,14 +2277,16 @@ while [ $# -gt 0 ]; do
                -q | --quiet )
                        QUIET="--quiet"; shift ;;
                --date )
-                       CVSDATE="${2}"; shift 2 ;;
+                       CVSDATE="${2}"; shift 2
+                       date -d "$CVSDATE" > /dev/null 2>&1 || { echo >&2 "No valid date specified"; exit 3; }
+                       ;;
                -r | --cvstag )
                        CVSTAG="$2"
                        shift 2
                        ;;
                -A)
                        shift
-                       CVSTAG="HEAD"
+                       CVSTAG="master"
                        ;;
                -R | --fetch-build-requires)
                        FETCH_BUILD_REQUIRES="yes"
@@ -2285,7 +2298,7 @@ while [ $# -gt 0 ]; do
                -FRB | --force-remove-build-requires)
                        REMOVE_BUILD_REQUIRES="force"
                        shift ;;
-               -sc | --sources-cvs)
+               -sc | --source-cvs)
                        COMMAND="list-sources-cvs"
                        shift ;;
                -sd | --source-distfiles)
@@ -2342,12 +2355,9 @@ while [ $# -gt 0 ]; do
                -Upi | --update-poldek-indexes )
                        UPDATE_POLDEK_INDEXES="yes"
                        shift ;;
-               --init-rpm-dir)
+               --init-rpm-dir|--init)
                        COMMAND="init_rpm_dir"
                        shift ;;
-               --use-greed-sources )
-                       GREEDSRC="1"
-                       shift;;
                -u | --try-upgrade )
                        TRY_UPGRADE="1"; shift ;;
                --upgrade-version )
@@ -2370,7 +2380,7 @@ while [ $# -gt 0 ]; do
                        ;;
                --alt_kernel)
                        shift
-                       RPMOPTS="${RPMOPTS} --define \"alt_kernel $1\""
+                       RPMOPTS="${RPMOPTS} --define \"alt_kernel $1\" --define \"build_kernels $1\""
                        shift
                        ;;
                --short-circuit)
@@ -2412,22 +2422,16 @@ while [ $# -gt 0 ]; do
        esac
 done
 
-[ -d "$ASSUMED_NAME" ] && CVS_ENTRIES="$ASSUMED_NAME/CVS/Entries" || CVS_ENTRIES="CVS/Entries"
-if [ -f "$CVS_ENTRIES" ] && [ -z "$CVSTAG" ]; then
-       CVSTAG=$(awk -vSPECFILE=$(basename $SPECFILE) -F/ '$2 == SPECFILE && $6 ~ /^T/{print substr($6, 2)}' ${CVS_ENTRIES})
-       if [ "$CVSTAG" ]; then
-               echo >&2 "builder: Sticky tag $CVSTAG active. Use -r TAGNAME to override."
-       fi
-elif [ "$CVSTAG" = "HEAD" ]; then
-       # assume -r HEAD is same as -A
-       CVSTAG=""
-fi
-
 if [ "$CVSTAG" ]; then
        # pass $CVSTAG used by builder to rpmbuild too, so specs could use it
        RPMOPTS="$RPMOPTS --define \"_cvstag $CVSTAG\""
 fi
 
+if [ -n "$ALL_BRANCHES" -a -z "$DEPTH" ]; then
+       echo >&2 "--all branches requires --depth <number>"
+       Exit_error err_invalid_cmdline
+fi
+
 if [ -n "$DEBUG" ]; then
        set -x
        set -v
@@ -2448,6 +2452,9 @@ else
        NICE_COMMAND="nice -n ${DEF_NICE_LEVEL}"
 fi
 
+# see time(1) for output format that could be used
+TIME_COMMAND="time -p"
+
 update_shell_title "$COMMAND"
 case "$COMMAND" in
        "show_bconds")
@@ -2530,18 +2537,25 @@ case "$COMMAND" in
                # ./builder -bs test.spec -r AC-branch -Tp auto-ac- -tt
                if [ -n "$TEST_TAG" ]; then
                        local TAGVER=`make_tagver`
-                       echo "Searching for tag $TAGVER..."
-                       TAGREL=$($CVS_COMMAND status -v $SPECFILE | grep -E "^[[:space:]]*${TAGVER}[[[:space:]]" | sed -e 's#.*(revision: ##g' -e 's#).*##g')
-                       if [ -n "$TAGREL" ]; then
-                               Exit_error err_tag_exists "$TAGVER" "$TAGREL"
-                       fi
-
+                       tag_exist $TAGVER || [ $TAGVER = $CVSTAG ] || Exit_error err_tag_exists $TAGVER
+                       # check also tags created in CVS
+                       local TAGVER_CVS=$(echo $TAGVER | tr '[.@]' '[_#]')
+                       local CVSTAG_CVS=$(echo $CVSTAG | tr '[.@]' '[_#]')
+                       tag_exist $TAGVER_CVS || [ $TAGVER_CVS = $CVSTAG_CVS ] \
+                               || Exit_error err_tag_exists $TAGVER_CVS
                        # - do not allow to build from HEAD when XX-branch exists
-                       TREE_PREFIX=$(echo "$TAG_PREFIX" | sed -e 's#^auto-\([a-zA-Z]\+\)-.*#\1#g')
-                       if [ "$TREE_PREFIX" != "$TAG_PREFIX" ]; then
+                       TREE_PREFIX=$(echo "$TAG_PREFIX" | sed -e 's#^auto/\([a-zA-Z]\+\)/.*#\1#g')
+                       if [ "$TAGVER" != "$CVSTAG" -a "$TAGVER_CVS" != "$CVSTAG" -a  "$TREE_PREFIX" != "$TAG_PREFIX" ]; then
                                TAG_BRANCH="${TREE_PREFIX}-branch"
-                               TAG_STATUS=$($CVS_COMMAND status -v $SPECFILE | grep -Ei "${TAG_BRANCH}.+(branch: [0-9.]+)")
-                               if [ -n "$TAG_STATUS" -a "$CVSTAG" = "HEAD" ]; then
+                               if [ -n "$DEPTH" ]; then
+                                       cmd_branches="git ls-remote --heads"
+                                       ref_prefix=refs/heads
+                               else
+                                       cmd_branches="git show-ref"
+                                       ref_prefix=refs/remotes/${REMOTE_PLD}
+                               fi
+                               TAG_STATUS=$($cmd_branches | grep -i "${ref_prefix}/$TAG_BRANCH$" | cut -c'-40')
+                               if [ -n "$TAG_STATUS" -a "$TAG_STATUS" != $(git rev-parse "$CVSTAG") ]; then
                                        Exit_error err_branch_exists "$TAG_STATUS"
                                fi
                        fi
@@ -2555,16 +2569,16 @@ case "$COMMAND" in
                case $? in
                        0)
                                get_files $SOURCES $PATCHES
-                               check_md5 $SOURCES
+                               check_md5 $SOURCES $PATCHES
                                ;;
                        *)
                                NODIST="yes" get_files $SOURCES $PATCHES
-                               update_md5 $SOURCES
+                               update_md5 $SOURCES $PATCHES
                                ;;
                esac
                build_package
-               if [ "$UPDATE_POLDEK_INDEXES" = "yes" -a "$COMMAND" != "build-prep" ]; then
-                       run_poldek --sdir="${POLDEK_INDEX_DIR}" --mkidxz
+               if [ "$UPDATE_POLDEK_INDEXES" = "yes" ] && [ "$COMMAND" = "build" -o "$COMMAND" = "build-binary" ]; then
+                       run_poldek --sdir="${POLDEK_INDEX_DIR}" ${UPDATE_POLDEK_INDEXES_OPTS} --mkidxz
                fi
                remove_build_requires
                ;;
@@ -2576,16 +2590,7 @@ case "$COMMAND" in
 
                get_spec
                parse_spec
-               # don't fetch sources from remote locations
-               new_SOURCES=""
-               for file in $SOURCES; do
-                       [ -n "`src_md5 $file`" ] && continue
-                       new_SOURCES="$new_SOURCES $file"
-               done
-               SOURCES="$new_SOURCES"
-               get_files $SOURCES $PATCHES
-               check_md5 $SOURCES
-               branch_files $TAG $SOURCES $PATCHES $ICONS
+               branch_files $TAG
                ;;
        "add_cvs" )
                init_builder
@@ -2593,14 +2598,22 @@ case "$COMMAND" in
                        Exit_error err_no_spec_in_cmdl
                fi
 
-               ADD_PACKAGE_CVS=yes get_spec
-               parse_spec
-
-               if [ -n "$NOSOURCE0" ] ; then
-                       SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
+               create_git_repo
+               if [ -n "$NEW_REPO" ]; then
+                       parse_spec
+                       local file
+                       for file in $SOURCES $PATCHES; do
+                               if [ -z $(src_md5 "$file") ]; then
+                                       git add $file || Exit_error err_no_source_in_repo $file
+                               else
+                                       cvsignore_df `nourl $file`
+                               fi
+                       done
+                       git add $SPECFILE
+                       echo "When you are ready commit your changes and run git push origin master"
+               else
+                       echo "You had already git repository. Push chosen branches to remote: ${REMOTE_PLD}"
                fi
-               get_files $SOURCES $PATCHES
-               check_md5 $SOURCES
                ;;
        "get" )
                init_builder
@@ -2616,6 +2629,7 @@ case "$COMMAND" in
                fi
                get_files $SOURCES $PATCHES
                check_md5 $SOURCES
+               fetch_build_requires
                ;;
        "update_md5" )
                init_builder
@@ -2629,7 +2643,7 @@ case "$COMMAND" in
                if [ -n "$NOSOURCE0" ] ; then
                        SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
                fi
-               update_md5 $SOURCES
+               update_md5 $SOURCES $PATCHES
                ;;
        "tag" )
                NOURLS=1
@@ -2639,19 +2653,12 @@ case "$COMMAND" in
                        Exit_error err_no_spec_in_cmdl
                fi
 
-               get_spec
                parse_spec
-
-               # don't fetch sources from remote locations
-               new_SOURCES=""
-               for file in $SOURCES; do
-                       [ -n "`src_md5 $file`" ] && continue
-                       new_SOURCES="$new_SOURCES $file"
-               done
-               SOURCES="$new_SOURCES"
-               get_files $SOURCES $PATCHES
-               check_md5 $SOURCES
-               tag_files $SOURCES $PATCHES $ICONS
+               if  [ ! -d .git ]; then
+                       echo "No git reposiotory" >&2
+                       exit 101
+               fi
+               tag_files
                ;;
        "mr-proper" )
                mr_proper
@@ -2667,13 +2674,13 @@ case "$COMMAND" in
                done
                ;;
        "list-sources-urls" )
-               init_builder
+               init_builder >&2
                NOCVSSPEC="yes"
                DONT_PRINT_REVISION="yes"
-               get_spec
-               parse_spec
+               get_spec >&2
+               parse_spec >&2
                SAPS="$SOURCES $PATCHES"
-               for SAP in $SAPS ; do
+               for SAP in $SAPS; do
                        echo $SAP
                done
                ;;
This page took 0.329777 seconds and 4 git commands to generate.