git-clean.shon commit Fix crash when reading the empty tree (b6c4a48)
   1#!/bin/sh
   2#
   3# Copyright (c) 2005-2006 Pavel Roskin
   4#
   5
   6USAGE="[-d] [-n] [-q] [-x | -X]"
   7LONG_USAGE='Clean untracked files from the working directory
   8        -d      remove directories as well
   9        -n      don'\''t remove anything, just show what would be done
  10        -q      be quiet, only report errors
  11        -x      remove ignored files as well
  12        -X      remove only ignored files as well'
  13SUBDIRECTORY_OK=Yes
  14. git-sh-setup
  15
  16ignored=
  17ignoredonly=
  18cleandir=
  19quiet=
  20rmf="rm -f"
  21rmrf="rm -rf"
  22rm_refuse="echo Not removing"
  23echo1="echo"
  24
  25while case "$#" in 0) break ;; esac
  26do
  27        case "$1" in
  28        -d)
  29                cleandir=1
  30                ;;
  31        -n)
  32                quiet=1
  33                rmf="echo Would remove"
  34                rmrf="echo Would remove"
  35                rm_refuse="echo Would not remove"
  36                echo1=":"
  37                ;;
  38        -q)
  39                quiet=1
  40                ;;
  41        -x)
  42                ignored=1
  43                ;;
  44        -X)
  45                ignoredonly=1
  46                ;;
  47        *)
  48                usage
  49        esac
  50        shift
  51done
  52
  53case "$ignored,$ignoredonly" in
  54        1,1) usage;;
  55esac
  56
  57if [ -z "$ignored" ]; then
  58        excl="--exclude-per-directory=.gitignore"
  59        if [ -f "$GIT_DIR/info/exclude" ]; then
  60                excl_info="--exclude-from=$GIT_DIR/info/exclude"
  61        fi
  62        if [ "$ignoredonly" ]; then
  63                excl="$excl --ignored"
  64        fi
  65fi
  66
  67git-ls-files --others --directory $excl ${excl_info:+"$excl_info"} |
  68while read -r file; do
  69        if [ -d "$file" -a ! -L "$file" ]; then
  70                if [ -z "$cleandir" ]; then
  71                        $rm_refuse "$file"
  72                        continue
  73                fi
  74                $echo1 "Removing $file"
  75                $rmrf "$file"
  76        else
  77                $echo1 "Removing $file"
  78                $rmf "$file"
  79        fi
  80done