作用:移除git pull冲突文件
说明:当我们本地的一些工程比较老旧时,想同步服务器上最新的代码,但是当我们git pull时,会出现很多文件冲突,如果不想保留自己的修改,直接同步代码的话,可以使用本脚本来进行冲突文件的删除
源码:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44#!/user/bin/python
# -*- coding:utf-8 -*-
import os
import os.path
#获取当前目录的完整路径
homedir = os.getcwd()
#把git pull的冲突文件导出到conflict.txt中
os.system("git pull > conflict.txt 2>&1;")
#判断conflict.txt是否存在,如果存在,变量赋值,否则为空
if os.path.exists("conflict.txt"):
conflict_file = open('conflict.txt','r')
else:
conflict_file = None
print ('there is no conflict.txt file')
#删除指定目录文件函数
def removeFileInFile(dir,targetFile):
if os.path.exists(dir + '/' + targetFile):
os.remove(dir + '/' + targetFile)
else:
print ('no such file:%s' % dir + targetFile)
#while循环,从conflict.txt中按行读取,如果这一行是一个文件,则删除它,如果不是执行下一个
#当读到行尾后,跳出while循环
while 1:
if conflict_file != None:
target_line = conflict_file.readline()
target_line = target_line.strip()
if not target_line:
break
#print target_line
#print homedir + target_line
if os.path.isfile(target_line):
removeFileInFile(homedir , target_line)
else:
continue
if os.path.exists("conflict.txt"):
os.remove("conflict.txt")
os.system("git pull")