1. 程式人生 > >git生成patch,應用到rpmbuild 打補丁

git生成patch,應用到rpmbuild 打補丁

git diff > test.patch
git format-patch
不要再用diff命令 對比生成patch了,太土

centos下 rpm製作

執行環境

[root@localhost myrpm_build]# uname -a
Linux localhost 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

不同的執行環境需要調整自己的spec檔案

[root@localhost myrpm_build]# tree helloworld-0.1
helloworld-0.1 ├── configure ├── helloworld.c └── readme 0 directories, 3 files [root@localhost myrpm_build]#

configure

#!/bin/sh
cat >Makefile  << EOF
all:helloworld
helloworld.o:helloworld.c
        gcc -c helloworld.c
helloworld:helloworld.o
        gcc helloworld.o -o helloworld
fresh:
rm -f Makefile clean: rm -f helloworld helloworld.o install: cp helloworld /usr/bin uninstall: rm -f /usr/bin/helloworld EOF

helloword.c

#include <stdio.h>

int main()
{
    printf("Hello World!\n");
    return 0;
}

helloworld.spec

Summary:the first rpm
Name:helloworld
Version:0
.1 Release:1%{?dist} Vendor:john License:Share Group:Applications/Text Source0:%{name}-%{version}.tar.gz # add patch %description helloworld.rpm build test %prep export RPM_SOURCES_DIR=/root/rpmbuild/SOURCES export RPM_BUILD_DIR=/root/rpmbuild/BUILD tar -xzvf $RPM_SOURCES_DIR/%{name}-%{version}.tar.gz %build cd $RPM_BUILD_DIR/%{name}-%{version} ./configure make %install cd $RPM_BUILD_DIR/%{name}-%{version} make install mkdir -p /root/rpmbuild/BUILDROOT/%{name}-%{version}-1.el6.x86_64/usr/bin/ cp $RPM_BUILD_DIR/%{name}-%{version}/%{name} /root/rpmbuild/BUILDROOT/%{name}-%{version}-1.el6.x86_64/usr/bin/ %clean rm -rf $RPM_BUILD_DIR/%{name}-%{version} %files %defattr(-,root,root) /usr/bin/%{name} %doc $RPM_BUILD_DIR/%{name}-%{version}/readme

如下兩句是由於rpmbuild -ba helloworld.spec報錯,根據報錯資訊新增的,可能不同的平臺不同,也可能不需要

mkdir -p /root/rpmbuild/BUILDROOT/%{name}-%{version}-1.el6.x86_64/usr/bin/

cp $RPM_BUILD_DIR/%{name}-%{version}/%{name} /root/rpmbuild/BUILDROOT/%{name}-%{version}-1.el6.x86_64/usr/bin/

檔案全部完成後,tar,並rpmbuild檢查結果

tar -czvf helloworld-0.1.tar.gz helloworld-0.1/

cp helloworld-0.1.tar.gz /root/rpmbuild/SOURCES

rpmbuild -ba helloworld.spec

git patch應用到rpmbuild

主要是git庫可以方便的管理檔案,並生成patch檔案(這裡都是本地操作,所以git push這種命令是沒有的,使用者配置也不需要,你只需要一個高版本的git)

這裡寫圖片描述

這裡寫圖片描述

這裡寫圖片描述

修改helloworld.c後並提交
這裡寫圖片描述

生成patch

確定git版本是支援 git format-patch命令的,下面是我用windows的實驗(我的虛擬機器centos沒裝好高版本的git)

仍然是兩個提交日誌

這裡寫圖片描述

生成最近的兩個patch檔案

git format-patch -2 

這裡寫圖片描述

可以直接檢視patch檔案,linux下可以直接vim開啟
這裡寫圖片描述

如下把 0002-fix-bug.patch 打上,重新生成rpm

  • 1 先把 0002-fix-bug.patch 檔案複製到/root/rpmbuild/SOURCES下面,與原來的helloworld-0.1.tar.gz一起

  • 2 修改helloworld.spec檔案,修改後的檔案如下

注意打patch的位置

Summary:the first rpm
Name:helloworld
Version:0.1
Release:1%{?dist}
Vendor:john
License:Share
Group:Applications/Text
Source0:%{name}-%{version}.tar.gz

# add patch
Patch1:0002-fix-bug.patch

%description
helloworld.rpm build test

%prep
export RPM_SOURCES_DIR=/root/rpmbuild/SOURCES
export RPM_BUILD_DIR=/root/rpmbuild/BUILD
tar -xzvf $RPM_SOURCES_DIR/%{name}-%{version}.tar.gz

%setup
%patch1 -p1

%build
cd $RPM_BUILD_DIR/%{name}-%{version}
./configure
make

%install
cd $RPM_BUILD_DIR/%{name}-%{version}
make install
mkdir -p /root/rpmbuild/BUILDROOT/%{name}-%{version}-1.el6.x86_64/usr/bin/
cp $RPM_BUILD_DIR/%{name}-%{version}/%{name} /root/rpmbuild/BUILDROOT/%{name}-%{version}-1.el6.x86_64/usr/bin/

%clean
rm -rf $RPM_BUILD_DIR/%{name}-%{version}

%files
%defattr(-,root,root)
/usr/bin/%{name}

%doc $RPM_BUILD_DIR/%{name}-%{version}/readme
  • 3 rpmbuild -ba helloworld.spec ,並驗證

這裡寫圖片描述