18.4.3 开发者user1完成功能开发

开发者user1开始在user1/getopt分支中工作,重构hello-world中的命令行参数解析的代码。重构时采用getopt_long函数。

您可以试着更改,不过在hello-world中已经保存了一份改好的代码,可以直接检出。

(1)确保是在user1的工作区中。


$cd/path/to/user1/workspace/hello-world/


(2)执行下面的命令,用里程碑jx/v2.0标记的内容(已实现用getopt进行命令行解析的功能)替换暂存区和工作区。

下面的git checkout命令的最后是一个点“.”,因此检出只更改了暂存区和工作区,而没有修改头指针。


$cd/path/to/user1/workspace/hello-world/

$git checkout jx/v2.0—.


(3)查看状态,会看到分支仍保持为user1/getopt,但文件src/main.c被修改了。


$git status

On branch user1/getopt

Changes to be committed:

(use "git reset HEAD<file>…" to unstage)

#

modified:src/main.c

#


(4)比较暂存区和HEAD的文件差异,可以看到为实现用getopt进行命令行解析功能而对代码的改动。


$git diff—cached

diff—git a/src/main.c b/src/main.c

index 6ee936f..fa5244a 100644

—-a/src/main.c

+++b/src/main.c

@@-1,4+1,6@@

include<stdio.h>

+#include<getopt.h>

+

include "version.h"

int usage(int code)

@@-19,15+21,44@@int usage(int code)

int

main(int argc,char**argv)

{

-if(argc==1){

+int c;

+char*uname=NULL;

+

+while(1){

+int option_index=0;

+static struct option long_options[]={

+{"help",0,0,'h'},

+{0,0,0,0}

+};


(5)开发者user1提交代码,完成开发任务。


$git commit-m "Refactor:use getopt_long for arguments parsing."

[user1/getopt 0881ca3]Refactor:use getopt_long for arguments parsing.

1 files changed,36 insertions(+),5 deletions(-)


(6)提交完成之后,可以看到这时user1/getopt分支和master分支的指向不同了。


$git rev-parse user1/getopt master

0881ca3f62ddadcddec08bd9f2f529a44d17cfbf

ebcf6d6b06545331df156687ca2940800a3c599d


(7)编译运行hello-world。

注意输出中的版本号显示。


$cd src

$make clean

rm-f hello main.o version.h

$make

version.h.in=>version.h

cc-c-o main.o main.c

cc-o hello main.o

$./hello

Hello world.

(version:v1.0-1-g0881ca3)