代码之家  ›  专栏  ›  技术社区  ›  rich

使用性能触发器拒绝具有Windows行尾的文件

  •  2
  • rich  · 技术社区  · 16 年前

    使用Perforce,我希望能够拒绝包含带有Windows行尾的文件的提交(\r\n iirc,可能是\r任何地方,实际上我们只需要带有Unix行尾的文件)。

    与dos2unix传入文件或类似文件不同,为了帮助跟踪用户试图用Windows行结尾提交文件的实例,我想添加一个触发器来拒绝包含非unix行结尾文件的文本提交。

    是否有人可以演示如何使用bash或python编写触发器本身?

    谢谢

    1 回复  |  直到 13 年前
        1
  •  4
  •   pavel.mileshchenko Alex Martelli    13 年前

    下面是我能做的关于bash示例的最小编辑 the p4 docs :

    #!/bin/sh
    # Set target string, files to search, location of p4 executable...
    TARGET='\r\n'
    DEPOT_PATH="//depot/src/..."
    CHANGE=$1
    P4CMD="/usr/local/bin/p4 -p 1666 -c copychecker"
    XIT=0
    echo ""
    # For each file, strip off #version and other non-filename info
    # Use sed to swap spaces w/"%" to obtain single arguments for "for"
    for FILE in `$P4CMD files $DEPOT_PATH@=$CHANGE | \
      sed -e 's/\(.*\)\#[0-9]* - .*$/\1/' -e 's/ /%/g'`
    do
      # Undo the replacement to obtain filename...
      FILE="`echo $FILE | sed -e 's/%/ /g'`"
    # ...and use @= specifier to access file contents:
      # p4 print -q //depot/src/file.c@=12345
      if $P4CMD print -q "$FILE@=$CHANGE" | fgrep "$TARGET" > /dev/null
      then 
          echo "Submit fails: '$TARGET' not found in $FILE"
          XIT=1
      else
          echo ""
    
      fi
    done
    exit $XIT
    

    如果目标丢失,则原始示例失败;如果存在,则此示例失败——只需切换 then 和 else 分支机构 if . 当然,你可以进一步编辑它(例如 grep 或 fgrep , the -q 如果您的grep像GNU那样支持输出,则标记为禁止输出)。